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