source: osm/applications/editors/josm/plugins/CommandLine/src/CommandLine/AnyAction.java@ 27551

Last change on this file since 27551 was 27551, checked in by stoecker, 13 years ago

ImproveWayAccuracy is in core, some small fixes

File size: 5.4 KB
Line 
1/*
2 * AnyAction.java
3 *
4 * Copyright 2010 Hind <foxhind@gmail.com>
5 *
6 */
7
8package CommandLine;
9
10import static org.openstreetmap.josm.tools.I18n.tr;
11
12import java.awt.AWTEvent;
13import java.awt.Cursor;
14import java.awt.EventQueue;
15import java.awt.event.AWTEventListener;
16import java.awt.event.KeyEvent;
17import java.awt.event.MouseEvent;
18import java.awt.Point;
19import java.awt.Toolkit;
20import java.util.Collection;
21import javax.swing.JOptionPane;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.actions.mapmode.MapMode;
25import org.openstreetmap.josm.data.coor.LatLon;
26import org.openstreetmap.josm.data.osm.DataSet;
27import org.openstreetmap.josm.data.osm.Node;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.data.osm.PrimitiveId;
30import org.openstreetmap.josm.gui.MapFrame;
31import org.openstreetmap.josm.tools.ImageProvider;
32
33public class AnyAction extends MapMode implements AWTEventListener {
34 private CommandLine parentPlugin;
35 final private Cursor cursorNormal, cursorActive;
36 private Cursor currentCursor;
37 private Point mousePos;
38 private OsmPrimitive nearestPrimitive;
39 private boolean isCtrlDown;
40
41 public AnyAction(MapFrame mapFrame, CommandLine parentPlugin) {
42 super(null, "addsegment.png", null, mapFrame, ImageProvider.getCursor("normal", "selection"));
43 this.parentPlugin = parentPlugin;
44 cursorNormal = ImageProvider.getCursor("normal", "selection");
45 cursorActive = ImageProvider.getCursor("normal", "joinnode");
46 currentCursor = cursorNormal;
47 nearestPrimitive = null;
48 }
49
50 @Override public void enterMode() {
51 super.enterMode();
52 currentCursor = cursorNormal;
53 Main.map.mapView.addMouseListener(this);
54 Main.map.mapView.addMouseMotionListener(this);
55 try {
56 Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
57 } catch (SecurityException ex) {
58 }
59 }
60
61 @Override public void exitMode() {
62 super.exitMode();
63 Main.map.mapView.removeMouseListener(this);
64 Main.map.mapView.removeMouseMotionListener(this);
65 try {
66 Toolkit.getDefaultToolkit().removeAWTEventListener(this);
67 } catch (SecurityException ex) {
68 }
69 }
70
71 @Override
72 public void mouseMoved(MouseEvent e) {
73 if (!Main.map.mapView.isActiveLayerDrawable())
74 return;
75 processMouseEvent(e);
76 updCursor();
77 Main.map.mapView.repaint();
78 super.mouseMoved(e);
79 }
80
81 @Override
82 public void mousePressed(MouseEvent e) {
83 if (!Main.map.mapView.isActiveLayerDrawable())
84 return;
85 processMouseEvent(e);
86 if (nearestPrimitive != null) {
87 if (isCtrlDown) {
88 Main.main.getCurrentDataSet().clearSelection(nearestPrimitive);
89 Main.map.mapView.repaint();
90 }
91 else {
92 int maxInstances = parentPlugin.currentCommand.parameters.get(parentPlugin.currentCommand.currentParameterNum).maxInstances;
93 switch (maxInstances) {
94 case 0:
95 Main.main.getCurrentDataSet().addSelected(nearestPrimitive);
96 Main.map.mapView.repaint();
97 break;
98 case 1:
99 Main.main.getCurrentDataSet().addSelected(nearestPrimitive);
100 Main.map.mapView.repaint();
101 parentPlugin.loadParameter(nearestPrimitive, true);
102 exitMode();
103 break;
104 default:
105 if (Main.main.getCurrentDataSet().getSelected().size() < maxInstances) {
106 Main.main.getCurrentDataSet().addSelected(nearestPrimitive);
107 Main.map.mapView.repaint();
108 }
109 else
110 System.out.println("Maximum instances!");
111 }
112 }
113 }
114 super.mousePressed(e);
115 }
116
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 (!Main.isDisplayingMapView())
131 return;
132 nearestPrimitive = Main.map.mapView.getNearestNodeOrWay(mousePos, OsmPrimitive.isUsablePredicate, false);
133 if (nearestPrimitive != null) {
134 setCursor(cursorActive);
135 }
136 else {
137 setCursor(cursorNormal);
138 }
139 }
140 }
141
142 private void processMouseEvent(MouseEvent e) {
143 if (e != null) { mousePos = e.getPoint(); }
144 }
145
146 private void setCursor(final Cursor c) {
147 if (currentCursor.equals(c))
148 return;
149 try {
150 // We invoke this to prevent strange things from happening
151 EventQueue.invokeLater(new Runnable() {
152 public void run() {
153 // Don't change cursor when mode has changed already
154 if (!(Main.map.mapMode instanceof AnyAction))
155 return;
156 Main.map.mapView.setCursor(c);
157 }
158 });
159 currentCursor = c;
160 } catch (Exception e) {
161 }
162 }
163
164 public void cancelDrawing() {
165 if (Main.map == null || Main.map.mapView == null)
166 return;
167 Main.map.statusLine.setHeading(-1);
168 Main.map.statusLine.setAngle(-1);
169 Main.map.mapView.repaint();
170 updateStatusLine();
171 parentPlugin.abortInput();
172 }
173}
Note: See TracBrowser for help on using the repository browser.