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.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.Arrays;
|
---|
14 | import java.util.Collection;
|
---|
15 | import java.util.Collections;
|
---|
16 | import java.util.HashSet;
|
---|
17 | import java.util.Iterator;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.Set;
|
---|
20 |
|
---|
21 | import javax.swing.AbstractAction;
|
---|
22 | import javax.swing.AbstractListModel;
|
---|
23 | import javax.swing.Action;
|
---|
24 | import javax.swing.DefaultListSelectionModel;
|
---|
25 | import javax.swing.JList;
|
---|
26 | import javax.swing.JMenuItem;
|
---|
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 | import javax.swing.event.PopupMenuListener;
|
---|
33 |
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
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.DatasetEventManager.FireMode;
|
---|
43 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
---|
44 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
---|
45 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
---|
46 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
---|
47 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
---|
48 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
---|
49 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
50 | import org.openstreetmap.josm.gui.MapView;
|
---|
51 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
52 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
53 | import org.openstreetmap.josm.gui.SideButton;
|
---|
54 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
|
---|
55 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
|
---|
56 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
57 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
58 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
59 | import org.openstreetmap.josm.gui.widgets.ListPopupMenu;
|
---|
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 | /** 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,
|
---|
90 | Shortcut.GROUP_LAYER, Shortcut.SHIFT_DEFAULT), 150);
|
---|
91 |
|
---|
92 | // create the list of relations
|
---|
93 | //
|
---|
94 | DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
|
---|
95 | model = new RelationListModel(selectionModel);
|
---|
96 | displaylist = new JList(model);
|
---|
97 | displaylist.setSelectionModel(selectionModel);
|
---|
98 | displaylist.setCellRenderer(new OsmPrimitivRenderer() {
|
---|
99 | /**
|
---|
100 | * Don't show the default tooltip in the relation list.
|
---|
101 | */
|
---|
102 | @Override
|
---|
103 | protected String getComponentToolTipText(OsmPrimitive value) {
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 | });
|
---|
107 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
108 | displaylist.addMouseListener(new MouseEventHandler());
|
---|
109 |
|
---|
110 | // the new action
|
---|
111 | //
|
---|
112 | newAction = new NewAction();
|
---|
113 |
|
---|
114 | // the edit action
|
---|
115 | //
|
---|
116 | editAction = new EditAction();
|
---|
117 | displaylist.addListSelectionListener(editAction);
|
---|
118 |
|
---|
119 | // the duplicate action
|
---|
120 | //
|
---|
121 | DuplicateAction duplicateAction = new DuplicateAction();
|
---|
122 | displaylist.addListSelectionListener(duplicateAction);
|
---|
123 |
|
---|
124 | // the delete action
|
---|
125 | //
|
---|
126 | deleteAction = new DeleteAction();
|
---|
127 | displaylist.addListSelectionListener(deleteAction);
|
---|
128 |
|
---|
129 | // the select action
|
---|
130 | //
|
---|
131 | SelectAction selectAction = new SelectAction(false);
|
---|
132 | displaylist.addListSelectionListener(selectAction);
|
---|
133 |
|
---|
134 | createLayout(displaylist, true, Arrays.asList(new SideButton[] {
|
---|
135 | new SideButton(newAction, false),
|
---|
136 | new SideButton(editAction, false),
|
---|
137 | new SideButton(duplicateAction, false),
|
---|
138 | new SideButton(deleteAction, false),
|
---|
139 | new SideButton(selectAction, false)
|
---|
140 | }));
|
---|
141 |
|
---|
142 | // activate DEL in the list of relations
|
---|
143 | displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0), "deleteRelation");
|
---|
144 | displaylist.getActionMap().put("deleteRelation", deleteAction);
|
---|
145 |
|
---|
146 | popupMenu = new RelationDialogPopupMenu(displaylist);
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override public void showNotify() {
|
---|
150 | MapView.addLayerChangeListener(newAction);
|
---|
151 | newAction.updateEnabledState();
|
---|
152 | DatasetEventManager.getInstance().addDatasetListener(this, FireMode.IN_EDT);
|
---|
153 | dataChanged(null);
|
---|
154 | }
|
---|
155 |
|
---|
156 | @Override public void hideNotify() {
|
---|
157 | MapView.removeLayerChangeListener(newAction);
|
---|
158 | DatasetEventManager.getInstance().removeDatasetListener(this);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Initializes the relation list dialog from a layer. If <code>layer</code> is null
|
---|
163 | * or if it isn't an {@see OsmDataLayer} the dialog is reset to an empty dialog.
|
---|
164 | * Otherwise it is initialized with the list of non-deleted and visible relations
|
---|
165 | * in the layer's dataset.
|
---|
166 | *
|
---|
167 | * @param layer the layer. May be null.
|
---|
168 | */
|
---|
169 | protected void initFromLayer(Layer layer) {
|
---|
170 | if (layer == null || ! (layer instanceof OsmDataLayer)) {
|
---|
171 | model.setRelations(null);
|
---|
172 | return;
|
---|
173 | }
|
---|
174 | OsmDataLayer l = (OsmDataLayer)layer;
|
---|
175 | model.setRelations(l.data.getRelations());
|
---|
176 | model.updateTitle();
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Adds a selection listener to the relation list.
|
---|
181 | *
|
---|
182 | * @param listener the listener to add
|
---|
183 | */
|
---|
184 | public void addListSelectionListener(ListSelectionListener listener) {
|
---|
185 | displaylist.addListSelectionListener(listener);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Removes a selection listener from the relation list.
|
---|
190 | *
|
---|
191 | * @param listener the listener to remove
|
---|
192 | */
|
---|
193 | public void removeListSelectionListener(ListSelectionListener listener) {
|
---|
194 | displaylist.removeListSelectionListener(listener);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /**
|
---|
198 | * @return The selected relation in the list
|
---|
199 | */
|
---|
200 | private Relation getSelected() {
|
---|
201 | if(model.getSize() == 1) {
|
---|
202 | displaylist.setSelectedIndex(0);
|
---|
203 | }
|
---|
204 | return (Relation) displaylist.getSelectedValue();
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Selects the relation <code>relation</code> in the list of relations.
|
---|
209 | *
|
---|
210 | * @param relation the relation
|
---|
211 | */
|
---|
212 | public void selectRelation(Relation relation) {
|
---|
213 | selectRelations(Collections.singleton(relation));
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * Selects the relations in the list of relations.
|
---|
218 | * @param relations the relations to be selected
|
---|
219 | */
|
---|
220 | public void selectRelations(Collection<Relation> relations) {
|
---|
221 | if (relations == null || relations.isEmpty()) {
|
---|
222 | model.setSelectedRelations(null);
|
---|
223 | } else {
|
---|
224 | model.setSelectedRelations(relations);
|
---|
225 | Integer i = model.getRelationIndex(relations.iterator().next());
|
---|
226 | 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)
|
---|
227 | displaylist.scrollRectToVisible(displaylist.getCellBounds(i, i));
|
---|
228 | }
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | class MouseEventHandler extends MouseAdapter {
|
---|
233 | protected void setCurrentRelationAsSelection() {
|
---|
234 | Main.main.getCurrentDataSet().setSelected((Relation)displaylist.getSelectedValue());
|
---|
235 | }
|
---|
236 |
|
---|
237 | protected void editCurrentRelation() {
|
---|
238 | new EditAction().launchEditor(getSelected());
|
---|
239 | }
|
---|
240 |
|
---|
241 | @Override public void mouseClicked(MouseEvent e) {
|
---|
242 | if (Main.main.getEditLayer() == null) return;
|
---|
243 | if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
|
---|
244 | if (e.isControlDown()) {
|
---|
245 | editCurrentRelation();
|
---|
246 | } else {
|
---|
247 | setCurrentRelationAsSelection();
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 | private void openPopup(MouseEvent e) {
|
---|
252 | Point p = e.getPoint();
|
---|
253 | int index = displaylist.locationToIndex(p);
|
---|
254 | if (index < 0) return;
|
---|
255 | if (!displaylist.getCellBounds(index, index).contains(e.getPoint()))
|
---|
256 | return;
|
---|
257 | if (! displaylist.isSelectedIndex(index)) {
|
---|
258 | displaylist.setSelectedIndex(index);
|
---|
259 | }
|
---|
260 | popupMenu.show(displaylist, p.x, p.y-3);
|
---|
261 | }
|
---|
262 | @Override public void mousePressed(MouseEvent e) {
|
---|
263 | if (Main.main.getEditLayer() == null) return;
|
---|
264 | if (e.isPopupTrigger()) {
|
---|
265 | openPopup(e);
|
---|
266 | }
|
---|
267 | }
|
---|
268 | @Override public void mouseReleased(MouseEvent e) {
|
---|
269 | if (Main.main.getEditLayer() == null) return;
|
---|
270 | if (e.isPopupTrigger()) {
|
---|
271 | openPopup(e);
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * The edit action
|
---|
278 | *
|
---|
279 | */
|
---|
280 | class EditAction extends AbstractAction implements ListSelectionListener{
|
---|
281 | public EditAction() {
|
---|
282 | putValue(SHORT_DESCRIPTION,tr( "Open an editor for the selected relation"));
|
---|
283 | //putValue(NAME, tr("Edit"));
|
---|
284 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
|
---|
285 | setEnabled(false);
|
---|
286 | }
|
---|
287 | protected Collection<RelationMember> getMembersForCurrentSelection(Relation r) {
|
---|
288 | Collection<RelationMember> members = new HashSet<RelationMember>();
|
---|
289 | Collection<OsmPrimitive> selection = Main.map.mapView.getEditLayer().data.getSelected();
|
---|
290 | for (RelationMember member: r.getMembers()) {
|
---|
291 | if (selection.contains(member.getMember())) {
|
---|
292 | members.add(member);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | return members;
|
---|
296 | }
|
---|
297 |
|
---|
298 | public void launchEditor(Relation toEdit) {
|
---|
299 | if (toEdit == null)
|
---|
300 | return;
|
---|
301 | RelationEditor.getEditor(Main.map.mapView.getEditLayer(),toEdit, getMembersForCurrentSelection(toEdit)).setVisible(true);
|
---|
302 | }
|
---|
303 |
|
---|
304 | public void actionPerformed(ActionEvent e) {
|
---|
305 | if (!isEnabled())
|
---|
306 | return;
|
---|
307 | launchEditor(getSelected());
|
---|
308 | }
|
---|
309 |
|
---|
310 | public void valueChanged(ListSelectionEvent e) {
|
---|
311 | setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length == 1);
|
---|
312 | }
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * The delete action
|
---|
317 | *
|
---|
318 | */
|
---|
319 | class DeleteAction extends AbstractAction implements ListSelectionListener {
|
---|
320 | class AbortException extends Exception {}
|
---|
321 |
|
---|
322 | public DeleteAction() {
|
---|
323 | putValue(SHORT_DESCRIPTION,tr("Delete the selected relation"));
|
---|
324 | //putValue(NAME, tr("Delete"));
|
---|
325 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
|
---|
326 | setEnabled(false);
|
---|
327 | }
|
---|
328 |
|
---|
329 | protected void deleteRelation(Relation toDelete) {
|
---|
330 | if (toDelete == null)
|
---|
331 | return;
|
---|
332 | org.openstreetmap.josm.actions.mapmode.DeleteAction.deleteRelation(
|
---|
333 | Main.main.getEditLayer(),
|
---|
334 | toDelete
|
---|
335 | );
|
---|
336 | }
|
---|
337 |
|
---|
338 | public void actionPerformed(ActionEvent e) {
|
---|
339 | if (!isEnabled()) return;
|
---|
340 | for (int i: displaylist.getSelectedIndices()) {
|
---|
341 | deleteRelation(model.getRelation(i));
|
---|
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 | boolean add;
|
---|
431 | public SelectAction(boolean add) {
|
---|
432 | putValue(SHORT_DESCRIPTION, add ? tr("Add the selected relations to the current selection")
|
---|
433 | : tr("Set the current selection to the list of selected relations"));
|
---|
434 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "select"));
|
---|
435 | putValue(NAME, add ? tr("Select relation (add)") : tr("Select relation"));
|
---|
436 | this.add = add;
|
---|
437 | updateEnabledState();
|
---|
438 | }
|
---|
439 |
|
---|
440 | public void actionPerformed(ActionEvent e) {
|
---|
441 | if (!isEnabled()) return;
|
---|
442 | int [] idx = displaylist.getSelectedIndices();
|
---|
443 | if (idx == null || idx.length == 0) return;
|
---|
444 | ArrayList<OsmPrimitive> selection = new ArrayList<OsmPrimitive>(idx.length);
|
---|
445 | for (int i: idx) {
|
---|
446 | selection.add(model.getRelation(i));
|
---|
447 | }
|
---|
448 | if(add) {
|
---|
449 | Main.map.mapView.getEditLayer().data.addSelected(selection);
|
---|
450 | } else {
|
---|
451 | Main.map.mapView.getEditLayer().data.setSelected(selection);
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | protected void updateEnabledState() {
|
---|
456 | setEnabled(displaylist.getSelectedIndices() != null && displaylist.getSelectedIndices().length > 0);
|
---|
457 | }
|
---|
458 |
|
---|
459 | public void valueChanged(ListSelectionEvent e) {
|
---|
460 | updateEnabledState();
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * Sets the current selection to the list of relations selected in this dialog
|
---|
466 | *
|
---|
467 | */
|
---|
468 | class SelectMembersAction extends AbstractAction implements ListSelectionListener{
|
---|
469 | boolean add;
|
---|
470 | public SelectMembersAction(boolean add) {
|
---|
471 | putValue(SHORT_DESCRIPTION,add ? tr("Add the members of all selected relations to current selection")
|
---|
472 | : tr("Select the members of all selected relations"));
|
---|
473 | putValue(SMALL_ICON, ImageProvider.get("selectall"));
|
---|
474 | putValue(NAME, add ? tr("Select members (add)") : tr("Select members"));
|
---|
475 | this.add = add;
|
---|
476 | updateEnabledState();
|
---|
477 | }
|
---|
478 |
|
---|
479 | public void actionPerformed(ActionEvent e) {
|
---|
480 | if (!isEnabled()) return;
|
---|
481 | List<Relation> relations = model.getSelectedRelations();
|
---|
482 | HashSet<OsmPrimitive> members = new HashSet<OsmPrimitive>();
|
---|
483 | for(Relation r: relations) {
|
---|
484 | members.addAll(r.getMemberPrimitives());
|
---|
485 | }
|
---|
486 | if(add) {
|
---|
487 | Main.map.mapView.getEditLayer().data.addSelected(members);
|
---|
488 | } else {
|
---|
489 | Main.map.mapView.getEditLayer().data.setSelected(members);
|
---|
490 | }
|
---|
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(Collection<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 ListPopupMenu {
|
---|
785 |
|
---|
786 | public RelationDialogPopupMenu(JList list) {
|
---|
787 | super(list);
|
---|
788 |
|
---|
789 | // -- download members action
|
---|
790 | //
|
---|
791 | add(new DownloadMembersAction());
|
---|
792 |
|
---|
793 | // -- download incomplete members action
|
---|
794 | //
|
---|
795 | add(new DownloadSelectedIncompleteMembersAction());
|
---|
796 |
|
---|
797 | addSeparator();
|
---|
798 |
|
---|
799 | // -- select members action
|
---|
800 | //
|
---|
801 | add(new SelectMembersAction(false));
|
---|
802 | add(new SelectMembersAction(true));
|
---|
803 |
|
---|
804 | // -- select action
|
---|
805 | //
|
---|
806 | add(new SelectAction(false));
|
---|
807 | add(new SelectAction(true));
|
---|
808 | }
|
---|
809 | }
|
---|
810 |
|
---|
811 | public void addPopupMenuSeparator() {
|
---|
812 | popupMenu.addSeparator();
|
---|
813 | }
|
---|
814 |
|
---|
815 | public JMenuItem addPopupMenuAction(Action a) {
|
---|
816 | return popupMenu.add(a);
|
---|
817 | }
|
---|
818 |
|
---|
819 | public void addPopupMenuListener(PopupMenuListener l) {
|
---|
820 | popupMenu.addPopupMenuListener(l);
|
---|
821 | }
|
---|
822 |
|
---|
823 | public void removePopupMenuListener(PopupMenuListener l) {
|
---|
824 | popupMenu.addPopupMenuListener(l);
|
---|
825 | }
|
---|
826 |
|
---|
827 | public Collection<Relation> getSelectedRelations() {
|
---|
828 | return model.getSelectedRelations();
|
---|
829 | }
|
---|
830 |
|
---|
831 | /* ---------------------------------------------------------------------------------- */
|
---|
832 | /* DataSetListener */
|
---|
833 | /* ---------------------------------------------------------------------------------- */
|
---|
834 |
|
---|
835 | public void nodeMoved(NodeMovedEvent event) {/* irrelevant in this context */}
|
---|
836 |
|
---|
837 | public void wayNodesChanged(WayNodesChangedEvent event) {/* irrelevant in this context */}
|
---|
838 |
|
---|
839 | public void primitivesAdded(final PrimitivesAddedEvent event) {
|
---|
840 | model.addRelations(event.getPrimitives());
|
---|
841 | model.updateTitle();
|
---|
842 | }
|
---|
843 |
|
---|
844 | public void primitivesRemoved(final PrimitivesRemovedEvent event) {
|
---|
845 | model.removeRelations(event.getPrimitives());
|
---|
846 | model.updateTitle();
|
---|
847 | }
|
---|
848 |
|
---|
849 | public void relationMembersChanged(final RelationMembersChangedEvent event) {
|
---|
850 | List<Relation> sel = model.getSelectedRelations();
|
---|
851 | model.sort();
|
---|
852 | model.setSelectedRelations(sel);
|
---|
853 | displaylist.repaint();
|
---|
854 | }
|
---|
855 |
|
---|
856 | public void tagsChanged(TagsChangedEvent event) {
|
---|
857 | OsmPrimitive prim = event.getPrimitive();
|
---|
858 | if (prim == null || ! (prim instanceof Relation))
|
---|
859 | return;
|
---|
860 | // trigger a sort of the relation list because the display name may
|
---|
861 | // have changed
|
---|
862 | //
|
---|
863 | List<Relation> sel = model.getSelectedRelations();
|
---|
864 | model.sort();
|
---|
865 | model.setSelectedRelations(sel);
|
---|
866 | displaylist.repaint();
|
---|
867 | }
|
---|
868 |
|
---|
869 | public void dataChanged(DataChangedEvent event) {
|
---|
870 | initFromLayer(Main.main.getEditLayer());
|
---|
871 | }
|
---|
872 |
|
---|
873 | public void otherDatasetChange(AbstractDatasetChangedEvent event) {/* ignore */}
|
---|
874 | }
|
---|