1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.mapmode;
|
---|
3 |
|
---|
4 | import java.awt.Cursor;
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.InputEvent;
|
---|
7 | import java.awt.event.MouseEvent;
|
---|
8 | import java.awt.event.MouseListener;
|
---|
9 | import java.awt.event.MouseMotionListener;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.Main;
|
---|
12 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
13 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
14 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
15 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
16 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * A class implementing MapMode is able to be selected as an mode for map editing.
|
---|
20 | * As example scrolling the map is a MapMode, connecting Nodes to new Ways
|
---|
21 | * is another.
|
---|
22 | *
|
---|
23 | * MapModes should register/deregister all necessary listeners on the map's view control.
|
---|
24 | */
|
---|
25 | public abstract class MapMode extends JosmAction implements MouseListener, MouseMotionListener {
|
---|
26 | protected final Cursor cursor;
|
---|
27 | protected boolean ctrl;
|
---|
28 | protected boolean alt;
|
---|
29 | protected boolean shift;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Constructor for mapmodes without an menu
|
---|
33 | * @param mapFrame unused but kept for plugin compatibility. Can be {@code null}
|
---|
34 | */
|
---|
35 | public MapMode(String name, String iconName, String tooltip, Shortcut shortcut, MapFrame mapFrame, Cursor cursor) {
|
---|
36 | super(name, "mapmode/"+iconName, tooltip, shortcut, false);
|
---|
37 | this.cursor = cursor;
|
---|
38 | putValue("active", Boolean.FALSE);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Constructor for mapmodes with an menu (no shortcut will be registered)
|
---|
43 | * @param mapFrame unused but kept for plugin compatibility. Can be {@code null}
|
---|
44 | */
|
---|
45 | public MapMode(String name, String iconName, String tooltip, MapFrame mapFrame, Cursor cursor) {
|
---|
46 | putValue(NAME, name);
|
---|
47 | putValue(SMALL_ICON, ImageProvider.get("mapmode", iconName));
|
---|
48 | putValue(SHORT_DESCRIPTION, tooltip);
|
---|
49 | this.cursor = cursor;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Makes this map mode active.
|
---|
54 | */
|
---|
55 | public void enterMode() {
|
---|
56 | putValue("active", Boolean.TRUE);
|
---|
57 | Main.map.mapView.setNewCursor(cursor, this);
|
---|
58 | updateStatusLine();
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Makes this map mode inactive.
|
---|
63 | */
|
---|
64 | public void exitMode() {
|
---|
65 | putValue("active", Boolean.FALSE);
|
---|
66 | Main.map.mapView.resetCursor(this);
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected void updateStatusLine() {
|
---|
70 | Main.map.statusLine.setHelpText(getModeHelpText());
|
---|
71 | Main.map.statusLine.repaint();
|
---|
72 | }
|
---|
73 |
|
---|
74 | public String getModeHelpText() {
|
---|
75 | return "";
|
---|
76 | }
|
---|
77 | /**
|
---|
78 | * Call selectMapMode(this) on the parent mapFrame.
|
---|
79 | */
|
---|
80 | @Override
|
---|
81 | public void actionPerformed(ActionEvent e) {
|
---|
82 | if (Main.isDisplayingMapView()) {
|
---|
83 | Main.map.selectMapMode(this);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Determines if layer {@code l} is supported by this map mode.
|
---|
89 | * By default, all tools will work with all layers.
|
---|
90 | * Can be overwritten to require a special type of layer
|
---|
91 | * @param l layer
|
---|
92 | * @return {@code true} if the layer is supported by this map mode
|
---|
93 | */
|
---|
94 | public boolean layerIsSupported(Layer l) {
|
---|
95 | return l != null;
|
---|
96 | }
|
---|
97 |
|
---|
98 | protected void updateKeyModifiers(InputEvent e) {
|
---|
99 | updateKeyModifiers(e.getModifiers());
|
---|
100 | }
|
---|
101 |
|
---|
102 | protected void updateKeyModifiers(MouseEvent e) {
|
---|
103 | updateKeyModifiers(e.getModifiers());
|
---|
104 | }
|
---|
105 |
|
---|
106 | protected void updateKeyModifiers(int modifiers) {
|
---|
107 | ctrl = (modifiers & ActionEvent.CTRL_MASK) != 0;
|
---|
108 | alt = (modifiers & (ActionEvent.ALT_MASK | InputEvent.ALT_GRAPH_MASK)) != 0;
|
---|
109 | shift = (modifiers & ActionEvent.SHIFT_MASK) != 0;
|
---|
110 | }
|
---|
111 |
|
---|
112 | protected void requestFocusInMapView() {
|
---|
113 | if (isEnabled()) {
|
---|
114 | // request focus in order to enable the expected keyboard shortcuts (see #8710)
|
---|
115 | Main.map.mapView.requestFocus();
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | @Override
|
---|
120 | public void mouseReleased(MouseEvent e) {
|
---|
121 | requestFocusInMapView();
|
---|
122 | }
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public void mouseExited(MouseEvent e) {
|
---|
126 | // Do nothing
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | public void mousePressed(MouseEvent e) {
|
---|
131 | requestFocusInMapView();
|
---|
132 | }
|
---|
133 |
|
---|
134 | @Override
|
---|
135 | public void mouseClicked(MouseEvent e) {
|
---|
136 | // Do nothing
|
---|
137 | }
|
---|
138 |
|
---|
139 | @Override
|
---|
140 | public void mouseEntered(MouseEvent e) {
|
---|
141 | // Do nothing
|
---|
142 | }
|
---|
143 |
|
---|
144 | @Override
|
---|
145 | public void mouseMoved(MouseEvent e) {
|
---|
146 | // Do nothing
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void mouseDragged(MouseEvent e) {
|
---|
151 | // Do nothing
|
---|
152 | }
|
---|
153 | }
|
---|