source: osm/applications/editors/josm/plugins/public_transport/src/public_transport/GTFSImporterDialog.java@ 26345

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

see #josm6227 - fix wrong quote escaping in strings

File size: 17.1 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.DefaultCellEditor;
24import javax.swing.DefaultListModel;
25import javax.swing.JButton;
26import javax.swing.JComboBox;
27import javax.swing.JComponent;
28import javax.swing.JDialog;
29import javax.swing.JFileChooser;
30import javax.swing.JLabel;
31import javax.swing.JList;
32import javax.swing.JOptionPane;
33import javax.swing.JPanel;
34import javax.swing.JScrollPane;
35import javax.swing.JTabbedPane;
36import javax.swing.JTable;
37import javax.swing.JTextField;
38import javax.swing.KeyStroke;
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 GTFSImporterDialog
65{
66 private JDialog jDialog = null;
67 private JTabbedPane tabbedPane = null;
68 private JComboBox cbStoptype = null;
69 private JList tracksList = null;
70 private JTextField tfGPSTimeStart = null;
71 private JTextField tfStopwatchStart = null;
72 private JTextField tfTimeWindow = null;
73 private JTextField tfThreshold = null;
74 private JTable stoplistTable = null;
75 private JTable gtfsStopTable = null;
76 private final String[] stoptypes = new String[]{marktr("bus"), marktr("tram"), marktr("light_rail"), marktr("subway"), marktr("rail")};
77
78 public GTFSImporterDialog(GTFSImporterAction controller)
79 {
80 Frame frame = JOptionPane.getFrameForComponent(Main.parent);
81 jDialog = new JDialog(frame, tr("Create Stops from GTFS"), false);
82 tabbedPane = new JTabbedPane();
83 JPanel tabSettings = new JPanel();
84 tabbedPane.addTab(tr("Settings"), tabSettings);
85 JPanel tabWaypoints = new JPanel();
86 tabbedPane.addTab(tr("GTFS-Stops"), tabWaypoints);
87 tabbedPane.setEnabledAt(0, false);
88 tabbedPane.setEnabledAt(1, true);
89 jDialog.add(tabbedPane);
90
91 //Settings Tab
92 JPanel contentPane = tabSettings;
93 GridBagLayout gridbag = new GridBagLayout();
94 GridBagConstraints layoutCons = new GridBagConstraints();
95 contentPane.setLayout(gridbag);
96
97 JLabel label = new JLabel(tr("Type of stops to add"));
98
99 layoutCons.gridx = 0;
100 layoutCons.gridy = 0;
101 layoutCons.gridwidth = 2;
102 layoutCons.weightx = 0.0;
103 layoutCons.weighty = 0.0;
104 layoutCons.fill = GridBagConstraints.BOTH;
105 gridbag.setConstraints(label, layoutCons);
106 contentPane.add(label);
107
108 cbStoptype = new JComboBox();
109 cbStoptype.setEditable(false);
110 for(String type : stoptypes)
111 cbStoptype.addItem(new TransText(type));
112 cbStoptype.setActionCommand("gtfsImporter.settingsStoptype");
113 cbStoptype.addActionListener(controller);
114
115 layoutCons.gridx = 0;
116 layoutCons.gridy = 1;
117 layoutCons.gridwidth = 1;
118 layoutCons.weightx = 0.0;
119 layoutCons.weighty = 0.0;
120 layoutCons.fill = GridBagConstraints.BOTH;
121 gridbag.setConstraints(cbStoptype, layoutCons);
122 contentPane.add(cbStoptype);
123
124 label = new JLabel(tr("Time on your GPS device"));
125
126 layoutCons.gridx = 0;
127 layoutCons.gridy = 2;
128 layoutCons.gridwidth = 2;
129 layoutCons.weightx = 0.0;
130 layoutCons.weighty = 0.0;
131 layoutCons.fill = GridBagConstraints.BOTH;
132 gridbag.setConstraints(label, layoutCons);
133 contentPane.add(label);
134
135 tfGPSTimeStart = new JTextField("00:00:00", 15);
136 tfGPSTimeStart.setActionCommand("gtfsImporter.settingsGPSTimeStart");
137 tfGPSTimeStart.addActionListener(controller);
138
139 layoutCons.gridx = 0;
140 layoutCons.gridy = 3;
141 layoutCons.gridwidth = 1;
142 layoutCons.weightx = 0.0;
143 layoutCons.weighty = 0.0;
144 layoutCons.fill = GridBagConstraints.BOTH;
145 gridbag.setConstraints(tfGPSTimeStart, layoutCons);
146 contentPane.add(tfGPSTimeStart);
147
148 /* I18n: Don't change the time format, you only may translate the letters */
149 label = new JLabel(tr("HH:MM:SS.sss"));
150
151 layoutCons.gridx = 1;
152 layoutCons.gridy = 3;
153 layoutCons.gridwidth = 1;
154 layoutCons.weightx = 0.0;
155 layoutCons.weighty = 0.0;
156 layoutCons.fill = GridBagConstraints.BOTH;
157 gridbag.setConstraints(label, layoutCons);
158 contentPane.add(label);
159
160 label = new JLabel(tr("Time on your stopwatch"));
161
162 layoutCons.gridx = 0;
163 layoutCons.gridy = 4;
164 layoutCons.gridwidth = 2;
165 layoutCons.weightx = 0.0;
166 layoutCons.weighty = 0.0;
167 layoutCons.fill = GridBagConstraints.BOTH;
168 gridbag.setConstraints(label, layoutCons);
169 contentPane.add(label);
170
171 tfStopwatchStart = new JTextField("00:00:00", 15);
172 tfStopwatchStart.setActionCommand("gtfsImporter.settingsStopwatchStart");
173 tfStopwatchStart.addActionListener(controller);
174
175 layoutCons.gridx = 0;
176 layoutCons.gridy = 5;
177 layoutCons.gridwidth = 1;
178 layoutCons.weightx = 0.0;
179 layoutCons.weighty = 0.0;
180 layoutCons.fill = GridBagConstraints.BOTH;
181 gridbag.setConstraints(tfStopwatchStart, layoutCons);
182 contentPane.add(tfStopwatchStart);
183
184 /* I18n: Don't change the time format, you only may translate the letters */
185 label = new JLabel(tr("HH:MM:SS.sss"));
186
187 layoutCons.gridx = 1;
188 layoutCons.gridy = 5;
189 layoutCons.gridwidth = 1;
190 layoutCons.weightx = 0.0;
191 layoutCons.weighty = 0.0;
192 layoutCons.fill = GridBagConstraints.BOTH;
193 gridbag.setConstraints(label, layoutCons);
194 contentPane.add(label);
195
196 label = new JLabel(tr("Time window"));
197
198 layoutCons.gridx = 0;
199 layoutCons.gridy = 6;
200 layoutCons.gridwidth = 2;
201 layoutCons.weightx = 0.0;
202 layoutCons.weighty = 0.0;
203 layoutCons.fill = GridBagConstraints.BOTH;
204 gridbag.setConstraints(label, layoutCons);
205 contentPane.add(label);
206
207 tfTimeWindow = new JTextField("15", 4);
208 tfTimeWindow.setActionCommand("gtfsImporter.settingsTimeWindow");
209 tfTimeWindow.addActionListener(controller);
210
211 layoutCons.gridx = 0;
212 layoutCons.gridy = 7;
213 layoutCons.gridwidth = 1;
214 layoutCons.weightx = 0.0;
215 layoutCons.weighty = 0.0;
216 layoutCons.fill = GridBagConstraints.BOTH;
217 gridbag.setConstraints(tfTimeWindow, layoutCons);
218 contentPane.add(tfTimeWindow);
219
220 label = new JLabel(tr("seconds"));
221
222 layoutCons.gridx = 1;
223 layoutCons.gridy = 7;
224 layoutCons.gridwidth = 1;
225 layoutCons.weightx = 0.0;
226 layoutCons.weighty = 0.0;
227 layoutCons.fill = GridBagConstraints.BOTH;
228 gridbag.setConstraints(label, layoutCons);
229 contentPane.add(label);
230
231 label = new JLabel(tr("Move Threshold"));
232
233 layoutCons.gridx = 0;
234 layoutCons.gridy = 8;
235 layoutCons.gridwidth = 2;
236 layoutCons.weightx = 0.0;
237 layoutCons.weighty = 0.0;
238 layoutCons.fill = GridBagConstraints.BOTH;
239 gridbag.setConstraints(label, layoutCons);
240 contentPane.add(label);
241
242 tfThreshold = new JTextField("20", 4);
243 tfThreshold.setActionCommand("gtfsImporter.settingsThreshold");
244 tfThreshold.addActionListener(controller);
245
246 layoutCons.gridx = 0;
247 layoutCons.gridy = 9;
248 layoutCons.gridwidth = 1;
249 layoutCons.weightx = 0.0;
250 layoutCons.weighty = 0.0;
251 layoutCons.fill = GridBagConstraints.BOTH;
252 gridbag.setConstraints(tfThreshold, layoutCons);
253 contentPane.add(tfThreshold);
254
255 label = new JLabel(tr("meters"));
256
257 layoutCons.gridx = 1;
258 layoutCons.gridy = 9;
259 layoutCons.gridwidth = 1;
260 layoutCons.weightx = 0.0;
261 layoutCons.weighty = 0.0;
262 layoutCons.fill = GridBagConstraints.BOTH;
263 gridbag.setConstraints(label, layoutCons);
264 contentPane.add(label);
265
266 JButton bSuggestStops = new JButton(tr("Suggest Stops"));
267 bSuggestStops.setActionCommand("gtfsImporter.settingsSuggestStops");
268 bSuggestStops.addActionListener(controller);
269
270 layoutCons.gridx = 0;
271 layoutCons.gridy = 10;
272 layoutCons.gridwidth = 3;
273 layoutCons.weightx = 1.0;
274 layoutCons.weighty = 0.0;
275 layoutCons.fill = GridBagConstraints.BOTH;
276 gridbag.setConstraints(bSuggestStops, layoutCons);
277 contentPane.add(bSuggestStops);
278
279 //Waypoints Tab
280 contentPane = tabWaypoints;
281 gridbag = new GridBagLayout();
282 layoutCons = new GridBagConstraints();
283 contentPane.setLayout(gridbag);
284 contentPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put
285 (KeyStroke.getKeyStroke("alt N"), "gtfsImporter.gtfsStopsFocusAdd");
286 contentPane.getActionMap().put
287 ("gtfsImporter.gtfsStopsFocusAdd", controller.getFocusAddAction());
288/* contentPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put
289 (KeyStroke.getKeyStroke("alt S"), "gtfsImporter.focusShelterYes");
290 contentPane.getActionMap().put
291 ("gtfsImporter.focusShelterYes",
292 controller.getFocusWaypointShelterAction("yes"));
293 contentPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put
294 (KeyStroke.getKeyStroke("alt T"), "gtfsImporter.focusShelterNo");
295 contentPane.getActionMap().put
296 ("gtfsImporter.focusShelterNo",
297 controller.getFocusWaypointShelterAction("no"));
298 contentPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put
299 (KeyStroke.getKeyStroke("alt U"), "gtfsImporter.focusShelterImplicit");
300 contentPane.getActionMap().put
301 ("gtfsImporter.focusShelterImplicit",
302 controller.getFocusWaypointShelterAction("implicit"));
303 contentPane.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put
304 (KeyStroke.getKeyStroke("alt D"), "gtfsImporter.gtfsStopsDelete");
305 contentPane.getActionMap().put
306 ("gtfsImporter.gtfsStopsDelete",
307 controller.getFocusWaypointDeleteAction());*/
308
309 gtfsStopTable = new JTable();
310 JScrollPane tableSP = new JScrollPane(gtfsStopTable);
311
312 layoutCons.gridx = 0;
313 layoutCons.gridy = 0;
314 layoutCons.gridwidth = 4;
315 layoutCons.weightx = 1.0;
316 layoutCons.weighty = 1.0;
317 layoutCons.fill = GridBagConstraints.BOTH;
318 gridbag.setConstraints(tableSP, layoutCons);
319 contentPane.add(tableSP);
320
321 JButton bFind = new JButton(tr("Find"));
322 bFind.setActionCommand("gtfsImporter.gtfsStopsFind");
323 bFind.addActionListener(controller);
324
325 layoutCons.gridx = 0;
326 layoutCons.gridy = 1;
327 layoutCons.gridwidth = 1;
328 layoutCons.weightx = 1.0;
329 layoutCons.weighty = 0.0;
330 layoutCons.fill = GridBagConstraints.BOTH;
331 gridbag.setConstraints(bFind, layoutCons);
332 contentPane.add(bFind);
333
334 JButton bShow = new JButton(tr("Show"));
335 bShow.setActionCommand("gtfsImporter.gtfsStopsShow");
336 bShow.addActionListener(controller);
337
338 layoutCons.gridx = 0;
339 layoutCons.gridy = 2;
340 layoutCons.gridwidth = 1;
341 layoutCons.weightx = 1.0;
342 layoutCons.weighty = 0.0;
343 layoutCons.fill = GridBagConstraints.BOTH;
344 gridbag.setConstraints(bShow, layoutCons);
345 contentPane.add(bShow);
346
347 JButton bMark = new JButton(tr("Mark"));
348 bMark.setActionCommand("gtfsImporter.gtfsStopsMark");
349 bMark.addActionListener(controller);
350
351 layoutCons.gridx = 1;
352 layoutCons.gridy = 1;
353 layoutCons.gridheight = 2;
354 layoutCons.gridwidth = 1;
355 layoutCons.weightx = 1.0;
356 layoutCons.weighty = 0.0;
357 layoutCons.fill = GridBagConstraints.BOTH;
358 gridbag.setConstraints(bMark, layoutCons);
359 contentPane.add(bMark);
360
361 JButton bCatch = new JButton(tr("Catch"));
362 bCatch.setActionCommand("gtfsImporter.gtfsStopsCatch");
363 bCatch.addActionListener(controller);
364
365 layoutCons.gridx = 2;
366 layoutCons.gridy = 1;
367 layoutCons.gridheight = 1;
368 layoutCons.gridwidth = 1;
369 layoutCons.weightx = 1.0;
370 layoutCons.weighty = 0.0;
371 layoutCons.fill = GridBagConstraints.BOTH;
372 gridbag.setConstraints(bCatch, layoutCons);
373 contentPane.add(bCatch);
374
375 JButton bJoin = new JButton(tr("Join"));
376 bJoin.setActionCommand("gtfsImporter.gtfsStopsJoin");
377 bJoin.addActionListener(controller);
378
379 layoutCons.gridx = 2;
380 layoutCons.gridy = 2;
381 layoutCons.gridheight = 1;
382 layoutCons.gridwidth = 1;
383 layoutCons.weightx = 1.0;
384 layoutCons.weighty = 0.0;
385 layoutCons.fill = GridBagConstraints.BOTH;
386 gridbag.setConstraints(bJoin, layoutCons);
387 contentPane.add(bJoin);
388
389 JButton bAdd = new JButton(tr("Enable"));
390 bAdd.setActionCommand("gtfsImporter.gtfsStopsAdd");
391 bAdd.addActionListener(controller);
392
393 layoutCons.gridx = 3;
394 layoutCons.gridy = 1;
395 layoutCons.gridheight = 1;
396 layoutCons.gridwidth = 1;
397 layoutCons.weightx = 1.0;
398 layoutCons.weighty = 0.0;
399 layoutCons.fill = GridBagConstraints.BOTH;
400 gridbag.setConstraints(bAdd, layoutCons);
401 contentPane.add(bAdd);
402
403 JButton bDelete = new JButton(tr("Disable"));
404 bDelete.setActionCommand("gtfsImporter.gtfsStopsDelete");
405 bDelete.addActionListener(controller);
406
407 layoutCons.gridx = 3;
408 layoutCons.gridy = 2;
409 layoutCons.gridwidth = 1;
410 layoutCons.weightx = 1.0;
411 layoutCons.weighty = 0.0;
412 layoutCons.fill = GridBagConstraints.BOTH;
413 gridbag.setConstraints(bDelete, layoutCons);
414 contentPane.add(bDelete);
415
416 jDialog.pack();
417 jDialog.setLocationRelativeTo(frame);
418 }
419
420 public void setTrackValid(boolean valid)
421 {
422 tabbedPane.setEnabledAt(2, valid);
423 }
424
425 public void setVisible(boolean visible)
426 {
427 jDialog.setVisible(visible);
428 }
429
430 public void setSettings
431 (String gpsSyncTime, String stopwatchStart,
432 double timeWindow, double threshold)
433 {
434 tfGPSTimeStart.setText(gpsSyncTime);
435 tfStopwatchStart.setText(stopwatchStart);
436 tfTimeWindow.setText(Double.toString(timeWindow));
437 tfThreshold.setText(Double.toString(threshold));
438 }
439
440 public String getStoptype()
441 {
442 return ((TransText)cbStoptype.getSelectedItem()).text;
443 }
444
445 public boolean gpsTimeStartValid()
446 {
447 if (parseTime(tfGPSTimeStart.getText()) >= 0)
448 {
449 return true;
450 }
451 else
452 {
453 JOptionPane.showMessageDialog
454 (null, tr("Can''t parse a time from this string."), tr("Invalid value"),
455 JOptionPane.ERROR_MESSAGE);
456 return false;
457 }
458 }
459
460 public String getGpsTimeStart()
461 {
462 return tfGPSTimeStart.getText();
463 }
464
465 public void setGpsTimeStart(String s)
466 {
467 tfGPSTimeStart.setText(s);
468 }
469
470 public boolean stopwatchStartValid()
471 {
472 if (parseTime(tfStopwatchStart.getText()) >= 0)
473 {
474 return true;
475 }
476 else
477 {
478 JOptionPane.showMessageDialog
479 (null, tr("Can''t parse a time from this string."), tr("Invalid value"),
480 JOptionPane.ERROR_MESSAGE);
481 return false;
482 }
483 }
484
485 public String getStopwatchStart()
486 {
487 return tfStopwatchStart.getText();
488 }
489
490 public void setStopwatchStart(String s)
491 {
492 tfStopwatchStart.setText(s);
493 }
494
495 public double getTimeWindow()
496 {
497 return Double.parseDouble(tfTimeWindow.getText());
498 }
499
500 public double getThreshold()
501 {
502 return Double.parseDouble(tfThreshold.getText());
503 }
504
505 public JTable getGTFSStopTable()
506 {
507 return gtfsStopTable;
508 }
509
510 public void setGTFSStopTableModel(GTFSStopTableModel model)
511 {
512 gtfsStopTable.setModel(model);
513 int width = gtfsStopTable.getPreferredSize().width;
514 gtfsStopTable.getColumnModel().getColumn(0).setPreferredWidth((int)(width * 0.3));
515 gtfsStopTable.getColumnModel().getColumn(1).setPreferredWidth((int)(width * 0.6));
516 gtfsStopTable.getColumnModel().getColumn(2).setPreferredWidth((int)(width * 0.1));
517 }
518
519 public static double parseTime(String s)
520 {
521 double result = 0;
522 if ((s.charAt(2) != ':') || (s.charAt(2) != ':')
523 || (s.length() < 8))
524 return -1;
525 int hour = Integer.parseInt(s.substring(0, 2));
526 int minute = Integer.parseInt(s.substring(3, 5));
527 double second = Double.parseDouble(s.substring(6, s.length()));
528 if ((hour < 0) || (hour > 23) || (minute < 0) || (minute > 59)
529 || (second < 0) || (second >= 60.0))
530 return -1;
531 return (second + minute*60 + hour*60*60);
532 }
533
534/* private class TracksLSL implements ListSelectionListener
535 {
536 GTFSImporterAction root = null;
537
538 public TracksLSL(GTFSImporterAction sia)
539 {
540 root = sia;
541 }
542
543 public void valueChanged(ListSelectionEvent e)
544 {
545 int selectedPos = tracksList.getAnchorSelectionIndex();
546 if (tracksList.isSelectedIndex(selectedPos))
547 root.tracksSelectionChanged(selectedPos);
548 else
549 root.tracksSelectionChanged(-1);
550 }
551 };*/
552}
Note: See TracBrowser for help on using the repository browser.