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