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