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