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