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