source: osm/applications/editors/josm/plugins/public_transport/src/public_transport/GTFSImporterAction.java@ 26147

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

fix last checkin

File size: 17.3 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.BufferedReader;
12import java.io.File;
13import java.io.FileNotFoundException;
14import java.io.FileReader;
15import java.io.InputStream;
16import java.io.IOException;
17import java.text.DecimalFormat;
18import java.text.Format;
19import java.util.Collections;
20import java.util.Iterator;
21import java.util.Vector;
22import java.util.zip.GZIPInputStream;
23
24import javax.swing.AbstractAction;
25import javax.swing.Action;
26import javax.swing.DefaultListModel;
27import javax.swing.JButton;
28import javax.swing.JComboBox;
29import javax.swing.JDialog;
30import javax.swing.JFileChooser;
31import javax.swing.JLabel;
32import javax.swing.JList;
33import javax.swing.JOptionPane;
34import javax.swing.JPanel;
35import javax.swing.JScrollPane;
36import javax.swing.JTabbedPane;
37import javax.swing.JTable;
38import javax.swing.JTextField;
39import javax.swing.ListSelectionModel;
40import javax.swing.event.ListSelectionEvent;
41import javax.swing.event.ListSelectionListener;
42import javax.swing.event.TableModelEvent;
43import javax.swing.event.TableModelListener;
44import javax.swing.table.DefaultTableModel;
45
46import org.openstreetmap.josm.Main;
47import org.openstreetmap.josm.actions.JosmAction;
48import org.openstreetmap.josm.command.Command;
49import org.openstreetmap.josm.command.ChangeCommand;
50import org.openstreetmap.josm.command.DeleteCommand;
51import org.openstreetmap.josm.data.coor.LatLon;
52import org.openstreetmap.josm.data.gpx.GpxData;
53import org.openstreetmap.josm.data.gpx.GpxTrack;
54import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
55import org.openstreetmap.josm.data.gpx.WayPoint;
56import org.openstreetmap.josm.data.osm.DataSet;
57import org.openstreetmap.josm.data.osm.Node;
58import org.openstreetmap.josm.data.osm.OsmPrimitive;
59import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
60import org.openstreetmap.josm.io.GpxReader;
61
62import org.xml.sax.SAXException;
63
64public class GTFSImporterAction extends JosmAction
65{
66 private static GTFSImporterDialog dialog = null;
67 private static DefaultListModel tracksListModel = null;
68 private static Vector< String > data = null;
69 private static TrackReference currentTrack = null;
70 private static GTFSStopTableModel gtfsStopTM = null;
71 public boolean inEvent = false;
72
73 public GTFSImporterAction()
74 {
75 super(tr("Create Stops from GTFS ..."), null,
76 tr("Create Stops from a GTFS file"), null, false);
77 putValue("toolbar", "publictransport/gtfsimporter");
78 Main.toolbar.register(this);
79 }
80
81 public GTFSStopTableModel getGTFSStopTableModel()
82 {
83 return gtfsStopTM;
84 }
85
86 public GTFSImporterDialog getDialog()
87 {
88 return dialog;
89 }
90
91 public DefaultListModel getTracksListModel()
92 {
93 if (tracksListModel == null)
94 tracksListModel = new DefaultListModel();
95 return tracksListModel;
96 }
97
98 public TrackReference getCurrentTrack()
99 {
100 return currentTrack;
101 }
102
103 public void actionPerformed(ActionEvent event)
104 {
105 DataSet mainDataSet = Main.main.getCurrentDataSet();
106
107 if (dialog == null)
108 dialog = new GTFSImporterDialog(this);
109
110 dialog.setVisible(true);
111
112 if (tr("Create Stops from GTFS ...").equals(event.getActionCommand()))
113 {
114 String curDir = Main.pref.get("lastDirectory");
115 if (curDir.equals(""))
116 {
117 curDir = ".";
118 }
119 JFileChooser fc = new JFileChooser(new File(curDir));
120 fc.setDialogTitle("Select GTFS file (stops.txt)");
121 fc.setMultiSelectionEnabled(false);
122
123 int answer = fc.showOpenDialog(Main.parent);
124 if (answer != JFileChooser.APPROVE_OPTION)
125 return;
126
127 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir))
128 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
129
130 importData(fc.getSelectedFile());
131
132 refreshData();
133 }
134/* else if ("stopImporter.settingsGPSTimeStart".equals(event.getActionCommand()))
135 {
136 if ((!inEvent) && (dialog.gpsTimeStartValid()) && (currentTrack != null))
137 Main.main.undoRedo.add(new TrackStoplistRelocateCommand(this));
138 }
139 else if ("stopImporter.settingsStopwatchStart".equals(event.getActionCommand()))
140 {
141 if ((!inEvent) && (dialog.stopwatchStartValid()) && (currentTrack != null))
142 Main.main.undoRedo.add(new TrackStoplistRelocateCommand(this));
143 }
144 else if ("stopImporter.settingsTimeWindow".equals(event.getActionCommand()))
145 {
146 if (currentTrack != null)
147 currentTrack.timeWindow = dialog.getTimeWindow();
148 }
149 else if ("stopImporter.settingsThreshold".equals(event.getActionCommand()))
150 {
151 if (currentTrack != null)
152 currentTrack.threshold = dialog.getThreshold();
153 }
154 else if ("stopImporter.settingsSuggestStops".equals(event.getActionCommand()))
155 Main.main.undoRedo.add(new TrackSuggestStopsCommand(this));
156 else if ("stopImporter.stoplistFind".equals(event.getActionCommand()))
157 findNodesInTable(dialog.getStoplistTable(), currentTrack.stoplistTM.getNodes());
158 else if ("stopImporter.stoplistShow".equals(event.getActionCommand()))
159 showNodesFromTable(dialog.getStoplistTable(), currentTrack.stoplistTM.getNodes());
160 else if ("stopImporter.stoplistMark".equals(event.getActionCommand()))
161 markNodesFromTable(dialog.getStoplistTable(), currentTrack.stoplistTM.getNodes());
162 else if ("stopImporter.stoplistDetach".equals(event.getActionCommand()))
163 {
164 Main.main.undoRedo.add(new TrackStoplistDetachCommand(this));
165 dialog.getStoplistTable().clearSelection();
166 }*/
167 else if ("gtfsImporter.gtfsStopsAdd".equals(event.getActionCommand()))
168 Main.main.undoRedo.add(new GTFSAddCommand(this));
169 else if ("gtfsImporter.gtfsStopsDelete".equals(event.getActionCommand()))
170 Main.main.undoRedo.add(new GTFSDeleteCommand(this));
171 else if ("gtfsImporter.gtfsStopsCatch".equals(event.getActionCommand()))
172 Main.main.undoRedo.add(new GTFSCatchCommand(this));
173 else if ("gtfsImporter.gtfsStopsJoin".equals(event.getActionCommand()))
174 Main.main.undoRedo.add(new GTFSJoinCommand(this));
175 else if ("gtfsImporter.gtfsStopsFind".equals(event.getActionCommand()))
176 findNodesInTable(dialog.getGTFSStopTable(), gtfsStopTM.nodes);
177 else if ("gtfsImporter.gtfsStopsShow".equals(event.getActionCommand()))
178 showNodesFromTable(dialog.getGTFSStopTable(), gtfsStopTM.nodes);
179 else if ("gtfsImporter.gtfsStopsMark".equals(event.getActionCommand()))
180 markNodesFromTable(dialog.getGTFSStopTable(), gtfsStopTM.nodes);
181 }
182
183 private void importData(final File file)
184 {
185 try
186 {
187 FileReader is = new FileReader(file);
188 final BufferedReader r = new BufferedReader(is);
189
190 if (data == null)
191 data = new Vector< String >();
192 else
193 data.clear();
194
195 while (r.ready())
196 data.add(r.readLine());
197 }
198 catch (FileNotFoundException e)
199 {
200 e.printStackTrace();
201 JOptionPane.showMessageDialog(null, tr("File \"{0}\" does not exist", file.getName()));
202 }
203 catch (IOException e)
204 {
205 e.printStackTrace();
206 JOptionPane.showMessageDialog(null, tr("IOException \"{0}\" occurred", e.toString()));
207 }
208 }
209
210 private void refreshData()
211 {
212 if (data != null)
213 {
214 Vector< Node > existingStops = new Vector< Node >();
215
216 if (Main.main.getCurrentDataSet() == null)
217 {
218 JOptionPane.showMessageDialog(null, "There exists no dataset."
219 + " Try to download data from the server or open an OSM file.",
220 "No data found", JOptionPane.ERROR_MESSAGE);
221
222 System.out.println("Public Transport: StopInserter: No data found");
223
224 return;
225 }
226 else
227 {
228 Iterator< Node > iter =
229 Main.main.getCurrentDataSet().getNodes().iterator();
230 while (iter.hasNext())
231 {
232 Node node = iter.next();
233 if ("bus_stop".equals(node.get("highway")))
234 existingStops.add(node);
235 }
236 }
237
238 Iterator< String > iter = data.iterator();
239 if (iter.hasNext())
240 gtfsStopTM = new GTFSStopTableModel(this, iter.next());
241 else
242 {
243 JOptionPane.showMessageDialog
244 (null, "The GTFS file was empty.", "No data found",
245 JOptionPane.ERROR_MESSAGE);
246
247 System.out.println("Public Transport: GTFSImporter: No data found");
248
249 return;
250 }
251
252 while (iter.hasNext())
253 {
254 String s = iter.next();
255 gtfsStopTM.addRow(s, existingStops);
256 }
257 dialog.setGTFSStopTableModel(gtfsStopTM);
258 }
259 else
260 {
261 JOptionPane.showMessageDialog
262 (null, "The GTFS file was empty.", "No data found",
263 JOptionPane.ERROR_MESSAGE);
264
265 System.out.println("Public Transport: GTFSImporter: No data found");
266 }
267 }
268
269// public void tracksSelectionChanged(int selectedPos)
270// {
271// if (selectedPos >= 0)
272// {
273// currentTrack = ((TrackReference)tracksListModel.elementAt(selectedPos));
274// dialog.setTrackValid(true);
275//
276// //Prepare Settings
277// dialog.setSettings
278// (currentTrack.gpsSyncTime, currentTrack.stopwatchStart,
279// currentTrack.timeWindow, currentTrack.threshold);
280//
281// //Prepare Stoplist
282// dialog.setStoplistTableModel
283// (((TrackReference)tracksListModel.elementAt(selectedPos)).stoplistTM);
284// }
285// else
286// {
287// currentTrack = null;
288// dialog.setTrackValid(false);
289// }
290// }
291
292 public static Node createNode(LatLon latLon, String id, String name)
293 {
294 Node node = new Node(latLon);
295 node.put("highway", "bus_stop");
296 node.put("stop_id", id);
297 node.put("name", name);
298 if (Main.main.getCurrentDataSet() == null)
299 {
300 JOptionPane.showMessageDialog(null, "There exists no dataset."
301 + " Try to download data from the server or open an OSM file.",
302 "No data found", JOptionPane.ERROR_MESSAGE);
303
304 System.out.println("Public Transport: StopInserter: No data found");
305
306 return null;
307 }
308 Main.main.getCurrentDataSet().addPrimitive(node);
309 return node;
310 }
311
312 /* returns a collection of all selected lines or
313 a collection of all lines otherwise */
314 public static Vector< Integer > getConsideredLines(JTable table)
315 {
316 int[] selectedLines = table.getSelectedRows();
317 Vector< Integer > consideredLines = new Vector< Integer >();
318 if (selectedLines.length > 0)
319 {
320 for (int i = 0; i < selectedLines.length; ++i)
321 consideredLines.add(selectedLines[i]);
322 }
323 else
324 {
325 for (int i = 0; i < table.getRowCount(); ++i)
326 consideredLines.add(new Integer(i));
327 }
328 return consideredLines;
329 }
330
331 /* marks the table items whose nodes are marked on the map */
332 public static void findNodesInTable(JTable table, Vector< Node > nodes)
333 {
334 if (Main.main.getCurrentDataSet() == null)
335 return;
336
337 table.clearSelection();
338
339 for (int i = 0; i < table.getRowCount(); ++i)
340 {
341 if ((nodes.elementAt(i) != null) &&
342 (Main.main.getCurrentDataSet().isSelected(nodes.elementAt(i))))
343 table.addRowSelectionInterval(i, i);
344 }
345 }
346
347 /* shows the nodes that correspond to the marked lines in the table.
348 If no lines are marked in the table, show all nodes from the vector */
349 public static void showNodesFromTable(JTable table, Vector< Node > nodes)
350 {
351 BoundingXYVisitor box = new BoundingXYVisitor();
352 Vector< Integer > consideredLines = getConsideredLines(table);
353 for (int i = 0; i < consideredLines.size(); ++i)
354 {
355 int j = consideredLines.elementAt(i);
356 if (nodes.elementAt(j) != null)
357 nodes.elementAt(j).visit(box);
358 }
359 if (box.getBounds() == null)
360 return;
361 box.enlargeBoundingBox();
362 Main.map.mapView.recalculateCenterScale(box);
363 }
364
365 /* marks the nodes that correspond to the marked lines in the table.
366 If no lines are marked in the table, mark all nodes from the vector */
367 public static void markNodesFromTable(JTable table, Vector< Node > nodes)
368 {
369 OsmPrimitive[] osmp = { null };
370 Main.main.getCurrentDataSet().setSelected(osmp);
371 Vector< Integer > consideredLines = getConsideredLines(table);
372 for (int i = 0; i < consideredLines.size(); ++i)
373 {
374 int j = consideredLines.elementAt(i);
375 if (nodes.elementAt(j) != null)
376 Main.main.getCurrentDataSet().addSelected(nodes.elementAt(j));
377 }
378 }
379
380 public static String timeOf(double t)
381 {
382 t -= Math.floor(t/24/60/60)*24*60*60;
383
384 int hour = (int)Math.floor(t/60/60);
385 t -= Math.floor(t/60/60)*60*60;
386 int minute = (int)Math.floor(t/60);
387 t -= Math.floor(t/60)*60;
388 double second = t;
389
390 Format format = new DecimalFormat("00");
391 Format formatS = new DecimalFormat("00.###");
392 return (format.format(hour) + ":" + format.format(minute) + ":"
393 + formatS.format(second));
394 }
395
396 public Action getFocusAddAction()
397 {
398 return new FocusAddAction();
399 }
400
401 private class FocusAddAction extends AbstractAction
402 {
403 public void actionPerformed(ActionEvent e)
404 {
405 Main.main.undoRedo.add(new GTFSAddCommand(GTFSImporterAction.this));
406 showNodesFromTable(dialog.getGTFSStopTable(), gtfsStopTM.nodes);
407 }
408 };
409
410/* public Action getFocusWaypointShelterAction(String shelter)
411 {
412 return new FocusWaypointShelterAction(shelter);
413 }
414
415 public Action getFocusWaypointDeleteAction()
416 {
417 return new AbstractAction()
418 {
419 public void actionPerformed(ActionEvent e)
420 {
421 JTable table = dialog.getWaypointsTable();
422 int row = table.getEditingRow();
423 if (row < 0)
424 return;
425 table.clearSelection();
426 table.addRowSelectionInterval(row, row);
427/* Main.main.undoRedo.add
428 (new WaypointsDisableCommand(GTFSImporterAction.this));*
429 }
430 };
431 }
432
433 public Action getFocusTrackStoplistNameAction()
434 {
435 return new FocusTrackStoplistNameAction();
436 }
437
438 public Action getFocusTrackStoplistShelterAction(String shelter)
439 {
440 return new FocusTrackStoplistShelterAction(shelter);
441 }
442
443 public Action getFocusStoplistDeleteAction()
444 {
445 return new AbstractAction()
446 {
447 public void actionPerformed(ActionEvent e)
448 {
449 JTable table = dialog.getStoplistTable();
450 int row = table.getEditingRow();
451 if (row < 0)
452 return;
453 table.clearSelection();
454 table.addRowSelectionInterval(row, row);
455/* Main.main.undoRedo.add
456 (new TrackStoplistDeleteCommand(GTFSImporterAction.this));*
457 }
458 };
459 }
460
461 private class FocusWaypointNameAction extends AbstractAction
462 {
463 public void actionPerformed(ActionEvent e)
464 {
465 JTable table = dialog.getWaypointsTable();
466 showNodesFromTable(table, waypointTM.nodes);
467 markNodesFromTable(table, waypointTM.nodes);
468 int row = table.getEditingRow();
469 if (row < 0)
470 row = 0;
471 waypointTM.inEvent = true;
472 if (table.getCellEditor() != null)
473 {
474 if (!table.getCellEditor().stopCellEditing())
475 table.getCellEditor().cancelCellEditing();
476 }
477 table.editCellAt(row, 1);
478 table.getCellEditor().getTableCellEditorComponent
479 (table, "", true, row, 1);
480 waypointTM.inEvent = false;
481 }
482 };
483
484 private class FocusWaypointShelterAction extends AbstractAction
485 {
486 private String defaultShelter = null;
487
488 public FocusWaypointShelterAction(String defaultShelter)
489 {
490 this.defaultShelter = defaultShelter;
491 }
492
493 public void actionPerformed(ActionEvent e)
494 {
495 JTable table = dialog.getWaypointsTable();
496 showNodesFromTable(table, waypointTM.nodes);
497 markNodesFromTable(table, waypointTM.nodes);
498 int row = table.getEditingRow();
499 if (row < 0)
500 row = 0;
501 waypointTM.inEvent = true;
502 if (table.getCellEditor() != null)
503 {
504 if (!table.getCellEditor().stopCellEditing())
505 table.getCellEditor().cancelCellEditing();
506 }
507 table.editCellAt(row, 2);
508 waypointTM.inEvent = false;
509 table.getCellEditor().getTableCellEditorComponent
510 (table, defaultShelter, true, row, 2);
511 }
512 };
513
514 private class FocusTrackStoplistNameAction extends AbstractAction
515 {
516 public void actionPerformed(ActionEvent e)
517 {
518 JTable table = dialog.getStoplistTable();
519 showNodesFromTable(table, currentTrack.stoplistTM.getNodes());
520 markNodesFromTable(table, currentTrack.stoplistTM.getNodes());
521 int row = table.getEditingRow();
522 if (row < 0)
523 row = 0;
524 currentTrack.inEvent = true;
525 if (table.getCellEditor() != null)
526 {
527 if (!table.getCellEditor().stopCellEditing())
528 table.getCellEditor().cancelCellEditing();
529 }
530 table.editCellAt(row, 1);
531 table.getCellEditor().getTableCellEditorComponent
532 (table, "", true, row, 1);
533 currentTrack.inEvent = false;
534 }
535 };
536
537 private class FocusTrackStoplistShelterAction extends AbstractAction
538 {
539 private String defaultShelter = null;
540
541 public FocusTrackStoplistShelterAction(String defaultShelter)
542 {
543 this.defaultShelter = defaultShelter;
544 }
545
546 public void actionPerformed(ActionEvent e)
547 {
548 JTable table = dialog.getStoplistTable();
549 showNodesFromTable(table, currentTrack.stoplistTM.getNodes());
550 markNodesFromTable(table, currentTrack.stoplistTM.getNodes());
551 int row = table.getEditingRow();
552 if (row < 0)
553 row = 0;
554 currentTrack.inEvent = true;
555 if (table.getCellEditor() != null)
556 {
557 if (!table.getCellEditor().stopCellEditing())
558 table.getCellEditor().cancelCellEditing();
559 }
560 table.editCellAt(row, 2);
561 currentTrack.inEvent = false;
562 table.getCellEditor().getTableCellEditorComponent
563 (table, defaultShelter, true, row, 2);
564 }
565 };*/
566}
Note: See TracBrowser for help on using the repository browser.