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

Last change on this file since 12603 was 12460, checked in by bastiK, 7 years ago

see #14794 - javadoc

  • Property svn:eol-style set to native
File size: 5.2 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.tools.MultikeyActionsHandler;
15import org.openstreetmap.josm.tools.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 public static final class JumpToNextMarker extends JumpToMarker {
127
128 public JumpToNextMarker(JumpToMarkerLayer layer) {
129 super(layer, Shortcut.registerShortcut("core_multikey:nextMarker", tr("Multikey: {0}", tr("Next marker")),
130 KeyEvent.VK_J, Shortcut.ALT_CTRL));
131 putValue(SHORT_DESCRIPTION, tr("Jump to next marker"));
132 putValue(NAME, tr("Jump to next marker"));
133 }
134
135 @Override
136 protected void execute(JumpToMarkerLayer l) {
137 l.jumpToNextMarker();
138 setLastLayer((Layer) l);
139 }
140 }
141
142 public static final class JumpToPreviousMarker extends JumpToMarker {
143
144 public JumpToPreviousMarker(JumpToMarkerLayer layer) {
145 super(layer, Shortcut.registerShortcut("core_multikey:previousMarker", tr("Multikey: {0}", tr("Previous marker")),
146 KeyEvent.VK_P, Shortcut.ALT_CTRL));
147 putValue(SHORT_DESCRIPTION, tr("Jump to previous marker"));
148 putValue(NAME, tr("Jump to previous marker"));
149 }
150
151 @Override
152 protected void execute(JumpToMarkerLayer l) {
153 l.jumpToPreviousMarker();
154 setLastLayer((Layer) l);
155 }
156 }
157}
Note: See TracBrowser for help on using the repository browser.