source: osm/applications/editors/josm/plugins/CommandLine/src/CommandLine/PointAction.java@ 33984

Last change on this file since 33984 was 33984, checked in by donvip, 7 years ago

fix #josm15743 - Broken selection in commandline plugin (patch by langoor)

File size: 6.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package CommandLine;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.AWTEvent;
7import java.awt.Cursor;
8import java.awt.EventQueue;
9import java.awt.Point;
10import java.awt.Toolkit;
11import java.awt.event.AWTEventListener;
12import java.awt.event.KeyEvent;
13import java.awt.event.MouseEvent;
14import java.util.ArrayList;
15
16import javax.swing.JOptionPane;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.actions.mapmode.MapMode;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.data.osm.Node;
22import org.openstreetmap.josm.data.osm.OsmPrimitive;
23import org.openstreetmap.josm.gui.MainApplication;
24import org.openstreetmap.josm.gui.MapFrame;
25import org.openstreetmap.josm.tools.ImageProvider;
26import org.openstreetmap.josm.tools.Logging;
27
28public class PointAction extends MapMode implements AWTEventListener {
29 private final CommandLine parentPlugin;
30 private final Cursor cursorCrosshair;
31 private final Cursor cursorJoinNode;
32 private Cursor currentCursor;
33 private Point mousePos;
34 private Node nearestNode;
35 private final ArrayList<String> pointList;
36 private boolean isCtrlDown;
37
38 public PointAction(CommandLine parentPlugin) {
39 super(null, "addsegment.png", null, ImageProvider.getCursor("crosshair", null));
40 this.parentPlugin = parentPlugin;
41 cursorCrosshair = ImageProvider.getCursor("crosshair", null);
42 cursorJoinNode = ImageProvider.getCursor("crosshair", "joinnode");
43 currentCursor = cursorCrosshair;
44 nearestNode = null;
45 pointList = new ArrayList<>();
46 }
47
48 @Override public void enterMode() {
49 super.enterMode();
50 if (getLayerManager().getEditDataSet() == null) {
51 MainApplication.getMap().selectSelectTool(false);
52 return;
53 }
54 currentCursor = cursorCrosshair;
55 MainApplication.getMap().mapView.addMouseListener(this);
56 MainApplication.getMap().mapView.addMouseMotionListener(this);
57 try {
58 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
59 } catch (SecurityException ex) {
60 Logging.warn(ex);
61 }
62 }
63
64 @Override public void exitMode() {
65 super.exitMode();
66 MainApplication.getMap().mapView.removeMouseListener(this);
67 MainApplication.getMap().mapView.removeMouseMotionListener(this);
68 try {
69 Toolkit.getDefaultToolkit().removeAWTEventListener(this);
70 } catch (SecurityException ex) {
71 Logging.warn(ex);
72 }
73 }
74
75 @Override public void mousePressed(MouseEvent e) {
76 if (e.getButton() == MouseEvent.BUTTON1) {
77 if (isCtrlDown) {
78 if (pointList.size() > 0) {
79 pointList.remove(pointList.size() - 1);
80 updateTextEdit();
81 }
82 } else {
83 LatLon coor;
84 if (nearestNode == null)
85 coor = MainApplication.getMap().mapView.getLatLon(e.getX(), e.getY());
86 else
87 coor = nearestNode.getCoor();
88 if (coor.isOutSideWorld()) {
89 JOptionPane.showMessageDialog(Main.parent, tr("Can not draw outside of the world."));
90 return;
91 }
92 String point = String.valueOf(coor.getX()) + "," + String.valueOf(coor.getY());
93 int maxInstances = parentPlugin.currentCommand.parameters.get(parentPlugin.currentCommand.currentParameterNum).maxInstances;
94 if (maxInstances == 1) {
95 parentPlugin.loadParameter(point, true);
96 } else {
97 if (pointList.size() < maxInstances || maxInstances == 0) {
98 pointList.add(point);
99 updateTextEdit();
100 } else
101 Logging.info("Maximum instances!");
102 }
103 }
104 }
105 }
106
107 @Override
108 public void mouseMoved(MouseEvent e) {
109 if (!MainApplication.getMap().mapView.isActiveLayerDrawable())
110 return;
111 processMouseEvent(e);
112 updCursor();
113 MainApplication.getMap().mapView.repaint();
114 }
115
116 @Override
117 public void eventDispatched(AWTEvent arg0) {
118 if (!(arg0 instanceof KeyEvent))
119 return;
120 KeyEvent ev = (KeyEvent) arg0;
121 isCtrlDown = (ev.getModifiersEx() & KeyEvent.CTRL_DOWN_MASK) != 0;
122 if (ev.getKeyCode() == KeyEvent.VK_ESCAPE && ev.getID() == KeyEvent.KEY_PRESSED) {
123 ev.consume();
124 cancelDrawing();
125 }
126 }
127
128 private void updCursor() {
129 if (mousePos != null) {
130 if (!MainApplication.isDisplayingMapView())
131 return;
132 nearestNode = MainApplication.getMap().mapView.getNearestNode(mousePos, OsmPrimitive::isUsable);
133 if (nearestNode != null) {
134 setCursor(cursorJoinNode);
135 } else {
136 setCursor(cursorCrosshair);
137 }
138 }
139 }
140
141 private void processMouseEvent(MouseEvent e) {
142 if (e != null) {
143 mousePos = e.getPoint();
144 }
145 }
146
147 private void setCursor(final Cursor c) {
148 if (currentCursor.equals(c))
149 return;
150 try {
151 // We invoke this to prevent strange things from happening
152 EventQueue.invokeLater(() -> {
153 // Don't change cursor when mode has changed already
154 if (!(MainApplication.getMap().mapMode instanceof PointAction))
155 return;
156 MainApplication.getMap().mapView.setCursor(c);
157 });
158 currentCursor = c;
159 } catch (Exception e) {
160 Logging.warn(e);
161 }
162 }
163
164 public void cancelDrawing() {
165 if (!MainApplication.isDisplayingMapView())
166 return;
167 MapFrame map = MainApplication.getMap();
168 map.statusLine.setHeading(-1);
169 map.statusLine.setAngle(-1);
170 map.mapView.repaint();
171 updateStatusLine();
172 parentPlugin.abortInput();
173 }
174
175 public String currentValue() {
176 String out = "";
177 boolean first = true;
178 for (String point : pointList) {
179 if (!first)
180 out += ";";
181 out += point;
182 first = false;
183 }
184 return out;
185 }
186
187 private void updateTextEdit() {
188 Parameter currentParameter = parentPlugin.currentCommand.parameters.get(parentPlugin.currentCommand.currentParameterNum);
189 String prefix = tr(currentParameter.description);
190 prefix += parentPlugin.commandSymbol;
191 String value = currentValue();
192 parentPlugin.textField.setText(prefix + value);
193 }
194}
Note: See TracBrowser for help on using the repository browser.