1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.lang.ref.WeakReference;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.AbstractAction;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
14 | import org.openstreetmap.josm.tools.MultikeyActionsHandler;
|
---|
15 | import org.openstreetmap.josm.tools.MultikeyShortcutAction;
|
---|
16 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
17 |
|
---|
18 | public final class JumpToMarkerActions {
|
---|
19 |
|
---|
20 | public interface JumpToMarkerLayer {
|
---|
21 | void jumpToNextMarker();
|
---|
22 |
|
---|
23 | void jumpToPreviousMarker();
|
---|
24 | }
|
---|
25 |
|
---|
26 | private JumpToMarkerActions() {
|
---|
27 | // Hide default constructor for utils classes
|
---|
28 | }
|
---|
29 |
|
---|
30 | private static volatile JumpToNextMarker jumpToNextMarkerAction;
|
---|
31 | private static volatile JumpToPreviousMarker jumpToPreviousMarkerAction;
|
---|
32 |
|
---|
33 | public static void initialize() {
|
---|
34 | jumpToNextMarkerAction = new JumpToNextMarker(null);
|
---|
35 | jumpToPreviousMarkerAction = new JumpToPreviousMarker(null);
|
---|
36 | MultikeyActionsHandler.getInstance().addAction(jumpToNextMarkerAction);
|
---|
37 | MultikeyActionsHandler.getInstance().addAction(jumpToPreviousMarkerAction);
|
---|
38 | }
|
---|
39 |
|
---|
40 | public static void unregisterActions() {
|
---|
41 | MultikeyActionsHandler.getInstance().removeAction(jumpToNextMarkerAction);
|
---|
42 | MultikeyActionsHandler.getInstance().removeAction(jumpToPreviousMarkerAction);
|
---|
43 | }
|
---|
44 |
|
---|
45 | private abstract static class JumpToMarker extends AbstractAction implements MultikeyShortcutAction {
|
---|
46 |
|
---|
47 | private final transient JumpToMarkerLayer layer;
|
---|
48 | private final transient Shortcut multikeyShortcut;
|
---|
49 | private transient WeakReference<Layer> lastLayer;
|
---|
50 |
|
---|
51 | JumpToMarker(JumpToMarkerLayer layer, Shortcut shortcut) {
|
---|
52 | this.layer = layer;
|
---|
53 | this.multikeyShortcut = shortcut;
|
---|
54 | this.multikeyShortcut.setAccelerator(this);
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected final void setLastLayer(Layer l) {
|
---|
58 | lastLayer = new WeakReference<>(l);
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public Shortcut getMultikeyShortcut() {
|
---|
63 | return multikeyShortcut;
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void actionPerformed(ActionEvent e) {
|
---|
68 | execute(layer);
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public void executeMultikeyAction(int index, boolean repeat) {
|
---|
73 | Layer l = LayerListDialog.getLayerForIndex(index);
|
---|
74 | if (l != null) {
|
---|
75 | if (l instanceof JumpToMarkerLayer) {
|
---|
76 | execute((JumpToMarkerLayer) l);
|
---|
77 | }
|
---|
78 | } else if (repeat && lastLayer != null) {
|
---|
79 | l = lastLayer.get();
|
---|
80 | if (LayerListDialog.isLayerValid(l) && l instanceof JumpToMarkerLayer) {
|
---|
81 | execute((JumpToMarkerLayer) l);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | protected abstract void execute(JumpToMarkerLayer l);
|
---|
87 |
|
---|
88 | @Override
|
---|
89 | public List<MultikeyInfo> getMultikeyCombinations() {
|
---|
90 | return LayerListDialog.getLayerInfoByClass(JumpToMarkerLayer.class);
|
---|
91 | }
|
---|
92 |
|
---|
93 | @Override
|
---|
94 | public MultikeyInfo getLastMultikeyAction() {
|
---|
95 | if (lastLayer != null)
|
---|
96 | return LayerListDialog.getLayerInfo(lastLayer.get());
|
---|
97 | else
|
---|
98 | return null;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | public static final class JumpToNextMarker extends JumpToMarker {
|
---|
103 |
|
---|
104 | public JumpToNextMarker(JumpToMarkerLayer layer) {
|
---|
105 | super(layer, Shortcut.registerShortcut("core_multikey:nextMarker", tr("Multikey: {0}", tr("Next marker")),
|
---|
106 | KeyEvent.VK_J, Shortcut.ALT_CTRL));
|
---|
107 | putValue(SHORT_DESCRIPTION, tr("Jump to next marker"));
|
---|
108 | putValue(NAME, tr("Jump to next marker"));
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | protected void execute(JumpToMarkerLayer l) {
|
---|
113 | l.jumpToNextMarker();
|
---|
114 | setLastLayer((Layer) l);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | public static final class JumpToPreviousMarker extends JumpToMarker {
|
---|
119 |
|
---|
120 | public JumpToPreviousMarker(JumpToMarkerLayer layer) {
|
---|
121 | super(layer, Shortcut.registerShortcut("core_multikey:previousMarker", tr("Multikey: {0}", tr("Previous marker")),
|
---|
122 | KeyEvent.VK_P, Shortcut.ALT_CTRL));
|
---|
123 | putValue(SHORT_DESCRIPTION, tr("Jump to previous marker"));
|
---|
124 | putValue(NAME, tr("Jump to previous marker"));
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | protected void execute(JumpToMarkerLayer l) {
|
---|
129 | l.jumpToPreviousMarker();
|
---|
130 | setLastLayer((Layer) l);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|