1 | package public_transport;
|
---|
2 |
|
---|
3 | // import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
4 | // import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | //
|
---|
6 | // import java.awt.BorderLayout;
|
---|
7 | // import java.awt.Container;
|
---|
8 | // import java.awt.Dimension;
|
---|
9 | // import java.awt.Frame;
|
---|
10 | // import java.awt.GridBagConstraints;
|
---|
11 | // import java.awt.GridBagLayout;
|
---|
12 | // import java.awt.event.ActionEvent;
|
---|
13 | // import java.util.Collection;
|
---|
14 | // import java.util.Collections;
|
---|
15 | // import java.util.Iterator;
|
---|
16 | // import java.util.LinkedList;
|
---|
17 | // import java.util.List;
|
---|
18 | // import java.util.ListIterator;
|
---|
19 | // import java.util.Map;
|
---|
20 | // import java.util.TreeMap;
|
---|
21 | // import java.util.TreeSet;
|
---|
22 | import java.util.Vector;
|
---|
23 | //
|
---|
24 | // import javax.swing.DefaultCellEditor;
|
---|
25 | // import javax.swing.DefaultListModel;
|
---|
26 | // import javax.swing.JButton;
|
---|
27 | // import javax.swing.JCheckBox;
|
---|
28 | // import javax.swing.JComboBox;
|
---|
29 | // import javax.swing.JDialog;
|
---|
30 | // import javax.swing.JLabel;
|
---|
31 | // import javax.swing.JList;
|
---|
32 | // import javax.swing.JOptionPane;
|
---|
33 | // import javax.swing.JPanel;
|
---|
34 | // import javax.swing.JScrollPane;
|
---|
35 | // import javax.swing.JTabbedPane;
|
---|
36 | // import javax.swing.JTable;
|
---|
37 | // import javax.swing.JTextField;
|
---|
38 | // import javax.swing.ListSelectionModel;
|
---|
39 | // import javax.swing.event.ListSelectionEvent;
|
---|
40 | // import javax.swing.event.ListSelectionListener;
|
---|
41 | import javax.swing.event.TableModelEvent;
|
---|
42 | import javax.swing.event.TableModelListener;
|
---|
43 | import javax.swing.table.DefaultTableModel;
|
---|
44 | // import javax.swing.table.TableCellEditor;
|
---|
45 | //
|
---|
46 | // import org.openstreetmap.josm.Main;
|
---|
47 | // import org.openstreetmap.josm.actions.JosmAction;
|
---|
48 | // import org.openstreetmap.josm.actions.mapmode.DeleteAction;
|
---|
49 | // import org.openstreetmap.josm.data.osm.DataSet;
|
---|
50 | import org.openstreetmap.josm.data.osm.Node;
|
---|
51 | // import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
52 | // import org.openstreetmap.josm.data.osm.Relation;
|
---|
53 | // import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
54 | import org.openstreetmap.josm.data.osm.Way;
|
---|
55 | // import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
56 | // import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
57 | // import org.openstreetmap.josm.tools.GBC;
|
---|
58 | // import org.openstreetmap.josm.tools.Shortcut;
|
---|
59 | // import org.openstreetmap.josm.tools.UrlLabel;
|
---|
60 |
|
---|
61 | public class ItineraryTableModel extends DefaultTableModel
|
---|
62 | implements TableModelListener
|
---|
63 | {
|
---|
64 | public Vector<Way> ways = new Vector<Way>();
|
---|
65 | public boolean inEvent = false;
|
---|
66 |
|
---|
67 | public boolean isCellEditable(int row, int column)
|
---|
68 | {
|
---|
69 | if (column != 1)
|
---|
70 | return false;
|
---|
71 | if (ways.elementAt(row) == null)
|
---|
72 | return false;
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void addRow(Object[] obj)
|
---|
77 | {
|
---|
78 | ways.addElement(null);
|
---|
79 | super.addRow(obj);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public void insertRow(int insPos, Object[] obj)
|
---|
83 | {
|
---|
84 | if (insPos == -1)
|
---|
85 | {
|
---|
86 | ways.addElement(null);
|
---|
87 | super.addRow(obj);
|
---|
88 | }
|
---|
89 | else
|
---|
90 | {
|
---|
91 | ways.insertElementAt(null, insPos);
|
---|
92 | super.insertRow(insPos, obj);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void addRow(Way way, String role)
|
---|
97 | {
|
---|
98 | insertRow(-1, way, role);
|
---|
99 | }
|
---|
100 |
|
---|
101 | public void insertRow(int insPos, Way way, String role)
|
---|
102 | {
|
---|
103 | String[] buf = { "", "" };
|
---|
104 | String curName = way.get("name");
|
---|
105 | if (way.isIncomplete())
|
---|
106 | buf[0] = "[incomplete]";
|
---|
107 | else if (way.getNodesCount() < 1)
|
---|
108 | buf[0] = "[empty way]";
|
---|
109 | else if (curName != null)
|
---|
110 | buf[0] = curName;
|
---|
111 | else
|
---|
112 | buf[0] = "[ID] " + (new Long(way.getId())).toString();
|
---|
113 | buf[1] = role;
|
---|
114 | if (insPos == -1)
|
---|
115 | {
|
---|
116 | ways.addElement(way);
|
---|
117 | super.addRow(buf);
|
---|
118 | }
|
---|
119 | else
|
---|
120 | {
|
---|
121 | ways.insertElementAt(way, insPos);
|
---|
122 | super.insertRow(insPos, buf);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | public void clear()
|
---|
127 | {
|
---|
128 | ways.clear();
|
---|
129 | super.setRowCount(0);
|
---|
130 | }
|
---|
131 |
|
---|
132 | public void cleanupGaps()
|
---|
133 | {
|
---|
134 | inEvent = true;
|
---|
135 | Node lastNode = null;
|
---|
136 |
|
---|
137 | for (int i = 0; i < getRowCount(); ++i)
|
---|
138 | {
|
---|
139 | if (ways.elementAt(i) == null)
|
---|
140 | {
|
---|
141 | ++i;
|
---|
142 | if (i >= getRowCount())
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | while ((ways.elementAt(i) == null) &&
|
---|
146 | ((i == 0) || (ways.elementAt(i-1) == null)))
|
---|
147 | {
|
---|
148 | ways.removeElementAt(i);
|
---|
149 | removeRow(i);
|
---|
150 | if (i >= getRowCount())
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | if (i >= getRowCount())
|
---|
154 | break;
|
---|
155 |
|
---|
156 | boolean gapRequired = gapNecessary
|
---|
157 | (ways.elementAt(i), (String)(getValueAt(i, 1)), lastNode);
|
---|
158 | if ((i > 0) && (!gapRequired) && (ways.elementAt(i-1) == null))
|
---|
159 | {
|
---|
160 | ways.removeElementAt(i-1);
|
---|
161 | removeRow(i-1);
|
---|
162 | --i;
|
---|
163 | }
|
---|
164 | else if ((i > 0) && gapRequired && (ways.elementAt(i-1) != null))
|
---|
165 | {
|
---|
166 | String[] buf = { "", "" };
|
---|
167 | buf[0] = "[gap]";
|
---|
168 | insertRow(i, buf);
|
---|
169 | ++i;
|
---|
170 | }
|
---|
171 | lastNode = getLastNode(ways.elementAt(i), (String)(getValueAt(i, 1)));
|
---|
172 | }
|
---|
173 | while ((getRowCount() > 0) &&
|
---|
174 | (ways.elementAt(getRowCount()-1) == null))
|
---|
175 | {
|
---|
176 | ways.removeElementAt(getRowCount()-1);
|
---|
177 | removeRow(getRowCount()-1);
|
---|
178 | }
|
---|
179 | inEvent = false;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public void tableChanged(TableModelEvent e)
|
---|
183 | {
|
---|
184 | if (e.getType() == TableModelEvent.UPDATE)
|
---|
185 | {
|
---|
186 | if (inEvent)
|
---|
187 | return;
|
---|
188 | cleanupGaps();
|
---|
189 | RoutePatternAction.rebuildWays();
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | private Node getLastNode(Way way, String role)
|
---|
194 | {
|
---|
195 | if ((way == null) || (way.isIncomplete()) || (way.getNodesCount() < 1))
|
---|
196 | return null;
|
---|
197 | else
|
---|
198 | {
|
---|
199 | if ("backward".equals(role))
|
---|
200 | return way.getNode(0);
|
---|
201 | else
|
---|
202 | return way.getNode(way.getNodesCount() - 1);
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | private boolean gapNecessary(Way way, String role, Node lastNode)
|
---|
207 | {
|
---|
208 | if ((way != null) && (!(way.isIncomplete())) && (way.getNodesCount() >= 1))
|
---|
209 | {
|
---|
210 | Node firstNode = null;
|
---|
211 | if ("backward".equals(role))
|
---|
212 | firstNode = way.getNode(way.getNodesCount() - 1);
|
---|
213 | else
|
---|
214 | firstNode = way.getNode(0);
|
---|
215 | if ((lastNode != null) && (!lastNode.equals(firstNode)))
|
---|
216 | return true;
|
---|
217 | }
|
---|
218 | return false;
|
---|
219 | }
|
---|
220 | };
|
---|