source: osm/applications/editors/josm/plugins/public_transport/src/public_transport/TrackStoplistSortCommand.java@ 20839

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

Public Transport Plugin: Shelter

File size: 3.9 KB
Line 
1package public_transport;
2
3import org.openstreetmap.josm.Main;
4import org.openstreetmap.josm.command.Command;
5import org.openstreetmap.josm.data.osm.Node;
6import org.openstreetmap.josm.data.osm.OsmPrimitive;
7
8import java.util.Collection;
9import java.util.Collections;
10import java.util.Iterator;
11import java.util.Vector;
12import javax.swing.DefaultListModel;
13import javax.swing.tree.DefaultMutableTreeNode;
14import javax.swing.tree.MutableTreeNode;
15
16public class TrackStoplistSortCommand extends Command
17{
18 private TrackStoplistTableModel stoplistTM = null;
19 private Vector< Vector< Object > > tableDataModel = null;
20 private Vector< Node > nodes = null;
21 private Vector< String > times = null;
22 private Vector< Integer > workingLines = null;
23 private int insPos;
24 private String stopwatchStart;
25
26 public TrackStoplistSortCommand(StopImporterAction controller)
27 {
28 stoplistTM = controller.getCurrentTrack().stoplistTM;
29 workingLines = new Vector< Integer >();
30 insPos = controller.getDialog().getStoplistTable().getSelectedRow();
31 stopwatchStart = controller.getCurrentTrack().stopwatchStart;
32
33 // use either selected lines or all lines if no line is selected
34 int[] selectedLines = controller.getDialog().getStoplistTable().getSelectedRows();
35 if (selectedLines.length > 0)
36 {
37 for (int i = 0; i < selectedLines.length; ++i)
38 workingLines.add(selectedLines[i]);
39 }
40 else
41 {
42 for (int i = 0; i < stoplistTM.getRowCount(); ++i)
43 workingLines.add(new Integer(i));
44 }
45 }
46
47 @SuppressWarnings("unchecked")
48 public boolean executeCommand()
49 {
50 tableDataModel = (Vector< Vector< Object > >)stoplistTM.getDataVector()
51 .clone();
52 nodes = (Vector< Node >)stoplistTM.getNodes().clone();
53 times = (Vector< String >)stoplistTM.getTimes().clone();
54
55 Vector< NodeSortEntry > nodesToSort = new Vector< NodeSortEntry >();
56 for (int i = workingLines.size()-1; i >= 0; --i)
57 {
58 int j = workingLines.elementAt(i).intValue();
59 nodesToSort.add(new NodeSortEntry
60 (stoplistTM.nodeAt(j), (String)stoplistTM.getValueAt(j, 0),
61 (String)stoplistTM.getValueAt(j, 1),
62 (String)stoplistTM.getValueAt(j, 2),
63 StopImporterDialog.parseTime(stopwatchStart)));
64 stoplistTM.removeRow(j);
65 }
66
67 Collections.sort(nodesToSort);
68
69 int insPos = this.insPos;
70 Iterator< NodeSortEntry > iter = nodesToSort.iterator();
71 while (iter.hasNext())
72 {
73 NodeSortEntry nse = iter.next();
74 stoplistTM.insertRow(insPos, nse.node, nse.time, nse.name, nse.shelter);
75 if (insPos >= 0)
76 ++insPos;
77 }
78 return true;
79 }
80
81 public void undoCommand()
82 {
83 stoplistTM.setDataVector(tableDataModel);
84 stoplistTM.setNodes(nodes);
85 stoplistTM.setTimes(times);
86 }
87
88 public void fillModifiedData
89 (Collection< OsmPrimitive > modified, Collection< OsmPrimitive > deleted,
90 Collection< OsmPrimitive > added)
91 {
92 }
93
94 public MutableTreeNode description()
95 {
96 return new DefaultMutableTreeNode("public_transport.TrackStoplist.Sort");
97 }
98
99 private class NodeSortEntry implements Comparable< NodeSortEntry >
100 {
101 public Node node = null;
102 public String time = null;
103 public String name = null;
104 public String shelter = null;
105 public double startTime = 0;
106
107 public NodeSortEntry
108 (Node node, String time, String name, String shelter, double startTime)
109 {
110 this.node = node;
111 this.time = time;
112 this.name = name;
113 this.shelter = shelter;
114 }
115
116 public int compareTo(NodeSortEntry nse)
117 {
118 double time = StopImporterDialog.parseTime(this.time);
119 if (time - startTime > 12*60*60)
120 time -= 24*60*60;
121
122 double nseTime = StopImporterDialog.parseTime(nse.time);
123 if (nseTime - startTime > 12*60*60)
124 nseTime -= 24*60*60;
125
126 if (time < nseTime)
127 return -1;
128 else if (time > nseTime)
129 return 1;
130 else
131 return 0;
132 }
133 };
134};
Note: See TracBrowser for help on using the repository browser.