[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();
|
---|
| 98 | private final 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 |
|
---|
[8285] | 118 | private final 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());
|
---|
[2914] | 273 | 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)
|
---|
| 274 | displaylist.scrollRectToVisible(displaylist.getCellBounds(i, i));
|
---|
| 275 | }
|
---|
[1780] | 276 | }
|
---|
[1763] | 277 | }
|
---|
[1778] | 278 |
|
---|
[5886] | 279 | private JosmTextField setupFilter() {
|
---|
| 280 | final JosmTextField f = new DisableShortcutsOnFocusGainedTextField();
|
---|
[5793] | 281 | f.setToolTipText(tr("Relation list filter"));
|
---|
| 282 | f.getDocument().addDocumentListener(new DocumentListener() {
|
---|
| 283 |
|
---|
| 284 | private void setFilter() {
|
---|
| 285 | try {
|
---|
| 286 | f.setBackground(UIManager.getColor("TextField.background"));
|
---|
| 287 | f.setToolTipText(tr("Relation list filter"));
|
---|
| 288 | model.setFilter(SearchCompiler.compile(filter.getText(), false, false));
|
---|
| 289 | } catch (SearchCompiler.ParseError ex) {
|
---|
| 290 | f.setBackground(new Color(255, 224, 224));
|
---|
| 291 | f.setToolTipText(ex.getMessage());
|
---|
| 292 | model.setFilter(new SearchCompiler.Always());
|
---|
| 293 | }
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | @Override
|
---|
| 297 | public void insertUpdate(DocumentEvent e) {
|
---|
| 298 | setFilter();
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | @Override
|
---|
| 302 | public void removeUpdate(DocumentEvent e) {
|
---|
| 303 | setFilter();
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | @Override
|
---|
| 307 | public void changedUpdate(DocumentEvent e) {
|
---|
| 308 | setFilter();
|
---|
| 309 | }
|
---|
| 310 | });
|
---|
| 311 | return f;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[5958] | 314 | class MouseEventHandler extends PopupMenuLauncher {
|
---|
[6070] | 315 |
|
---|
[5958] | 316 | public MouseEventHandler() {
|
---|
| 317 | super(popupMenu);
|
---|
| 318 | }
|
---|
[6025] | 319 |
|
---|
| 320 | @Override
|
---|
| 321 | public void mouseExited(MouseEvent me) {
|
---|
[6058] | 322 | if (highlightEnabled) highlightHelper.clear();
|
---|
[6025] | 323 | }
|
---|
[6070] | 324 |
|
---|
[1916] | 325 | protected void setCurrentRelationAsSelection() {
|
---|
[7021] | 326 | Main.main.getCurrentDataSet().setSelected(displaylist.getSelectedValue());
|
---|
[1916] | 327 | }
|
---|
| 328 |
|
---|
| 329 | protected void editCurrentRelation() {
|
---|
[5793] | 330 | EditRelationAction.launchEditor(getSelected());
|
---|
[1916] | 331 | }
|
---|
[6070] | 332 |
|
---|
[7539] | 333 | @Override
|
---|
| 334 | public void mouseClicked(MouseEvent e) {
|
---|
[6336] | 335 | if (!Main.main.hasEditLayer()) return;
|
---|
[5958] | 336 | if (isDoubleClick(e)) {
|
---|
[1916] | 337 | if (e.isControlDown()) {
|
---|
| 338 | editCurrentRelation();
|
---|
| 339 | } else {
|
---|
| 340 | setCurrentRelationAsSelection();
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
[6070] | 345 |
|
---|
[1778] | 346 | /**
|
---|
[2487] | 347 | * The action for creating a new relation
|
---|
[1866] | 348 | *
|
---|
| 349 | */
|
---|
[2632] | 350 | static class NewAction extends AbstractAction implements LayerChangeListener{
|
---|
[1866] | 351 | public NewAction() {
|
---|
| 352 | putValue(SHORT_DESCRIPTION,tr("Create a new relation"));
|
---|
[5028] | 353 | putValue(NAME, tr("New"));
|
---|
[1866] | 354 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "addrelation"));
|
---|
[2632] | 355 | updateEnabledState();
|
---|
[1866] | 356 | }
|
---|
| 357 |
|
---|
| 358 | public void run() {
|
---|
[2487] | 359 | RelationEditor.getEditor(Main.main.getEditLayer(),null, null).setVisible(true);
|
---|
[1866] | 360 | }
|
---|
| 361 |
|
---|
[5799] | 362 | @Override
|
---|
[1866] | 363 | public void actionPerformed(ActionEvent e) {
|
---|
| 364 | run();
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | protected void updateEnabledState() {
|
---|
[6336] | 368 | setEnabled(Main.main != null && Main.main.hasEditLayer());
|
---|
[1866] | 369 | }
|
---|
| 370 |
|
---|
[5799] | 371 | @Override
|
---|
[1866] | 372 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
| 373 | updateEnabledState();
|
---|
[7001] | 374 | }
|
---|
[1866] | 375 |
|
---|
[5799] | 376 | @Override
|
---|
[1866] | 377 | public void layerAdded(Layer newLayer) {
|
---|
| 378 | updateEnabledState();
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[5799] | 381 | @Override
|
---|
[1866] | 382 | public void layerRemoved(Layer oldLayer) {
|
---|
| 383 | updateEnabledState();
|
---|
| 384 | }
|
---|
| 385 | }
|
---|
[1916] | 386 |
|
---|
| 387 | /**
|
---|
[7001] | 388 | * The list model for the list of relations displayed in the relation list dialog.
|
---|
[2487] | 389 | *
|
---|
| 390 | */
|
---|
[7001] | 391 | private class RelationListModel extends AbstractListModel<Relation> {
|
---|
[7005] | 392 | private final List<Relation> relations = new ArrayList<>();
|
---|
[6316] | 393 | private List<Relation> filteredRelations;
|
---|
[2317] | 394 | private DefaultListSelectionModel selectionModel;
|
---|
[5720] | 395 | private SearchCompiler.Match filter;
|
---|
[2126] | 396 |
|
---|
[2317] | 397 | public RelationListModel(DefaultListSelectionModel selectionModel) {
|
---|
| 398 | this.selectionModel = selectionModel;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[2741] | 401 | public void sort() {
|
---|
[2623] | 402 | Collections.sort(
|
---|
| 403 | relations,
|
---|
[4070] | 404 | DefaultNameFormatter.getInstance().getRelationComparator()
|
---|
[5028] | 405 | );
|
---|
[2623] | 406 | }
|
---|
| 407 |
|
---|
| 408 | private boolean isValid(Relation r) {
|
---|
| 409 | return !r.isDeleted() && r.isVisible() && !r.isIncomplete();
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[2741] | 412 | public void setRelations(Collection<Relation> relations) {
|
---|
[2488] | 413 | List<Relation> sel = getSelectedRelations();
|
---|
[2504] | 414 | this.relations.clear();
|
---|
[5619] | 415 | this.filteredRelations = null;
|
---|
[2506] | 416 | if (relations == null) {
|
---|
| 417 | selectionModel.clearSelection();
|
---|
| 418 | fireContentsChanged(this,0,getSize());
|
---|
| 419 | return;
|
---|
| 420 | }
|
---|
[2504] | 421 | for (Relation r: relations) {
|
---|
[2623] | 422 | if (isValid(r)) {
|
---|
[2504] | 423 | this.relations.add(r);
|
---|
[2487] | 424 | }
|
---|
| 425 | }
|
---|
| 426 | sort();
|
---|
[5744] | 427 | updateFilteredRelations();
|
---|
[2126] | 428 | fireIntervalAdded(this, 0, getSize());
|
---|
[2488] | 429 | setSelectedRelations(sel);
|
---|
[2126] | 430 | }
|
---|
| 431 |
|
---|
[2487] | 432 | /**
|
---|
| 433 | * Add all relations in <code>addedPrimitives</code> to the model for the
|
---|
| 434 | * relation list dialog
|
---|
[2512] | 435 | *
|
---|
[2487] | 436 | * @param addedPrimitives the collection of added primitives. May include nodes,
|
---|
| 437 | * ways, and relations.
|
---|
| 438 | */
|
---|
[2741] | 439 | public void addRelations(Collection<? extends OsmPrimitive> addedPrimitives) {
|
---|
[2484] | 440 | boolean added = false;
|
---|
| 441 | for (OsmPrimitive p: addedPrimitives) {
|
---|
| 442 | if (! (p instanceof Relation)) {
|
---|
| 443 | continue;
|
---|
| 444 | }
|
---|
[2623] | 445 |
|
---|
| 446 | Relation r = (Relation)p;
|
---|
| 447 | if (relations.contains(r)) {
|
---|
[2484] | 448 | continue;
|
---|
| 449 | }
|
---|
[2623] | 450 | if (isValid(r)) {
|
---|
| 451 | relations.add(r);
|
---|
| 452 | added = true;
|
---|
| 453 | }
|
---|
[2484] | 454 | }
|
---|
| 455 | if (added) {
|
---|
[2487] | 456 | List<Relation> sel = getSelectedRelations();
|
---|
| 457 | sort();
|
---|
[5744] | 458 | updateFilteredRelations();
|
---|
[2484] | 459 | fireIntervalAdded(this, 0, getSize());
|
---|
[2487] | 460 | setSelectedRelations(sel);
|
---|
[2484] | 461 | }
|
---|
| 462 | }
|
---|
| 463 |
|
---|
[2487] | 464 | /**
|
---|
| 465 | * Removes all relations in <code>removedPrimitives</code> from the model
|
---|
[2512] | 466 | *
|
---|
[2487] | 467 | * @param removedPrimitives the removed primitives. May include nodes, ways,
|
---|
| 468 | * and relations
|
---|
| 469 | */
|
---|
[2741] | 470 | public void removeRelations(Collection<? extends OsmPrimitive> removedPrimitives) {
|
---|
[2487] | 471 | if (removedPrimitives == null) return;
|
---|
| 472 | // extract the removed relations
|
---|
| 473 | //
|
---|
[7005] | 474 | Set<Relation> removedRelations = new HashSet<>();
|
---|
[2487] | 475 | for (OsmPrimitive p: removedPrimitives) {
|
---|
| 476 | if (! (p instanceof Relation)) {
|
---|
| 477 | continue;
|
---|
| 478 | }
|
---|
| 479 | removedRelations.add((Relation)p);
|
---|
| 480 | }
|
---|
| 481 | if (removedRelations.isEmpty())
|
---|
| 482 | return;
|
---|
| 483 | int size = relations.size();
|
---|
| 484 | relations.removeAll(removedRelations);
|
---|
[5709] | 485 | if (filteredRelations != null) {
|
---|
| 486 | filteredRelations.removeAll(removedRelations);
|
---|
| 487 | }
|
---|
[2487] | 488 | if (size != relations.size()) {
|
---|
| 489 | List<Relation> sel = getSelectedRelations();
|
---|
| 490 | sort();
|
---|
| 491 | fireContentsChanged(this, 0, getSize());
|
---|
| 492 | setSelectedRelations(sel);
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
[6070] | 495 |
|
---|
[5720] | 496 | private void updateFilteredRelations() {
|
---|
| 497 | if (filter != null) {
|
---|
[7005] | 498 | filteredRelations = new ArrayList<>(Utils.filter(relations, new Predicate<Relation>() {
|
---|
[5720] | 499 | @Override
|
---|
| 500 | public boolean evaluate(Relation r) {
|
---|
| 501 | return filter.match(r);
|
---|
| 502 | }
|
---|
| 503 | }));
|
---|
| 504 | } else if (filteredRelations != null) {
|
---|
| 505 | filteredRelations = null;
|
---|
| 506 | }
|
---|
| 507 | }
|
---|
[3102] | 508 |
|
---|
[5616] | 509 | public void setFilter(final SearchCompiler.Match filter) {
|
---|
[5720] | 510 | this.filter = filter;
|
---|
| 511 | updateFilteredRelations();
|
---|
[5616] | 512 | List<Relation> sel = getSelectedRelations();
|
---|
| 513 | fireContentsChanged(this, 0, getSize());
|
---|
| 514 | setSelectedRelations(sel);
|
---|
| 515 | updateTitle();
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | private List<Relation> getVisibleRelations() {
|
---|
| 519 | return filteredRelations == null ? relations : filteredRelations;
|
---|
| 520 | }
|
---|
[6070] | 521 |
|
---|
[5709] | 522 | private Relation getVisibleRelation(int index) {
|
---|
| 523 | if (index < 0 || index >= getVisibleRelations().size()) return null;
|
---|
| 524 | return getVisibleRelations().get(index);
|
---|
| 525 | }
|
---|
[5616] | 526 |
|
---|
| 527 | @Override
|
---|
[7001] | 528 | public Relation getElementAt(int index) {
|
---|
[5709] | 529 | return getVisibleRelation(index);
|
---|
[2126] | 530 | }
|
---|
| 531 |
|
---|
[5616] | 532 | @Override
|
---|
[2126] | 533 | public int getSize() {
|
---|
[5616] | 534 | return getVisibleRelations().size();
|
---|
[2126] | 535 | }
|
---|
| 536 |
|
---|
[2317] | 537 | /**
|
---|
| 538 | * Replies the list of selected relations. Empty list,
|
---|
| 539 | * if there are no selected relations.
|
---|
[2512] | 540 | *
|
---|
[2317] | 541 | * @return the list of selected, non-new relations.
|
---|
| 542 | */
|
---|
| 543 | public List<Relation> getSelectedRelations() {
|
---|
[7005] | 544 | List<Relation> ret = new ArrayList<>();
|
---|
[2317] | 545 | for (int i=0; i<getSize();i++) {
|
---|
| 546 | if (!selectionModel.isSelectedIndex(i)) {
|
---|
| 547 | continue;
|
---|
| 548 | }
|
---|
[5720] | 549 | ret.add(getVisibleRelation(i));
|
---|
[2317] | 550 | }
|
---|
| 551 | return ret;
|
---|
| 552 | }
|
---|
[2487] | 553 |
|
---|
| 554 | /**
|
---|
| 555 | * Sets the selected relations.
|
---|
[2512] | 556 | *
|
---|
[5709] | 557 | * @param sel the list of selected relations
|
---|
[2487] | 558 | */
|
---|
[4795] | 559 | public void setSelectedRelations(Collection<Relation> sel) {
|
---|
[2487] | 560 | selectionModel.clearSelection();
|
---|
[2506] | 561 | if (sel == null || sel.isEmpty())
|
---|
[2487] | 562 | return;
|
---|
[5725] | 563 | if (!getVisibleRelations().containsAll(sel)) {
|
---|
| 564 | resetFilter();
|
---|
| 565 | }
|
---|
[2487] | 566 | for (Relation r: sel) {
|
---|
[5728] | 567 | Integer i = getVisibleRelationIndex(r);
|
---|
| 568 | if (i != null) {
|
---|
| 569 | selectionModel.addSelectionInterval(i,i);
|
---|
[2487] | 570 | }
|
---|
| 571 | }
|
---|
| 572 | }
|
---|
[2894] | 573 |
|
---|
[5725] | 574 | private Integer getVisibleRelationIndex(Relation rel) {
|
---|
| 575 | int i = getVisibleRelations().indexOf(rel);
|
---|
| 576 | if (i<0)
|
---|
| 577 | return null;
|
---|
| 578 | return i;
|
---|
| 579 | }
|
---|
[2914] | 580 |
|
---|
[3020] | 581 | public void updateTitle() {
|
---|
[5616] | 582 | if (relations.size() > 0 && relations.size() != getSize()) {
|
---|
| 583 | RelationListDialog.this.setTitle(tr("Relations: {0}/{1}", getSize(), relations.size()));
|
---|
| 584 | } else if (getSize() > 0) {
|
---|
[3020] | 585 | RelationListDialog.this.setTitle(tr("Relations: {0}", getSize()));
|
---|
| 586 | } else {
|
---|
| 587 | RelationListDialog.this.setTitle(tr("Relations"));
|
---|
| 588 | }
|
---|
| 589 | }
|
---|
[2126] | 590 | }
|
---|
[2317] | 591 |
|
---|
[5821] | 592 | private final void setupPopupMenuHandler() {
|
---|
[6070] | 593 |
|
---|
[5825] | 594 | // -- select action
|
---|
| 595 | popupMenuHandler.addAction(selectRelationAction);
|
---|
| 596 | popupMenuHandler.addAction(addRelationToSelectionAction);
|
---|
| 597 |
|
---|
| 598 | // -- select members action
|
---|
| 599 | popupMenuHandler.addAction(selectMembersAction);
|
---|
| 600 | popupMenuHandler.addAction(addMembersToSelectionAction);
|
---|
| 601 |
|
---|
| 602 | popupMenuHandler.addSeparator();
|
---|
[5821] | 603 | // -- download members action
|
---|
| 604 | popupMenuHandler.addAction(downloadMembersAction);
|
---|
[4459] | 605 |
|
---|
[5821] | 606 | // -- download incomplete members action
|
---|
| 607 | popupMenuHandler.addAction(downloadSelectedIncompleteMembersAction);
|
---|
[2317] | 608 |
|
---|
[5821] | 609 | popupMenuHandler.addSeparator();
|
---|
[5825] | 610 | popupMenuHandler.addAction(editAction).setVisible(false);
|
---|
| 611 | popupMenuHandler.addAction(duplicateAction).setVisible(false);
|
---|
| 612 | popupMenuHandler.addAction(deleteRelationsAction).setVisible(false);
|
---|
[6070] | 613 |
|
---|
[5821] | 614 | popupMenuHandler.addAction(addSelectionToRelations);
|
---|
[4459] | 615 | }
|
---|
[6070] | 616 |
|
---|
[5799] | 617 | /* ---------------------------------------------------------------------------------- */
|
---|
[5821] | 618 | /* Methods that can be called from plugins */
|
---|
[5799] | 619 | /* ---------------------------------------------------------------------------------- */
|
---|
| 620 |
|
---|
[5821] | 621 | /**
|
---|
| 622 | * Replies the popup menu handler.
|
---|
| 623 | * @return The popup menu handler
|
---|
| 624 | */
|
---|
| 625 | public PopupMenuHandler getPopupMenuHandler() {
|
---|
| 626 | return popupMenuHandler;
|
---|
[2317] | 627 | }
|
---|
[2439] | 628 |
|
---|
[7029] | 629 | /**
|
---|
| 630 | * Replies the list of selected relations. Empty list, if there are no selected relations.
|
---|
| 631 | * @return the list of selected, non-new relations.
|
---|
| 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 | }
|
---|