1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.tracer2;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | //import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.Cursor;
|
---|
8 | import java.awt.Point;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.InputEvent;
|
---|
11 | import java.awt.event.KeyEvent;
|
---|
12 | import java.awt.event.KeyListener;
|
---|
13 | import java.awt.event.MouseEvent;
|
---|
14 | import java.awt.event.MouseListener;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.Collection;
|
---|
17 | import java.util.LinkedList;
|
---|
18 | import java.util.List;
|
---|
19 |
|
---|
20 | import javax.swing.JDialog;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
24 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
25 | import org.openstreetmap.josm.command.Command;
|
---|
26 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
27 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
28 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
29 | import org.openstreetmap.josm.data.osm.Node;
|
---|
30 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
31 | import org.openstreetmap.josm.data.osm.Way;
|
---|
32 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
33 | import org.openstreetmap.josm.gui.MapView;
|
---|
34 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
35 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
36 | import org.openstreetmap.josm.plugins.tracer2.preferences.ServerParam;
|
---|
37 | import org.openstreetmap.josm.plugins.tracer2.preferences.ServerParamList;
|
---|
38 | import org.openstreetmap.josm.plugins.tracer2.preferences.ServerParamSelectDialog;
|
---|
39 | import org.openstreetmap.josm.plugins.tracer2.server.GetTrace;
|
---|
40 | import org.openstreetmap.josm.plugins.tracer2.server.GetVersion;
|
---|
41 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
42 | import org.openstreetmap.josm.tools.Logging;
|
---|
43 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
44 | import org.xml.sax.SAXException;
|
---|
45 |
|
---|
46 | class TracerAction extends MapMode implements MouseListener, KeyListener {
|
---|
47 | private static final long serialVersionUID = 1L;
|
---|
48 | private static boolean s_bServerVersionOK = false;
|
---|
49 |
|
---|
50 | protected boolean m_bCancel;
|
---|
51 | private boolean m_bCtrl; // if pressed no tag is added + changes and connection are made to ways without tag
|
---|
52 | private boolean m_bAlt; //
|
---|
53 | private boolean m_bShift; // if pressed the new way will be add to the current selected
|
---|
54 | private boolean m_bEnter = false;
|
---|
55 |
|
---|
56 | private TagValues m_oTagValues = new TagValues();
|
---|
57 |
|
---|
58 | TracerPlugin m_oPlugin;
|
---|
59 |
|
---|
60 | TracerAction() {
|
---|
61 | super(tr("Tracer2"), "tracer2-sml", tr("Tracer2."),
|
---|
62 | Shortcut.registerShortcut("tools:tracer2", tr("More tools: {0}", tr("Tracer2")), KeyEvent.VK_T, Shortcut.DIRECT),
|
---|
63 | getCursor());
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void keyPressed(KeyEvent e) {
|
---|
68 | Collection<OsmPrimitive> selection = getLayerManager().getEditDataSet().getSelected();
|
---|
69 | List<Command> commands = new ArrayList<>();
|
---|
70 |
|
---|
71 | if (checkActiveServerParam() == false) return;
|
---|
72 |
|
---|
73 | switch (e.getKeyCode()) {
|
---|
74 | case 37: // left
|
---|
75 | m_oTagValues.left();
|
---|
76 | break;
|
---|
77 | case 38: // up
|
---|
78 | m_oTagValues.up();
|
---|
79 | break;
|
---|
80 | case 39: // right
|
---|
81 | m_oTagValues.right();
|
---|
82 | break;
|
---|
83 | case 40: // down
|
---|
84 | m_oTagValues.down();
|
---|
85 | break;
|
---|
86 | default:
|
---|
87 | return;
|
---|
88 | }
|
---|
89 |
|
---|
90 | if (selection.isEmpty()) {
|
---|
91 | return;
|
---|
92 | }
|
---|
93 |
|
---|
94 | String strTag = m_oTagValues.getTag();
|
---|
95 | String strTagValue = m_oTagValues.getTagValue();
|
---|
96 |
|
---|
97 | if (strTag != null && strTagValue != null) {
|
---|
98 | commands.add(new ChangePropertyCommand(selection, strTag, strTagValue));
|
---|
99 |
|
---|
100 | if (!commands.isEmpty()) {
|
---|
101 | UndoRedoHandler.getInstance().add(new SequenceCommand(tr("Change tag {0} to {1}", strTag, strTagValue), commands));
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void keyReleased(KeyEvent e) {
|
---|
108 | }
|
---|
109 |
|
---|
110 | @Override
|
---|
111 | public void keyTyped(KeyEvent e) {
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public void enterMode() {
|
---|
116 | m_bEnter = true;
|
---|
117 |
|
---|
118 | // is not working hear
|
---|
119 | // because if JOSM exit it is called too
|
---|
120 | //checkActiveServerParam();
|
---|
121 |
|
---|
122 | if (!isEnabled()) {
|
---|
123 | return;
|
---|
124 | }
|
---|
125 | super.enterMode();
|
---|
126 | MapView mapView = MainApplication.getMap().mapView;
|
---|
127 | mapView.setCursor(getCursor());
|
---|
128 | mapView.addMouseListener(this);
|
---|
129 | mapView.addKeyListener(this);
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | public void exitMode() {
|
---|
134 | m_bEnter = false;
|
---|
135 |
|
---|
136 | super.exitMode();
|
---|
137 | MapView mapView = MainApplication.getMap().mapView;
|
---|
138 | mapView.removeMouseListener(this);
|
---|
139 | mapView.removeKeyListener(this);
|
---|
140 | }
|
---|
141 |
|
---|
142 | private static Cursor getCursor() {
|
---|
143 | return ImageProvider.getCursor("crosshair", "tracer2-sml");
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected void traceAsync(Point clickPoint) {
|
---|
147 | m_bCancel = false;
|
---|
148 | /**
|
---|
149 | * Positional data
|
---|
150 | */
|
---|
151 | final LatLon pos = MainApplication.getMap().mapView.getLatLon(clickPoint.x, clickPoint.y);
|
---|
152 |
|
---|
153 | try {
|
---|
154 | PleaseWaitRunnable tracerTask = new PleaseWaitRunnable(tr("Tracing")) {
|
---|
155 | @Override
|
---|
156 | protected void realRun() throws SAXException {
|
---|
157 | traceSync(pos, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | protected void finish() {
|
---|
162 | }
|
---|
163 |
|
---|
164 | @Override
|
---|
165 | protected void cancel() {
|
---|
166 | TracerAction.this.cancel();
|
---|
167 | }
|
---|
168 | };
|
---|
169 | Thread executeTraceThread = new Thread(tracerTask);
|
---|
170 | executeTraceThread.start();
|
---|
171 | } catch (Exception e) {
|
---|
172 | Logging.error(e);
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | private void tagBuilding(Way way) {
|
---|
177 | String strTag = m_oTagValues.getTag();
|
---|
178 | String strTagValue = m_oTagValues.getTagValue();
|
---|
179 |
|
---|
180 | if (strTag != null && strTagValue != null && !m_bCtrl) {
|
---|
181 | way.put(strTag, strTagValue);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | private boolean checkServerVersion() {
|
---|
186 | int nMajor = 1;
|
---|
187 | int nMinor = 1;
|
---|
188 |
|
---|
189 | if (s_bServerVersionOK == false) {
|
---|
190 | GetVersion oGetVersion = new GetVersion();
|
---|
191 | oGetVersion.start();
|
---|
192 |
|
---|
193 | int nRetray = 500; // 5 seconds
|
---|
194 |
|
---|
195 | while (oGetVersion.isAlive() && nRetray > 0) {
|
---|
196 | try {
|
---|
197 | Thread.sleep(10);
|
---|
198 | } catch (Exception e) {
|
---|
199 | break;
|
---|
200 | }
|
---|
201 | nRetray--;
|
---|
202 | }
|
---|
203 |
|
---|
204 | if (oGetVersion.m_nVersionMajor < 0 || oGetVersion.m_nVersionMinor < 0) {
|
---|
205 | return false;
|
---|
206 | }
|
---|
207 | if (oGetVersion.m_nVersionMajor != nMajor) {
|
---|
208 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
---|
209 | tr("The Tracer2Server version isn''t compatible with this plugin. Please download version {0} from\n{1}.", nMajor + ".x",
|
---|
210 | "http://sourceforge.net/projects/tracer2server/"), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
211 | return false;
|
---|
212 | }
|
---|
213 | if (oGetVersion.m_nVersionMinor < nMinor) {
|
---|
214 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(),
|
---|
215 | tr("New version of Tracer2Server is available. For best results please upgrade to version {0}.", nMajor + "." + nMinor),
|
---|
216 | tr("Information"), JOptionPane.INFORMATION_MESSAGE);
|
---|
217 | }
|
---|
218 | s_bServerVersionOK = true;
|
---|
219 | }
|
---|
220 | return true;
|
---|
221 | }
|
---|
222 |
|
---|
223 | private boolean checkActiveServerParam() {
|
---|
224 | if (checkServerVersion() == false) {
|
---|
225 | return false;
|
---|
226 | }
|
---|
227 | if (m_bEnter == true || TracerPlugin.s_oPlugin.m_oParamList.getActivParam() == null) {
|
---|
228 |
|
---|
229 | ServerParamList listParam = TracerPlugin.s_oPlugin.m_oParamList;
|
---|
230 | List<ServerParam> listEnableParam = listParam.getEnableParamList();
|
---|
231 |
|
---|
232 | if (listEnableParam == null || listEnableParam.size() == 0) {
|
---|
233 | listParam.setActivParam(null);
|
---|
234 | JOptionPane.showMessageDialog(MainApplication.getMainFrame(), tr("No set of parameter is active!"), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
235 | return false;
|
---|
236 | }
|
---|
237 | if (listEnableParam.size() == 1) {
|
---|
238 | ServerParam param = listEnableParam.get(0);
|
---|
239 | listParam.setActivParam(param);
|
---|
240 | m_oTagValues.readBuildingTags(param);
|
---|
241 | return true;
|
---|
242 | }
|
---|
243 |
|
---|
244 | ServerParamSelectDialog dialog = new ServerParamSelectDialog(listEnableParam, listParam.getActivParam());
|
---|
245 |
|
---|
246 | if (dialog.getShow()) {
|
---|
247 | JOptionPane pane = new JOptionPane(dialog, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION);
|
---|
248 | JDialog dlg = pane.createDialog(MainApplication.getMainFrame(), tr("Tracer2") + " - " + tr("Select parameter"));
|
---|
249 | dlg.setVisible(true);
|
---|
250 | Object obj = pane.getValue();
|
---|
251 | dlg.dispose();
|
---|
252 | if (obj != null && ((Integer) obj) == JOptionPane.OK_OPTION) {
|
---|
253 | TracerPlugin.s_oPlugin.m_oParamList.setActivParam(dialog.getSelectedParam());
|
---|
254 | } else {
|
---|
255 | return false;
|
---|
256 | }
|
---|
257 | }
|
---|
258 | }
|
---|
259 | ServerParam param = TracerPlugin.s_oPlugin.m_oParamList.getActivParam();
|
---|
260 | if (param == null) {
|
---|
261 | return false;
|
---|
262 | }
|
---|
263 | m_bEnter = false;
|
---|
264 | m_oTagValues.readBuildingTags(param);
|
---|
265 | return true;
|
---|
266 | }
|
---|
267 |
|
---|
268 | private void traceSync(LatLon pos, ProgressMonitor progressMonitor) {
|
---|
269 | Collection<Command> commands = new LinkedList<>();
|
---|
270 |
|
---|
271 | progressMonitor.beginTask(null, 3);
|
---|
272 | try {
|
---|
273 | ArrayList<LatLon> coordList;
|
---|
274 |
|
---|
275 | if (checkActiveServerParam() == false) return;
|
---|
276 |
|
---|
277 | ServerParam param = TracerPlugin.s_oPlugin.m_oParamList.getActivParam();
|
---|
278 | GetTrace oTraceSimple = new GetTrace(pos, param);
|
---|
279 | oTraceSimple.start();
|
---|
280 | try {
|
---|
281 | while (oTraceSimple.isAlive()) {
|
---|
282 | Thread.sleep(50);
|
---|
283 | if (m_bCancel == true) {
|
---|
284 | oTraceSimple.interrupt();
|
---|
285 | break;
|
---|
286 | }
|
---|
287 | }
|
---|
288 | coordList = oTraceSimple.m_listLatLon;
|
---|
289 | } catch (Exception e) {
|
---|
290 | coordList = new ArrayList<>();
|
---|
291 | }
|
---|
292 |
|
---|
293 | if (m_bCancel == true || coordList.size() == 0) {
|
---|
294 | return;
|
---|
295 | }
|
---|
296 |
|
---|
297 | // make nodes a way
|
---|
298 | Way way = new Way();
|
---|
299 | Node firstNode = null;
|
---|
300 | for (LatLon coord : coordList) {
|
---|
301 | Node node = new Node(coord);
|
---|
302 | if (firstNode == null) {
|
---|
303 | firstNode = node;
|
---|
304 | }
|
---|
305 | //commands.add(new AddCommand(node));
|
---|
306 | way.addNode(node);
|
---|
307 | }
|
---|
308 | way.addNode(firstNode);
|
---|
309 |
|
---|
310 | tagBuilding(way);
|
---|
311 |
|
---|
312 | // connect to other buildings
|
---|
313 | commands.add(ConnectWays.connect(way, pos, param, m_bCtrl, m_bAlt));
|
---|
314 |
|
---|
315 | if (!commands.isEmpty()) {
|
---|
316 | String strCommand;
|
---|
317 | if (ConnectWays.s_bAddNewWay == true) {
|
---|
318 | strCommand = tr("Tracer2: add a way with {0} points", coordList.size());
|
---|
319 | } else {
|
---|
320 | strCommand = tr("Tracer2: modify way to {0} points", coordList.size());
|
---|
321 | }
|
---|
322 | UndoRedoHandler.getInstance().add(new SequenceCommand(strCommand, commands));
|
---|
323 |
|
---|
324 | if (m_bShift) {
|
---|
325 | getLayerManager().getEditDataSet().addSelected(ConnectWays.s_oWay);
|
---|
326 | } else {
|
---|
327 | getLayerManager().getEditDataSet().setSelected(ConnectWays.s_oWay);
|
---|
328 | }
|
---|
329 | } else {
|
---|
330 | System.out.println("Failed");
|
---|
331 | }
|
---|
332 |
|
---|
333 | } finally {
|
---|
334 | progressMonitor.finishTask();
|
---|
335 | }
|
---|
336 | }
|
---|
337 |
|
---|
338 | public void cancel() {
|
---|
339 | m_bCancel = true;
|
---|
340 | }
|
---|
341 |
|
---|
342 | @Override
|
---|
343 | public void mouseClicked(MouseEvent e) {
|
---|
344 | }
|
---|
345 |
|
---|
346 | @Override
|
---|
347 | public void mouseEntered(MouseEvent e) {
|
---|
348 | }
|
---|
349 |
|
---|
350 | @Override
|
---|
351 | public void mouseExited(MouseEvent e) {
|
---|
352 | }
|
---|
353 |
|
---|
354 | @Override
|
---|
355 | public void mousePressed(MouseEvent e) {
|
---|
356 | if (!MainApplication.getMap().mapView.isActiveLayerDrawable()) {
|
---|
357 | return;
|
---|
358 | }
|
---|
359 | requestFocusInMapView();
|
---|
360 | updateKeyModifiers(e);
|
---|
361 | if (e.getButton() == MouseEvent.BUTTON1) {
|
---|
362 | traceAsync(e.getPoint());
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | @Override
|
---|
367 | protected void updateKeyModifiers(MouseEvent e) {
|
---|
368 | m_bCtrl = (e.getModifiers() & ActionEvent.CTRL_MASK) != 0;
|
---|
369 | m_bAlt = (e.getModifiers() & (ActionEvent.ALT_MASK | InputEvent.ALT_GRAPH_MASK)) != 0;
|
---|
370 | m_bShift = (e.getModifiers() & ActionEvent.SHIFT_MASK) != 0;
|
---|
371 | }
|
---|
372 |
|
---|
373 | @Override
|
---|
374 | public void mouseReleased(MouseEvent e) {
|
---|
375 | }
|
---|
376 |
|
---|
377 | }
|
---|