source: josm/trunk/src/org/openstreetmap/josm/actions/mapmode/MapMode.java@ 9059

Last change on this file since 9059 was 9059, checked in by Don-vip, 8 years ago

checkstyle

  • 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.actions.mapmode;
3
4import java.awt.Cursor;
5import java.awt.event.ActionEvent;
6import java.awt.event.InputEvent;
7import java.awt.event.MouseEvent;
8import java.awt.event.MouseListener;
9import java.awt.event.MouseMotionListener;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.actions.JosmAction;
13import org.openstreetmap.josm.gui.MapFrame;
14import org.openstreetmap.josm.gui.layer.Layer;
15import org.openstreetmap.josm.tools.ImageProvider;
16import 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 */
25public 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 /**
79 * Call selectMapMode(this) on the parent mapFrame.
80 */
81 @Override
82 public void actionPerformed(ActionEvent e) {
83 if (Main.isDisplayingMapView()) {
84 Main.map.selectMapMode(this);
85 }
86 }
87
88 /**
89 * Determines if layer {@code l} is supported by this map mode.
90 * By default, all tools will work with all layers.
91 * Can be overwritten to require a special type of layer
92 * @param l layer
93 * @return {@code true} if the layer is supported by this map mode
94 */
95 public boolean layerIsSupported(Layer l) {
96 return l != null;
97 }
98
99 protected void updateKeyModifiers(InputEvent e) {
100 updateKeyModifiers(e.getModifiers());
101 }
102
103 protected void updateKeyModifiers(MouseEvent e) {
104 updateKeyModifiers(e.getModifiers());
105 }
106
107 protected void updateKeyModifiers(int modifiers) {
108 ctrl = (modifiers & ActionEvent.CTRL_MASK) != 0;
109 alt = (modifiers & (ActionEvent.ALT_MASK | InputEvent.ALT_GRAPH_MASK)) != 0;
110 shift = (modifiers & ActionEvent.SHIFT_MASK) != 0;
111 }
112
113 protected void requestFocusInMapView() {
114 if (isEnabled()) {
115 // request focus in order to enable the expected keyboard shortcuts (see #8710)
116 Main.map.mapView.requestFocus();
117 }
118 }
119
120 @Override
121 public void mouseReleased(MouseEvent e) {
122 requestFocusInMapView();
123 }
124
125 @Override
126 public void mouseExited(MouseEvent e) {
127 // Do nothing
128 }
129
130 @Override
131 public void mousePressed(MouseEvent e) {
132 requestFocusInMapView();
133 }
134
135 @Override
136 public void mouseClicked(MouseEvent e) {
137 // Do nothing
138 }
139
140 @Override
141 public void mouseEntered(MouseEvent e) {
142 // Do nothing
143 }
144
145 @Override
146 public void mouseMoved(MouseEvent e) {
147 // Do nothing
148 }
149
150 @Override
151 public void mouseDragged(MouseEvent e) {
152 // Do nothing
153 }
154}
Note: See TracBrowser for help on using the repository browser.