source: osm/applications/editors/josm/plugins/public_transport/src/public_transport/StopImporterAction.java@ 20729

Last change on this file since 20729 was 20729, checked in by roland, 14 years ago

Public Transport: stop type selection added

File size: 49.0 KB
Line 
1package public_transport;
2
3import static org.openstreetmap.josm.tools.I18n.marktr;
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Container;
7import java.awt.Frame;
8import java.awt.GridBagConstraints;
9import java.awt.GridBagLayout;
10import java.awt.event.ActionEvent;
11import java.io.File;
12import java.io.FileInputStream;
13import java.io.FileNotFoundException;
14import java.io.InputStream;
15import java.io.IOException;
16import java.text.DecimalFormat;
17import java.text.Format;
18import java.util.Collections;
19import java.util.Iterator;
20import java.util.Vector;
21import java.util.zip.GZIPInputStream;
22
23import javax.swing.DefaultListModel;
24import javax.swing.JButton;
25import javax.swing.JComboBox;
26import javax.swing.JDialog;
27import javax.swing.JFileChooser;
28import javax.swing.JLabel;
29import javax.swing.JList;
30import javax.swing.JOptionPane;
31import javax.swing.JPanel;
32import javax.swing.JScrollPane;
33import javax.swing.JTabbedPane;
34import javax.swing.JTable;
35import javax.swing.JTextField;
36import javax.swing.ListSelectionModel;
37import javax.swing.event.ListSelectionEvent;
38import javax.swing.event.ListSelectionListener;
39import javax.swing.event.TableModelEvent;
40import javax.swing.event.TableModelListener;
41import javax.swing.table.DefaultTableModel;
42
43import org.openstreetmap.josm.Main;
44import org.openstreetmap.josm.actions.JosmAction;
45import org.openstreetmap.josm.command.Command;
46import org.openstreetmap.josm.command.ChangeCommand;
47import org.openstreetmap.josm.command.DeleteCommand;
48import org.openstreetmap.josm.data.coor.LatLon;
49import org.openstreetmap.josm.data.gpx.GpxData;
50import org.openstreetmap.josm.data.gpx.GpxTrack;
51import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
52import org.openstreetmap.josm.data.gpx.WayPoint;
53import org.openstreetmap.josm.data.osm.DataSet;
54import org.openstreetmap.josm.data.osm.Node;
55import org.openstreetmap.josm.data.osm.OsmPrimitive;
56import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
57import org.openstreetmap.josm.io.GpxReader;
58
59import org.xml.sax.SAXException;
60
61public class StopImporterAction extends JosmAction
62{
63
64 private class TracksLSL implements ListSelectionListener
65 {
66 StopImporterAction root = null;
67
68 public TracksLSL(StopImporterAction sia)
69 {
70 root = sia;
71 }
72
73 public void valueChanged(ListSelectionEvent e)
74 {
75 root.tracksSelectionChanged();
76 }
77 };
78
79 private class TrackReference
80 implements Comparable< TrackReference >, TableModelListener
81 {
82 public GpxTrack track;
83 public StoplistTableModel stoplistTM;
84 public String stopwatchStart;
85 public String gpsStartTime;
86 public String gpsSyncTime;
87 public double timeWindow;
88 public double threshold;
89
90 public TrackReference(GpxTrack track)
91 {
92 this.track = track;
93 this.stoplistTM = new StoplistTableModel(this);
94 this.stopwatchStart = "00:00:00";
95 this.gpsStartTime = null;
96 this.gpsSyncTime = null;
97 if (track != null)
98 {
99 Iterator< GpxTrackSegment > siter = track.getSegments().iterator();
100 while ((siter.hasNext()) && (this.gpsSyncTime == null))
101 {
102 Iterator< WayPoint > witer = siter.next().getWayPoints().iterator();
103 if (witer.hasNext())
104 {
105 this.gpsStartTime = witer.next().getString("time");
106 if (this.gpsStartTime != null)
107 this.gpsSyncTime = this.gpsStartTime.substring(11, 19);
108 }
109 }
110 if (this.gpsSyncTime == null)
111 {
112 JOptionPane.showMessageDialog
113 (null, "The GPX file doesn't contain valid trackpoints. "
114 + "Please use a GPX file that has trackpoints.", "GPX File Trouble",
115 JOptionPane.ERROR_MESSAGE);
116
117 this.gpsStartTime = "1970-01-01T00:00:00Z";
118 this.gpsSyncTime = this.stopwatchStart;
119 }
120 }
121 else
122 this.gpsSyncTime = this.stopwatchStart;
123 this.timeWindow = 20;
124 this.threshold = 20;
125 }
126
127 public int compareTo(TrackReference tr)
128 {
129 String name = (String)track.getAttributes().get("name");
130 String tr_name = (String)tr.track.getAttributes().get("name");
131 if (name != null)
132 {
133 if (tr_name == null)
134 return -1;
135 return name.compareTo(tr_name);
136 }
137 return 1;
138 }
139
140 public String toString()
141 {
142 String buf = (String)track.getAttributes().get("name");
143 if (buf == null)
144 return "unnamed";
145 return buf;
146 }
147
148 public void tableChanged(TableModelEvent e)
149 {
150 if (e.getType() == TableModelEvent.UPDATE)
151 {
152 double time = parseTime
153 ((String)stoplistTM.getValueAt(e.getFirstRow(), 0));
154 if (time < 0)
155 {
156 JOptionPane.showMessageDialog
157 (null, "Can't parse a time from this string.", "Invalid value",
158 JOptionPane.ERROR_MESSAGE);
159 return;
160 }
161
162 LatLon latLon = computeCoor(time);
163
164 if (stoplistTM.nodes.elementAt(e.getFirstRow()) == null)
165 {
166 Node node = createNode
167 (latLon, (String)stoplistTM.getValueAt(e.getFirstRow(), 1));
168 stoplistTM.nodes.set(e.getFirstRow(), node);
169 }
170 else
171 {
172 Node node = new Node(stoplistTM.nodes.elementAt(e.getFirstRow()));
173 node.setCoor(latLon);
174 node.put("name", (String)stoplistTM.getValueAt(e.getFirstRow(), 1));
175 Command cmd = new ChangeCommand(stoplistTM.nodes.elementAt(e.getFirstRow()), node);
176 if (cmd != null) {
177 Main.main.undoRedo.add(cmd);
178 }
179 }
180 }
181 }
182
183 public LatLon computeCoor(double time)
184 {
185 double gpsSyncTime = parseTime(this.gpsSyncTime);
186 double dGpsStartTime = parseTime(gpsStartTime);
187 if (gpsSyncTime < dGpsStartTime - 12*60*60)
188 gpsSyncTime += 24*60*60;
189 double timeDelta = gpsSyncTime - parseTime(stopwatchStart);
190 time += timeDelta;
191
192 WayPoint wayPoint = null;
193 WayPoint lastWayPoint = null;
194 double wayPointTime = 0;
195 double lastWayPointTime = 0;
196 Iterator< GpxTrackSegment > siter = track.getSegments().iterator();
197 while (siter.hasNext())
198 {
199 Iterator< WayPoint > witer = siter.next().getWayPoints().iterator();
200 while (witer.hasNext())
201 {
202 wayPoint = witer.next();
203 String startTime = wayPoint.getString("time");
204 wayPointTime = parseTime(startTime.substring(11, 19));
205 if (startTime.substring(11, 19).compareTo(gpsStartTime.substring(11, 19)) == -1)
206 wayPointTime += 24*60*60;
207 if (wayPointTime >= time)
208 break;
209 lastWayPoint = wayPoint;
210 lastWayPointTime = wayPointTime;
211 }
212 if (wayPointTime >= time)
213 break;
214 }
215
216 double lat = 0;
217 if ((wayPointTime == lastWayPointTime) || (lastWayPoint == null))
218 lat = wayPoint.getCoor().lat();
219 else
220 lat = wayPoint.getCoor().lat()
221 *(time - lastWayPointTime)/(wayPointTime - lastWayPointTime)
222 + lastWayPoint.getCoor().lat()
223 *(wayPointTime - time)/(wayPointTime - lastWayPointTime);
224 double lon = 0;
225 if ((wayPointTime == lastWayPointTime) || (lastWayPoint == null))
226 lon = wayPoint.getCoor().lon();
227 else
228 lon = wayPoint.getCoor().lon()
229 *(time - lastWayPointTime)/(wayPointTime - lastWayPointTime)
230 + lastWayPoint.getCoor().lon()
231 *(wayPointTime - time)/(wayPointTime - lastWayPointTime);
232
233 return new LatLon(lat, lon);
234 }
235
236 public void relocateNodes()
237 {
238 for (int i = 0; i < stoplistTM.nodes.size(); ++i)
239 {
240 Node node = stoplistTM.nodes.elementAt(i);
241 if (node == null)
242 continue;
243
244 double time = parseTime
245 ((String)stoplistTM.getValueAt(i, 0));
246 LatLon latLon = computeCoor(time);
247
248 Node newNode = new Node(node);
249 newNode.setCoor(latLon);
250 Command cmd = new ChangeCommand(node, newNode);
251 if (cmd != null)
252 {
253 Main.main.undoRedo.add(cmd);
254 }
255 }
256 }
257
258 public void suggestStops()
259 {
260 Vector< WayPoint > wayPoints = new Vector< WayPoint >();
261 Iterator< GpxTrackSegment > siter = track.getSegments().iterator();
262 while (siter.hasNext())
263 {
264 Iterator< WayPoint > witer = siter.next().getWayPoints().iterator();
265 while (witer.hasNext())
266 wayPoints.add(witer.next());
267 }
268 Vector< Double > wayPointsDist = new Vector< Double >(wayPoints.size());
269
270 int i = 0;
271 double time = -48*60*60;
272 double dGpsStartTime = parseTime(gpsStartTime);
273 while ((i < wayPoints.size()) && (time < dGpsStartTime + timeWindow/2))
274 {
275 if (wayPoints.elementAt(i).getString("time") != null)
276 time = parseTime(wayPoints.elementAt(i).getString("time").substring(11,19));
277 if (time < dGpsStartTime)
278 time += 24*60*60;
279 wayPointsDist.add(Double.valueOf(Double.POSITIVE_INFINITY));
280 ++i;
281 }
282 while (i < wayPoints.size())
283 {
284 int j = i;
285 double time2 = time;
286 while ((j > 0) && (time - timeWindow/2 < time2))
287 {
288 --j;
289 if (wayPoints.elementAt(j).getString("time") != null)
290 time2 = parseTime(wayPoints.elementAt(j).getString("time").substring(11,19));
291 if (time2 < dGpsStartTime)
292 time2 += 24*60*60;
293 }
294 int k = i + 1;
295 time2 = time;
296 while ((k < wayPoints.size()) && (time + timeWindow/2 > time2))
297 {
298 if (wayPoints.elementAt(k).getString("time") != null)
299 time2 = parseTime(wayPoints.elementAt(k).getString("time").substring(11,19));
300 if (time2 < dGpsStartTime)
301 time2 += 24*60*60;
302 ++k;
303 }
304
305 if (j < k)
306 {
307 double dist = 0;
308 LatLon latLonI = wayPoints.elementAt(i).getCoor();
309 for (int l = j; l < k; ++l)
310 {
311 double distL = latLonI.greatCircleDistance(wayPoints.elementAt(l).getCoor());
312 if (distL > dist)
313 dist = distL;
314 }
315 wayPointsDist.add(Double.valueOf(dist));
316 }
317 else
318 wayPointsDist.add(Double.valueOf(Double.POSITIVE_INFINITY));
319
320 if (wayPoints.elementAt(i).getString("time") != null)
321 time = parseTime(wayPoints.elementAt(i).getString("time").substring(11,19));
322 if (time < dGpsStartTime)
323 time += 24*60*60;
324 ++i;
325 }
326
327 Vector< Node > toDelete = new Vector< Node >();
328 for (i = 0; i < stoplistTM.getRowCount(); ++i)
329 {
330 if ((Node)stoplistTM.nodes.elementAt(i) != null)
331 toDelete.add((Node)stoplistTM.nodes.elementAt(i));
332 }
333 if (!toDelete.isEmpty())
334 {
335 Command cmd = DeleteCommand.delete
336 (Main.main.getEditLayer(), toDelete);
337 if (cmd == null)
338 return;
339 Main.main.undoRedo.add(cmd);
340 }
341 stoplistTM.clear();
342
343 LatLon lastStopCoor = null;
344 for (i = 1; i < wayPoints.size()-1; ++i)
345 {
346 if (wayPointsDist.elementAt(i).doubleValue() >= threshold)
347 continue;
348 if ((wayPointsDist.elementAt(i).compareTo(wayPointsDist.elementAt(i-1)) != -1)
349 || (wayPointsDist.elementAt(i).compareTo(wayPointsDist.elementAt(i+1)) != -1))
350 continue;
351
352 LatLon latLon = wayPoints.elementAt(i).getCoor();
353 if ((lastStopCoor != null) && (lastStopCoor.greatCircleDistance(latLon) < threshold))
354 continue;
355
356 if (wayPoints.elementAt(i).getString("time") != null)
357 {
358 time = parseTime(wayPoints.elementAt(i).getString("time").substring(11,19));
359 double gpsSyncTime = parseTime(this.gpsSyncTime);
360 if (gpsSyncTime < dGpsStartTime - 12*60*60)
361 gpsSyncTime += 24*60*60;
362 double timeDelta = gpsSyncTime - parseTime(stopwatchStart);
363 time -= timeDelta;
364 stoplistTM.insertRow(-1, timeOf(time));
365 Node node = createNode(latLon, "");
366 stoplistTM.nodes.set(stoplistTM.getRowCount()-1, node);
367 }
368
369 lastStopCoor = latLon;
370 }
371 }
372 };
373
374 private class NodeSortEntry implements Comparable< NodeSortEntry >
375 {
376 public Node node = null;
377 public String time = null;
378 public String name = null;
379 public double startTime = 0;
380
381 public NodeSortEntry(Node node, String time, String name, double startTime)
382 {
383 this.node = node;
384 this.time = time;
385 this.name = name;
386 }
387
388 public int compareTo(NodeSortEntry nse)
389 {
390 double time = parseTime(this.time);
391 if (time - startTime > 12*60*60)
392 time -= 24*60*60;
393
394 double nseTime = parseTime(nse.time);
395 if (nseTime - startTime > 12*60*60)
396 nseTime -= 24*60*60;
397
398 if (time < nseTime)
399 return -1;
400 else if (time > nseTime)
401 return 1;
402 else
403 return 0;
404 }
405 };
406
407 private class StoplistTableModel extends DefaultTableModel
408 {
409 public Vector< Node > nodes = new Vector< Node >();
410
411 public StoplistTableModel(TrackReference tr)
412 {
413 addColumn("Time");
414 addColumn("Name");
415 addTableModelListener(tr);
416 }
417
418 public boolean isCellEditable(int row, int column) {
419 return true;
420 }
421
422 public void addRow(Object[] obj) {
423 throw new UnsupportedOperationException();
424 }
425
426 public void insertRow(int insPos, Object[] obj) {
427 throw new UnsupportedOperationException();
428 }
429
430 public void addRow(String time) {
431 insertRow(-1, time);
432 }
433
434 public void insertRow(int insPos, String time)
435 {
436 insertRow(insPos, null, time, "");
437 }
438
439 public void insertRow(int insPos, Node node, String time, String name)
440 {
441 String[] buf = { "", "" };
442 buf[0] = time;
443 buf[1] = name;
444 if (insPos == -1)
445 {
446 nodes.addElement(node);
447 super.addRow(buf);
448 }
449 else
450 {
451 nodes.insertElementAt(node, insPos);
452 super.insertRow(insPos, buf);
453 }
454 }
455
456 public void clear()
457 {
458 nodes.clear();
459 super.setRowCount(0);
460 }
461 };
462
463 private class WaypointTableModel extends DefaultTableModel
464 implements TableModelListener
465 {
466 public Vector< Node > nodes = new Vector< Node >();
467 public Vector< LatLon > coors = new Vector< LatLon >();
468
469 public WaypointTableModel()
470 {
471 addColumn("Time");
472 addColumn("Stopname");
473 addTableModelListener(this);
474 }
475
476 public boolean isCellEditable(int row, int column)
477 {
478 if (column == 1)
479 return true;
480 return false;
481 }
482
483 public void addRow(Object[] obj)
484 {
485 throw new UnsupportedOperationException();
486 }
487
488 public void insertRow(int insPos, Object[] obj)
489 {
490 throw new UnsupportedOperationException();
491 }
492
493 public void addRow(WayPoint wp)
494 {
495 insertRow(-1, wp);
496 }
497
498 public void insertRow(int insPos, WayPoint wp)
499 {
500 String[] buf = { "", "" };
501 buf[0] = wp.getString("time");
502 if (buf[0] == null)
503 buf[0] = "";
504 buf[1] = wp.getString("name");
505 if (buf[1] == null)
506 buf[1] = "";
507
508 Node node = createNode(wp.getCoor(), buf[1]);
509
510 if (insPos == -1)
511 {
512 nodes.addElement(node);
513 coors.addElement(wp.getCoor());
514 super.addRow(buf);
515 }
516 else
517 {
518 nodes.insertElementAt(node, insPos);
519 coors.insertElementAt(wp.getCoor(), insPos);
520 super.insertRow(insPos, buf);
521 }
522 }
523
524 public void clear()
525 {
526 nodes.clear();
527 super.setRowCount(0);
528 }
529
530 public void tableChanged(TableModelEvent e)
531 {
532 if (e.getType() == TableModelEvent.UPDATE)
533 {
534 if (nodes.elementAt(e.getFirstRow()) != null)
535 {
536 Node node = nodes.elementAt(e.getFirstRow());
537 node.put("name", (String)getValueAt(e.getFirstRow(), 1));
538 }
539 }
540 }
541 };
542
543 private static JDialog jDialog = null;
544 private static JTabbedPane tabbedPane = null;
545 private static DefaultListModel tracksListModel = null;
546 private static JComboBox cbStoptype = null;
547 private static JList tracksList = null;
548 private static JTextField tfGPSTimeStart = null;
549 private static JTextField tfStopwatchStart = null;
550 private static JTextField tfTimeWindow = null;
551 private static JTextField tfThreshold = null;
552 private static JTable stoplistTable = null;
553 private static JTable waypointTable = null;
554 private static GpxData data = null;
555 private static TrackReference currentTrack = null;
556 private static WaypointTableModel waypointTM = null;
557
558 public StopImporterAction()
559 {
560 super(tr("Create Stops from GPX ..."), null,
561 tr("Create Stops from a GPX file"), null, true);
562 }
563
564 public void actionPerformed(ActionEvent event)
565 {
566 Frame frame = JOptionPane.getFrameForComponent(Main.parent);
567 DataSet mainDataSet = Main.main.getCurrentDataSet();
568
569 if (jDialog == null)
570 {
571 jDialog = new JDialog(frame, "Create Stops from GPX", false);
572 tabbedPane = new JTabbedPane();
573 JPanel tabTracks = new JPanel();
574 tabbedPane.addTab(marktr("Tracks"), tabTracks);
575 JPanel tabSettings = new JPanel();
576 tabbedPane.addTab(marktr("Settings"), tabSettings);
577 JPanel tabStops = new JPanel();
578 tabbedPane.addTab(marktr("Stops"), tabStops);
579 JPanel tabWaypoints = new JPanel();
580 tabbedPane.addTab(marktr("Waypoints"), tabWaypoints);
581 tabbedPane.setEnabledAt(0, true);
582 tabbedPane.setEnabledAt(1, false);
583 tabbedPane.setEnabledAt(2, false);
584 tabbedPane.setEnabledAt(3, true);
585 jDialog.add(tabbedPane);
586
587 //Tracks Tab
588 Container contentPane = tabTracks;
589 GridBagLayout gridbag = new GridBagLayout();
590 GridBagConstraints layoutCons = new GridBagConstraints();
591 contentPane.setLayout(gridbag);
592
593 JLabel label = new JLabel("Tracks in this GPX file:");
594
595 layoutCons.gridx = 0;
596 layoutCons.gridy = 0;
597 layoutCons.gridwidth = 3;
598 layoutCons.weightx = 0.0;
599 layoutCons.weighty = 0.0;
600 layoutCons.fill = GridBagConstraints.BOTH;
601 gridbag.setConstraints(label, layoutCons);
602 contentPane.add(label);
603
604 tracksListModel = new DefaultListModel();
605 tracksList = new JList(tracksListModel);
606 JScrollPane rpListSP = new JScrollPane(tracksList);
607 String[] data = {"1", "2", "3", "4", "5", "6"};
608 tracksListModel.copyInto(data);
609 tracksList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
610 tracksList.addListSelectionListener(new TracksLSL(this));
611
612 layoutCons.gridx = 0;
613 layoutCons.gridy = 1;
614 layoutCons.gridwidth = 3;
615 layoutCons.weightx = 1.0;
616 layoutCons.weighty = 1.0;
617 layoutCons.fill = GridBagConstraints.BOTH;
618 gridbag.setConstraints(rpListSP, layoutCons);
619 contentPane.add(rpListSP);
620
621 //Settings Tab
622 /*Container*/ contentPane = tabSettings;
623 /*GridBagLayout*/ gridbag = new GridBagLayout();
624 /*GridBagConstraints*/ layoutCons = new GridBagConstraints();
625 contentPane.setLayout(gridbag);
626
627 /*JLabel*/ label = new JLabel("Type of stops to add");
628
629 layoutCons.gridx = 0;
630 layoutCons.gridy = 0;
631 layoutCons.gridwidth = 2;
632 layoutCons.weightx = 0.0;
633 layoutCons.weighty = 0.0;
634 layoutCons.fill = GridBagConstraints.BOTH;
635 gridbag.setConstraints(label, layoutCons);
636 contentPane.add(label);
637
638 cbStoptype = new JComboBox();
639 cbStoptype.setEditable(false);
640 cbStoptype.addItem("bus");
641 cbStoptype.addItem("tram");
642 cbStoptype.addItem("light_rail");
643 cbStoptype.addItem("subway");
644 cbStoptype.addItem("rail");
645 cbStoptype.setActionCommand("stopImporter.settingsStoptype");
646 cbStoptype.addActionListener(this);
647
648 layoutCons.gridx = 0;
649 layoutCons.gridy = 1;
650 layoutCons.gridwidth = 1;
651 layoutCons.weightx = 0.0;
652 layoutCons.weighty = 0.0;
653 layoutCons.fill = GridBagConstraints.BOTH;
654 gridbag.setConstraints(cbStoptype, layoutCons);
655 contentPane.add(cbStoptype);
656
657 /*JLabel*/ label = new JLabel("Time on your GPS device");
658
659 layoutCons.gridx = 0;
660 layoutCons.gridy = 2;
661 layoutCons.gridwidth = 2;
662 layoutCons.weightx = 0.0;
663 layoutCons.weighty = 0.0;
664 layoutCons.fill = GridBagConstraints.BOTH;
665 gridbag.setConstraints(label, layoutCons);
666 contentPane.add(label);
667
668 tfGPSTimeStart = new JTextField("00:00:00", 15);
669 tfGPSTimeStart.setActionCommand("stopImporter.settingsGPSTimeStart");
670 tfGPSTimeStart.addActionListener(this);
671
672 layoutCons.gridx = 0;
673 layoutCons.gridy = 3;
674 layoutCons.gridwidth = 1;
675 layoutCons.weightx = 0.0;
676 layoutCons.weighty = 0.0;
677 layoutCons.fill = GridBagConstraints.BOTH;
678 gridbag.setConstraints(tfGPSTimeStart, layoutCons);
679 contentPane.add(tfGPSTimeStart);
680
681 /*JLabel*/ label = new JLabel("HH:MM:SS.sss");
682
683 layoutCons.gridx = 1;
684 layoutCons.gridy = 3;
685 layoutCons.gridwidth = 1;
686 layoutCons.weightx = 0.0;
687 layoutCons.weighty = 0.0;
688 layoutCons.fill = GridBagConstraints.BOTH;
689 gridbag.setConstraints(label, layoutCons);
690 contentPane.add(label);
691
692 /*JLabel*/ label = new JLabel("Time on your stopwatch");
693
694 layoutCons.gridx = 0;
695 layoutCons.gridy = 4;
696 layoutCons.gridwidth = 2;
697 layoutCons.weightx = 0.0;
698 layoutCons.weighty = 0.0;
699 layoutCons.fill = GridBagConstraints.BOTH;
700 gridbag.setConstraints(label, layoutCons);
701 contentPane.add(label);
702
703 tfStopwatchStart = new JTextField("00:00:00", 15);
704 tfStopwatchStart.setActionCommand("stopImporter.settingsStopwatchStart");
705 tfStopwatchStart.addActionListener(this);
706
707 layoutCons.gridx = 0;
708 layoutCons.gridy = 5;
709 layoutCons.gridwidth = 1;
710 layoutCons.weightx = 0.0;
711 layoutCons.weighty = 0.0;
712 layoutCons.fill = GridBagConstraints.BOTH;
713 gridbag.setConstraints(tfStopwatchStart, layoutCons);
714 contentPane.add(tfStopwatchStart);
715
716 /*JLabel*/ label = new JLabel("HH:MM:SS.sss");
717
718 layoutCons.gridx = 1;
719 layoutCons.gridy = 5;
720 layoutCons.gridwidth = 1;
721 layoutCons.weightx = 0.0;
722 layoutCons.weighty = 0.0;
723 layoutCons.fill = GridBagConstraints.BOTH;
724 gridbag.setConstraints(label, layoutCons);
725 contentPane.add(label);
726
727 /*JLabel*/ label = new JLabel("Time window");
728
729 layoutCons.gridx = 0;
730 layoutCons.gridy = 6;
731 layoutCons.gridwidth = 2;
732 layoutCons.weightx = 0.0;
733 layoutCons.weighty = 0.0;
734 layoutCons.fill = GridBagConstraints.BOTH;
735 gridbag.setConstraints(label, layoutCons);
736 contentPane.add(label);
737
738 tfTimeWindow = new JTextField("15", 4);
739 tfTimeWindow.setActionCommand("stopImporter.settingsTimeWindow");
740 tfTimeWindow.addActionListener(this);
741
742 layoutCons.gridx = 0;
743 layoutCons.gridy = 7;
744 layoutCons.gridwidth = 1;
745 layoutCons.weightx = 0.0;
746 layoutCons.weighty = 0.0;
747 layoutCons.fill = GridBagConstraints.BOTH;
748 gridbag.setConstraints(tfTimeWindow, layoutCons);
749 contentPane.add(tfTimeWindow);
750
751 /*JLabel*/ label = new JLabel("seconds");
752
753 layoutCons.gridx = 1;
754 layoutCons.gridy = 7;
755 layoutCons.gridwidth = 1;
756 layoutCons.weightx = 0.0;
757 layoutCons.weighty = 0.0;
758 layoutCons.fill = GridBagConstraints.BOTH;
759 gridbag.setConstraints(label, layoutCons);
760 contentPane.add(label);
761
762 /*JLabel*/ label = new JLabel("Move Threshold");
763
764 layoutCons.gridx = 0;
765 layoutCons.gridy = 8;
766 layoutCons.gridwidth = 2;
767 layoutCons.weightx = 0.0;
768 layoutCons.weighty = 0.0;
769 layoutCons.fill = GridBagConstraints.BOTH;
770 gridbag.setConstraints(label, layoutCons);
771 contentPane.add(label);
772
773 tfThreshold = new JTextField("20", 4);
774 tfThreshold.setActionCommand("stopImporter.settingsThreshold");
775 tfThreshold.addActionListener(this);
776
777 layoutCons.gridx = 0;
778 layoutCons.gridy = 9;
779 layoutCons.gridwidth = 1;
780 layoutCons.weightx = 0.0;
781 layoutCons.weighty = 0.0;
782 layoutCons.fill = GridBagConstraints.BOTH;
783 gridbag.setConstraints(tfThreshold, layoutCons);
784 contentPane.add(tfThreshold);
785
786 /*JLabel*/ label = new JLabel("meters");
787
788 layoutCons.gridx = 1;
789 layoutCons.gridy = 9;
790 layoutCons.gridwidth = 1;
791 layoutCons.weightx = 0.0;
792 layoutCons.weighty = 0.0;
793 layoutCons.fill = GridBagConstraints.BOTH;
794 gridbag.setConstraints(label, layoutCons);
795 contentPane.add(label);
796
797 JButton bSuggestStops = new JButton("Suggest Stops");
798 bSuggestStops.setActionCommand("stopImporter.settingsSuggestStops");
799 bSuggestStops.addActionListener(this);
800
801 layoutCons.gridx = 0;
802 layoutCons.gridy = 10;
803 layoutCons.gridwidth = 3;
804 layoutCons.weightx = 1.0;
805 layoutCons.weighty = 0.0;
806 layoutCons.fill = GridBagConstraints.BOTH;
807 gridbag.setConstraints(bSuggestStops, layoutCons);
808 contentPane.add(bSuggestStops);
809
810 //Stops Tab
811 contentPane = tabStops;
812 gridbag = new GridBagLayout();
813 layoutCons = new GridBagConstraints();
814 contentPane.setLayout(gridbag);
815
816 stoplistTable = new JTable();
817 JScrollPane tableSP = new JScrollPane(stoplistTable);
818
819 layoutCons.gridx = 0;
820 layoutCons.gridy = 0;
821 layoutCons.gridwidth = 4;
822 layoutCons.weightx = 1.0;
823 layoutCons.weighty = 1.0;
824 layoutCons.fill = GridBagConstraints.BOTH;
825 gridbag.setConstraints(tableSP, layoutCons);
826 contentPane.add(tableSP);
827
828 JButton bFind = new JButton("Find");
829 bFind.setActionCommand("stopImporter.stoplistFind");
830 bFind.addActionListener(this);
831
832 layoutCons.gridx = 0;
833 layoutCons.gridy = 1;
834 layoutCons.gridwidth = 1;
835 layoutCons.weightx = 1.0;
836 layoutCons.weighty = 0.0;
837 layoutCons.fill = GridBagConstraints.BOTH;
838 gridbag.setConstraints(bFind, layoutCons);
839 contentPane.add(bFind);
840
841 JButton bShow = new JButton("Show");
842 bShow.setActionCommand("stopImporter.stoplistShow");
843 bShow.addActionListener(this);
844
845 layoutCons.gridx = 0;
846 layoutCons.gridy = 2;
847 layoutCons.gridwidth = 1;
848 layoutCons.weightx = 1.0;
849 layoutCons.weighty = 0.0;
850 layoutCons.fill = GridBagConstraints.BOTH;
851 gridbag.setConstraints(bShow, layoutCons);
852 contentPane.add(bShow);
853
854 JButton bMark = new JButton("Mark");
855 bMark.setActionCommand("stopImporter.stoplistMark");
856 bMark.addActionListener(this);
857
858 layoutCons.gridx = 1;
859 layoutCons.gridy = 1;
860 layoutCons.gridheight = 1;
861 layoutCons.gridwidth = 1;
862 layoutCons.weightx = 1.0;
863 layoutCons.weighty = 0.0;
864 layoutCons.fill = GridBagConstraints.BOTH;
865 gridbag.setConstraints(bMark, layoutCons);
866 contentPane.add(bMark);
867
868 JButton bDetach = new JButton("Detach");
869 bDetach.setActionCommand("stopImporter.stoplistDetach");
870 bDetach.addActionListener(this);
871
872 layoutCons.gridx = 1;
873 layoutCons.gridy = 2;
874 layoutCons.gridheight = 1;
875 layoutCons.gridwidth = 1;
876 layoutCons.weightx = 1.0;
877 layoutCons.weighty = 0.0;
878 layoutCons.fill = GridBagConstraints.BOTH;
879 gridbag.setConstraints(bDetach, layoutCons);
880 contentPane.add(bDetach);
881
882 JButton bAdd = new JButton("Add");
883 bAdd.setActionCommand("stopImporter.stoplistAdd");
884 bAdd.addActionListener(this);
885
886 layoutCons.gridx = 2;
887 layoutCons.gridy = 1;
888 layoutCons.gridheight = 1;
889 layoutCons.gridwidth = 1;
890 layoutCons.weightx = 1.0;
891 layoutCons.weighty = 0.0;
892 layoutCons.fill = GridBagConstraints.BOTH;
893 gridbag.setConstraints(bAdd, layoutCons);
894 contentPane.add(bAdd);
895
896 JButton bDelete = new JButton("Delete");
897 bDelete.setActionCommand("stopImporter.stoplistDelete");
898 bDelete.addActionListener(this);
899
900 layoutCons.gridx = 2;
901 layoutCons.gridy = 2;
902 layoutCons.gridwidth = 1;
903 layoutCons.weightx = 1.0;
904 layoutCons.weighty = 0.0;
905 layoutCons.fill = GridBagConstraints.BOTH;
906 gridbag.setConstraints(bDelete, layoutCons);
907 contentPane.add(bDelete);
908
909 JButton bSort = new JButton("Sort");
910 bSort.setActionCommand("stopImporter.stoplistSort");
911 bSort.addActionListener(this);
912
913 layoutCons.gridx = 3;
914 layoutCons.gridy = 1;
915 layoutCons.gridheight = 2;
916 layoutCons.gridwidth = 1;
917 layoutCons.weightx = 1.0;
918 layoutCons.weighty = 0.0;
919 layoutCons.fill = GridBagConstraints.BOTH;
920 gridbag.setConstraints(bSort, layoutCons);
921 contentPane.add(bSort);
922
923 //Waypoints Tab
924 contentPane = tabWaypoints;
925 gridbag = new GridBagLayout();
926 layoutCons = new GridBagConstraints();
927 contentPane.setLayout(gridbag);
928
929 waypointTable = new JTable();
930 /*JScrollPane*/ tableSP = new JScrollPane(waypointTable);
931
932 layoutCons.gridx = 0;
933 layoutCons.gridy = 0;
934 layoutCons.gridwidth = 3;
935 layoutCons.weightx = 1.0;
936 layoutCons.weighty = 1.0;
937 layoutCons.fill = GridBagConstraints.BOTH;
938 gridbag.setConstraints(tableSP, layoutCons);
939 contentPane.add(tableSP);
940
941 /*JButton*/ bFind = new JButton("Find");
942 bFind.setActionCommand("stopImporter.waypointsFind");
943 bFind.addActionListener(this);
944
945 layoutCons.gridx = 0;
946 layoutCons.gridy = 1;
947 layoutCons.gridwidth = 1;
948 layoutCons.weightx = 1.0;
949 layoutCons.weighty = 0.0;
950 layoutCons.fill = GridBagConstraints.BOTH;
951 gridbag.setConstraints(bFind, layoutCons);
952 contentPane.add(bFind);
953
954 /*JButton*/ bShow = new JButton("Show");
955 bShow.setActionCommand("stopImporter.waypointsShow");
956 bShow.addActionListener(this);
957
958 layoutCons.gridx = 0;
959 layoutCons.gridy = 2;
960 layoutCons.gridwidth = 1;
961 layoutCons.weightx = 1.0;
962 layoutCons.weighty = 0.0;
963 layoutCons.fill = GridBagConstraints.BOTH;
964 gridbag.setConstraints(bShow, layoutCons);
965 contentPane.add(bShow);
966
967 /*JButton*/ bMark = new JButton("Mark");
968 bMark.setActionCommand("stopImporter.waypointsMark");
969 bMark.addActionListener(this);
970
971 layoutCons.gridx = 1;
972 layoutCons.gridy = 1;
973 layoutCons.gridheight = 1;
974 layoutCons.gridwidth = 1;
975 layoutCons.weightx = 1.0;
976 layoutCons.weighty = 0.0;
977 layoutCons.fill = GridBagConstraints.BOTH;
978 gridbag.setConstraints(bMark, layoutCons);
979 contentPane.add(bMark);
980
981 /*JButton*/ bDetach = new JButton("Detach");
982 bDetach.setActionCommand("stopImporter.waypointsDetach");
983 bDetach.addActionListener(this);
984
985 layoutCons.gridx = 1;
986 layoutCons.gridy = 2;
987 layoutCons.gridheight = 1;
988 layoutCons.gridwidth = 1;
989 layoutCons.weightx = 1.0;
990 layoutCons.weighty = 0.0;
991 layoutCons.fill = GridBagConstraints.BOTH;
992 gridbag.setConstraints(bDetach, layoutCons);
993 contentPane.add(bDetach);
994
995 /*JButton*/ bAdd = new JButton("Enable");
996 bAdd.setActionCommand("stopImporter.waypointsAdd");
997 bAdd.addActionListener(this);
998
999 layoutCons.gridx = 2;
1000 layoutCons.gridy = 1;
1001 layoutCons.gridheight = 1;
1002 layoutCons.gridwidth = 1;
1003 layoutCons.weightx = 1.0;
1004 layoutCons.weighty = 0.0;
1005 layoutCons.fill = GridBagConstraints.BOTH;
1006 gridbag.setConstraints(bAdd, layoutCons);
1007 contentPane.add(bAdd);
1008
1009 /*JButton*/ bDelete = new JButton("Disable");
1010 bDelete.setActionCommand("stopImporter.waypointsDelete");
1011 bDelete.addActionListener(this);
1012
1013 layoutCons.gridx = 2;
1014 layoutCons.gridy = 2;
1015 layoutCons.gridwidth = 1;
1016 layoutCons.weightx = 1.0;
1017 layoutCons.weighty = 0.0;
1018 layoutCons.fill = GridBagConstraints.BOTH;
1019 gridbag.setConstraints(bDelete, layoutCons);
1020 contentPane.add(bDelete);
1021
1022 jDialog.pack();
1023 jDialog.setLocationRelativeTo(frame);
1024 }
1025
1026 jDialog.setVisible(true);
1027
1028 if (tr("Create Stops from GPX ...").equals(event.getActionCommand()))
1029 {
1030 String curDir = Main.pref.get("lastDirectory");
1031 if (curDir.equals(""))
1032 {
1033 curDir = ".";
1034 }
1035 JFileChooser fc = new JFileChooser(new File(curDir));
1036 fc.setDialogTitle("Select GPX file");
1037 fc.setMultiSelectionEnabled(false);
1038
1039 int answer = fc.showOpenDialog(Main.parent);
1040 if (answer != JFileChooser.APPROVE_OPTION)
1041 return;
1042
1043 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir))
1044 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
1045
1046 importData(fc.getSelectedFile());
1047
1048 refreshData();
1049
1050/* Iterator< GpxTrack > iter = data.tracks.iterator();
1051 while (iter.hasNext())
1052 {
1053 GpxTrack track = iter.next();
1054 System.out.println("");
1055 System.out.println(track.getAttributes().get("name"));
1056 Iterator< GpxTrackSegment > siter = track.getSegments().iterator();
1057 while (siter.hasNext())
1058 {
1059 Iterator< WayPoint > witer = siter.next().getWayPoints().iterator();
1060 while (witer.hasNext())
1061 {
1062 System.out.println(witer.next());
1063 }
1064 }
1065 }*/
1066 }
1067 else if ("stopImporter.settingsGPSTimeStart"
1068 .equals(event.getActionCommand()))
1069 {
1070 if (parseTime(tfGPSTimeStart.getText()) >= 0)
1071 {
1072 if (currentTrack != null)
1073 {
1074 currentTrack.gpsSyncTime = tfGPSTimeStart.getText();
1075 currentTrack.relocateNodes();
1076 }
1077 }
1078 else
1079 {
1080 JOptionPane.showMessageDialog
1081 (null, "Can't parse a time from this string.", "Invalid value",
1082 JOptionPane.ERROR_MESSAGE);
1083 }
1084 }
1085 else if ("stopImporter.settingsStopwatchStart"
1086 .equals(event.getActionCommand()))
1087 {
1088 if (parseTime(tfStopwatchStart.getText()) >= 0)
1089 {
1090 if (currentTrack != null)
1091 {
1092 currentTrack.stopwatchStart = tfStopwatchStart.getText();
1093 currentTrack.relocateNodes();
1094 }
1095 }
1096 else
1097 {
1098 }
1099 }
1100 else if ("stopImporter.settingsTimeWindow"
1101 .equals(event.getActionCommand()))
1102 {
1103 if (currentTrack != null)
1104 currentTrack.timeWindow = Double.parseDouble(tfTimeWindow.getText());
1105 }
1106 else if ("stopImporter.settingsThreshold".equals(event.getActionCommand()))
1107 {
1108 if (currentTrack != null)
1109 currentTrack.threshold = Double.parseDouble(tfThreshold.getText());
1110 }
1111 else if ("stopImporter.settingsSuggestStops".equals(event.getActionCommand()))
1112 {
1113 currentTrack.suggestStops();
1114 }
1115 else if ("stopImporter.stoplistFind".equals(event.getActionCommand()))
1116 {
1117 if (Main.main.getCurrentDataSet() == null)
1118 return;
1119
1120 stoplistTable.clearSelection();
1121
1122 for (int i = 0; i < currentTrack.stoplistTM.getRowCount(); ++i)
1123 {
1124 if ((currentTrack.stoplistTM.nodes.elementAt(i) != null) &&
1125 (Main.main.getCurrentDataSet().isSelected(currentTrack.stoplistTM.nodes.elementAt(i))))
1126 stoplistTable.addRowSelectionInterval(i, i);
1127 }
1128 }
1129 else if ("stopImporter.stoplistShow".equals(event.getActionCommand()))
1130 {
1131 BoundingXYVisitor box = new BoundingXYVisitor();
1132 if (stoplistTable.getSelectedRowCount() > 0)
1133 {
1134 for (int i = 0; i < currentTrack.stoplistTM.getRowCount(); ++i)
1135 {
1136 if ((stoplistTable.isRowSelected(i)) &&
1137 (currentTrack.stoplistTM.nodes.elementAt(i) != null))
1138 {
1139 currentTrack.stoplistTM.nodes.elementAt(i).visit(box);
1140 }
1141 }
1142 }
1143 else
1144 {
1145 for (int i = 0; i < currentTrack.stoplistTM.getRowCount(); ++i)
1146 {
1147 if (currentTrack.stoplistTM.nodes.elementAt(i) != null)
1148 currentTrack.stoplistTM.nodes.elementAt(i).visit(box);
1149 }
1150 }
1151 if (box.getBounds() == null)
1152 return;
1153 box.enlargeBoundingBox();
1154 Main.map.mapView.recalculateCenterScale(box);
1155 }
1156 else if ("stopImporter.stoplistMark".equals(event.getActionCommand()))
1157 {
1158 OsmPrimitive[] osmp = { null };
1159 Main.main.getCurrentDataSet().setSelected(osmp);
1160 if (stoplistTable.getSelectedRowCount() > 0)
1161 {
1162 for (int i = 0; i < currentTrack.stoplistTM.getRowCount(); ++i)
1163 {
1164 if ((stoplistTable.isRowSelected(i)) &&
1165 (currentTrack.stoplistTM.nodes.elementAt(i) != null))
1166 {
1167 Main.main.getCurrentDataSet().addSelected(currentTrack.stoplistTM.nodes.elementAt(i));
1168 }
1169 }
1170 }
1171 else
1172 {
1173 for (int i = 0; i < currentTrack.stoplistTM.getRowCount(); ++i)
1174 {
1175 if (currentTrack.stoplistTM.nodes.elementAt(i) != null)
1176 Main.main.getCurrentDataSet().addSelected(currentTrack.stoplistTM.nodes.elementAt(i));
1177 }
1178 }
1179 }
1180 else if ("stopImporter.stoplistDetach".equals(event.getActionCommand()))
1181 {
1182 if (stoplistTable.getSelectedRowCount() > 0)
1183 {
1184 for (int i = 0; i < currentTrack.stoplistTM.getRowCount(); ++i)
1185 {
1186 if ((stoplistTable.isRowSelected(i)) &&
1187 (currentTrack.stoplistTM.nodes.elementAt(i) != null))
1188 {
1189 currentTrack.stoplistTM.nodes.set(i, null);
1190 }
1191 }
1192 }
1193 else
1194 {
1195 for (int i = 0; i < currentTrack.stoplistTM.getRowCount(); ++i)
1196 {
1197 if (currentTrack.stoplistTM.nodes.elementAt(i) != null)
1198 currentTrack.stoplistTM.nodes.set(i, null);
1199 }
1200 }
1201 stoplistTable.clearSelection();
1202 }
1203 else if ("stopImporter.stoplistAdd".equals(event.getActionCommand()))
1204 {
1205 int insPos = stoplistTable.getSelectedRow();
1206 if (currentTrack != null)
1207 currentTrack.stoplistTM.insertRow(insPos, "00:00:00");
1208 }
1209 else if ("stopImporter.stoplistDelete".equals(event.getActionCommand()))
1210 {
1211 Vector< Node > toDelete = new Vector< Node >();
1212 if (currentTrack == null)
1213 return;
1214 for (int i = currentTrack.stoplistTM.getRowCount()-1; i >=0; --i)
1215 {
1216 if (stoplistTable.isRowSelected(i))
1217 {
1218 if ((Node)currentTrack.stoplistTM.nodes.elementAt(i) != null)
1219 toDelete.add((Node)currentTrack.stoplistTM.nodes.elementAt(i));
1220 currentTrack.stoplistTM.nodes.removeElementAt(i);
1221 currentTrack.stoplistTM.removeRow(i);
1222 }
1223 }
1224 Command cmd = DeleteCommand.delete
1225 (Main.main.getEditLayer(), toDelete);
1226 if (cmd != null) {
1227 // cmd can be null if the user cancels dialogs DialogCommand displays
1228 Main.main.undoRedo.add(cmd);
1229 }
1230 }
1231 else if ("stopImporter.stoplistSort".equals(event.getActionCommand()))
1232 {
1233 int insPos = stoplistTable.getSelectedRow();
1234 Vector< NodeSortEntry > nodesToSort = new Vector< NodeSortEntry >();
1235 if (currentTrack == null)
1236 return;
1237 if (stoplistTable.getSelectedRowCount() > 0)
1238 {
1239 for (int i = currentTrack.stoplistTM.getRowCount()-1; i >=0; --i)
1240 {
1241 if (stoplistTable.isRowSelected(i))
1242 {
1243 nodesToSort.add(new NodeSortEntry
1244 (currentTrack.stoplistTM.nodes.elementAt(i),
1245 (String)currentTrack.stoplistTM.getValueAt(i, 0),
1246 (String)currentTrack.stoplistTM.getValueAt(i, 1),
1247 parseTime(currentTrack.stopwatchStart)));
1248 currentTrack.stoplistTM.nodes.removeElementAt(i);
1249 currentTrack.stoplistTM.removeRow(i);
1250 }
1251 }
1252 }
1253 else
1254 {
1255 for (int i = 0; i < currentTrack.stoplistTM.getRowCount(); ++i)
1256 {
1257 nodesToSort.add(new NodeSortEntry
1258 (currentTrack.stoplistTM.nodes.elementAt(i),
1259 (String)currentTrack.stoplistTM.getValueAt(i, 0),
1260 (String)currentTrack.stoplistTM.getValueAt(i, 1),
1261 parseTime(currentTrack.stopwatchStart)));
1262 }
1263 currentTrack.stoplistTM.clear();
1264 }
1265
1266 Collections.sort(nodesToSort);
1267
1268 Iterator< NodeSortEntry > iter = nodesToSort.iterator();
1269 while (iter.hasNext())
1270 {
1271 NodeSortEntry nse = iter.next();
1272 currentTrack.stoplistTM.insertRow
1273 (insPos, nse.node, nse.time, nse.name);
1274 if (insPos >= 0)
1275 ++insPos;
1276 }
1277 }
1278 else if ("stopImporter.waypointsFind".equals(event.getActionCommand()))
1279 {
1280 if (Main.main.getCurrentDataSet() == null)
1281 return;
1282
1283 waypointTable.clearSelection();
1284
1285 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1286 {
1287 if ((waypointTM.nodes.elementAt(i) != null) &&
1288 (Main.main.getCurrentDataSet().isSelected(waypointTM.nodes.elementAt(i))))
1289 waypointTable.addRowSelectionInterval(i, i);
1290 }
1291 }
1292 else if ("stopImporter.waypointsShow".equals(event.getActionCommand()))
1293 {
1294 BoundingXYVisitor box = new BoundingXYVisitor();
1295 if (waypointTable.getSelectedRowCount() > 0)
1296 {
1297 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1298 {
1299 if ((waypointTable.isRowSelected(i)) &&
1300 (waypointTM.nodes.elementAt(i) != null))
1301 {
1302 waypointTM.nodes.elementAt(i).visit(box);
1303 }
1304 }
1305 }
1306 else
1307 {
1308 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1309 {
1310 if (waypointTM.nodes.elementAt(i) != null)
1311 waypointTM.nodes.elementAt(i).visit(box);
1312 }
1313 }
1314 if (box.getBounds() == null)
1315 return;
1316 box.enlargeBoundingBox();
1317 Main.map.mapView.recalculateCenterScale(box);
1318 }
1319 else if ("stopImporter.waypointsMark".equals(event.getActionCommand()))
1320 {
1321 OsmPrimitive[] osmp = { null };
1322 Main.main.getCurrentDataSet().setSelected(osmp);
1323 if (waypointTable.getSelectedRowCount() > 0)
1324 {
1325 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1326 {
1327 if ((waypointTable.isRowSelected(i)) &&
1328 (waypointTM.nodes.elementAt(i) != null))
1329 {
1330 Main.main.getCurrentDataSet().addSelected(waypointTM.nodes.elementAt(i));
1331 }
1332 }
1333 }
1334 else
1335 {
1336 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1337 {
1338 if (waypointTM.nodes.elementAt(i) != null)
1339 Main.main.getCurrentDataSet().addSelected(waypointTM.nodes.elementAt(i));
1340 }
1341 }
1342 }
1343 else if ("stopImporter.waypointsDetach".equals(event.getActionCommand()))
1344 {
1345 if (waypointTable.getSelectedRowCount() > 0)
1346 {
1347 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1348 {
1349 if ((waypointTable.isRowSelected(i)) &&
1350 (waypointTM.nodes.elementAt(i) != null))
1351 {
1352 waypointTM.nodes.set(i, null);
1353 }
1354 }
1355 }
1356 else
1357 {
1358 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1359 {
1360 if (waypointTM.nodes.elementAt(i) != null)
1361 waypointTM.nodes.set(i, null);
1362 }
1363 }
1364 waypointTable.clearSelection();
1365 }
1366 else if ("stopImporter.waypointsAdd".equals(event.getActionCommand()))
1367 {
1368 if (waypointTable.getSelectedRowCount() > 0)
1369 {
1370 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1371 {
1372 if ((waypointTable.isRowSelected(i)) &&
1373 (waypointTM.nodes.elementAt(i) == null))
1374 {
1375 Node node = createNode(waypointTM.coors.elementAt(i), (String)waypointTM.getValueAt(i, 1));
1376 waypointTM.nodes.set(i, node);
1377 Main.main.getCurrentDataSet().addSelected(waypointTM.nodes.elementAt(i));
1378 }
1379 }
1380 }
1381 else
1382 {
1383 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1384 {
1385 if (waypointTM.nodes.elementAt(i) == null)
1386 Main.main.getCurrentDataSet().addSelected(waypointTM.nodes.elementAt(i));
1387 }
1388 }
1389 }
1390 else if ("stopImporter.waypointsDelete".equals(event.getActionCommand()))
1391 {
1392 Vector< Node > toDelete = new Vector< Node >();
1393 for (int i = waypointTM.getRowCount()-1; i >=0; --i)
1394 {
1395 if (waypointTable.isRowSelected(i))
1396 {
1397 if ((Node)waypointTM.nodes.elementAt(i) != null)
1398 toDelete.add((Node)waypointTM.nodes.elementAt(i));
1399 waypointTM.nodes.set(i, null);
1400 }
1401 }
1402 Command cmd = DeleteCommand.delete
1403 (Main.main.getEditLayer(), toDelete);
1404 if (cmd != null) {
1405 // cmd can be null if the user cancels dialogs DialogCommand displays
1406 Main.main.undoRedo.add(cmd);
1407 }
1408 }
1409 else if ("stopImporter.settingsStoptype".equals(event.getActionCommand()))
1410 {
1411 for (int i = 0; i < waypointTM.getRowCount(); ++i)
1412 {
1413 if ((Node)waypointTM.nodes.elementAt(i) != null)
1414 {
1415 Node node = (Node)waypointTM.nodes.elementAt(i);
1416 node.remove("highway");
1417 node.remove("railway");
1418 if ("bus".equals((String)cbStoptype.getSelectedItem()))
1419 node.put("highway", "bus_stop");
1420 else if ("tram".equals((String)cbStoptype.getSelectedItem()))
1421 node.put("railway", "tram_stop");
1422 else if ("light_rail".equals((String)cbStoptype.getSelectedItem()))
1423 node.put("railway", "station");
1424 else if ("subway".equals((String)cbStoptype.getSelectedItem()))
1425 node.put("railway", "station");
1426 else if ("rail".equals((String)cbStoptype.getSelectedItem()))
1427 node.put("railway", "station");
1428 }
1429 }
1430 for (int j = 0; j < tracksListModel.size(); ++j)
1431 {
1432 TrackReference track = (TrackReference)tracksListModel.elementAt(j);
1433 for (int i = 0; i < track.stoplistTM.getRowCount(); ++i)
1434 {
1435 if ((Node)track.stoplistTM.nodes.elementAt(i) != null)
1436 {
1437 Node node = (Node)track.stoplistTM.nodes.elementAt(i);
1438 node.remove("highway");
1439 node.remove("railway");
1440 if ("bus".equals((String)cbStoptype.getSelectedItem()))
1441 node.put("highway", "bus_stop");
1442 else if ("tram".equals((String)cbStoptype.getSelectedItem()))
1443 node.put("railway", "tram_stop");
1444 else if ("light_rail".equals((String)cbStoptype.getSelectedItem()))
1445 node.put("railway", "station");
1446 else if ("subway".equals((String)cbStoptype.getSelectedItem()))
1447 node.put("railway", "station");
1448 else if ("rail".equals((String)cbStoptype.getSelectedItem()))
1449 node.put("railway", "station");
1450 }
1451 }
1452 }
1453 }
1454 }
1455
1456 private void importData(final File file)
1457 {
1458 try
1459 {
1460 InputStream is;
1461 if (file.getName().endsWith(".gpx.gz"))
1462 is = new GZIPInputStream(new FileInputStream(file));
1463 else
1464 is = new FileInputStream(file);
1465 // Workaround for SAX BOM bug
1466 // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6206835
1467 if (!((is.read() == 0xef) && (is.read() == 0xbb) && (is.read() == 0xbf)))
1468 {
1469 is.close();
1470 if (file.getName().endsWith(".gpx.gz"))
1471 is = new GZIPInputStream(new FileInputStream(file));
1472 else
1473 is = new FileInputStream(file);
1474 }
1475 final GpxReader r = new GpxReader(is);
1476 final boolean parsedProperly = r.parse(true);
1477 data = r.data;
1478
1479 if (!parsedProperly)
1480 {
1481 JOptionPane.showMessageDialog(null, tr("Error occured while parsing gpx file {0}. Only part of the file will be available", file.getName()));
1482 }
1483 }
1484 catch (FileNotFoundException e)
1485 {
1486 e.printStackTrace();
1487 JOptionPane.showMessageDialog(null, tr("File \"{0}\" does not exist", file.getName()));
1488 }
1489 catch (SAXException e)
1490 {
1491 e.printStackTrace();
1492 JOptionPane.showMessageDialog(null, tr("Parsing file \"{0}\" failed", file.getName()));
1493 }
1494 catch (IOException e)
1495 {
1496 e.printStackTrace();
1497 JOptionPane.showMessageDialog(null, tr("IOException \"{0}\" occurred", e.toString()));
1498 }
1499 }
1500
1501 private void refreshData()
1502 {
1503 tracksListModel.clear();
1504 if (data != null)
1505 {
1506 Vector< TrackReference > trackRefs = new Vector< TrackReference >();
1507 Iterator< GpxTrack > trackIter = data.tracks.iterator();
1508 while (trackIter.hasNext())
1509 {
1510 GpxTrack track = trackIter.next();
1511 trackRefs.add(new TrackReference(track));
1512 }
1513
1514 Collections.sort(trackRefs);
1515
1516 Iterator< TrackReference > iter = trackRefs.iterator();
1517 while (iter.hasNext())
1518 tracksListModel.addElement(iter.next());
1519
1520 waypointTM = new WaypointTableModel();
1521 Iterator< WayPoint > waypointIter = data.waypoints.iterator();
1522 while (waypointIter.hasNext())
1523 {
1524 WayPoint waypoint = waypointIter.next();
1525 waypointTM.addRow(waypoint);
1526 }
1527 waypointTable.setModel(waypointTM);
1528 }
1529 else
1530 {
1531 JOptionPane.showMessageDialog
1532 (null, "The GPX file contained no tracks or waypoints.", "No data found",
1533 JOptionPane.ERROR_MESSAGE);
1534
1535 System.out.println("Public Transport: StopImporter: No data found");
1536 }
1537 }
1538
1539 private void tracksSelectionChanged()
1540 {
1541 int selectedPos = tracksList.getAnchorSelectionIndex();
1542 if (tracksList.isSelectedIndex(selectedPos))
1543 {
1544 currentTrack = ((TrackReference)tracksListModel.elementAt(selectedPos));
1545 tabbedPane.setEnabledAt(1, true);
1546 tabbedPane.setEnabledAt(2, true);
1547
1548 //Prepare Settings
1549 tfGPSTimeStart.setText(currentTrack.gpsSyncTime);
1550 tfStopwatchStart.setText(currentTrack.stopwatchStart);
1551 tfTimeWindow.setText(Double.toString(currentTrack.timeWindow));
1552 tfThreshold.setText(Double.toString(currentTrack.threshold));
1553
1554 //Prepare Stoplist
1555 stoplistTable.setModel
1556 (((TrackReference)tracksListModel.elementAt(selectedPos)).stoplistTM);
1557 }
1558 else
1559 {
1560 currentTrack = null;
1561 tabbedPane.setEnabledAt(1, false);
1562 tabbedPane.setEnabledAt(2, false);
1563 }
1564 }
1565
1566 private Node createNode(LatLon latLon, String name)
1567 {
1568 Node node = new Node(latLon);
1569 if ("bus".equals((String)cbStoptype.getSelectedItem()))
1570 node.put("highway", "bus_stop");
1571 else if ("tram".equals((String)cbStoptype.getSelectedItem()))
1572 node.put("railway", "tram_stop");
1573 else if ("light_rail".equals((String)cbStoptype.getSelectedItem()))
1574 node.put("railway", "station");
1575 else if ("subway".equals((String)cbStoptype.getSelectedItem()))
1576 node.put("railway", "station");
1577 else if ("rail".equals((String)cbStoptype.getSelectedItem()))
1578 node.put("railway", "station");
1579 node.put("name", name);
1580 if (Main.main.getCurrentDataSet() == null)
1581 {
1582 JOptionPane.showMessageDialog(null, "There exists no dataset."
1583 + " Try to download data from the server or open an OSM file.",
1584 "No data found", JOptionPane.ERROR_MESSAGE);
1585
1586 System.out.println("Public Transport: StopInserter: No data found");
1587
1588 return null;
1589 }
1590 Main.main.getCurrentDataSet().addPrimitive(node);
1591 return node;
1592 }
1593
1594 private static double parseTime(String s)
1595 {
1596 double result = 0;
1597 if ((s.charAt(2) != ':') || (s.charAt(2) != ':')
1598 || (s.length() < 8))
1599 return -1;
1600 int hour = Integer.parseInt(s.substring(0, 2));
1601 int minute = Integer.parseInt(s.substring(3, 5));
1602 double second = Double.parseDouble(s.substring(6, s.length()));
1603 if ((hour < 0) || (hour > 23) || (minute < 0) || (minute > 59)
1604 || (second < 0) || (second >= 60.0))
1605 return -1;
1606 return (second + minute*60 + hour*60*60);
1607 }
1608
1609 private static String timeOf(double t)
1610 {
1611 t -= Math.floor(t/24/60/60)*24*60*60;
1612
1613 int hour = (int)Math.floor(t/60/60);
1614 t -= Math.floor(t/60/60)*60*60;
1615 int minute = (int)Math.floor(t/60);
1616 t -= Math.floor(t/60)*60;
1617 double second = t;
1618
1619 Format format = new DecimalFormat("00");
1620 Format formatS = new DecimalFormat("00.###");
1621 return (format.format(hour) + ":" + format.format(minute) + ":"
1622 + formatS.format(second));
1623 }
1624}
Note: See TracBrowser for help on using the repository browser.