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

Last change on this file since 10997 was 8836, checked in by Don-vip, 9 years ago

fix Checkstyle issues

  • Property svn:eol-style set to native
File size: 4.4 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
18public 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 Layer layer;
48 private final transient Shortcut multikeyShortcut;
49 private transient WeakReference<Layer> lastLayer;
50
51 JumpToMarker(JumpToMarkerLayer layer, Shortcut shortcut) {
52 this.layer = (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(l);
77 }
78 } else if (repeat && lastLayer != null) {
79 l = lastLayer.get();
80 if (LayerListDialog.isLayerValid(l)) {
81 execute(l);
82 }
83 }
84 }
85
86 protected abstract void execute(Layer 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(Layer l) {
113 ((JumpToMarkerLayer) l).jumpToNextMarker();
114 setLastLayer(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(Layer l) {
129 ((JumpToMarkerLayer) l).jumpToPreviousMarker();
130 setLastLayer(l);
131 }
132 }
133}
Note: See TracBrowser for help on using the repository browser.