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