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