source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/RelationListDialog.java@ 10619

Last change on this file since 10619 was 10619, checked in by Don-vip, 8 years ago

see #11390 - Java 8: use List.sort(Comparator) instead of Collections.sort(list, Comparator)

  • Property svn:eol-style set to native
File size: 25.6 KB
RevLine 
[3719]1// License: GPL. For details, see LICENSE file.
[626]2package org.openstreetmap.josm.gui.dialogs;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
[5616]6import java.awt.BorderLayout;
[6038]7import java.awt.Component;
[626]8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.awt.event.MouseEvent;
[2126]11import java.util.ArrayList;
[4353]12import java.util.Arrays;
[1886]13import java.util.Collection;
[2126]14import java.util.Collections;
[1886]15import java.util.HashSet;
[2317]16import java.util.List;
[2487]17import java.util.Set;
[626]18
[1778]19import javax.swing.AbstractAction;
[2126]20import javax.swing.AbstractListModel;
[2317]21import javax.swing.DefaultListSelectionModel;
[6038]22import javax.swing.FocusManager;
[5200]23import javax.swing.JComponent;
[626]24import javax.swing.JList;
[9365]25import javax.swing.JMenuItem;
[5616]26import javax.swing.JPanel;
[5793]27import javax.swing.JPopupMenu;
[5616]28import javax.swing.JScrollPane;
[5200]29import javax.swing.KeyStroke;
[626]30import javax.swing.ListSelectionModel;
[1778]31
[626]32import org.openstreetmap.josm.Main;
[9365]33import org.openstreetmap.josm.actions.ExpertToggleAction;
[5799]34import org.openstreetmap.josm.actions.relation.AddSelectionToRelations;
35import org.openstreetmap.josm.actions.relation.DeleteRelationsAction;
[5793]36import org.openstreetmap.josm.actions.relation.DownloadMembersAction;
37import org.openstreetmap.josm.actions.relation.DownloadSelectedIncompleteMembersAction;
[5799]38import org.openstreetmap.josm.actions.relation.DuplicateRelationAction;
[5793]39import org.openstreetmap.josm.actions.relation.EditRelationAction;
[9668]40import org.openstreetmap.josm.actions.relation.RecentRelationsAction;
[5793]41import org.openstreetmap.josm.actions.relation.SelectMembersAction;
42import org.openstreetmap.josm.actions.relation.SelectRelationAction;
[5616]43import org.openstreetmap.josm.actions.search.SearchCompiler;
[5082]44import org.openstreetmap.josm.data.osm.DataSet;
[755]45import org.openstreetmap.josm.data.osm.OsmPrimitive;
[626]46import org.openstreetmap.josm.data.osm.Relation;
[2655]47import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
[2622]48import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
49import org.openstreetmap.josm.data.osm.event.DataSetListener;
50import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
[4070]51import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
[2622]52import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
53import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
54import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
55import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
56import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
57import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
[2126]58import org.openstreetmap.josm.gui.DefaultNameFormatter;
[2621]59import org.openstreetmap.josm.gui.MapView;
[9264]60import org.openstreetmap.josm.gui.NavigatableComponent;
[626]61import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
[5821]62import org.openstreetmap.josm.gui.PopupMenuHandler;
[2710]63import org.openstreetmap.josm.gui.SideButton;
[1599]64import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
[626]65import org.openstreetmap.josm.gui.layer.Layer;
[10386]66import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
67import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
68import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
69import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
70import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
71import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
[626]72import org.openstreetmap.josm.gui.layer.OsmDataLayer;
[7547]73import org.openstreetmap.josm.gui.util.GuiHelper;
[6025]74import org.openstreetmap.josm.gui.util.HighlightHelper;
[8979]75import org.openstreetmap.josm.gui.widgets.CompileSearchTextDecorator;
[5696]76import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField;
[5958]77import org.openstreetmap.josm.gui.widgets.JosmTextField;
78import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
[1778]79import org.openstreetmap.josm.tools.ImageProvider;
[5200]80import org.openstreetmap.josm.tools.InputMapUtils;
[1084]81import org.openstreetmap.josm.tools.Shortcut;
[5616]82import org.openstreetmap.josm.tools.Utils;
[626]83
84/**
[10386]85 * A dialog showing all known relations, with buttons to add, edit, and delete them.
[1023]86 *
[1554]87 * We don't have such dialogs for nodes, segments, and ways, because those
[626]88 * objects are visible on the map and can be selected there. Relations are not.
89 */
[9365]90public class RelationListDialog extends ToggleDialog
91 implements DataSetListener, NavigatableComponent.ZoomChangeListener, ExpertToggleAction.ExpertModeChangeListener {
[2126]92 /** The display list. */
[7001]93 private final JList<Relation> displaylist;
[2126]94 /** the list model used */
[5725]95 private final RelationListModel model;
[626]96
[5725]97 private final NewAction newAction;
[6070]98
[5821]99 /** the popup menu and its handler */
100 private final JPopupMenu popupMenu = new JPopupMenu();
[8308]101 private final transient PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
[1677]102
[5886]103 private final JosmTextField filter;
[6070]104
[5793]105 // Actions
106 /** the edit action */
[5799]107 private final EditRelationAction editAction = new EditRelationAction();
108 /** the delete action */
109 private final DeleteRelationsAction deleteRelationsAction = new DeleteRelationsAction();
110 /** the duplicate action */
111 private final DuplicateRelationAction duplicateAction = new DuplicateRelationAction();
[5793]112 private final DownloadMembersAction downloadMembersAction = new DownloadMembersAction();
[8540]113 private final DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction =
114 new DownloadSelectedIncompleteMembersAction();
[5821]115 private final SelectMembersAction selectMembersAction = new SelectMembersAction(false);
[5793]116 private final SelectMembersAction addMembersToSelectionAction = new SelectMembersAction(true);
117 private final SelectRelationAction selectRelationAction = new SelectRelationAction(false);
118 private final SelectRelationAction addRelationToSelectionAction = new SelectRelationAction(true);
[5821]119 /** add all selected primitives to the given relations */
[5799]120 private final AddSelectionToRelations addSelectionToRelations = new AddSelectionToRelations();
[9365]121 private transient JMenuItem addSelectionToRelationMenuItem;
[6070]122
[8308]123 private final transient HighlightHelper highlightHelper = new HighlightHelper();
[9078]124 private final boolean highlightEnabled = Main.pref.getBoolean("draw.target-highlight", true);
[10306]125 private final transient RecentRelationsAction recentRelationsAction;
[7539]126
[1778]127 /**
[5799]128 * Constructs <code>RelationListDialog</code>
[1778]129 */
[1169]130 public RelationListDialog() {
131 super(tr("Relations"), "relationlist", tr("Open a list of all relations."),
[4957]132 Shortcut.registerShortcut("subwindow:relations", tr("Toggle: {0}", tr("Relations")),
[6726]133 KeyEvent.VK_R, Shortcut.ALT_SHIFT), 150, true);
[1778]134
135 // create the list of relations
136 //
[2317]137 DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
138 model = new RelationListModel(selectionModel);
[7005]139 displaylist = new JList<>(model);
[2317]140 displaylist.setSelectionModel(selectionModel);
[2885]141 displaylist.setCellRenderer(new OsmPrimitivRenderer() {
142 /**
143 * Don't show the default tooltip in the relation list.
144 */
145 @Override
146 protected String getComponentToolTipText(OsmPrimitive value) {
147 return null;
148 }
149 });
[2194]150 displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
[2317]151 displaylist.addMouseListener(new MouseEventHandler());
[626]152
[1866]153 // the new action
154 //
[2485]155 newAction = new NewAction();
[1866]156
[5793]157 filter = setupFilter();
[5616]158
[10611]159 displaylist.addListSelectionListener(e -> updateActionsRelationLists());
[5821]160
161 // Setup popup menu handler
162 setupPopupMenuHandler();
[6070]163
[5616]164 JPanel pane = new JPanel(new BorderLayout());
165 pane.add(filter, BorderLayout.NORTH);
166 pane.add(new JScrollPane(displaylist), BorderLayout.CENTER);
[9668]167
168 SideButton editButton = new SideButton(editAction, false);
169 recentRelationsAction = new RecentRelationsAction(editButton);
170
[5616]171 createLayout(pane, false, Arrays.asList(new SideButton[]{
[5028]172 new SideButton(newAction, false),
[9668]173 editButton,
[5028]174 new SideButton(duplicateAction, false),
[5799]175 new SideButton(deleteRelationsAction, false),
[5793]176 new SideButton(selectRelationAction, false)
[4353]177 }));
[2487]178
[5200]179 InputMapUtils.unassignCtrlShiftUpDown(displaylist, JComponent.WHEN_FOCUSED);
[6070]180
[7547]181 // Select relation on Enter
[5793]182 InputMapUtils.addEnterAction(displaylist, selectRelationAction);
[5200]183
184 // Edit relation on Ctrl-Enter
185 displaylist.getActionMap().put("edit", editAction);
186 displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK), "edit");
[6070]187
[7547]188 // Do not hide copy action because of default JList override (fix #9815)
189 displaylist.getActionMap().put("copy", Main.main.menu.copy);
190 displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, GuiHelper.getMenuShortcutKeyMaskEx()), "copy");
191
[5799]192 updateActionsRelationLists();
[2485]193 }
[6070]194
[9668]195 public void enableRecentRelations() {
196 recentRelationsAction.enableArrow();
197 }
198
[5799]199 // inform all actions about list of relations they need
200 private void updateActionsRelationLists() {
[6025]201 List<Relation> sel = model.getSelectedRelations();
202 popupMenuHandler.setPrimitives(sel);
[6070]203
[6038]204 Component focused = FocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
[6070]205
[6025]206 //update highlights
[8510]207 if (highlightEnabled && focused == displaylist && Main.isDisplayingMapView()) {
[6038]208 if (highlightHelper.highlightOnly(sel)) {
209 Main.map.mapView.repaint();
210 }
[6025]211 }
[5799]212 }
[6070]213
[7539]214 @Override
215 public void showNotify() {
[10386]216 Main.getLayerManager().addLayerChangeListener(newAction);
217 Main.getLayerManager().addActiveLayerChangeListener(newAction);
[9264]218 MapView.addZoomChangeListener(this);
[2691]219 newAction.updateEnabledState();
[2741]220 DatasetEventManager.getInstance().addDatasetListener(this, FireMode.IN_EDT);
[5799]221 DataSet.addSelectionListener(addSelectionToRelations);
[2623]222 dataChanged(null);
[9365]223 ExpertToggleAction.addExpertModeChangeListener(this);
224 expertChanged(ExpertToggleAction.isExpert());
[1169]225 }
[626]226
[7539]227 @Override
228 public void hideNotify() {
[10386]229 Main.getLayerManager().removeActiveLayerChangeListener(newAction);
230 Main.getLayerManager().removeLayerChangeListener(newAction);
[9264]231 MapView.removeZoomChangeListener(this);
[2622]232 DatasetEventManager.getInstance().removeDatasetListener(this);
[5799]233 DataSet.removeSelectionListener(addSelectionToRelations);
[9365]234 ExpertToggleAction.removeExpertModeChangeListener(this);
[1169]235 }
[6070]236
[5725]237 private void resetFilter() {
238 filter.setText(null);
239 }
[1023]240
[2126]241 /**
[2487]242 * Initializes the relation list dialog from a layer. If <code>layer</code> is null
[5266]243 * or if it isn't an {@link OsmDataLayer} the dialog is reset to an empty dialog.
[2487]244 * Otherwise it is initialized with the list of non-deleted and visible relations
245 * in the layer's dataset.
[2512]246 *
[2487]247 * @param layer the layer. May be null.
[2126]248 */
[2487]249 protected void initFromLayer(Layer layer) {
[6105]250 if (!(layer instanceof OsmDataLayer)) {
[2126]251 model.setRelations(null);
[1897]252 return;
253 }
[8510]254 OsmDataLayer l = (OsmDataLayer) layer;
[2487]255 model.setRelations(l.data.getRelations());
[3020]256 model.updateTitle();
[5799]257 updateActionsRelationLists();
[1169]258 }
[1023]259
[1169]260 /**
[1419]261 * @return The selected relation in the list
262 */
263 private Relation getSelected() {
[7001]264 if (model.getSize() == 1) {
[1419]265 displaylist.setSelectedIndex(0);
[1763]266 }
[7001]267 return displaylist.getSelectedValue();
[1419]268 }
[1763]269
270 /**
271 * Selects the relation <code>relation</code> in the list of relations.
[1897]272 *
[1763]273 * @param relation the relation
274 */
275 public void selectRelation(Relation relation) {
[4795]276 selectRelations(Collections.singleton(relation));
277 }
278
279 /**
280 * Selects the relations in the list of relations.
281 * @param relations the relations to be selected
282 */
283 public void selectRelations(Collection<Relation> relations) {
284 if (relations == null || relations.isEmpty()) {
[2487]285 model.setSelectedRelations(null);
[2126]286 } else {
[4795]287 model.setSelectedRelations(relations);
[5725]288 Integer i = model.getVisibleRelationIndex(relations.iterator().next());
[8509]289 if (i != null) {
290 // Not all relations have to be in the list
291 // (for example when the relation list is hidden, it's not updated with new relations)
[2914]292 displaylist.scrollRectToVisible(displaylist.getCellBounds(i, i));
293 }
[1780]294 }
[1763]295 }
[1778]296
[10378]297 private JosmTextField setupFilter() {
[5886]298 final JosmTextField f = new DisableShortcutsOnFocusGainedTextField();
[5793]299 f.setToolTipText(tr("Relation list filter"));
[8979]300 final CompileSearchTextDecorator decorator = CompileSearchTextDecorator.decorate(f);
[10611]301 f.addPropertyChangeListener("filter", evt -> model.setFilter(decorator.getMatch()));
[5793]302 return f;
303 }
304
[5958]305 class MouseEventHandler extends PopupMenuLauncher {
[6070]306
[8836]307 MouseEventHandler() {
[5958]308 super(popupMenu);
309 }
[6025]310
311 @Override
312 public void mouseExited(MouseEvent me) {
[6058]313 if (highlightEnabled) highlightHelper.clear();
[6025]314 }
[6070]315
[1916]316 protected void setCurrentRelationAsSelection() {
[10446]317 Main.getLayerManager().getEditDataSet().setSelected(displaylist.getSelectedValue());
[1916]318 }
319
320 protected void editCurrentRelation() {
[5793]321 EditRelationAction.launchEditor(getSelected());
[1916]322 }
[6070]323
[7539]324 @Override
325 public void mouseClicked(MouseEvent e) {
[10453]326 if (Main.getLayerManager().getEditLayer() == null) return;
[5958]327 if (isDoubleClick(e)) {
[1916]328 if (e.isControlDown()) {
329 editCurrentRelation();
330 } else {
331 setCurrentRelationAsSelection();
332 }
333 }
334 }
335 }
[6070]336
[1778]337 /**
[10386]338 * The action for creating a new relation.
[1866]339 */
[10386]340 static class NewAction extends AbstractAction implements LayerChangeListener, ActiveLayerChangeListener {
[8836]341 NewAction() {
[8510]342 putValue(SHORT_DESCRIPTION, tr("Create a new relation"));
[5028]343 putValue(NAME, tr("New"));
[10428]344 new ImageProvider("dialogs", "addrelation").getResource().attachImageIcon(this, true);
[2632]345 updateEnabledState();
[1866]346 }
347
348 public void run() {
[10413]349 RelationEditor.getEditor(Main.getLayerManager().getEditLayer(), null, null).setVisible(true);
[1866]350 }
351
[5799]352 @Override
[1866]353 public void actionPerformed(ActionEvent e) {
354 run();
355 }
356
357 protected void updateEnabledState() {
[10453]358 setEnabled(Main.getLayerManager().getEditLayer() != null);
[1866]359 }
360
[5799]361 @Override
[10386]362 public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
[1866]363 updateEnabledState();
[7001]364 }
[1866]365
[5799]366 @Override
[10386]367 public void layerAdded(LayerAddEvent e) {
[1866]368 updateEnabledState();
369 }
370
[5799]371 @Override
[10386]372 public void layerRemoving(LayerRemoveEvent e) {
[1866]373 updateEnabledState();
374 }
[10386]375
376 @Override
377 public void layerOrderChanged(LayerOrderChangeEvent e) {
378 // Do nothing
379 }
[1866]380 }
[1916]381
382 /**
[7001]383 * The list model for the list of relations displayed in the relation list dialog.
[2487]384 *
385 */
[7001]386 private class RelationListModel extends AbstractListModel<Relation> {
[8308]387 private final transient List<Relation> relations = new ArrayList<>();
388 private transient List<Relation> filteredRelations;
[9078]389 private final DefaultListSelectionModel selectionModel;
[8308]390 private transient SearchCompiler.Match filter;
[2126]391
[8836]392 RelationListModel(DefaultListSelectionModel selectionModel) {
[2317]393 this.selectionModel = selectionModel;
394 }
395
[2741]396 public void sort() {
[10619]397 relations.sort(DefaultNameFormatter.getInstance().getRelationComparator());
[2623]398 }
399
400 private boolean isValid(Relation r) {
401 return !r.isDeleted() && r.isVisible() && !r.isIncomplete();
402 }
403
[2741]404 public void setRelations(Collection<Relation> relations) {
[10378]405 List<Relation> sel = getSelectedRelations();
[2504]406 this.relations.clear();
[5619]407 this.filteredRelations = null;
[2506]408 if (relations == null) {
409 selectionModel.clearSelection();
[8510]410 fireContentsChanged(this, 0, getSize());
[2506]411 return;
412 }
[2504]413 for (Relation r: relations) {
[2623]414 if (isValid(r)) {
[2504]415 this.relations.add(r);
[2487]416 }
417 }
418 sort();
[5744]419 updateFilteredRelations();
[2126]420 fireIntervalAdded(this, 0, getSize());
[2488]421 setSelectedRelations(sel);
[2126]422 }
423
[2487]424 /**
425 * Add all relations in <code>addedPrimitives</code> to the model for the
426 * relation list dialog
[2512]427 *
[2487]428 * @param addedPrimitives the collection of added primitives. May include nodes,
429 * ways, and relations.
430 */
[2741]431 public void addRelations(Collection<? extends OsmPrimitive> addedPrimitives) {
[2484]432 boolean added = false;
433 for (OsmPrimitive p: addedPrimitives) {
[8444]434 if (!(p instanceof Relation)) {
[2484]435 continue;
436 }
[2623]437
[8510]438 Relation r = (Relation) p;
[2623]439 if (relations.contains(r)) {
[2484]440 continue;
441 }
[2623]442 if (isValid(r)) {
443 relations.add(r);
444 added = true;
445 }
[2484]446 }
447 if (added) {
[2487]448 List<Relation> sel = getSelectedRelations();
449 sort();
[5744]450 updateFilteredRelations();
[2484]451 fireIntervalAdded(this, 0, getSize());
[2487]452 setSelectedRelations(sel);
[2484]453 }
454 }
455
[2487]456 /**
457 * Removes all relations in <code>removedPrimitives</code> from the model
[2512]458 *
[2487]459 * @param removedPrimitives the removed primitives. May include nodes, ways,
460 * and relations
461 */
[2741]462 public void removeRelations(Collection<? extends OsmPrimitive> removedPrimitives) {
[2487]463 if (removedPrimitives == null) return;
464 // extract the removed relations
465 //
[7005]466 Set<Relation> removedRelations = new HashSet<>();
[2487]467 for (OsmPrimitive p: removedPrimitives) {
[8444]468 if (!(p instanceof Relation)) {
[2487]469 continue;
470 }
[8510]471 removedRelations.add((Relation) p);
[2487]472 }
473 if (removedRelations.isEmpty())
474 return;
475 int size = relations.size();
476 relations.removeAll(removedRelations);
[5709]477 if (filteredRelations != null) {
478 filteredRelations.removeAll(removedRelations);
479 }
[2487]480 if (size != relations.size()) {
481 List<Relation> sel = getSelectedRelations();
482 sort();
483 fireContentsChanged(this, 0, getSize());
484 setSelectedRelations(sel);
485 }
486 }
[6070]487
[5720]488 private void updateFilteredRelations() {
489 if (filter != null) {
[10611]490 filteredRelations = new ArrayList<>(Utils.filter(relations, filter::match));
[5720]491 } else if (filteredRelations != null) {
492 filteredRelations = null;
493 }
494 }
[3102]495
[5616]496 public void setFilter(final SearchCompiler.Match filter) {
[5720]497 this.filter = filter;
498 updateFilteredRelations();
[5616]499 List<Relation> sel = getSelectedRelations();
500 fireContentsChanged(this, 0, getSize());
501 setSelectedRelations(sel);
502 updateTitle();
503 }
504
505 private List<Relation> getVisibleRelations() {
506 return filteredRelations == null ? relations : filteredRelations;
507 }
[6070]508
[5709]509 private Relation getVisibleRelation(int index) {
510 if (index < 0 || index >= getVisibleRelations().size()) return null;
511 return getVisibleRelations().get(index);
512 }
[5616]513
514 @Override
[7001]515 public Relation getElementAt(int index) {
[5709]516 return getVisibleRelation(index);
[2126]517 }
518
[5616]519 @Override
[2126]520 public int getSize() {
[5616]521 return getVisibleRelations().size();
[2126]522 }
523
[2317]524 /**
525 * Replies the list of selected relations. Empty list,
526 * if there are no selected relations.
[2512]527 *
[2317]528 * @return the list of selected, non-new relations.
529 */
530 public List<Relation> getSelectedRelations() {
[7005]531 List<Relation> ret = new ArrayList<>();
[8510]532 for (int i = 0; i < getSize(); i++) {
[2317]533 if (!selectionModel.isSelectedIndex(i)) {
534 continue;
535 }
[5720]536 ret.add(getVisibleRelation(i));
[2317]537 }
538 return ret;
539 }
[2487]540
541 /**
542 * Sets the selected relations.
[2512]543 *
[5709]544 * @param sel the list of selected relations
[2487]545 */
[4795]546 public void setSelectedRelations(Collection<Relation> sel) {
[2487]547 selectionModel.clearSelection();
[2506]548 if (sel == null || sel.isEmpty())
[2487]549 return;
[5725]550 if (!getVisibleRelations().containsAll(sel)) {
551 resetFilter();
552 }
[2487]553 for (Relation r: sel) {
[5728]554 Integer i = getVisibleRelationIndex(r);
555 if (i != null) {
[8510]556 selectionModel.addSelectionInterval(i, i);
[2487]557 }
558 }
559 }
[2894]560
[5725]561 private Integer getVisibleRelationIndex(Relation rel) {
562 int i = getVisibleRelations().indexOf(rel);
[8510]563 if (i < 0)
[5725]564 return null;
565 return i;
566 }
[2914]567
[3020]568 public void updateTitle() {
[8318]569 if (!relations.isEmpty() && relations.size() != getSize()) {
[5616]570 RelationListDialog.this.setTitle(tr("Relations: {0}/{1}", getSize(), relations.size()));
571 } else if (getSize() > 0) {
[3020]572 RelationListDialog.this.setTitle(tr("Relations: {0}", getSize()));
573 } else {
574 RelationListDialog.this.setTitle(tr("Relations"));
575 }
576 }
[2126]577 }
[2317]578
[8512]579 private void setupPopupMenuHandler() {
[6070]580
[5825]581 // -- select action
582 popupMenuHandler.addAction(selectRelationAction);
583 popupMenuHandler.addAction(addRelationToSelectionAction);
584
585 // -- select members action
586 popupMenuHandler.addAction(selectMembersAction);
587 popupMenuHandler.addAction(addMembersToSelectionAction);
588
589 popupMenuHandler.addSeparator();
[5821]590 // -- download members action
591 popupMenuHandler.addAction(downloadMembersAction);
[4459]592
[5821]593 // -- download incomplete members action
594 popupMenuHandler.addAction(downloadSelectedIncompleteMembersAction);
[2317]595
[5821]596 popupMenuHandler.addSeparator();
[5825]597 popupMenuHandler.addAction(editAction).setVisible(false);
598 popupMenuHandler.addAction(duplicateAction).setVisible(false);
599 popupMenuHandler.addAction(deleteRelationsAction).setVisible(false);
[6070]600
[9365]601 addSelectionToRelationMenuItem = popupMenuHandler.addAction(addSelectionToRelations);
[4459]602 }
[6070]603
[5799]604 /* ---------------------------------------------------------------------------------- */
[5821]605 /* Methods that can be called from plugins */
[5799]606 /* ---------------------------------------------------------------------------------- */
607
[5821]608 /**
609 * Replies the popup menu handler.
610 * @return The popup menu handler
611 */
612 public PopupMenuHandler getPopupMenuHandler() {
613 return popupMenuHandler;
[2317]614 }
[2439]615
[7029]616 /**
617 * Replies the list of selected relations. Empty list, if there are no selected relations.
618 * @return the list of selected, non-new relations.
619 */
[4536]620 public Collection<Relation> getSelectedRelations() {
621 return model.getSelectedRelations();
622 }
623
[2487]624 /* ---------------------------------------------------------------------------------- */
625 /* DataSetListener */
626 /* ---------------------------------------------------------------------------------- */
627
[5799]628 @Override
[8413]629 public void nodeMoved(NodeMovedEvent event) {
630 /* irrelevant in this context */
631 }
[2487]632
[5799]633 @Override
[8413]634 public void wayNodesChanged(WayNodesChangedEvent event) {
635 /* irrelevant in this context */
636 }
[2487]637
[5799]638 @Override
[4223]639 public void primitivesAdded(final PrimitivesAddedEvent event) {
[2622]640 model.addRelations(event.getPrimitives());
[3020]641 model.updateTitle();
[2439]642 }
643
[5799]644 @Override
[4223]645 public void primitivesRemoved(final PrimitivesRemovedEvent event) {
[2622]646 model.removeRelations(event.getPrimitives());
[3020]647 model.updateTitle();
[2439]648 }
649
[5799]650 @Override
[2622]651 public void relationMembersChanged(final RelationMembersChangedEvent event) {
652 List<Relation> sel = model.getSelectedRelations();
653 model.sort();
654 model.setSelectedRelations(sel);
655 displaylist.repaint();
[2439]656 }
657
[5799]658 @Override
[2622]659 public void tagsChanged(TagsChangedEvent event) {
660 OsmPrimitive prim = event.getPrimitive();
[6105]661 if (!(prim instanceof Relation))
[2487]662 return;
[7001]663 // trigger a sort of the relation list because the display name may have changed
[2622]664 //
665 List<Relation> sel = model.getSelectedRelations();
666 model.sort();
667 model.setSelectedRelations(sel);
668 displaylist.repaint();
[2439]669 }
[2488]670
[5799]671 @Override
[2622]672 public void dataChanged(DataChangedEvent event) {
[10413]673 initFromLayer(Main.getLayerManager().getEditLayer());
[2497]674 }
[2655]675
[5799]676 @Override
[7001]677 public void otherDatasetChange(AbstractDatasetChangedEvent event) {
678 /* ignore */
679 }
[9264]680
681 @Override
682 public void zoomChanged() {
683 // re-filter relations
684 if (model.filter != null) {
685 model.setFilter(model.filter);
686 }
687 }
[9365]688
689 @Override
690 public void expertChanged(boolean isExpert) {
691 addSelectionToRelationMenuItem.setVisible(isExpert);
692 }
[626]693}
Note: See TracBrowser for help on using the repository browser.