source: josm/trunk/src/org/openstreetmap/josm/gui/layer/JumpToMarkerActions.java

Last change on this file was 18972, checked in by taylor.smock, 8 months ago

See #23465: Add additional javadoc comments

This also fixes some sonarlint issues

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.lang.ref.WeakReference;
9import java.util.List;
10
11import javax.swing.AbstractAction;
12
13import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
14import org.openstreetmap.josm.gui.util.MultikeyActionsHandler;
15import org.openstreetmap.josm.gui.util.MultikeyShortcutAction;
16import org.openstreetmap.josm.tools.Shortcut;
17
18/**
19 * Manages actions to jump from one marker to the next for layers that show markers
20 * ({@link org.openstreetmap.josm.gui.layer.geoimage.GeoImageLayer},
21 * {@link org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer}).
22 *
23 * Registers global multi-key shortcuts and offers actions for the right-click menu of
24 * the layers.
25 */
26public final class JumpToMarkerActions {
27
28 /**
29 * Interface for a layer that displays markers and supports jumping from
30 * one marker to the next.
31 */
32 public interface JumpToMarkerLayer {
33 /**
34 * Jump (move the viewport) to the next marker.
35 */
36 void jumpToNextMarker();
37
38 /**
39 * Jump (move the viewport) to the previous marker.
40 */
41 void jumpToPreviousMarker();
42 }
43
44 private JumpToMarkerActions() {
45 // Hide default constructor for utils classes
46 }
47
48 private static volatile JumpToNextMarker jumpToNextMarkerAction;
49 private static volatile JumpToPreviousMarker jumpToPreviousMarkerAction;
50
51 /**
52 * Initialize the actions, register shortcuts.
53 */
54 public static void initialize() {
55 jumpToNextMarkerAction = new JumpToNextMarker(null);
56 jumpToPreviousMarkerAction = new JumpToPreviousMarker(null);
57 MultikeyActionsHandler.getInstance().addAction(jumpToNextMarkerAction);
58 MultikeyActionsHandler.getInstance().addAction(jumpToPreviousMarkerAction);
59 }
60
61 /**
62 * Unregister the actions.
63 */
64 public static void unregisterActions() {
65 MultikeyActionsHandler.getInstance().removeAction(jumpToNextMarkerAction);
66 MultikeyActionsHandler.getInstance().removeAction(jumpToPreviousMarkerAction);
67 }
68
69 private abstract static class JumpToMarker extends AbstractAction implements MultikeyShortcutAction {
70
71 private final transient JumpToMarkerLayer layer;
72 private final transient Shortcut multikeyShortcut;
73 private transient WeakReference<Layer> lastLayer;
74
75 JumpToMarker(JumpToMarkerLayer layer, Shortcut shortcut) {
76 this.layer = layer;
77 this.multikeyShortcut = shortcut;
78 this.multikeyShortcut.setAccelerator(this);
79 }
80
81 protected final void setLastLayer(Layer l) {
82 lastLayer = new WeakReference<>(l);
83 }
84
85 @Override
86 public Shortcut getMultikeyShortcut() {
87 return multikeyShortcut;
88 }
89
90 @Override
91 public void actionPerformed(ActionEvent e) {
92 execute(layer);
93 }
94
95 @Override
96 public void executeMultikeyAction(int index, boolean repeat) {
97 Layer l = LayerListDialog.getLayerForIndex(index);
98 if (l != null) {
99 if (l instanceof JumpToMarkerLayer) {
100 execute((JumpToMarkerLayer) l);
101 }
102 } else if (repeat && lastLayer != null) {
103 l = lastLayer.get();
104 if (LayerListDialog.isLayerValid(l) && l instanceof JumpToMarkerLayer) {
105 execute((JumpToMarkerLayer) l);
106 }
107 }
108 }
109
110 protected abstract void execute(JumpToMarkerLayer l);
111
112 @Override
113 public List<MultikeyInfo> getMultikeyCombinations() {
114 return LayerListDialog.getLayerInfoByClass(JumpToMarkerLayer.class);
115 }
116
117 @Override
118 public MultikeyInfo getLastMultikeyAction() {
119 if (lastLayer != null)
120 return LayerListDialog.getLayerInfo(lastLayer.get());
121 else
122 return null;
123 }
124 }
125
126 /**
127 * Go to the next marker in a layer
128 */
129 public static final class JumpToNextMarker extends JumpToMarker {
130
131 /**
132 * Create a new {@link JumpToNextMarker} action
133 * @param layer The layer to use when jumping to the next marker
134 */
135 public JumpToNextMarker(JumpToMarkerLayer layer) {
136 super(layer, Shortcut.registerShortcut("core_multikey:nextMarker", tr("Multikey: {0}", tr("Next marker")),
137 KeyEvent.VK_J, Shortcut.ALT_CTRL));
138 putValue(SHORT_DESCRIPTION, tr("Jump to next marker"));
139 putValue(NAME, tr("Jump to next marker"));
140 }
141
142 @Override
143 protected void execute(JumpToMarkerLayer l) {
144 l.jumpToNextMarker();
145 setLastLayer((Layer) l);
146 }
147 }
148
149 /**
150 * Go to the previous marker in a layer
151 */
152 public static final class JumpToPreviousMarker extends JumpToMarker {
153
154 /**
155 * Create a new {@link JumpToPreviousMarker} action
156 * @param layer The layer to use when jumping to the previous marker
157 */
158 public JumpToPreviousMarker(JumpToMarkerLayer layer) {
159 super(layer, Shortcut.registerShortcut("core_multikey:previousMarker", tr("Multikey: {0}", tr("Previous marker")),
160 KeyEvent.VK_P, Shortcut.ALT_CTRL));
161 putValue(SHORT_DESCRIPTION, tr("Jump to previous marker"));
162 putValue(NAME, tr("Jump to previous marker"));
163 }
164
165 @Override
166 protected void execute(JumpToMarkerLayer l) {
167 l.jumpToPreviousMarker();
168 setLastLayer((Layer) l);
169 }
170 }
171}
Note: See TracBrowser for help on using the repository browser.