1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.Point;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.awt.event.MouseAdapter;
|
---|
12 | import java.awt.event.MouseEvent;
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.Comparator;
|
---|
17 | import java.util.HashSet;
|
---|
18 | import java.util.Iterator;
|
---|
19 | import java.util.List;
|
---|
20 | import java.util.Set;
|
---|
21 |
|
---|
22 | import javax.swing.AbstractAction;
|
---|
23 | import javax.swing.AbstractListModel;
|
---|
24 | import javax.swing.DefaultListSelectionModel;
|
---|
25 | import javax.swing.JList;
|
---|
26 | import javax.swing.JPanel;
|
---|
27 | import javax.swing.JPopupMenu;
|
---|
28 | import javax.swing.JScrollPane;
|
---|
29 | import javax.swing.KeyStroke;
|
---|
30 | import javax.swing.ListSelectionModel;
|
---|
31 | import javax.swing.SwingUtilities;
|
---|
32 | import javax.swing.event.ListSelectionEvent;
|
---|
33 | import javax.swing.event.ListSelectionListener;
|
---|
34 |
|
---|
35 | import org.openstreetmap.josm.Main;
|
---|
36 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
37 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
38 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
39 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
---|
40 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
---|
41 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
---|
42 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
|
---|
43 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
|
---|
44 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
---|
45 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
---|
46 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
---|
47 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
---|
48 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
---|
49 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
---|
50 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
51 | import org.openstreetmap.josm.gui.MapView;
|
---|
52 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
53 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
54 | import org.openstreetmap.josm.gui.SideButton;
|
---|
55 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
|
---|
56 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
|
---|
57 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
58 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
59 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
60 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
61 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * A dialog showing all known relations, with buttons to add, edit, and
|
---|
65 | * delete them.
|
---|
66 | *
|
---|
67 | * We don't have such dialogs for nodes, segments, and ways, because those
|
---|
68 | * objects are visible on the map and can be selected there. Relations are not.
|
---|
69 | */
|
---|
70 | public class RelationListDialog extends ToggleDialog implements DataSetListener {
|
---|
71 | //private static final Logger logger = Logger.getLogger(RelationListDialog.class.getName());
|
---|
72 |
|
---|
73 | /** The display list. */
|
---|
74 | private JList displaylist;
|
---|
75 | /** the list model used */
|
---|
76 | private RelationListModel model;
|
---|
77 |
|
---|
78 | /** the edit action */
|
---|
79 | private EditAction editAction;
|
---|
80 | /** the delete action */
|
---|
81 | private DeleteAction deleteAction;
|
---|
82 | private NewAction newAction;
|
---|
83 | /** the popup menu */
|
---|
84 | private RelationDialogPopupMenu popupMenu;
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * constructor
|
---|
88 | */
|
---|
89 | public RelationListDialog() {
|
---|
90 | super(tr("Relations"), "relationlist", tr("Open a list of all relations."),
|
---|
91 | Shortcut.registerShortcut("subwindow:relations", tr("Toggle: {0}", tr("Relations")), KeyEvent.VK_R,
|
---|
92 | Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150);
|
---|
93 |
|
---|
94 | // create the list of relations
|
---|
95 | //
|
---|
96 | DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
|
---|
97 | model = new RelationListModel(selectionModel);
|
---|
98 | displaylist = new JList(model);
|
---|
99 | displaylist.setSelectionModel(selectionModel);
|
---|
100 | displaylist.setCellRenderer(new OsmPrimitivRenderer() {
|
---|
101 | /**
|
---|
102 | * Don't show the default tooltip in the relation list.
|
---|
103 | */
|
---|
104 | @Override
|
---|
105 | protected String getComponentToolTipText(OsmPrimitive value) {
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 | });
|
---|
109 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
110 | displaylist.addMouseListener(new MouseEventHandler());
|
---|
111 | add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
112 |
|
---|
113 | // create the panel with buttons
|
---|
114 | //
|
---|
115 | JPanel tp = getButtonPanel(5);
|
---|
116 | // the new action
|
---|
117 | //
|
---|
118 | newAction = new NewAction();
|
---|
119 | tp.add(new SideButton(newAction, false));
|
---|
120 |
|
---|
121 | // the edit action
|
---|
122 | //
|
---|
123 | editAction = new EditAction();
|
---|
124 | displaylist.addListSelectionListener(editAction);
|
---|
125 | tp.add(new SideButton(editAction, false));
|
---|
126 |
|
---|
127 | // the duplicate action
|
---|
128 | //
|
---|
129 | DuplicateAction duplicateAction = new DuplicateAction();
|
---|
130 | displaylist.addListSelectionListener(duplicateAction);
|
---|
131 | tp.add(new SideButton(duplicateAction, false));
|
---|
132 |
|
---|
133 | // the delete action
|
---|
134 | //
|
---|
135 | deleteAction = new DeleteAction();
|
---|
136 | displaylist.addListSelectionListener(deleteAction);
|
---|
137 | tp.add(new SideButton(deleteAction, false));
|
---|
138 |
|
---|
139 | // the select action
|
---|
140 | //
|
---|
141 | SelectAction selectAction = new SelectAction(false);
|
---|
142 | displaylist.addListSelectionListener(selectAction);
|
---|
143 | tp.add(new SideButton(selectAction, false));
|
---|
144 |
|
---|
145 | add(tp, BorderLayout.SOUTH);
|
---|
146 |
|
---|
147 | // activate DEL in the list of relations
|
---|
148 | displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0), "deleteRelation");
|
---|
149 | displaylist.getActionMap().put("deleteRelation", deleteAction);
|
---|
150 |
|
---|
151 | popupMenu = new RelationDialogPopupMenu();
|
---|
152 | }
|
---|
153 |
|
---|
154 | @Override public void showNotify() {
|
---|
155 | MapView.addLayerChangeListener(newAction);
|
---|
156 | newAction.updateEnabledState();
|
---|
157 | DatasetEventManager.getInstance().addDatasetListener(this, FireMode.IN_EDT);
|
---|
158 | dataChanged(null);
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Override public void hideNotify() {
|
---|
162 | MapView.removeLayerChangeListener(newAction);
|
---|
163 | DatasetEventManager.getInstance().removeDatasetListener(this);
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Initializes the relation list dialog from a layer. If <code>layer</code> is null
|
---|
168 | * or if it isn't an {@see OsmDataLayer} the dialog is reset to an empty dialog.
|
---|
169 | * Otherwise it is initialized with the list of non-deleted and visible relations
|
---|
170 | * in the layer's dataset.
|
---|
171 | *
|
---|
172 | * @param layer the layer. May be null.
|
---|
173 | */
|
---|
174 | protected void initFromLayer(Layer layer) {
|
---|
175 | if (layer == null || ! (layer instanceof OsmDataLayer)) {
|
---|
176 | model.setRelations(null);
|
---|
177 | return;
|
---|
178 | }
|
---|
179 | OsmDataLayer l = (OsmDataLayer)layer;
|
---|
180 | model.setRelations(l.data.getRelations());
|
---|
181 | model.updateTitle();
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * Adds a selection listener to the relation list.
|
---|
186 | *
|
---|
187 | * @param listener the listener to add
|
---|
188 | */
|
---|
189 | public void addListSelectionListener(ListSelectionListener listener) {
|
---|
190 | displaylist.addListSelectionListener(listener);
|
---|
191 | }
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Removes a selection listener from the relation list.
|
---|
195 | *
|
---|
196 | * @param listener the listener to remove
|
---|
197 | */
|
---|
198 | public void removeListSelectionListener(ListSelectionListener listener) {
|
---|
199 | displaylist.removeListSelectionListener(listener);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * @return The selected relation in the list
|
---|
204 | */
|
---|
205 | private Relation getSelected() {
|
---|
206 | if(model.getSize() == 1) {
|
---|
207 | displaylist.setSelectedIndex(0);
|
---|
208 | }
|
---|
209 | return (Relation) displaylist.getSelectedValue();
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Selects the relation <code>relation</code> in the list of relations.
|
---|
214 | *
|
---|
215 | * @param relation the relation
|
---|
216 | */
|
---|
217 | public void selectRelation(Relation relation) {
|
---|
218 | if (relation == null) {
|
---|
219 | model.setSelectedRelations(null);
|
---|
220 | } else {
|
---|
221 | model.setSelectedRelations(Collections.singletonList(relation));
|
---|
222 | Integer i = model.getRelationIndex(relation);
|
---|
223 | if (i != null) { // Not all relations have to be in the list (for example when the relation list is hidden, it's not updated with new relations)
|
---|
224 | displaylist.scrollRectToVisible(displaylist.getCellBounds(i, i));
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | class MouseEventHandler extends MouseAdapter {
|
---|
230 | protected void setCurrentRelationAsSelection() {
|
---|
231 | Main.main.getCurrentDataSet().setSelected((Relation)displaylist.getSelectedValue());
|
---|
232 | }
|
---|
233 |
|
---|
234 | protected void editCurrentRelation() {
|
---|
235 | new EditAction().launchEditor(getSelected());
|
---|
236 | }
|
---|
237 |
|
---|
238 | @Override public void mouseClicked(MouseEvent e) {
|
---|
239 | if (Main.main.getEditLayer() == null) return;
|
---|
240 | if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
|
---|
241 | if (e.isControlDown()) {
|
---|
242 | editCurrentRelation();
|
---|
243 | } else {
|
---|
244 | setCurrentRelationAsSelection();
|
---|
245 | }
|
---|
246 | }
|
---|
247 | }
|
---|
248 | private void openPopup(MouseEvent e) {
|
---|
249 | Point p = e.getPoint();
|
---|
250 | int index = displaylist.locationToIndex(p);
|
---|
251 | if (index < 0) return;
|
---|
252 | if (!displaylist.getCellBounds(index, index).contains(e.getPoint()))
|
---|
253 | return;
|
---|
254 | if (! displaylist.isSelectedIndex(index)) {
|
---|
255 | displaylist.setSelectedIndex(index);
|
---|
256 | }
|
---|
257 | popupMenu.show(displaylist, p.x, p.y-3);
|
---|
258 | }
|
---|
259 | @Override public void mousePressed(MouseEvent e) {
|
---|
260 | if (Main.main.getEditLayer() == null) return;
|
---|
261 | if (e.isPopupTrigger()) {
|
---|
262 | openPopup(e);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | @Override public void mouseReleased(MouseEvent e) {
|
---|
266 | if (Main.main.getEditLayer() == null) return;
|
---|
267 | if (e.isPopupTrigger()) {
|
---|
268 | openPopup(e);
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | /**
|
---|
274 | * The edit action
|
---|
275 | *
|
---|
276 | */
|
---|
277 | class EditAction extends AbstractAction implements ListSelectionListener{
|
---|
278 | public EditAction() {
|
---|
279 | putValue(SHORT_DESCRIPTION,tr( "Open an editor for the selected relation"));
|
---|
280 | //putValue(NAME, tr("Edit"));
|
---|
281 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
|
---|
282 | setEnabled(false);
|
---|
283 | }
|
---|
284 | protected Collection<RelationMember> getMembersForCurrentSelection(Relation r) {
|
---|
285 | Collection<RelationMember> members = new HashSet<RelationMember>();
|
---|
286 | Collection<OsmPrimitive> selection = Main.map.mapView.getEditLayer().data.getSelected();
|
---|
287 | for (RelationMember member: r.getMembers()) {
|
---|
288 | if (selection.contains(member.getMember())) {
|
---|
289 | members.add(member);
|
---|
290 | }
|
---|
291 | }
|
---|
292 | return members;
|
---|
293 | }
|
---|
294 |
|
---|
295 | public void launchEditor(Relation toEdit) {
|
---|
296 | if (toEdit == null)
|
---|
297 | return;
|
---|
298 | RelationEditor.getEditor(Main.map.mapView.getEditLayer(),toEdit, getMembersForCurrentSelection(toEdit)).setVisible(true);
|
---|
299 | }
|
---|
300 |
|
---|
301 | public void actionPerformed(ActionEvent e) {
|
---|
302 | if (!isEnabled())
|
---|
303 | return;
|
---|
304 | launchEditor(getSelected());
|
---|
305 | }
|
---|
306 |
|
---|
307 | public void valueChanged(ListSelectionEvent e) {
|
---|
308 | setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length == 1);
|
---|
309 | }
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * The delete action
|
---|
314 | *
|
---|
315 | */
|
---|
316 | class DeleteAction extends AbstractAction implements ListSelectionListener {
|
---|
317 | class AbortException extends Exception {}
|
---|
318 |
|
---|
319 | public DeleteAction() {
|
---|
320 | putValue(SHORT_DESCRIPTION,tr("Delete the selected relation"));
|
---|
321 | //putValue(NAME, tr("Delete"));
|
---|
322 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
|
---|
323 | setEnabled(false);
|
---|
324 | }
|
---|
325 |
|
---|
326 | protected void deleteRelation(Relation toDelete) {
|
---|
327 | if (toDelete == null)
|
---|
328 | return;
|
---|
329 | org.openstreetmap.josm.actions.mapmode.DeleteAction.deleteRelation(
|
---|
330 | Main.main.getEditLayer(),
|
---|
331 | toDelete
|
---|
332 | );
|
---|
333 | }
|
---|
334 |
|
---|
335 | public void actionPerformed(ActionEvent e) {
|
---|
336 | if (!isEnabled()) return;
|
---|
337 | int [] idx = displaylist.getSelectedIndices();
|
---|
338 | ArrayList<Relation> toDelete = new ArrayList<Relation>(idx.length);
|
---|
339 | for (int i: idx) {
|
---|
340 | toDelete.add(model.getRelation(i));
|
---|
341 | }
|
---|
342 | for (Relation r: toDelete) {
|
---|
343 | deleteRelation(r);
|
---|
344 | }
|
---|
345 | }
|
---|
346 |
|
---|
347 | public void valueChanged(ListSelectionEvent e) {
|
---|
348 | setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length > 0);
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * The action for creating a new relation
|
---|
354 | *
|
---|
355 | */
|
---|
356 | static class NewAction extends AbstractAction implements LayerChangeListener{
|
---|
357 | public NewAction() {
|
---|
358 | putValue(SHORT_DESCRIPTION,tr("Create a new relation"));
|
---|
359 | //putValue(NAME, tr("New"));
|
---|
360 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "addrelation"));
|
---|
361 | updateEnabledState();
|
---|
362 | }
|
---|
363 |
|
---|
364 | public void run() {
|
---|
365 | RelationEditor.getEditor(Main.main.getEditLayer(),null, null).setVisible(true);
|
---|
366 | }
|
---|
367 |
|
---|
368 | public void actionPerformed(ActionEvent e) {
|
---|
369 | run();
|
---|
370 | }
|
---|
371 |
|
---|
372 | protected void updateEnabledState() {
|
---|
373 | setEnabled(Main.main != null && Main.main.getEditLayer() != null);
|
---|
374 | }
|
---|
375 |
|
---|
376 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
377 | updateEnabledState();
|
---|
378 | }
|
---|
379 |
|
---|
380 | public void layerAdded(Layer newLayer) {
|
---|
381 | updateEnabledState();
|
---|
382 | }
|
---|
383 |
|
---|
384 | public void layerRemoved(Layer oldLayer) {
|
---|
385 | updateEnabledState();
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Creates a new relation with a copy of the current editor state
|
---|
391 | *
|
---|
392 | */
|
---|
393 | class DuplicateAction extends AbstractAction implements ListSelectionListener {
|
---|
394 | public DuplicateAction() {
|
---|
395 | putValue(SHORT_DESCRIPTION, tr("Create a copy of this relation and open it in another editor window"));
|
---|
396 | putValue(SMALL_ICON, ImageProvider.get("duplicate"));
|
---|
397 | //putValue(NAME, tr("Duplicate"));
|
---|
398 | updateEnabledState();
|
---|
399 | }
|
---|
400 |
|
---|
401 | public void launchEditorForDuplicate(Relation original) {
|
---|
402 | Relation copy = new Relation(original, true);
|
---|
403 | copy.setModified(true);
|
---|
404 | RelationEditor editor = RelationEditor.getEditor(
|
---|
405 | Main.main.getEditLayer(),
|
---|
406 | copy,
|
---|
407 | null /* no selected members */
|
---|
408 | );
|
---|
409 | editor.setVisible(true);
|
---|
410 | }
|
---|
411 |
|
---|
412 | public void actionPerformed(ActionEvent e) {
|
---|
413 | if (!isEnabled())
|
---|
414 | return;
|
---|
415 | launchEditorForDuplicate(getSelected());
|
---|
416 | }
|
---|
417 |
|
---|
418 | protected void updateEnabledState() {
|
---|
419 | setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length == 1);
|
---|
420 | }
|
---|
421 |
|
---|
422 | public void valueChanged(ListSelectionEvent e) {
|
---|
423 | updateEnabledState();
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Sets the current selection to the list of relations selected in this dialog
|
---|
429 | *
|
---|
430 | */
|
---|
431 | class SelectAction extends AbstractAction implements ListSelectionListener{
|
---|
432 | boolean add;
|
---|
433 | public SelectAction(boolean add) {
|
---|
434 | putValue(SHORT_DESCRIPTION, add ? tr("Add the selected relations to the current selection")
|
---|
435 | : tr("Set the current selection to the list of selected relations"));
|
---|
436 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
|
---|
437 | putValue(NAME, add ? tr("Select relation (add)") : tr("Select relation"));
|
---|
438 | this.add = add;
|
---|
439 | updateEnabledState();
|
---|
440 | }
|
---|
441 |
|
---|
442 | public void actionPerformed(ActionEvent e) {
|
---|
443 | if (!isEnabled()) return;
|
---|
444 | int [] idx = displaylist.getSelectedIndices();
|
---|
445 | if (idx == null || idx.length == 0) return;
|
---|
446 | ArrayList<OsmPrimitive> selection = new ArrayList<OsmPrimitive>(idx.length);
|
---|
447 | for (int i: idx) {
|
---|
448 | selection.add(model.getRelation(i));
|
---|
449 | }
|
---|
450 | if(add)
|
---|
451 | Main.map.mapView.getEditLayer().data.addSelected(selection);
|
---|
452 | else
|
---|
453 | Main.map.mapView.getEditLayer().data.setSelected(selection);
|
---|
454 | }
|
---|
455 |
|
---|
456 | protected void updateEnabledState() {
|
---|
457 | setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length > 0);
|
---|
458 | }
|
---|
459 |
|
---|
460 | public void valueChanged(ListSelectionEvent e) {
|
---|
461 | updateEnabledState();
|
---|
462 | }
|
---|
463 | }
|
---|
464 |
|
---|
465 | /**
|
---|
466 | * Sets the current selection to the list of relations selected in this dialog
|
---|
467 | *
|
---|
468 | */
|
---|
469 | class SelectMembersAction extends AbstractAction implements ListSelectionListener{
|
---|
470 | boolean add;
|
---|
471 | public SelectMembersAction(boolean add) {
|
---|
472 | putValue(SHORT_DESCRIPTION,add ? tr("Add the members of all selected relations to current selection")
|
---|
473 | : tr("Select the members of all selected relations"));
|
---|
474 | putValue(SMALL_ICON, ImageProvider.get("selectall"));
|
---|
475 | putValue(NAME, add ? tr("Select members (add)") : tr("Select members"));
|
---|
476 | this.add = add;
|
---|
477 | updateEnabledState();
|
---|
478 | }
|
---|
479 |
|
---|
480 | public void actionPerformed(ActionEvent e) {
|
---|
481 | if (!isEnabled()) return;
|
---|
482 | List<Relation> relations = model.getSelectedRelations();
|
---|
483 | HashSet<OsmPrimitive> members = new HashSet<OsmPrimitive>();
|
---|
484 | for(Relation r: relations) {
|
---|
485 | members.addAll(r.getMemberPrimitives());
|
---|
486 | }
|
---|
487 | if(add)
|
---|
488 | Main.map.mapView.getEditLayer().data.addSelected(members);
|
---|
489 | else
|
---|
490 | Main.map.mapView.getEditLayer().data.setSelected(members);
|
---|
491 | }
|
---|
492 |
|
---|
493 | protected void updateEnabledState() {
|
---|
494 | setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length > 0);
|
---|
495 | }
|
---|
496 |
|
---|
497 | public void valueChanged(ListSelectionEvent e) {
|
---|
498 | updateEnabledState();
|
---|
499 | }
|
---|
500 | }
|
---|
501 |
|
---|
502 | /**
|
---|
503 | * The action for downloading members of all selected relations
|
---|
504 | *
|
---|
505 | */
|
---|
506 | class DownloadMembersAction extends AbstractAction implements ListSelectionListener{
|
---|
507 |
|
---|
508 | public DownloadMembersAction() {
|
---|
509 | putValue(SHORT_DESCRIPTION,tr("Download all members of the selected relations"));
|
---|
510 | putValue(NAME, tr("Download members"));
|
---|
511 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "downloadincomplete"));
|
---|
512 | putValue("help", ht("/Dialog/RelationList#DownloadMembers"));
|
---|
513 | updateEnabledState();
|
---|
514 | }
|
---|
515 |
|
---|
516 | protected void updateEnabledState() {
|
---|
517 | setEnabled(! model.getSelectedNonNewRelations().isEmpty());
|
---|
518 | }
|
---|
519 |
|
---|
520 | public void valueChanged(ListSelectionEvent e) {
|
---|
521 | updateEnabledState();
|
---|
522 | }
|
---|
523 |
|
---|
524 | public void actionPerformed(ActionEvent e) {
|
---|
525 | List<Relation> relations = model.getSelectedNonNewRelations();
|
---|
526 | if (relations.isEmpty())
|
---|
527 | return;
|
---|
528 | Main.worker.submit(new DownloadRelationTask(
|
---|
529 | model.getSelectedNonNewRelations(),
|
---|
530 | Main.map.mapView.getEditLayer())
|
---|
531 | );
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | /**
|
---|
536 | * Action for downloading incomplete members of selected relations
|
---|
537 | *
|
---|
538 | */
|
---|
539 | class DownloadSelectedIncompleteMembersAction extends AbstractAction implements ListSelectionListener{
|
---|
540 | public DownloadSelectedIncompleteMembersAction() {
|
---|
541 | putValue(SHORT_DESCRIPTION, tr("Download incomplete members of selected relations"));
|
---|
542 | putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "downloadincompleteselected"));
|
---|
543 | putValue(NAME, tr("Download incomplete members"));
|
---|
544 | updateEnabledState();
|
---|
545 | }
|
---|
546 |
|
---|
547 | public Set<OsmPrimitive> buildSetOfIncompleteMembers(List<Relation> rels) {
|
---|
548 | Set<OsmPrimitive> ret = new HashSet<OsmPrimitive>();
|
---|
549 | for(Relation r: rels) {
|
---|
550 | ret.addAll(r.getIncompleteMembers());
|
---|
551 | }
|
---|
552 | return ret;
|
---|
553 | }
|
---|
554 |
|
---|
555 | public void actionPerformed(ActionEvent e) {
|
---|
556 | if (!isEnabled())
|
---|
557 | return;
|
---|
558 | List<Relation> rels = model.getSelectedRelationsWithIncompleteMembers();
|
---|
559 | if (rels.isEmpty()) return;
|
---|
560 | Main.worker.submit(new DownloadRelationMemberTask(
|
---|
561 | rels,
|
---|
562 | buildSetOfIncompleteMembers(rels),
|
---|
563 | Main.map.mapView.getEditLayer()
|
---|
564 | ));
|
---|
565 | }
|
---|
566 |
|
---|
567 | protected void updateEnabledState() {
|
---|
568 | setEnabled(!model.getSelectedRelationsWithIncompleteMembers().isEmpty());
|
---|
569 | }
|
---|
570 |
|
---|
571 | public void valueChanged(ListSelectionEvent e) {
|
---|
572 | updateEnabledState();
|
---|
573 | }
|
---|
574 | }
|
---|
575 |
|
---|
576 | /**
|
---|
577 | * The list model for the list of relations displayed in the relation list
|
---|
578 | * dialog.
|
---|
579 | *
|
---|
580 | */
|
---|
581 | private class RelationListModel extends AbstractListModel {
|
---|
582 | private final ArrayList<Relation> relations = new ArrayList<Relation>();
|
---|
583 | private DefaultListSelectionModel selectionModel;
|
---|
584 |
|
---|
585 | public RelationListModel(DefaultListSelectionModel selectionModel) {
|
---|
586 | this.selectionModel = selectionModel;
|
---|
587 | }
|
---|
588 |
|
---|
589 | public Relation getRelation(int idx) {
|
---|
590 | return relations.get(idx);
|
---|
591 | }
|
---|
592 |
|
---|
593 | public void sort() {
|
---|
594 | Collections.sort(
|
---|
595 | relations,
|
---|
596 | DefaultNameFormatter.getInstance().getRelationComparator()
|
---|
597 | );
|
---|
598 | }
|
---|
599 |
|
---|
600 | private boolean isValid(Relation r) {
|
---|
601 | return !r.isDeleted() && r.isVisible() && !r.isIncomplete();
|
---|
602 | }
|
---|
603 |
|
---|
604 | public void setRelations(Collection<Relation> relations) {
|
---|
605 | List<Relation> sel = getSelectedRelations();
|
---|
606 | this.relations.clear();
|
---|
607 | if (relations == null) {
|
---|
608 | selectionModel.clearSelection();
|
---|
609 | fireContentsChanged(this,0,getSize());
|
---|
610 | return;
|
---|
611 |
|
---|
612 | }
|
---|
613 | for (Relation r: relations) {
|
---|
614 | if (isValid(r)) {
|
---|
615 | this.relations.add(r);
|
---|
616 | }
|
---|
617 | }
|
---|
618 | sort();
|
---|
619 | fireIntervalAdded(this, 0, getSize());
|
---|
620 | setSelectedRelations(sel);
|
---|
621 | }
|
---|
622 |
|
---|
623 | /**
|
---|
624 | * Add all relations in <code>addedPrimitives</code> to the model for the
|
---|
625 | * relation list dialog
|
---|
626 | *
|
---|
627 | * @param addedPrimitives the collection of added primitives. May include nodes,
|
---|
628 | * ways, and relations.
|
---|
629 | */
|
---|
630 | public void addRelations(Collection<? extends OsmPrimitive> addedPrimitives) {
|
---|
631 | boolean added = false;
|
---|
632 | for (OsmPrimitive p: addedPrimitives) {
|
---|
633 | if (! (p instanceof Relation)) {
|
---|
634 | continue;
|
---|
635 | }
|
---|
636 |
|
---|
637 | Relation r = (Relation)p;
|
---|
638 | if (relations.contains(r)) {
|
---|
639 | continue;
|
---|
640 | }
|
---|
641 | if (isValid(r)) {
|
---|
642 | relations.add(r);
|
---|
643 | added = true;
|
---|
644 | }
|
---|
645 | }
|
---|
646 | if (added) {
|
---|
647 | List<Relation> sel = getSelectedRelations();
|
---|
648 | sort();
|
---|
649 | fireIntervalAdded(this, 0, getSize());
|
---|
650 | setSelectedRelations(sel);
|
---|
651 | }
|
---|
652 | }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Removes all relations in <code>removedPrimitives</code> from the model
|
---|
656 | *
|
---|
657 | * @param removedPrimitives the removed primitives. May include nodes, ways,
|
---|
658 | * and relations
|
---|
659 | */
|
---|
660 | public void removeRelations(Collection<? extends OsmPrimitive> removedPrimitives) {
|
---|
661 | if (removedPrimitives == null) return;
|
---|
662 | // extract the removed relations
|
---|
663 | //
|
---|
664 | Set<Relation> removedRelations = new HashSet<Relation>();
|
---|
665 | for (OsmPrimitive p: removedPrimitives) {
|
---|
666 | if (! (p instanceof Relation)) {
|
---|
667 | continue;
|
---|
668 | }
|
---|
669 | removedRelations.add((Relation)p);
|
---|
670 | }
|
---|
671 | if (removedRelations.isEmpty())
|
---|
672 | return;
|
---|
673 | int size = relations.size();
|
---|
674 | relations.removeAll(removedRelations);
|
---|
675 | if (size != relations.size()) {
|
---|
676 | List<Relation> sel = getSelectedRelations();
|
---|
677 | sort();
|
---|
678 | fireContentsChanged(this, 0, getSize());
|
---|
679 | setSelectedRelations(sel);
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * Replies the list of selected relations with incomplete members
|
---|
685 | *
|
---|
686 | * @return the list of selected relations with incomplete members
|
---|
687 | */
|
---|
688 | public List<Relation> getSelectedRelationsWithIncompleteMembers() {
|
---|
689 | List<Relation> ret = getSelectedNonNewRelations();
|
---|
690 | Iterator<Relation> it = ret.iterator();
|
---|
691 | while(it.hasNext()) {
|
---|
692 | Relation r = it.next();
|
---|
693 | if (!r.hasIncompleteMembers()) {
|
---|
694 | it.remove();
|
---|
695 | }
|
---|
696 | }
|
---|
697 | return ret;
|
---|
698 | }
|
---|
699 |
|
---|
700 | public Object getElementAt(int index) {
|
---|
701 | return relations.get(index);
|
---|
702 | }
|
---|
703 |
|
---|
704 | public int getSize() {
|
---|
705 | return relations.size();
|
---|
706 | }
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * Replies the list of selected, non-new relations. Empty list,
|
---|
710 | * if there are no selected, non-new relations.
|
---|
711 | *
|
---|
712 | * @return the list of selected, non-new relations.
|
---|
713 | */
|
---|
714 | public List<Relation> getSelectedNonNewRelations() {
|
---|
715 | ArrayList<Relation> ret = new ArrayList<Relation>();
|
---|
716 | for (int i=0; i<getSize();i++) {
|
---|
717 | if (!selectionModel.isSelectedIndex(i)) {
|
---|
718 | continue;
|
---|
719 | }
|
---|
720 | if (relations.get(i).isNew()) {
|
---|
721 | continue;
|
---|
722 | }
|
---|
723 | ret.add(relations.get(i));
|
---|
724 | }
|
---|
725 | return ret;
|
---|
726 | }
|
---|
727 |
|
---|
728 | /**
|
---|
729 | * Replies the list of selected relations. Empty list,
|
---|
730 | * if there are no selected relations.
|
---|
731 | *
|
---|
732 | * @return the list of selected, non-new relations.
|
---|
733 | */
|
---|
734 | public List<Relation> getSelectedRelations() {
|
---|
735 | ArrayList<Relation> ret = new ArrayList<Relation>();
|
---|
736 | for (int i=0; i<getSize();i++) {
|
---|
737 | if (!selectionModel.isSelectedIndex(i)) {
|
---|
738 | continue;
|
---|
739 | }
|
---|
740 | ret.add(relations.get(i));
|
---|
741 | }
|
---|
742 | return ret;
|
---|
743 | }
|
---|
744 |
|
---|
745 | /**
|
---|
746 | * Sets the selected relations.
|
---|
747 | *
|
---|
748 | * @return sel the list of selected relations
|
---|
749 | */
|
---|
750 | public void setSelectedRelations(List<Relation> sel) {
|
---|
751 | selectionModel.clearSelection();
|
---|
752 | if (sel == null || sel.isEmpty())
|
---|
753 | return;
|
---|
754 | for (Relation r: sel) {
|
---|
755 | int i = relations.indexOf(r);
|
---|
756 | if (i<0) {
|
---|
757 | continue;
|
---|
758 | }
|
---|
759 | selectionModel.addSelectionInterval(i,i);
|
---|
760 | }
|
---|
761 | }
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * Returns the index of the relation
|
---|
765 | *
|
---|
766 | * @return index of relation (null if it cannot be found)
|
---|
767 | */
|
---|
768 | public Integer getRelationIndex(Relation rel) {
|
---|
769 | int i = relations.indexOf(rel);
|
---|
770 | if (i<0)
|
---|
771 | return null;
|
---|
772 | return i;
|
---|
773 | }
|
---|
774 |
|
---|
775 | public void updateTitle() {
|
---|
776 | if (getSize() > 0) {
|
---|
777 | RelationListDialog.this.setTitle(tr("Relations: {0}", getSize()));
|
---|
778 | } else {
|
---|
779 | RelationListDialog.this.setTitle(tr("Relations"));
|
---|
780 | }
|
---|
781 | }
|
---|
782 | }
|
---|
783 |
|
---|
784 | class RelationDialogPopupMenu extends JPopupMenu {
|
---|
785 | protected void build() {
|
---|
786 | // -- download members action
|
---|
787 | //
|
---|
788 | DownloadMembersAction downloadMembersAction = new DownloadMembersAction();
|
---|
789 | displaylist.addListSelectionListener(downloadMembersAction);
|
---|
790 | add(downloadMembersAction);
|
---|
791 |
|
---|
792 | // -- download incomplete members action
|
---|
793 | //
|
---|
794 | DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembers = new DownloadSelectedIncompleteMembersAction();
|
---|
795 | displaylist.addListSelectionListener(downloadSelectedIncompleteMembers);
|
---|
796 | add(downloadSelectedIncompleteMembers);
|
---|
797 |
|
---|
798 | addSeparator();
|
---|
799 |
|
---|
800 | // -- select members action
|
---|
801 | //
|
---|
802 | SelectMembersAction selectMembersAction = new SelectMembersAction(false);
|
---|
803 | displaylist.addListSelectionListener(selectMembersAction);
|
---|
804 | add(selectMembersAction);
|
---|
805 |
|
---|
806 | selectMembersAction = new SelectMembersAction(true);
|
---|
807 | displaylist.addListSelectionListener(selectMembersAction);
|
---|
808 | add(selectMembersAction);
|
---|
809 |
|
---|
810 | // -- select action
|
---|
811 | //
|
---|
812 | SelectAction selectAction = new SelectAction(false);
|
---|
813 | displaylist.addListSelectionListener(selectAction);
|
---|
814 | add(selectAction);
|
---|
815 |
|
---|
816 | selectAction = new SelectAction(true);
|
---|
817 | displaylist.addListSelectionListener(selectAction);
|
---|
818 | add(selectAction);
|
---|
819 | }
|
---|
820 |
|
---|
821 | public RelationDialogPopupMenu() {
|
---|
822 | build();
|
---|
823 | }
|
---|
824 | }
|
---|
825 |
|
---|
826 | /* ---------------------------------------------------------------------------------- */
|
---|
827 | /* DataSetListener */
|
---|
828 | /* ---------------------------------------------------------------------------------- */
|
---|
829 |
|
---|
830 | public void nodeMoved(NodeMovedEvent event) {/* irrelevant in this context */}
|
---|
831 |
|
---|
832 | public void wayNodesChanged(WayNodesChangedEvent event) {/* irrelevant in this context */}
|
---|
833 |
|
---|
834 | public void primtivesAdded(final PrimitivesAddedEvent event) {
|
---|
835 | model.addRelations(event.getPrimitives());
|
---|
836 | model.updateTitle();
|
---|
837 | }
|
---|
838 |
|
---|
839 | public void primtivesRemoved(final PrimitivesRemovedEvent event) {
|
---|
840 | model.removeRelations(event.getPrimitives());
|
---|
841 | model.updateTitle();
|
---|
842 | }
|
---|
843 |
|
---|
844 | public void relationMembersChanged(final RelationMembersChangedEvent event) {
|
---|
845 | List<Relation> sel = model.getSelectedRelations();
|
---|
846 | model.sort();
|
---|
847 | model.setSelectedRelations(sel);
|
---|
848 | displaylist.repaint();
|
---|
849 | }
|
---|
850 |
|
---|
851 | public void tagsChanged(TagsChangedEvent event) {
|
---|
852 | OsmPrimitive prim = event.getPrimitive();
|
---|
853 | if (prim == null || ! (prim instanceof Relation))
|
---|
854 | return;
|
---|
855 | // trigger a sort of the relation list because the display name may
|
---|
856 | // have changed
|
---|
857 | //
|
---|
858 | List<Relation> sel = model.getSelectedRelations();
|
---|
859 | model.sort();
|
---|
860 | model.setSelectedRelations(sel);
|
---|
861 | displaylist.repaint();
|
---|
862 | }
|
---|
863 |
|
---|
864 | public void dataChanged(DataChangedEvent event) {
|
---|
865 | initFromLayer(Main.main.getEditLayer());
|
---|
866 | }
|
---|
867 |
|
---|
868 | public void otherDatasetChange(AbstractDatasetChangedEvent event) {/* ignore */}
|
---|
869 | }
|
---|