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

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

add toolbar names

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