1 | /*
|
---|
2 | * This program is free software: you can redistribute it and/or modify it under
|
---|
3 | * the terms of the GNU General Public License as published by the
|
---|
4 | * Free Software Foundation, either version 3 of the License, or
|
---|
5 | * (at your option) any later version.
|
---|
6 | *
|
---|
7 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
---|
8 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
9 | * See the GNU General Public License for more details.
|
---|
10 | *
|
---|
11 | * You should have received a copy of the GNU General Public License along with this program.
|
---|
12 | * If not, see <http://www.gnu.org/licenses/>.
|
---|
13 | */
|
---|
14 | package org.openstreetmap.josm.plugins.fixAddresses.gui;
|
---|
15 |
|
---|
16 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
17 |
|
---|
18 | import java.awt.BorderLayout;
|
---|
19 | import java.awt.Color;
|
---|
20 | import java.awt.Dimension;
|
---|
21 | import java.awt.Font;
|
---|
22 | import java.awt.GridLayout;
|
---|
23 | import java.awt.HeadlessException;
|
---|
24 | import java.awt.event.ActionEvent;
|
---|
25 | import java.awt.event.ActionListener;
|
---|
26 | import java.awt.event.KeyEvent;
|
---|
27 | import java.awt.event.KeyListener;
|
---|
28 | import java.util.List;
|
---|
29 |
|
---|
30 | import javax.swing.JButton;
|
---|
31 | import javax.swing.JDialog;
|
---|
32 | import javax.swing.JLabel;
|
---|
33 | import javax.swing.JOptionPane;
|
---|
34 | import javax.swing.JPanel;
|
---|
35 | import javax.swing.JScrollPane;
|
---|
36 | import javax.swing.JSeparator;
|
---|
37 | import javax.swing.JSplitPane;
|
---|
38 | import javax.swing.JTable;
|
---|
39 | import javax.swing.ListSelectionModel;
|
---|
40 | import javax.swing.border.EmptyBorder;
|
---|
41 | import javax.swing.event.ListSelectionEvent;
|
---|
42 | import javax.swing.event.ListSelectionListener;
|
---|
43 | import javax.swing.table.TableModel;
|
---|
44 |
|
---|
45 | import org.openstreetmap.gui.jmapviewer.JMapViewer;
|
---|
46 | import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
|
---|
47 | import org.openstreetmap.josm.Main;
|
---|
48 | import org.openstreetmap.josm.data.osm.Node;
|
---|
49 | import org.openstreetmap.josm.data.osm.Way;
|
---|
50 | import org.openstreetmap.josm.gui.SideButton;
|
---|
51 | import org.openstreetmap.josm.plugins.fixAddresses.AddressEditContainer;
|
---|
52 | import org.openstreetmap.josm.plugins.fixAddresses.OSMAddress;
|
---|
53 | import org.openstreetmap.josm.plugins.fixAddresses.IAddressEditContainerListener;
|
---|
54 | import org.openstreetmap.josm.plugins.fixAddresses.IOSMEntity;
|
---|
55 | import org.openstreetmap.josm.plugins.fixAddresses.OSMStreet;
|
---|
56 | import org.openstreetmap.josm.plugins.fixAddresses.StringUtils;
|
---|
57 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
58 |
|
---|
59 | @SuppressWarnings("serial")
|
---|
60 | public class AddressEditDialog extends JDialog implements ActionListener, ListSelectionListener, IAddressEditContainerListener {
|
---|
61 | private static final String UNRESOLVED_ADDRESS = tr("Unresolved Addresses");
|
---|
62 | private static final String STREETS = tr("Streets");
|
---|
63 | private static final String UNRESOLVED_HEADER_FMT = tr("%s (%d)");
|
---|
64 | private static final String STREET_HEADER_FMT = tr("%s (%d)");
|
---|
65 | private static final String OK_COMMAND = tr("Close");
|
---|
66 | private static final String SELECT_AND_CLOSE = tr("Select and close");
|
---|
67 |
|
---|
68 | private AddressEditContainer editContainer;
|
---|
69 | private JTable unresolvedTable;
|
---|
70 | private JTable streetTable;
|
---|
71 |
|
---|
72 | /* Actions */
|
---|
73 | private AssignAddressToStreetAction resolveAction = new AssignAddressToStreetAction();
|
---|
74 | private ApplyAllGuessesAction applyAllGuessesAction = new ApplyAllGuessesAction();
|
---|
75 | private GuessAddressDataAction guessAddressAction = new GuessAddressDataAction();
|
---|
76 | private SelectAddressesInMapAction selectAddressesInMapAction = new SelectAddressesInMapAction();
|
---|
77 | private RemoveAddressTagsAction removeAddressTagsAction = new RemoveAddressTagsAction();
|
---|
78 |
|
---|
79 | private AbstractAddressEditAction[] actions = new AbstractAddressEditAction[] {
|
---|
80 | resolveAction,
|
---|
81 | guessAddressAction,
|
---|
82 | applyAllGuessesAction,
|
---|
83 | selectAddressesInMapAction,
|
---|
84 | removeAddressTagsAction
|
---|
85 | };
|
---|
86 | private JLabel streetLabel;
|
---|
87 | private JLabel unresolvedAddressesLabel;
|
---|
88 | private JMapViewer mapViewer;
|
---|
89 |
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * @param arg0
|
---|
93 | * @throws HeadlessException
|
---|
94 | */
|
---|
95 | public AddressEditDialog(AddressEditContainer addressEditContainer) throws HeadlessException {
|
---|
96 | super(JOptionPane.getFrameForComponent(Main.parent), tr("Fix unresolved addresses"), true);
|
---|
97 |
|
---|
98 | this.editContainer = addressEditContainer;
|
---|
99 | this.editContainer.addChangedListener(this);
|
---|
100 | setLayout(new BorderLayout());
|
---|
101 | setSize(1024,600);
|
---|
102 | setLocationRelativeTo(null);
|
---|
103 |
|
---|
104 | if (addressEditContainer != null) {
|
---|
105 | /* Panel for street table */
|
---|
106 | JPanel streetPanel = new JPanel(new BorderLayout());
|
---|
107 | streetTable = new JTable(new StreetTableModel(editContainer));
|
---|
108 | streetTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
---|
109 | streetTable.getSelectionModel().addListSelectionListener(this);
|
---|
110 | streetTable.addKeyListener(new JumpToEntryListener(1));
|
---|
111 |
|
---|
112 | JScrollPane scroll1 = new JScrollPane(streetTable);
|
---|
113 | streetPanel.add(scroll1, BorderLayout.CENTER);
|
---|
114 |
|
---|
115 | streetLabel = createHeaderLabel(STREET_HEADER_FMT,
|
---|
116 | STREETS,
|
---|
117 | editContainer.getNumberOfStreets());
|
---|
118 |
|
---|
119 | JPanel headerPanel = new JPanel(new GridLayout(1, 4));
|
---|
120 | headerPanel.setMinimumSize(new Dimension(100, 30));
|
---|
121 | headerPanel.add(streetLabel);
|
---|
122 |
|
---|
123 | streetPanel.add(headerPanel, BorderLayout.NORTH);
|
---|
124 | streetPanel.setMinimumSize(new Dimension(350, 200));
|
---|
125 |
|
---|
126 | /* Panel for unresolved addresses table */
|
---|
127 | JPanel unresolvedPanel = new JPanel(new BorderLayout());
|
---|
128 | unresolvedTable = new JTable(new UnresolvedAddressesTableModel(editContainer));
|
---|
129 | unresolvedTable.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
130 | unresolvedTable.getSelectionModel().addListSelectionListener(this);
|
---|
131 | unresolvedTable.getSelectionModel().addListSelectionListener(new IncompleteAddressListener());
|
---|
132 | unresolvedTable.addMouseListener(applyAllGuessesAction);
|
---|
133 |
|
---|
134 | JScrollPane scroll2 = new JScrollPane(unresolvedTable);
|
---|
135 | unresolvedPanel.add(scroll2, BorderLayout.CENTER);
|
---|
136 | unresolvedAddressesLabel = createHeaderLabel(
|
---|
137 | UNRESOLVED_HEADER_FMT,
|
---|
138 | UNRESOLVED_ADDRESS,
|
---|
139 | editContainer.getNumberOfUnresolvedAddresses());
|
---|
140 |
|
---|
141 | JPanel headerPanel2 = new JPanel(new GridLayout(1, 4));
|
---|
142 | headerPanel2.setMinimumSize(new Dimension(100, 30));
|
---|
143 | headerPanel2.add(unresolvedAddressesLabel);
|
---|
144 | unresolvedPanel.add(headerPanel2 , BorderLayout.NORTH);
|
---|
145 | unresolvedPanel.setMinimumSize(new Dimension(350, 200));
|
---|
146 |
|
---|
147 |
|
---|
148 | try {
|
---|
149 | JPanel unresolvedButtons = new JPanel(new GridLayout(2,5, 5, 5));
|
---|
150 | SideButton assign = new SideButton(resolveAction);
|
---|
151 | unresolvedButtons.add(assign);
|
---|
152 |
|
---|
153 | SideButton guess = new SideButton(guessAddressAction);
|
---|
154 | unresolvedButtons.add(guess);
|
---|
155 | SideButton applyAllGuesses = new SideButton(applyAllGuessesAction);
|
---|
156 | unresolvedButtons.add(applyAllGuesses);
|
---|
157 |
|
---|
158 | SideButton removeAddressTags = new SideButton(removeAddressTagsAction);
|
---|
159 | unresolvedButtons.add(removeAddressTags);
|
---|
160 |
|
---|
161 | unresolvedButtons.add(new JPanel());
|
---|
162 |
|
---|
163 | SideButton selectInMap = new SideButton(selectAddressesInMapAction);
|
---|
164 | unresolvedButtons.add(selectInMap);
|
---|
165 | headerPanel2.setMinimumSize(new Dimension(100, 70));
|
---|
166 |
|
---|
167 | unresolvedPanel.add(unresolvedButtons, BorderLayout.SOUTH);
|
---|
168 | } catch (Exception e) {
|
---|
169 | e.printStackTrace();
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /* Map Panel */
|
---|
174 | JPanel mapPanel = new JPanel(new BorderLayout());
|
---|
175 | mapViewer = new JMapViewer();
|
---|
176 | mapPanel.add(mapViewer, BorderLayout.CENTER);
|
---|
177 | mapPanel.setMinimumSize(new Dimension(200, 200));
|
---|
178 | mapViewer.setVisible(false);
|
---|
179 |
|
---|
180 | /* Combine panels */
|
---|
181 | JSplitPane unresSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, streetPanel, unresolvedPanel);
|
---|
182 | JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, unresSplitPane, mapPanel);
|
---|
183 |
|
---|
184 | this.getContentPane().add(pane, BorderLayout.CENTER);
|
---|
185 | //this.getContentPane().add(mapPanel, BorderLayout.SOUTH);
|
---|
186 | } else {
|
---|
187 | this.getContentPane().add(new JLabel(tr("(No data)")), BorderLayout.CENTER);
|
---|
188 | }
|
---|
189 |
|
---|
190 | for (int i = 0; i < actions.length; i++) {
|
---|
191 | actions[i].setContainer(addressEditContainer);
|
---|
192 | }
|
---|
193 |
|
---|
194 | JPanel buttonPanel = new JPanel(new GridLayout(1,10));
|
---|
195 | JButton ok = new JButton(OK_COMMAND, ImageProvider.getIfAvailable(null, "ok"));
|
---|
196 | ok.addActionListener(this);
|
---|
197 | JButton selectAndClose = new JButton(SELECT_AND_CLOSE);
|
---|
198 | selectAndClose.addActionListener(this);
|
---|
199 |
|
---|
200 | // Murks
|
---|
201 | for (int i = 0; i < 8; i++) {
|
---|
202 | buttonPanel.add(new JSeparator());
|
---|
203 | }
|
---|
204 |
|
---|
205 | buttonPanel.add(ok);
|
---|
206 |
|
---|
207 |
|
---|
208 | this.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Creates a header label in the form "title (number)" with bold font.
|
---|
213 | * @param fmtString The format string having a string and a numeric placeholder.
|
---|
214 | * @param title The title of the header.
|
---|
215 | * @param n The number to show in the header.
|
---|
216 | * @return
|
---|
217 | */
|
---|
218 | private JLabel createHeaderLabel(String fmtString, String title, int n) {
|
---|
219 | JLabel label = new JLabel(String.format(fmtString, title, n));
|
---|
220 | label.setFont(label.getFont().deriveFont(Font.BOLD, label.getFont().getSize() + 2));
|
---|
221 | label.setBorder(new EmptyBorder(5,2,4,5));
|
---|
222 | return label;
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Updates the list headings.
|
---|
227 | */
|
---|
228 | private void updateHeaders() {
|
---|
229 | if (editContainer != null) {
|
---|
230 | streetLabel.setText(String.format(
|
---|
231 | STREET_HEADER_FMT,
|
---|
232 | STREETS,
|
---|
233 | editContainer.getNumberOfStreets()));
|
---|
234 | unresolvedAddressesLabel.setText(
|
---|
235 | String.format(UNRESOLVED_HEADER_FMT,
|
---|
236 | UNRESOLVED_ADDRESS,
|
---|
237 | editContainer.getNumberOfUnresolvedAddresses()));
|
---|
238 | } else {
|
---|
239 | streetLabel.setText(String.format(STREET_HEADER_FMT, 0));
|
---|
240 | unresolvedAddressesLabel.setText(String.format(UNRESOLVED_HEADER_FMT, 0));
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | @Override
|
---|
245 | public void actionPerformed(ActionEvent e) {
|
---|
246 | if (OK_COMMAND.equals(e.getActionCommand())) {
|
---|
247 | this.setVisible(false);
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Override
|
---|
252 | public void valueChanged(ListSelectionEvent e) {
|
---|
253 |
|
---|
254 | AddressEditSelectionEvent ev = new AddressEditSelectionEvent(e.getSource(),
|
---|
255 | streetTable, unresolvedTable, editContainer);
|
---|
256 |
|
---|
257 | for (AbstractAddressEditAction action : actions) {
|
---|
258 | action.setEvent(ev);
|
---|
259 | }
|
---|
260 |
|
---|
261 | clearMapViewer();
|
---|
262 | OSMStreet sNode = ev.getSelectedStreet();
|
---|
263 | if (sNode != null) {
|
---|
264 |
|
---|
265 | //mapViewer.addMapRectangle(new BBoxMapRectangle(bb));
|
---|
266 | for (IOSMEntity seg : sNode.getChildren()) {
|
---|
267 | Way way = (Way) seg.getOsmObject();
|
---|
268 | //BBox bb = way.getBBox();
|
---|
269 |
|
---|
270 | for (Node node : way.getNodes()) {
|
---|
271 | mapViewer.addMapMarker(new MapMarkerDot(Color.GREEN, node.getCoor().lat(), node.getCoor().lon()));
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | // show addresses as blue marker
|
---|
276 | if (sNode.hasAddresses()) {
|
---|
277 | for (OSMAddress aNode : sNode.getAddresses()) {
|
---|
278 | Color markerCol = Color.BLUE;
|
---|
279 | if (!aNode.isComplete()) {
|
---|
280 | markerCol = Color.RED;
|
---|
281 | }
|
---|
282 | mapViewer.addMapMarker(new MapMarkerDot(markerCol, aNode.getCoor().lat(), aNode.getCoor().lon()));
|
---|
283 | }
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | List<OSMAddress> unrAddresses = ev.getSelectedUnresolvedAddresses();
|
---|
288 | if (unrAddresses != null) {
|
---|
289 | for (OSMAddress aNode : unrAddresses) {
|
---|
290 | mapViewer.addMapMarker(new MapMarkerDot(Color.ORANGE, aNode.getCoor().lat(), aNode.getCoor().lon()));
|
---|
291 | }
|
---|
292 | }
|
---|
293 | mapViewer.setDisplayToFitMapMarkers();
|
---|
294 | mapViewer.setVisible(true);
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Removes all markers and rectangles from the map viewer.
|
---|
299 | */
|
---|
300 | private void clearMapViewer() {
|
---|
301 | mapViewer.setVisible(false);
|
---|
302 | // remove markers and rectangles from map viewer
|
---|
303 | mapViewer.getMapMarkerList().clear();
|
---|
304 | mapViewer.getMapRectangleList().clear();
|
---|
305 | }
|
---|
306 |
|
---|
307 | @Override
|
---|
308 | public void containerChanged(AddressEditContainer container) {
|
---|
309 | updateHeaders();
|
---|
310 |
|
---|
311 | for (int i = 0; i < actions.length; i++) {
|
---|
312 | actions[i].setEvent(null);
|
---|
313 | actions[i].setContainer(container);
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | @Override
|
---|
318 | public void entityChanged(IOSMEntity entity) {
|
---|
319 | updateHeaders();
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Special listener to react on selection changes in the incomplete address list.
|
---|
324 | * It searches the street table for the streets which matches best matching to the
|
---|
325 | * street name given in the address.
|
---|
326 | *
|
---|
327 | * @author Oliver Wieland <oliver.wieland@online.de>
|
---|
328 | */
|
---|
329 | class IncompleteAddressListener implements ListSelectionListener {
|
---|
330 |
|
---|
331 | @Override
|
---|
332 | public void valueChanged(ListSelectionEvent e) {
|
---|
333 | if (unresolvedTable.getSelectedRowCount() == 1) {
|
---|
334 | String streetOfAddr = (String) unresolvedTable.
|
---|
335 | getModel().getValueAt(unresolvedTable.getSelectedRow(), 0);
|
---|
336 |
|
---|
337 | int maxScore = 0, score = 0, row = -1;
|
---|
338 | for (int i = 0; i < streetTable.getRowCount(); i++) {
|
---|
339 | String streetName = (String) streetTable.getModel().getValueAt(i, 1);
|
---|
340 |
|
---|
341 | score = StringUtils.lcsLength(streetOfAddr, streetName);
|
---|
342 | if (score > maxScore) {
|
---|
343 | maxScore = score;
|
---|
344 | row = i;
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | if (row > 0) {
|
---|
349 | streetTable.getSelectionModel().clearSelection();
|
---|
350 | streetTable.getSelectionModel().addSelectionInterval(row, row);
|
---|
351 | streetTable.scrollRectToVisible(streetTable.getCellRect(row, 0, true));
|
---|
352 | }
|
---|
353 | }
|
---|
354 | }
|
---|
355 |
|
---|
356 | }
|
---|
357 |
|
---|
358 | /**
|
---|
359 | * The listener interface for receiving key events of a table.
|
---|
360 | * The class that is interested in processing a jumpToEntry
|
---|
361 | * event implements this interface, and the object created
|
---|
362 | * with that class is registered with a component using the
|
---|
363 | * component's <code>addJumpToEntryListener<code> method. When
|
---|
364 | * the jumpToEntry event occurs, that object's appropriate
|
---|
365 | * method is invoked.
|
---|
366 | *
|
---|
367 | * @see JumpToEntryEvent
|
---|
368 | */
|
---|
369 | class JumpToEntryListener implements KeyListener {
|
---|
370 | private int column;
|
---|
371 |
|
---|
372 | /**
|
---|
373 | * Instantiates a new jump-to-entry listener.
|
---|
374 | * @param column the column of the table to use for the comparison
|
---|
375 | */
|
---|
376 | public JumpToEntryListener(int column) {
|
---|
377 | super();
|
---|
378 | this.column = column;
|
---|
379 | }
|
---|
380 |
|
---|
381 | @Override
|
---|
382 | public void keyPressed(KeyEvent arg0) {
|
---|
383 | // TODO Auto-generated method stub
|
---|
384 |
|
---|
385 | }
|
---|
386 |
|
---|
387 | @Override
|
---|
388 | public void keyReleased(KeyEvent arg0) {
|
---|
389 | // TODO Auto-generated method stub
|
---|
390 |
|
---|
391 | }
|
---|
392 |
|
---|
393 | @Override
|
---|
394 | public void keyTyped(KeyEvent arg0) {
|
---|
395 | JTable table = (JTable) arg0.getSource();
|
---|
396 |
|
---|
397 | if (table == null) return;
|
---|
398 |
|
---|
399 | TableModel model = table.getModel();
|
---|
400 |
|
---|
401 | if (model == null || model.getColumnCount() == 0) {
|
---|
402 | return;
|
---|
403 | }
|
---|
404 | // clip column
|
---|
405 | if (column < 0 || column >= model.getColumnCount()) {
|
---|
406 | column = 0; // use the first column
|
---|
407 | }
|
---|
408 |
|
---|
409 | char firstChar = Character.toLowerCase(arg0.getKeyChar());
|
---|
410 |
|
---|
411 | // visit every row and find a matching entry
|
---|
412 | for (int i = 0; i < model.getRowCount(); i++) {
|
---|
413 | Object obj = model.getValueAt(i, column);
|
---|
414 | if (obj != null) {
|
---|
415 | String s = obj.toString();
|
---|
416 | if (s.length() > 0 && firstChar == Character.toLowerCase(s.charAt(0))) {
|
---|
417 | // select entry and make it visible in the table
|
---|
418 | table.getSelectionModel().setSelectionInterval(i, i);
|
---|
419 | table.scrollRectToVisible(streetTable.getCellRect(i, 0, true));
|
---|
420 | return;
|
---|
421 | }
|
---|
422 | }
|
---|
423 | }
|
---|
424 | }
|
---|
425 | }
|
---|
426 | }
|
---|