1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.Color;
|
---|
9 | import java.awt.Component;
|
---|
10 | import java.awt.GridLayout;
|
---|
11 | import java.awt.Rectangle;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.ActionListener;
|
---|
14 | import java.awt.event.KeyEvent;
|
---|
15 | import java.awt.event.MouseAdapter;
|
---|
16 | import java.awt.event.MouseEvent;
|
---|
17 | import java.util.ArrayList;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.Collections;
|
---|
20 | import java.util.Comparator;
|
---|
21 | import java.util.HashMap;
|
---|
22 | import java.util.HashSet;
|
---|
23 | import java.util.LinkedList;
|
---|
24 | import java.util.List;
|
---|
25 | import java.util.Set;
|
---|
26 | import java.util.logging.Logger;
|
---|
27 |
|
---|
28 | import javax.swing.AbstractAction;
|
---|
29 | import javax.swing.AbstractListModel;
|
---|
30 | import javax.swing.BorderFactory;
|
---|
31 | import javax.swing.DefaultListSelectionModel;
|
---|
32 | import javax.swing.JButton;
|
---|
33 | import javax.swing.JList;
|
---|
34 | import javax.swing.JMenuItem;
|
---|
35 | import javax.swing.JPanel;
|
---|
36 | import javax.swing.JPopupMenu;
|
---|
37 | import javax.swing.JScrollPane;
|
---|
38 | import javax.swing.ListSelectionModel;
|
---|
39 | import javax.swing.SwingConstants;
|
---|
40 | import javax.swing.SwingUtilities;
|
---|
41 | import javax.swing.event.ListDataEvent;
|
---|
42 | import javax.swing.event.ListDataListener;
|
---|
43 | import javax.swing.event.ListSelectionEvent;
|
---|
44 | import javax.swing.event.ListSelectionListener;
|
---|
45 | import javax.swing.plaf.basic.BasicArrowButton;
|
---|
46 |
|
---|
47 | import org.openstreetmap.josm.Main;
|
---|
48 | import org.openstreetmap.josm.actions.AutoScaleAction;
|
---|
49 | import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
|
---|
50 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
51 | import org.openstreetmap.josm.data.osm.Node;
|
---|
52 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
53 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
54 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
55 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
56 | import org.openstreetmap.josm.data.osm.Way;
|
---|
57 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
58 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
---|
59 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
---|
60 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
|
---|
61 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
---|
62 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
---|
63 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
---|
64 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
---|
65 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
---|
66 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
---|
67 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
---|
68 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
|
---|
69 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
70 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
71 | import org.openstreetmap.josm.gui.MapView;
|
---|
72 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
73 | import org.openstreetmap.josm.gui.SideButton;
|
---|
74 | import org.openstreetmap.josm.gui.MapView.EditLayerChangeListener;
|
---|
75 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
|
---|
76 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
77 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
78 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
79 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
80 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * A small tool dialog for displaying the current selection.
|
---|
84 | *
|
---|
85 | */
|
---|
86 | public class SelectionListDialog extends ToggleDialog {
|
---|
87 | @SuppressWarnings("unused")
|
---|
88 | static private final Logger logger = Logger.getLogger(SelectionListDialog.class.getName());
|
---|
89 |
|
---|
90 | private JList lstPrimitives;
|
---|
91 | private SelectionListModel model;
|
---|
92 |
|
---|
93 | private SelectAction actSelect;
|
---|
94 | private SearchAction actSearch;
|
---|
95 | private ZoomToJOSMSelectionAction actZoomToJOSMSelection;
|
---|
96 | private ZoomToListSelection actZoomToListSelection;
|
---|
97 | private SetRelationSelection actSetRelationSelection;
|
---|
98 | private EditRelationSelection actEditRelationSelection;
|
---|
99 | private DownloadSelectedIncompleteMembersAction actDownloadSelectedIncompleteMembers;
|
---|
100 | private InspectAction actInspect;
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Builds the panel with the list of selected OSM primitives
|
---|
104 | *
|
---|
105 | * @return the panel with the list of selected OSM primitives
|
---|
106 | */
|
---|
107 | protected JPanel buildListPanel() {
|
---|
108 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
109 | DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
|
---|
110 | model = new SelectionListModel(selectionModel);
|
---|
111 | lstPrimitives = new JList(model);
|
---|
112 | lstPrimitives.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
113 | lstPrimitives.setSelectionModel(selectionModel);
|
---|
114 | lstPrimitives.setCellRenderer(new OsmPrimitivRenderer());
|
---|
115 | pnl.add(new JScrollPane(lstPrimitives), BorderLayout.CENTER);
|
---|
116 |
|
---|
117 | return pnl;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Builds the row of action buttons at the bottom of this dialog
|
---|
122 | *
|
---|
123 | * @return the panel
|
---|
124 | */
|
---|
125 | protected JPanel buildActionPanel() {
|
---|
126 | JPanel pnl = new JPanel(new GridLayout(1,2));
|
---|
127 |
|
---|
128 | // the select action
|
---|
129 | final JButton selectButton = new SideButton(actSelect = new SelectAction());
|
---|
130 | lstPrimitives.getSelectionModel().addListSelectionListener(actSelect);
|
---|
131 | pnl.add(selectButton);
|
---|
132 | BasicArrowButton selectionHistoryMenuButton = createArrowButton(selectButton);
|
---|
133 | selectionHistoryMenuButton.addActionListener(new ActionListener() {
|
---|
134 | public void actionPerformed(ActionEvent e) {
|
---|
135 | SelectionHistoryPopup.launch(selectButton, model.getSelectionHistory());
|
---|
136 | }
|
---|
137 | });
|
---|
138 |
|
---|
139 | // the search button
|
---|
140 | final JButton searchButton = new SideButton(actSearch = new SearchAction());
|
---|
141 | pnl.add(searchButton);
|
---|
142 |
|
---|
143 | BasicArrowButton searchHistoryMenuButton = createArrowButton(searchButton);
|
---|
144 | searchHistoryMenuButton.addActionListener(new ActionListener() {
|
---|
145 | public void actionPerformed(ActionEvent e) {
|
---|
146 | SearchPopupMenu.launch(searchButton);
|
---|
147 | }
|
---|
148 | });
|
---|
149 |
|
---|
150 | return pnl;
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Builds the content panel for this dialog
|
---|
155 | *
|
---|
156 | * @return the content panel
|
---|
157 | */
|
---|
158 | protected JPanel buildContentPanel() {
|
---|
159 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
160 | pnl.add(buildListPanel(), BorderLayout.CENTER);
|
---|
161 | pnl.add(buildActionPanel(), BorderLayout.SOUTH);
|
---|
162 | return pnl;
|
---|
163 | }
|
---|
164 |
|
---|
165 | public SelectionListDialog() {
|
---|
166 | super(tr("Current Selection"), "selectionlist", tr("Open a selection list window."),
|
---|
167 | Shortcut.registerShortcut("subwindow:selection", tr("Toggle: {0}", tr("Current Selection")), KeyEvent.VK_T, Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT),
|
---|
168 | 150, // default height
|
---|
169 | true // default is "show dialog"
|
---|
170 | );
|
---|
171 |
|
---|
172 | add(buildContentPanel(), BorderLayout.CENTER);
|
---|
173 | model.addListDataListener(new TitleUpdater());
|
---|
174 | actZoomToJOSMSelection = new ZoomToJOSMSelectionAction();
|
---|
175 | model.addListDataListener(actZoomToJOSMSelection);
|
---|
176 |
|
---|
177 | actZoomToListSelection = new ZoomToListSelection();
|
---|
178 | lstPrimitives.getSelectionModel().addListSelectionListener(actZoomToListSelection);
|
---|
179 |
|
---|
180 | actSetRelationSelection = new SetRelationSelection();
|
---|
181 | lstPrimitives.getSelectionModel().addListSelectionListener(actSetRelationSelection);
|
---|
182 |
|
---|
183 | actEditRelationSelection = new EditRelationSelection();
|
---|
184 | lstPrimitives.getSelectionModel().addListSelectionListener(actEditRelationSelection);
|
---|
185 |
|
---|
186 | actDownloadSelectedIncompleteMembers = new DownloadSelectedIncompleteMembersAction();
|
---|
187 | lstPrimitives.getSelectionModel().addListSelectionListener(actDownloadSelectedIncompleteMembers);
|
---|
188 |
|
---|
189 | actInspect = new InspectAction();
|
---|
190 | lstPrimitives.getSelectionModel().addListSelectionListener(actInspect);
|
---|
191 |
|
---|
192 | lstPrimitives.addMouseListener(new SelectionPopupMenuLauncher());
|
---|
193 | lstPrimitives.addMouseListener(new DblClickHandler());
|
---|
194 | }
|
---|
195 |
|
---|
196 | @Override
|
---|
197 | public void showNotify() {
|
---|
198 | MapView.addEditLayerChangeListener(model);
|
---|
199 | SelectionEventManager.getInstance().addSelectionListener(model, FireMode.IN_EDT_CONSOLIDATED);
|
---|
200 | DatasetEventManager.getInstance().addDatasetListener(model, FireMode.IN_EDT);
|
---|
201 | MapView.addEditLayerChangeListener(actSearch);
|
---|
202 | if (Main.map.mapView.getEditLayer() != null) {
|
---|
203 | model.setJOSMSelection(Main.map.mapView.getEditLayer().data.getSelected());
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | @Override
|
---|
208 | public void hideNotify() {
|
---|
209 | MapView.removeEditLayerChangeListener(actSearch);
|
---|
210 | MapView.removeEditLayerChangeListener(model);
|
---|
211 | SelectionEventManager.getInstance().removeSelectionListener(model);
|
---|
212 | DatasetEventManager.getInstance().removeDatasetListener(model);
|
---|
213 | }
|
---|
214 |
|
---|
215 | private BasicArrowButton createArrowButton(JButton parentButton) {
|
---|
216 | BasicArrowButton arrowButton = new BasicArrowButton(SwingConstants.SOUTH, null, null, Color.BLACK, null);
|
---|
217 | arrowButton.setBorder(BorderFactory.createEmptyBorder());
|
---|
218 | parentButton.setLayout(new BorderLayout());
|
---|
219 | parentButton.add(arrowButton, BorderLayout.EAST);
|
---|
220 | return arrowButton;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Responds to double clicks on the list of selected objects
|
---|
225 | */
|
---|
226 | class DblClickHandler extends MouseAdapter {
|
---|
227 | @Override
|
---|
228 | public void mouseClicked(MouseEvent e) {
|
---|
229 | if (e.getClickCount() < 2 || ! SwingUtilities.isLeftMouseButton(e)) return;
|
---|
230 | int idx = lstPrimitives.locationToIndex(e.getPoint());
|
---|
231 | if (idx < 0) return;
|
---|
232 | OsmDataLayer layer = Main.main.getEditLayer();
|
---|
233 | if(layer == null) return;
|
---|
234 | layer.data.setSelected(Collections.singleton((OsmPrimitive)model.getElementAt(idx)));
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * The popup menu launcher
|
---|
240 | */
|
---|
241 | class SelectionPopupMenuLauncher extends PopupMenuLauncher {
|
---|
242 | private SelectionPopup popup = new SelectionPopup();
|
---|
243 |
|
---|
244 | @Override
|
---|
245 | public void launch(MouseEvent evt) {
|
---|
246 | if (model.getSelected().isEmpty()) {
|
---|
247 | int idx = lstPrimitives.locationToIndex(evt.getPoint());
|
---|
248 | if (idx < 0) return;
|
---|
249 | model.setSelected(Collections.singleton((OsmPrimitive)model.getElementAt(idx)));
|
---|
250 | }
|
---|
251 | popup.show(lstPrimitives, evt.getX(), evt.getY());
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * The popup menu for the selection list
|
---|
257 | */
|
---|
258 | class SelectionPopup extends JPopupMenu {
|
---|
259 | public SelectionPopup() {
|
---|
260 | add(actZoomToJOSMSelection);
|
---|
261 | add(actZoomToListSelection);
|
---|
262 | addSeparator();
|
---|
263 | add(actSetRelationSelection);
|
---|
264 | add(actEditRelationSelection);
|
---|
265 | addSeparator();
|
---|
266 | add(actDownloadSelectedIncompleteMembers);
|
---|
267 | addSeparator();
|
---|
268 | add(actInspect);
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Updates the dialog title with a summary of the current JOSM selection
|
---|
274 | */
|
---|
275 | class TitleUpdater implements ListDataListener {
|
---|
276 | protected void updateTitle() {
|
---|
277 | setTitle(model.getJOSMSelectionSummary());
|
---|
278 | }
|
---|
279 |
|
---|
280 | public void contentsChanged(ListDataEvent e) {
|
---|
281 | updateTitle();
|
---|
282 | }
|
---|
283 |
|
---|
284 | public void intervalAdded(ListDataEvent e) {
|
---|
285 | updateTitle();
|
---|
286 | }
|
---|
287 |
|
---|
288 | public void intervalRemoved(ListDataEvent e) {
|
---|
289 | updateTitle();
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | /**
|
---|
294 | * Launches the search dialog
|
---|
295 | */
|
---|
296 | static class SearchAction extends AbstractAction implements EditLayerChangeListener {
|
---|
297 | public SearchAction() {
|
---|
298 | putValue(NAME, tr("Search"));
|
---|
299 | putValue(SHORT_DESCRIPTION, tr("Search for objects"));
|
---|
300 | putValue(SMALL_ICON, ImageProvider.get("dialogs","select"));
|
---|
301 | updateEnabledState();
|
---|
302 | }
|
---|
303 |
|
---|
304 | public void actionPerformed(ActionEvent e) {
|
---|
305 | if (!isEnabled()) return;
|
---|
306 | org.openstreetmap.josm.actions.search.SearchAction.search();
|
---|
307 | }
|
---|
308 |
|
---|
309 | public void updateEnabledState() {
|
---|
310 | setEnabled(Main.main != null && Main.main.getEditLayer() != null);
|
---|
311 | }
|
---|
312 |
|
---|
313 | public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
|
---|
314 | updateEnabledState();
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Sets the current JOSM selection to the OSM primitives selected in the list
|
---|
320 | * of this dialog
|
---|
321 | */
|
---|
322 | class SelectAction extends AbstractAction implements ListSelectionListener {
|
---|
323 | public SelectAction() {
|
---|
324 | putValue(NAME, tr("Select"));
|
---|
325 | putValue(SHORT_DESCRIPTION, tr("Set the selected elements on the map to the selected items in the list above."));
|
---|
326 | putValue(SMALL_ICON, ImageProvider.get("dialogs","select"));
|
---|
327 | updateEnabledState();
|
---|
328 | }
|
---|
329 |
|
---|
330 | public void actionPerformed(ActionEvent e) {
|
---|
331 | Collection<OsmPrimitive> sel = model.getSelected();
|
---|
332 | if (sel.isEmpty())return;
|
---|
333 | if (Main.map == null || Main.map.mapView == null || Main.map.mapView.getEditLayer() == null) return;
|
---|
334 | Main.map.mapView.getEditLayer().data.setSelected(sel);
|
---|
335 | }
|
---|
336 |
|
---|
337 | public void updateEnabledState() {
|
---|
338 | setEnabled(!model.getSelected().isEmpty());
|
---|
339 | }
|
---|
340 |
|
---|
341 | public void valueChanged(ListSelectionEvent e) {
|
---|
342 | updateEnabledState();
|
---|
343 | }
|
---|
344 | }
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * The action for zooming to the primitives in the current JOSM selection
|
---|
348 | *
|
---|
349 | */
|
---|
350 | class ZoomToJOSMSelectionAction extends AbstractAction implements ListDataListener {
|
---|
351 |
|
---|
352 | public ZoomToJOSMSelectionAction() {
|
---|
353 | putValue(NAME,tr("Zoom to selection"));
|
---|
354 | putValue(SHORT_DESCRIPTION, tr("Zoom to selection"));
|
---|
355 | putValue(SMALL_ICON, ImageProvider.get("dialogs/autoscale", "selection"));
|
---|
356 | updateEnabledState();
|
---|
357 | }
|
---|
358 |
|
---|
359 | public void actionPerformed(ActionEvent e) {
|
---|
360 | AutoScaleAction.autoScale("selection");
|
---|
361 | }
|
---|
362 |
|
---|
363 | public void updateEnabledState() {
|
---|
364 | setEnabled(model.getSize() > 0);
|
---|
365 | }
|
---|
366 |
|
---|
367 | public void contentsChanged(ListDataEvent e) {
|
---|
368 | updateEnabledState();
|
---|
369 | }
|
---|
370 |
|
---|
371 | public void intervalAdded(ListDataEvent e) {
|
---|
372 | updateEnabledState();
|
---|
373 | }
|
---|
374 |
|
---|
375 | public void intervalRemoved(ListDataEvent e) {
|
---|
376 | updateEnabledState();
|
---|
377 | }
|
---|
378 | }
|
---|
379 |
|
---|
380 | /**
|
---|
381 | * The action for zooming to the primitives which are currently selected in
|
---|
382 | * the list displaying the JOSM selection
|
---|
383 | *
|
---|
384 | */
|
---|
385 | class ZoomToListSelection extends AbstractAction implements ListSelectionListener{
|
---|
386 | public ZoomToListSelection() {
|
---|
387 | putValue(NAME, tr("Zoom to selected element(s)"));
|
---|
388 | putValue(SHORT_DESCRIPTION, tr("Zoom to selected element(s)"));
|
---|
389 | putValue(SMALL_ICON, ImageProvider.get("dialogs/autoscale", "selection"));
|
---|
390 | updateEnabledState();
|
---|
391 | }
|
---|
392 |
|
---|
393 | public void actionPerformed(ActionEvent e) {
|
---|
394 | BoundingXYVisitor box = new BoundingXYVisitor();
|
---|
395 | Collection<OsmPrimitive> sel = model.getSelected();
|
---|
396 | if (sel.isEmpty()) return;
|
---|
397 | box.computeBoundingBox(sel);
|
---|
398 | if (box.getBounds() == null)
|
---|
399 | return;
|
---|
400 | box.enlargeBoundingBox();
|
---|
401 | Main.map.mapView.recalculateCenterScale(box);
|
---|
402 | }
|
---|
403 |
|
---|
404 | public void updateEnabledState() {
|
---|
405 | setEnabled(!model.getSelected().isEmpty());
|
---|
406 | }
|
---|
407 |
|
---|
408 | public void valueChanged(ListSelectionEvent e) {
|
---|
409 | updateEnabledState();
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * The action for setting and editing a relation in relation list dialog
|
---|
415 | *
|
---|
416 | */
|
---|
417 | class EditRelationSelection extends SetRelationSelection {
|
---|
418 | public EditRelationSelection() {
|
---|
419 | putValue(NAME, tr("Call editor for relation"));
|
---|
420 | putValue(SHORT_DESCRIPTION, tr("Call relation editor for selected relation"));
|
---|
421 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
|
---|
422 | updateEnabledState();
|
---|
423 | }
|
---|
424 |
|
---|
425 | @Override
|
---|
426 | public void actionPerformed(ActionEvent e) {
|
---|
427 | Relation relation = (Relation)model.getSelected().toArray()[0];
|
---|
428 | Collection<RelationMember> members = new HashSet<RelationMember>();
|
---|
429 | Collection<OsmPrimitive> selection = model.getAllElements();
|
---|
430 | for (RelationMember member: relation.getMembers()) {
|
---|
431 | if (selection.contains(member.getMember())) {
|
---|
432 | members.add(member);
|
---|
433 | }
|
---|
434 | }
|
---|
435 | Main.map.relationListDialog.selectRelation(relation);
|
---|
436 | RelationEditor.getEditor(Main.map.mapView.getEditLayer(), relation,
|
---|
437 | members).setVisible(true);
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | /**
|
---|
442 | * The action for setting a relation in relation list dialog
|
---|
443 | *
|
---|
444 | */
|
---|
445 | class SetRelationSelection extends AbstractAction implements ListSelectionListener{
|
---|
446 | public SetRelationSelection() {
|
---|
447 | putValue(NAME, tr("Select in relation list"));
|
---|
448 | putValue(SHORT_DESCRIPTION, tr("Select relation in relation list."));
|
---|
449 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "selectionlist"));
|
---|
450 | updateEnabledState();
|
---|
451 | }
|
---|
452 |
|
---|
453 | public void actionPerformed(ActionEvent e) {
|
---|
454 | Relation relation = (Relation)model.getSelected().toArray()[0];
|
---|
455 | Main.map.relationListDialog.selectRelation(relation);
|
---|
456 | }
|
---|
457 |
|
---|
458 | public void updateEnabledState() {
|
---|
459 | Object[] sel = model.getSelected().toArray();
|
---|
460 | setEnabled(sel.length == 1 && sel[0] instanceof Relation);
|
---|
461 | }
|
---|
462 |
|
---|
463 | public void valueChanged(ListSelectionEvent e) {
|
---|
464 | updateEnabledState();
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * The list model for the list of OSM primitives in the current JOSM selection.
|
---|
470 | *
|
---|
471 | * The model also maintains a history of the last {@see SelectionListModel#SELECTION_HISTORY_SIZE}
|
---|
472 | * JOSM selection.
|
---|
473 | *
|
---|
474 | */
|
---|
475 | static private class SelectionListModel extends AbstractListModel implements EditLayerChangeListener, SelectionChangedListener, DataSetListener{
|
---|
476 |
|
---|
477 | private static final int SELECTION_HISTORY_SIZE = 10;
|
---|
478 |
|
---|
479 | private final LinkedList<Collection<? extends OsmPrimitive>> history = new LinkedList<Collection<? extends OsmPrimitive>>();
|
---|
480 | private final List<OsmPrimitive> selection = new ArrayList<OsmPrimitive>();
|
---|
481 | private DefaultListSelectionModel selectionModel;
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Constructor
|
---|
485 | * @param selectionModel the selection model used in the list
|
---|
486 | */
|
---|
487 | public SelectionListModel(DefaultListSelectionModel selectionModel) {
|
---|
488 | this.selectionModel = selectionModel;
|
---|
489 | }
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Replies a summary of the current JOSM selection
|
---|
493 | *
|
---|
494 | * @return a summary of the current JOSM selection
|
---|
495 | */
|
---|
496 | public String getJOSMSelectionSummary() {
|
---|
497 | if (selection.isEmpty()) return tr("Selection");
|
---|
498 | int numNodes = 0;
|
---|
499 | int numWays = 0;
|
---|
500 | int numRelations = 0;
|
---|
501 | for (OsmPrimitive p: selection) {
|
---|
502 | switch(p.getType()) {
|
---|
503 | case NODE: numNodes++; break;
|
---|
504 | case WAY: numWays++; break;
|
---|
505 | case RELATION: numRelations++; break;
|
---|
506 | }
|
---|
507 | }
|
---|
508 | return tr("Sel.: Rel.:{0} / Ways:{1} / Nodes:{2}", numRelations, numWays, numNodes);
|
---|
509 | }
|
---|
510 |
|
---|
511 | /**
|
---|
512 | * Remembers a JOSM selection the history of JOSM selections
|
---|
513 | *
|
---|
514 | * @param selection the JOSM selection. Ignored if null or empty.
|
---|
515 | */
|
---|
516 | public void remember(Collection<? extends OsmPrimitive> selection) {
|
---|
517 | if (selection == null)return;
|
---|
518 | if (selection.isEmpty())return;
|
---|
519 | if (history.isEmpty()) {
|
---|
520 | history.add(selection);
|
---|
521 | return;
|
---|
522 | }
|
---|
523 | if (history.getFirst().equals(selection)) return;
|
---|
524 | history.addFirst(selection);
|
---|
525 | for(int i = 1; i < history.size(); ++i) {
|
---|
526 | if(history.get(i).equals(selection)) {
|
---|
527 | history.remove(i);
|
---|
528 | break;
|
---|
529 | }
|
---|
530 | }
|
---|
531 | while (history.size() > SELECTION_HISTORY_SIZE) {
|
---|
532 | history.removeLast();
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 | /**
|
---|
537 | * Replies the history of JOSM selections
|
---|
538 | *
|
---|
539 | * @return
|
---|
540 | */
|
---|
541 | public List<Collection<? extends OsmPrimitive>> getSelectionHistory() {
|
---|
542 | return history;
|
---|
543 | }
|
---|
544 |
|
---|
545 | public Object getElementAt(int index) {
|
---|
546 | return selection.get(index);
|
---|
547 | }
|
---|
548 |
|
---|
549 | public int getSize() {
|
---|
550 | return selection.size();
|
---|
551 | }
|
---|
552 |
|
---|
553 | /**
|
---|
554 | * Replies the collection of OSM primitives currently selected in the view
|
---|
555 | * of this model
|
---|
556 | *
|
---|
557 | * @return
|
---|
558 | */
|
---|
559 | public Collection<OsmPrimitive> getSelected() {
|
---|
560 | Set<OsmPrimitive> sel = new HashSet<OsmPrimitive>();
|
---|
561 | for(int i=0; i< getSize();i++) {
|
---|
562 | if (selectionModel.isSelectedIndex(i)) {
|
---|
563 | sel.add(selection.get(i));
|
---|
564 | }
|
---|
565 | }
|
---|
566 | return sel;
|
---|
567 | }
|
---|
568 |
|
---|
569 | /**
|
---|
570 | * Replies the collection of OSM primitives in the view
|
---|
571 | * of this model
|
---|
572 | *
|
---|
573 | * @return
|
---|
574 | */
|
---|
575 | public Collection<OsmPrimitive> getAllElements() {
|
---|
576 | return selection;
|
---|
577 | }
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Sets the OSM primitives to be selected in the view of this model
|
---|
581 | *
|
---|
582 | * @param sel the collection of primitives to select
|
---|
583 | */
|
---|
584 | public void setSelected(Collection<OsmPrimitive> sel) {
|
---|
585 | selectionModel.clearSelection();
|
---|
586 | if (sel == null) return;
|
---|
587 | for (OsmPrimitive p: sel){
|
---|
588 | int i = selection.indexOf(p);
|
---|
589 | if (i >= 0){
|
---|
590 | selectionModel.addSelectionInterval(i, i);
|
---|
591 | }
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 | @Override
|
---|
596 | protected void fireContentsChanged(Object source, int index0, int index1) {
|
---|
597 | Collection<OsmPrimitive> sel = getSelected();
|
---|
598 | super.fireContentsChanged(source, index0, index1);
|
---|
599 | setSelected(sel);
|
---|
600 | }
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Sets the collection of currently selected OSM objects
|
---|
604 | *
|
---|
605 | * @param selection the collection of currently selected OSM objects
|
---|
606 | */
|
---|
607 | public void setJOSMSelection(Collection<? extends OsmPrimitive> selection) {
|
---|
608 | this.selection.clear();
|
---|
609 | if (selection == null) {
|
---|
610 | fireContentsChanged(this, 0, getSize());
|
---|
611 | return;
|
---|
612 | }
|
---|
613 | this.selection.addAll(selection);
|
---|
614 | sort();
|
---|
615 | fireContentsChanged(this, 0, getSize());
|
---|
616 | remember(selection);
|
---|
617 | }
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Triggers a refresh of the view for all primitives in {@code toUpdate}
|
---|
621 | * which are currently displayed in the view
|
---|
622 | *
|
---|
623 | * @param toUpdate the collection of primitives to update
|
---|
624 | */
|
---|
625 | public void update(Collection<? extends OsmPrimitive> toUpdate) {
|
---|
626 | if (toUpdate == null) return;
|
---|
627 | if (toUpdate.isEmpty()) return;
|
---|
628 | Collection<OsmPrimitive> sel = getSelected();
|
---|
629 | for (OsmPrimitive p: toUpdate){
|
---|
630 | int i = selection.indexOf(p);
|
---|
631 | if (i >= 0) {
|
---|
632 | super.fireContentsChanged(this, i,i);
|
---|
633 | }
|
---|
634 | }
|
---|
635 | setSelected(sel);
|
---|
636 | }
|
---|
637 |
|
---|
638 | /**
|
---|
639 | * Replies the list of selected relations with incomplete members
|
---|
640 | *
|
---|
641 | * @return the list of selected relations with incomplete members
|
---|
642 | */
|
---|
643 | public List<Relation> getSelectedRelationsWithIncompleteMembers() {
|
---|
644 | List<Relation> ret = new LinkedList<Relation>();
|
---|
645 | for(int i=0; i<getSize(); i++) {
|
---|
646 | if (!selectionModel.isSelectedIndex(i)) {
|
---|
647 | continue;
|
---|
648 | }
|
---|
649 | OsmPrimitive p = selection.get(i);
|
---|
650 | if (! (p instanceof Relation)) {
|
---|
651 | continue;
|
---|
652 | }
|
---|
653 | if (p.isNew()) {
|
---|
654 | continue;
|
---|
655 | }
|
---|
656 | Relation r = (Relation)p;
|
---|
657 | if (r.hasIncompleteMembers()) {
|
---|
658 | ret.add(r);
|
---|
659 | }
|
---|
660 | }
|
---|
661 | return ret;
|
---|
662 | }
|
---|
663 |
|
---|
664 | /**
|
---|
665 | * Sorts the current elements in the selection
|
---|
666 | */
|
---|
667 | public void sort() {
|
---|
668 | Collections.sort(this.selection, new OsmPrimitiveComparator());
|
---|
669 | }
|
---|
670 |
|
---|
671 | /* ------------------------------------------------------------------------ */
|
---|
672 | /* interface EditLayerChangeListener */
|
---|
673 | /* ------------------------------------------------------------------------ */
|
---|
674 | public void editLayerChanged(OsmDataLayer oldLayer, OsmDataLayer newLayer) {
|
---|
675 | if (newLayer == null) {
|
---|
676 | setJOSMSelection(null);
|
---|
677 | } else {
|
---|
678 | setJOSMSelection(newLayer.data.getSelected());
|
---|
679 | }
|
---|
680 | }
|
---|
681 |
|
---|
682 | /* ------------------------------------------------------------------------ */
|
---|
683 | /* interface SelectionChangeListener */
|
---|
684 | /* ------------------------------------------------------------------------ */
|
---|
685 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
686 | setJOSMSelection(newSelection);
|
---|
687 | }
|
---|
688 |
|
---|
689 | /* ------------------------------------------------------------------------ */
|
---|
690 | /* interface DataSetListener */
|
---|
691 | /* ------------------------------------------------------------------------ */
|
---|
692 | public void dataChanged(DataChangedEvent event) {
|
---|
693 | // refresh the whole list
|
---|
694 | fireContentsChanged(this, 0, getSize());
|
---|
695 | }
|
---|
696 |
|
---|
697 | public void nodeMoved(NodeMovedEvent event) {
|
---|
698 | // may influence the display name of primitives, update the data
|
---|
699 | update(event.getPrimitives());
|
---|
700 | }
|
---|
701 |
|
---|
702 | public void otherDatasetChange(AbstractDatasetChangedEvent event) {
|
---|
703 | // may influence the display name of primitives, update the data
|
---|
704 | update(event.getPrimitives());
|
---|
705 | }
|
---|
706 |
|
---|
707 | public void relationMembersChanged(RelationMembersChangedEvent event) {
|
---|
708 | // may influence the display name of primitives, update the data
|
---|
709 | update(event.getPrimitives());
|
---|
710 | }
|
---|
711 |
|
---|
712 | public void tagsChanged(TagsChangedEvent event) {
|
---|
713 | // may influence the display name of primitives, update the data
|
---|
714 | update(event.getPrimitives());
|
---|
715 | }
|
---|
716 |
|
---|
717 | public void wayNodesChanged(WayNodesChangedEvent event) {
|
---|
718 | // may influence the display name of primitives, update the data
|
---|
719 | update(event.getPrimitives());
|
---|
720 | }
|
---|
721 |
|
---|
722 | public void primtivesAdded(PrimitivesAddedEvent event) {/* ignored - handled by SelectionChangeListener */}
|
---|
723 | public void primtivesRemoved(PrimitivesRemovedEvent event) {/* ignored - handled by SelectionChangeListener*/}
|
---|
724 | }
|
---|
725 |
|
---|
726 | /**
|
---|
727 | * A specialized {@link JMenuItem} for presenting one entry of the search history
|
---|
728 | *
|
---|
729 | * @author Jan Peter Stotz
|
---|
730 | */
|
---|
731 | protected static class SearchMenuItem extends JMenuItem implements ActionListener {
|
---|
732 | final protected SearchSetting s;
|
---|
733 |
|
---|
734 | public SearchMenuItem(SearchSetting s) {
|
---|
735 | super(s.toString());
|
---|
736 | this.s = s;
|
---|
737 | addActionListener(this);
|
---|
738 | }
|
---|
739 |
|
---|
740 | public void actionPerformed(ActionEvent e) {
|
---|
741 | org.openstreetmap.josm.actions.search.SearchAction.searchWithoutHistory(s);
|
---|
742 | }
|
---|
743 | }
|
---|
744 |
|
---|
745 | /**
|
---|
746 | * The popup menu for the search history entries
|
---|
747 | *
|
---|
748 | */
|
---|
749 | protected static class SearchPopupMenu extends JPopupMenu {
|
---|
750 | static public void launch(Component parent) {
|
---|
751 | if (org.openstreetmap.josm.actions.search.SearchAction.getSearchHistory().isEmpty())
|
---|
752 | return;
|
---|
753 | JPopupMenu menu = new SearchPopupMenu();
|
---|
754 | Rectangle r = parent.getBounds();
|
---|
755 | menu.show(parent, r.x, r.y + r.height);
|
---|
756 | }
|
---|
757 |
|
---|
758 | public SearchPopupMenu() {
|
---|
759 | for (SearchSetting ss: org.openstreetmap.josm.actions.search.SearchAction.getSearchHistory()) {
|
---|
760 | add(new SearchMenuItem(ss));
|
---|
761 | }
|
---|
762 | }
|
---|
763 | }
|
---|
764 |
|
---|
765 | /**
|
---|
766 | * A specialized {@link JMenuItem} for presenting one entry of the selection history
|
---|
767 | *
|
---|
768 | * @author Jan Peter Stotz
|
---|
769 | */
|
---|
770 | protected static class SelectionMenuItem extends JMenuItem implements ActionListener {
|
---|
771 | final private DefaultNameFormatter df = DefaultNameFormatter.getInstance();
|
---|
772 | protected Collection<? extends OsmPrimitive> sel;
|
---|
773 |
|
---|
774 | public SelectionMenuItem(Collection<? extends OsmPrimitive> sel) {
|
---|
775 | super();
|
---|
776 | this.sel = sel;
|
---|
777 | int ways = 0;
|
---|
778 | int nodes = 0;
|
---|
779 | int relations = 0;
|
---|
780 | for (OsmPrimitive o : sel) {
|
---|
781 | if (o instanceof Way) {
|
---|
782 | ways++;
|
---|
783 | } else if (o instanceof Node) {
|
---|
784 | nodes++;
|
---|
785 | } else if (o instanceof Relation) {
|
---|
786 | relations++;
|
---|
787 | }
|
---|
788 | }
|
---|
789 | StringBuffer text = new StringBuffer();
|
---|
790 | if(ways != 0) {
|
---|
791 | text.append(text.length() > 0 ? ", " : "")
|
---|
792 | .append(trn("{0} way", "{0} ways", ways, ways));
|
---|
793 | }
|
---|
794 | if(nodes != 0) {
|
---|
795 | text.append(text.length() > 0 ? ", " : "")
|
---|
796 | .append(trn("{0} node", "{0} nodes", nodes, nodes));
|
---|
797 | }
|
---|
798 | if(relations != 0) {
|
---|
799 | text.append(text.length() > 0 ? ", " : "")
|
---|
800 | .append(trn("{0} relation", "{0} relations", relations, relations));
|
---|
801 | }
|
---|
802 | if(ways + nodes + relations == 1)
|
---|
803 | {
|
---|
804 | text.append(": ");
|
---|
805 | for(OsmPrimitive o : sel) {
|
---|
806 | text.append(o.getDisplayName(df));
|
---|
807 | }
|
---|
808 | setText(text.toString());
|
---|
809 | } else {
|
---|
810 | setText(tr("Selection: {0}", text));
|
---|
811 | }
|
---|
812 | addActionListener(this);
|
---|
813 | }
|
---|
814 |
|
---|
815 | public void actionPerformed(ActionEvent e) {
|
---|
816 | Main.main.getCurrentDataSet().setSelected(sel);
|
---|
817 | }
|
---|
818 | }
|
---|
819 |
|
---|
820 | /**
|
---|
821 | * The popup menue for the JOSM selection history entries
|
---|
822 | *
|
---|
823 | */
|
---|
824 | protected static class SelectionHistoryPopup extends JPopupMenu {
|
---|
825 | static public void launch(Component parent, Collection<Collection<? extends OsmPrimitive>> history) {
|
---|
826 | if (history == null || history.isEmpty()) return;
|
---|
827 | JPopupMenu menu = new SelectionHistoryPopup(history);
|
---|
828 | Rectangle r = parent.getBounds();
|
---|
829 | menu.show(parent, r.x, r.y + r.height);
|
---|
830 | }
|
---|
831 |
|
---|
832 | public SelectionHistoryPopup(Collection<Collection<? extends OsmPrimitive>> history) {
|
---|
833 | for (Collection<? extends OsmPrimitive> sel : history) {
|
---|
834 | add(new SelectionMenuItem(sel));
|
---|
835 | }
|
---|
836 | }
|
---|
837 | }
|
---|
838 |
|
---|
839 | /**
|
---|
840 | * Action for downloading incomplete members of selected relations
|
---|
841 | *
|
---|
842 | */
|
---|
843 | class DownloadSelectedIncompleteMembersAction extends AbstractAction implements ListSelectionListener {
|
---|
844 | public DownloadSelectedIncompleteMembersAction() {
|
---|
845 | putValue(SHORT_DESCRIPTION, tr("Download incomplete members of selected relations"));
|
---|
846 | putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "downloadincompleteselected"));
|
---|
847 | putValue(NAME, tr("Download incomplete members"));
|
---|
848 | updateEnabledState();
|
---|
849 | }
|
---|
850 |
|
---|
851 | public Set<OsmPrimitive> buildSetOfIncompleteMembers(List<Relation> rels) {
|
---|
852 | Set<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
|
---|
853 | for(Relation r: rels) {
|
---|
854 | ret.addAll(r.getIncompleteMembers());
|
---|
855 | }
|
---|
856 | return ret;
|
---|
857 | }
|
---|
858 |
|
---|
859 | public void actionPerformed(ActionEvent e) {
|
---|
860 | if (!isEnabled())
|
---|
861 | return;
|
---|
862 | List<Relation> rels = model.getSelectedRelationsWithIncompleteMembers();
|
---|
863 | if (rels.isEmpty()) return;
|
---|
864 | Main.worker.submit(new DownloadRelationMemberTask(
|
---|
865 | rels,
|
---|
866 | buildSetOfIncompleteMembers(rels),
|
---|
867 | Main.map.mapView.getEditLayer()
|
---|
868 | ));
|
---|
869 | }
|
---|
870 |
|
---|
871 | protected void updateEnabledState() {
|
---|
872 | setEnabled(!model.getSelectedRelationsWithIncompleteMembers().isEmpty());
|
---|
873 | }
|
---|
874 |
|
---|
875 | public void valueChanged(ListSelectionEvent e) {
|
---|
876 | updateEnabledState();
|
---|
877 | }
|
---|
878 | }
|
---|
879 |
|
---|
880 | class InspectAction extends AbstractAction implements ListSelectionListener {
|
---|
881 | public InspectAction() {
|
---|
882 | putValue(SHORT_DESCRIPTION, tr("Get detailed information on the internal state of the objects."));
|
---|
883 | putValue(NAME, tr("Inspect"));
|
---|
884 | updateEnabledState();
|
---|
885 | }
|
---|
886 |
|
---|
887 | public void actionPerformed(ActionEvent e) {
|
---|
888 | Collection<OsmPrimitive> sel = model.getSelected();
|
---|
889 | if (sel.isEmpty()) return;
|
---|
890 | InspectPrimitiveDialog inspectDialog = new InspectPrimitiveDialog(sel);
|
---|
891 | inspectDialog.showDialog();
|
---|
892 | }
|
---|
893 |
|
---|
894 | public void updateEnabledState() {
|
---|
895 | setEnabled(!model.getSelected().isEmpty());
|
---|
896 | }
|
---|
897 |
|
---|
898 | public void valueChanged(ListSelectionEvent e) {
|
---|
899 | updateEnabledState();
|
---|
900 | }
|
---|
901 | }
|
---|
902 |
|
---|
903 | static private class OsmPrimitiveComparator implements Comparator<OsmPrimitive> {
|
---|
904 | final private HashMap<OsmPrimitive, String> cache= new HashMap<OsmPrimitive, String>();
|
---|
905 | final private DefaultNameFormatter df = DefaultNameFormatter.getInstance();
|
---|
906 |
|
---|
907 | private String cachedName(OsmPrimitive p) {
|
---|
908 | String name = cache.get(p);
|
---|
909 | if (name == null) {
|
---|
910 | name = p.getDisplayName(df);
|
---|
911 | cache.put(p, name);
|
---|
912 | }
|
---|
913 | return name;
|
---|
914 | }
|
---|
915 |
|
---|
916 | private int compareName(OsmPrimitive a, OsmPrimitive b) {
|
---|
917 | String an = cachedName(a);
|
---|
918 | String bn = cachedName(b);
|
---|
919 | // make sure display names starting with digits are the end of the
|
---|
920 | // list
|
---|
921 | if (Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
|
---|
922 | return an.compareTo(bn);
|
---|
923 | else if (Character.isDigit(an.charAt(0)) && !Character.isDigit(bn.charAt(0)))
|
---|
924 | return 1;
|
---|
925 | else if (!Character.isDigit(an.charAt(0)) && Character.isDigit(bn.charAt(0)))
|
---|
926 | return -1;
|
---|
927 | return an.compareTo(bn);
|
---|
928 | }
|
---|
929 |
|
---|
930 | private int compareType(OsmPrimitive a, OsmPrimitive b) {
|
---|
931 | // show ways before relations, then nodes
|
---|
932 | //
|
---|
933 | if (a.getType().equals(b.getType())) return 0;
|
---|
934 | if (a.getType().equals(OsmPrimitiveType.WAY)) return -1;
|
---|
935 | if (a.getType().equals(OsmPrimitiveType.NODE)) return 1;
|
---|
936 | // a is a relation
|
---|
937 | if (b.getType().equals(OsmPrimitiveType.WAY)) return 1;
|
---|
938 | // b is a node
|
---|
939 | return -1;
|
---|
940 | }
|
---|
941 | public int compare(OsmPrimitive a, OsmPrimitive b) {
|
---|
942 | if (a.getType().equals(b.getType()))
|
---|
943 | return compareName(a, b);
|
---|
944 | return compareType(a, b);
|
---|
945 | }
|
---|
946 | }
|
---|
947 | }
|
---|