| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.BorderLayout;
|
|---|
| 7 | import java.awt.Component;
|
|---|
| 8 | import java.awt.event.ActionEvent;
|
|---|
| 9 | import java.awt.event.KeyEvent;
|
|---|
| 10 | import java.awt.event.MouseEvent;
|
|---|
| 11 | import java.util.ArrayList;
|
|---|
| 12 | import java.util.Arrays;
|
|---|
| 13 | import java.util.Collection;
|
|---|
| 14 | import java.util.Collections;
|
|---|
| 15 | import java.util.EnumSet;
|
|---|
| 16 | import java.util.HashSet;
|
|---|
| 17 | import java.util.List;
|
|---|
| 18 | import java.util.Set;
|
|---|
| 19 | import java.util.stream.Collectors;
|
|---|
| 20 | import java.util.stream.IntStream;
|
|---|
| 21 |
|
|---|
| 22 | import javax.swing.AbstractListModel;
|
|---|
| 23 | import javax.swing.DefaultListSelectionModel;
|
|---|
| 24 | import javax.swing.FocusManager;
|
|---|
| 25 | import javax.swing.JComponent;
|
|---|
| 26 | import javax.swing.JList;
|
|---|
| 27 | import javax.swing.JMenuItem;
|
|---|
| 28 | import javax.swing.JPanel;
|
|---|
| 29 | import javax.swing.JPopupMenu;
|
|---|
| 30 | import javax.swing.JScrollPane;
|
|---|
| 31 | import javax.swing.KeyStroke;
|
|---|
| 32 | import javax.swing.ListSelectionModel;
|
|---|
| 33 | import javax.swing.event.PopupMenuEvent;
|
|---|
| 34 | import javax.swing.event.PopupMenuListener;
|
|---|
| 35 |
|
|---|
| 36 | import org.openstreetmap.josm.actions.ExpertToggleAction;
|
|---|
| 37 | import org.openstreetmap.josm.actions.HistoryInfoAction;
|
|---|
| 38 | import org.openstreetmap.josm.actions.JosmAction;
|
|---|
| 39 | import org.openstreetmap.josm.actions.relation.AddSelectionToRelations;
|
|---|
| 40 | import org.openstreetmap.josm.actions.relation.DeleteRelationsAction;
|
|---|
| 41 | import org.openstreetmap.josm.actions.relation.DuplicateRelationAction;
|
|---|
| 42 | import org.openstreetmap.josm.actions.relation.EditRelationAction;
|
|---|
| 43 | import org.openstreetmap.josm.actions.relation.ExportRelationToGpxAction;
|
|---|
| 44 | import org.openstreetmap.josm.actions.relation.ExportRelationToGpxAction.Mode;
|
|---|
| 45 | import org.openstreetmap.josm.actions.relation.RecentRelationsAction;
|
|---|
| 46 | import org.openstreetmap.josm.actions.relation.SelectInRelationListAction;
|
|---|
| 47 | import org.openstreetmap.josm.actions.relation.SelectRelationAction;
|
|---|
| 48 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 49 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
|---|
| 50 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
|---|
| 51 | import org.openstreetmap.josm.data.osm.IRelation;
|
|---|
| 52 | import org.openstreetmap.josm.data.osm.OsmData;
|
|---|
| 53 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 54 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 55 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
|---|
| 56 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent.DatasetEventType;
|
|---|
| 57 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
|---|
| 58 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
|---|
| 59 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
|
|---|
| 60 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
|
|---|
| 61 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
|---|
| 62 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
|---|
| 63 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
|---|
| 64 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
|---|
| 65 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
|---|
| 66 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
|---|
| 67 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
|---|
| 68 | import org.openstreetmap.josm.data.osm.search.SearchCompiler;
|
|---|
| 69 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 70 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 71 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
|---|
| 72 | import org.openstreetmap.josm.gui.PopupMenuHandler;
|
|---|
| 73 | import org.openstreetmap.josm.gui.PrimitiveRenderer;
|
|---|
| 74 | import org.openstreetmap.josm.gui.SideButton;
|
|---|
| 75 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
|---|
| 76 | import org.openstreetmap.josm.gui.dialogs.relation.RelationPopupMenus;
|
|---|
| 77 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent;
|
|---|
| 78 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener;
|
|---|
| 79 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent;
|
|---|
| 80 | import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent;
|
|---|
| 81 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
|---|
| 82 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
|---|
| 83 | import org.openstreetmap.josm.gui.util.AbstractTag2LinkPopupListener;
|
|---|
| 84 | import org.openstreetmap.josm.gui.util.HighlightHelper;
|
|---|
| 85 | import org.openstreetmap.josm.gui.util.TableHelper;
|
|---|
| 86 | import org.openstreetmap.josm.gui.widgets.CompileSearchTextDecorator;
|
|---|
| 87 | import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField;
|
|---|
| 88 | import org.openstreetmap.josm.gui.widgets.FilterField;
|
|---|
| 89 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
|---|
| 90 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
|---|
| 91 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 92 | import org.openstreetmap.josm.tools.InputMapUtils;
|
|---|
| 93 | import org.openstreetmap.josm.tools.PlatformManager;
|
|---|
| 94 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 95 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
|---|
| 96 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * A dialog showing all known relations, with buttons to add, edit, and delete them.
|
|---|
| 100 | *
|
|---|
| 101 | * We don't have such dialogs for nodes, segments, and ways, because those
|
|---|
| 102 | * objects are visible on the map and can be selected there. Relations are not.
|
|---|
| 103 | */
|
|---|
| 104 | public class RelationListDialog extends ToggleDialog
|
|---|
| 105 | implements DataSetListener, NavigatableComponent.ZoomChangeListener {
|
|---|
| 106 | /** The display list. */
|
|---|
| 107 | private final JList<IRelation<?>> displaylist;
|
|---|
| 108 | /** the list model used */
|
|---|
| 109 | private final RelationListModel model;
|
|---|
| 110 |
|
|---|
| 111 | private final NewAction newAction;
|
|---|
| 112 |
|
|---|
| 113 | /** the popup menu and its handler */
|
|---|
| 114 | private final JPopupMenu popupMenu = new JPopupMenu();
|
|---|
| 115 | private final transient PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
|
|---|
| 116 |
|
|---|
| 117 | private final JosmTextField filter;
|
|---|
| 118 |
|
|---|
| 119 | // Actions
|
|---|
| 120 | /** the edit action */
|
|---|
| 121 | private final EditRelationAction editAction = new EditRelationAction();
|
|---|
| 122 | /** the delete action */
|
|---|
| 123 | private final DeleteRelationsAction deleteRelationsAction = new DeleteRelationsAction();
|
|---|
| 124 | /** the duplicate action */
|
|---|
| 125 | private final DuplicateRelationAction duplicateAction = new DuplicateRelationAction();
|
|---|
| 126 | /** the select relation action */
|
|---|
| 127 | private final SelectRelationAction selectRelationAction = new SelectRelationAction(false);
|
|---|
| 128 | /** add all selected primitives to the given relations */
|
|---|
| 129 | private final AddSelectionToRelations addSelectionToRelations = new AddSelectionToRelations();
|
|---|
| 130 |
|
|---|
| 131 | /** export relation to GPX track action */
|
|---|
| 132 | private final ExportRelationToGpxAction exportRelationFromFirstAction =
|
|---|
| 133 | new ExportRelationToGpxAction(EnumSet.of(Mode.FROM_FIRST_MEMBER, Mode.TO_FILE));
|
|---|
| 134 | private final ExportRelationToGpxAction exportRelationFromLastAction =
|
|---|
| 135 | new ExportRelationToGpxAction(EnumSet.of(Mode.FROM_LAST_MEMBER, Mode.TO_FILE));
|
|---|
| 136 | private final ExportRelationToGpxAction exportRelationFromFirstToLayerAction =
|
|---|
| 137 | new ExportRelationToGpxAction(EnumSet.of(Mode.FROM_FIRST_MEMBER, Mode.TO_LAYER));
|
|---|
| 138 | private final ExportRelationToGpxAction exportRelationFromLastToLayerAction =
|
|---|
| 139 | new ExportRelationToGpxAction(EnumSet.of(Mode.FROM_LAST_MEMBER, Mode.TO_LAYER));
|
|---|
| 140 |
|
|---|
| 141 | private final transient HighlightHelper highlightHelper = new HighlightHelper();
|
|---|
| 142 | private final boolean highlightEnabled = Config.getPref().getBoolean("draw.target-highlight", true);
|
|---|
| 143 | private final transient RecentRelationsAction recentRelationsAction;
|
|---|
| 144 |
|
|---|
| 145 | /**
|
|---|
| 146 | * Constructs <code>RelationListDialog</code>
|
|---|
| 147 | */
|
|---|
| 148 | public RelationListDialog() {
|
|---|
| 149 | super(tr("Relations"), "relationlist", tr("Open a list of all relations."),
|
|---|
| 150 | Shortcut.registerShortcut("subwindow:relations", tr("Windows: {0}", tr("Relations")),
|
|---|
| 151 | KeyEvent.VK_R, Shortcut.ALT_SHIFT), 150, true);
|
|---|
| 152 |
|
|---|
| 153 | // create the list of relations
|
|---|
| 154 | //
|
|---|
| 155 | DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
|
|---|
| 156 | model = new RelationListModel(selectionModel);
|
|---|
| 157 | displaylist = new JList<>(model);
|
|---|
| 158 | displaylist.setSelectionModel(selectionModel);
|
|---|
| 159 | displaylist.setCellRenderer(new NoTooltipOsmRenderer());
|
|---|
| 160 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
|---|
| 161 | displaylist.addMouseListener(new MouseEventHandler());
|
|---|
| 162 |
|
|---|
| 163 | // the new action
|
|---|
| 164 | //
|
|---|
| 165 | newAction = new NewAction();
|
|---|
| 166 |
|
|---|
| 167 | filter = setupFilter();
|
|---|
| 168 |
|
|---|
| 169 | displaylist.addListSelectionListener(e -> {
|
|---|
| 170 | if (!e.getValueIsAdjusting()) updateActionsRelationLists();
|
|---|
| 171 | });
|
|---|
| 172 |
|
|---|
| 173 | // Setup popup menu handler
|
|---|
| 174 | setupPopupMenuHandler();
|
|---|
| 175 |
|
|---|
| 176 | JPanel pane = new JPanel(new BorderLayout());
|
|---|
| 177 | pane.add(filter, BorderLayout.NORTH);
|
|---|
| 178 | pane.add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
|---|
| 179 |
|
|---|
| 180 | SideButton editButton = new SideButton(editAction, false);
|
|---|
| 181 | recentRelationsAction = new RecentRelationsAction(editButton);
|
|---|
| 182 |
|
|---|
| 183 | createLayout(pane, false, Arrays.asList(
|
|---|
| 184 | new SideButton(newAction, false),
|
|---|
| 185 | editButton,
|
|---|
| 186 | new SideButton(duplicateAction, false),
|
|---|
| 187 | new SideButton(deleteRelationsAction, false),
|
|---|
| 188 | new SideButton(selectRelationAction, false)
|
|---|
| 189 | ));
|
|---|
| 190 |
|
|---|
| 191 | InputMapUtils.unassignCtrlShiftUpDown(displaylist, JComponent.WHEN_FOCUSED);
|
|---|
| 192 |
|
|---|
| 193 | // Select relation on Enter
|
|---|
| 194 | InputMapUtils.addEnterAction(displaylist, selectRelationAction);
|
|---|
| 195 |
|
|---|
| 196 | // Edit relation on Ctrl-Enter
|
|---|
| 197 | displaylist.getActionMap().put("edit", editAction);
|
|---|
| 198 | displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_DOWN_MASK), "edit");
|
|---|
| 199 |
|
|---|
| 200 | // Do not hide copy action because of default JList override (fix #9815)
|
|---|
| 201 | displaylist.getActionMap().put("copy", MainApplication.getMenu().copy);
|
|---|
| 202 | displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, PlatformManager.getPlatform().getMenuShortcutKeyMaskEx()), "copy");
|
|---|
| 203 |
|
|---|
| 204 | HistoryInfoAction historyAction = MainApplication.getMenu().historyinfo;
|
|---|
| 205 | displaylist.getActionMap().put("historyAction", historyAction);
|
|---|
| 206 | displaylist.getInputMap().put(historyAction.getShortcut().getKeyStroke(), "historyAction");
|
|---|
| 207 |
|
|---|
| 208 | updateActionsRelationLists();
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | @Override
|
|---|
| 212 | public void destroy() {
|
|---|
| 213 | recentRelationsAction.destroy();
|
|---|
| 214 | popupMenuHandler.setPrimitives(Collections.emptyList());
|
|---|
| 215 | selectRelationAction.setPrimitives(Collections.emptyList());
|
|---|
| 216 | model.clear();
|
|---|
| 217 | super.destroy();
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /**
|
|---|
| 221 | * Enable the "recent relations" dropdown menu next to edit button.
|
|---|
| 222 | */
|
|---|
| 223 | public void enableRecentRelations() {
|
|---|
| 224 | recentRelationsAction.enableArrow();
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | // inform all actions about list of relations they need
|
|---|
| 228 | private void updateActionsRelationLists() {
|
|---|
| 229 | List<IRelation<?>> sel = model.getSelectedRelations();
|
|---|
| 230 | popupMenuHandler.setPrimitives(sel);
|
|---|
| 231 | selectRelationAction.setPrimitives(sel);
|
|---|
| 232 |
|
|---|
| 233 | Component focused = FocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
|
|---|
| 234 |
|
|---|
| 235 | //update highlights
|
|---|
| 236 | if (highlightEnabled && focused == displaylist && MainApplication.isDisplayingMapView()
|
|---|
| 237 | && highlightHelper.highlightOnly(Utils.filteredCollection(sel, Relation.class))) {
|
|---|
| 238 | MainApplication.getMap().mapView.repaint();
|
|---|
| 239 | }
|
|---|
| 240 | }
|
|---|
| 241 |
|
|---|
| 242 | @Override
|
|---|
| 243 | public void showNotify() {
|
|---|
| 244 | MainApplication.getLayerManager().addLayerChangeListener(newAction);
|
|---|
| 245 | MainApplication.getLayerManager().addActiveLayerChangeListener(newAction);
|
|---|
| 246 | MapView.addZoomChangeListener(this);
|
|---|
| 247 | newAction.updateEnabledState();
|
|---|
| 248 | DatasetEventManager.getInstance().addDatasetListener(this, FireMode.IN_EDT_CONSOLIDATED);
|
|---|
| 249 | SelectionEventManager.getInstance().addSelectionListener(addSelectionToRelations);
|
|---|
| 250 | dataChanged(null);
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | @Override
|
|---|
| 254 | public void hideNotify() {
|
|---|
| 255 | MainApplication.getLayerManager().removeActiveLayerChangeListener(newAction);
|
|---|
| 256 | MainApplication.getLayerManager().removeLayerChangeListener(newAction);
|
|---|
| 257 | MapView.removeZoomChangeListener(this);
|
|---|
| 258 | DatasetEventManager.getInstance().removeDatasetListener(this);
|
|---|
| 259 | SelectionEventManager.getInstance().removeSelectionListener(addSelectionToRelations);
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | private void resetFilter() {
|
|---|
| 263 | filter.setText(null);
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /**
|
|---|
| 267 | * Initializes the relation list dialog from a dataset. If <code>data</code> is null
|
|---|
| 268 | * the dialog is reset to an empty dialog.
|
|---|
| 269 | * Otherwise it is initialized with the list of non-deleted and visible relations
|
|---|
| 270 | * in the dataset.
|
|---|
| 271 | *
|
|---|
| 272 | * @param data the dataset. May be null.
|
|---|
| 273 | * @since 13957
|
|---|
| 274 | */
|
|---|
| 275 | protected void initFromData(OsmData<?, ?, ?, ?> data) {
|
|---|
| 276 | if (data == null) {
|
|---|
| 277 | model.setRelations(null);
|
|---|
| 278 | return;
|
|---|
| 279 | }
|
|---|
| 280 | model.setRelations(data.getRelations());
|
|---|
| 281 | model.updateTitle();
|
|---|
| 282 | updateActionsRelationLists();
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /**
|
|---|
| 286 | * @return The selected relation in the list
|
|---|
| 287 | */
|
|---|
| 288 | private IRelation<?> getSelected() {
|
|---|
| 289 | if (model.getSize() == 1) {
|
|---|
| 290 | displaylist.setSelectedIndex(0);
|
|---|
| 291 | }
|
|---|
| 292 | return displaylist.getSelectedValue();
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | /**
|
|---|
| 296 | * Selects the relation <code>relation</code> in the list of relations.
|
|---|
| 297 | *
|
|---|
| 298 | * @param relation the relation
|
|---|
| 299 | */
|
|---|
| 300 | public void selectRelation(Relation relation) {
|
|---|
| 301 | selectRelations(Collections.singleton(relation));
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | /**
|
|---|
| 305 | * Selects the relations in the list of relations.
|
|---|
| 306 | * @param relations the relations to be selected
|
|---|
| 307 | * @since 13957 (signature)
|
|---|
| 308 | */
|
|---|
| 309 | public void selectRelations(Collection<? extends IRelation<?>> relations) {
|
|---|
| 310 | if (Utils.isEmpty(relations)) {
|
|---|
| 311 | model.setSelectedRelations(null);
|
|---|
| 312 | } else {
|
|---|
| 313 | model.setSelectedRelations(relations);
|
|---|
| 314 | int i = model.getVisibleRelations().indexOf(relations.iterator().next());
|
|---|
| 315 | if (i >= 0) {
|
|---|
| 316 | // Not all relations have to be in the list
|
|---|
| 317 | // (for example when the relation list is hidden, it's not updated with new relations)
|
|---|
| 318 | displaylist.scrollRectToVisible(displaylist.getCellBounds(i, i));
|
|---|
| 319 | }
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | private JosmTextField setupFilter() {
|
|---|
| 324 | final JosmTextField f = new DisableShortcutsOnFocusGainedTextField();
|
|---|
| 325 | FilterField.setSearchIcon(f);
|
|---|
| 326 | f.setToolTipText(tr("Relation list filter"));
|
|---|
| 327 | final CompileSearchTextDecorator decorator = CompileSearchTextDecorator.decorate(f);
|
|---|
| 328 | f.addPropertyChangeListener("filter", evt -> model.setFilter(decorator.getMatch()));
|
|---|
| 329 | return f;
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | static final class NoTooltipOsmRenderer extends PrimitiveRenderer {
|
|---|
| 333 | @Override
|
|---|
| 334 | protected String getComponentToolTipText(IPrimitive value) {
|
|---|
| 335 | // Don't show the default tooltip in the relation list
|
|---|
| 336 | return null;
|
|---|
| 337 | }
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | class MouseEventHandler extends PopupMenuLauncher {
|
|---|
| 341 |
|
|---|
| 342 | MouseEventHandler() {
|
|---|
| 343 | super(popupMenu);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | @Override
|
|---|
| 347 | public void mouseExited(MouseEvent me) {
|
|---|
| 348 | if (highlightEnabled) highlightHelper.clear();
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | protected void setCurrentRelationAsSelection() {
|
|---|
| 352 | MainApplication.getLayerManager().getActiveData().setSelected(displaylist.getSelectedValue());
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | protected void editCurrentRelation() {
|
|---|
| 356 | IRelation<?> rel = getSelected();
|
|---|
| 357 | if (rel instanceof Relation) {
|
|---|
| 358 | EditRelationAction.launchEditor((Relation) rel);
|
|---|
| 359 | }
|
|---|
| 360 | }
|
|---|
| 361 |
|
|---|
| 362 | @Override
|
|---|
| 363 | public void mouseClicked(MouseEvent e) {
|
|---|
| 364 | DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
|
|---|
| 365 | if (ds != null && isDoubleClick(e)) {
|
|---|
| 366 | if (e.isControlDown() && !ds.isLocked()) {
|
|---|
| 367 | editCurrentRelation();
|
|---|
| 368 | } else {
|
|---|
| 369 | setCurrentRelationAsSelection();
|
|---|
| 370 | }
|
|---|
| 371 | }
|
|---|
| 372 | }
|
|---|
| 373 | }
|
|---|
| 374 |
|
|---|
| 375 | /**
|
|---|
| 376 | * The action for creating a new relation.
|
|---|
| 377 | */
|
|---|
| 378 | static class NewAction extends JosmAction implements LayerChangeListener, ActiveLayerChangeListener {
|
|---|
| 379 | NewAction() {
|
|---|
| 380 | super(tr("New"), "dialogs/add", tr("Create a new relation"),
|
|---|
| 381 | Shortcut.registerShortcut("relation:new", tr("Create a new relation"), KeyEvent.VK_UNDEFINED, Shortcut.NONE),
|
|---|
| 382 | false, false);
|
|---|
| 383 | updateEnabledState();
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | /**
|
|---|
| 387 | * Make a new relation
|
|---|
| 388 | */
|
|---|
| 389 | public void run() {
|
|---|
| 390 | RelationEditor.getEditor(MainApplication.getLayerManager().getEditLayer(), null, null).setVisible(true);
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | @Override
|
|---|
| 394 | public void actionPerformed(ActionEvent e) {
|
|---|
| 395 | run();
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | @Override
|
|---|
| 399 | protected void updateEnabledState() {
|
|---|
| 400 | setEnabled(MainApplication.getLayerManager().getEditLayer() != null);
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | @Override
|
|---|
| 404 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
|---|
| 405 | updateEnabledState();
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | @Override
|
|---|
| 409 | public void layerAdded(LayerAddEvent e) {
|
|---|
| 410 | updateEnabledState();
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | @Override
|
|---|
| 414 | public void layerRemoving(LayerRemoveEvent e) {
|
|---|
| 415 | updateEnabledState();
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | @Override
|
|---|
| 419 | public void layerOrderChanged(LayerOrderChangeEvent e) {
|
|---|
| 420 | // Do nothing
|
|---|
| 421 | }
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | /**
|
|---|
| 425 | * The list model for the list of relations displayed in the relation list dialog.
|
|---|
| 426 | */
|
|---|
| 427 | private class RelationListModel extends AbstractListModel<IRelation<?>> {
|
|---|
| 428 | private final transient List<IRelation<?>> relations = new ArrayList<>();
|
|---|
| 429 | private transient List<IRelation<?>> filteredRelations;
|
|---|
| 430 | private final DefaultListSelectionModel selectionModel;
|
|---|
| 431 | private transient SearchCompiler.Match filter;
|
|---|
| 432 |
|
|---|
| 433 | RelationListModel(DefaultListSelectionModel selectionModel) {
|
|---|
| 434 | this.selectionModel = selectionModel;
|
|---|
| 435 | }
|
|---|
| 436 |
|
|---|
| 437 | /**
|
|---|
| 438 | * Clears the model.
|
|---|
| 439 | */
|
|---|
| 440 | public void clear() {
|
|---|
| 441 | relations.clear();
|
|---|
| 442 | if (filteredRelations != null)
|
|---|
| 443 | filteredRelations.clear();
|
|---|
| 444 | filter = null;
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | /**
|
|---|
| 448 | * Sorts the model using {@link DefaultNameFormatter} relation comparator.
|
|---|
| 449 | */
|
|---|
| 450 | public void sort() {
|
|---|
| 451 | relations.sort(DefaultNameFormatter.getInstance().getRelationComparator());
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | private boolean isValid(IRelation<?> r) {
|
|---|
| 455 | return !r.isDeleted() && !r.isIncomplete();
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | public void setRelations(Collection<? extends IRelation<?>> relations) {
|
|---|
| 459 | List<IRelation<?>> sel = getSelectedRelations();
|
|---|
| 460 | this.relations.clear();
|
|---|
| 461 | this.filteredRelations = null;
|
|---|
| 462 | if (relations == null) {
|
|---|
| 463 | selectionModel.clearSelection();
|
|---|
| 464 | fireContentsChanged(this, 0, getSize());
|
|---|
| 465 | return;
|
|---|
| 466 | }
|
|---|
| 467 | for (IRelation<?> r: relations) {
|
|---|
| 468 | if (isValid(r)) {
|
|---|
| 469 | this.relations.add(r);
|
|---|
| 470 | }
|
|---|
| 471 | }
|
|---|
| 472 | sort();
|
|---|
| 473 | updateFilteredRelations();
|
|---|
| 474 | fireIntervalAdded(this, 0, getSize());
|
|---|
| 475 | setSelectedRelations(sel);
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | /**
|
|---|
| 479 | * Add all relations in <code>addedPrimitives</code> to the model for the
|
|---|
| 480 | * relation list dialog
|
|---|
| 481 | *
|
|---|
| 482 | * @param addedPrimitives the collection of added primitives. May include nodes,
|
|---|
| 483 | * ways, and relations.
|
|---|
| 484 | */
|
|---|
| 485 | public void addRelations(Collection<? extends OsmPrimitive> addedPrimitives) {
|
|---|
| 486 | boolean added = false;
|
|---|
| 487 | for (OsmPrimitive p: addedPrimitives) {
|
|---|
| 488 | if (!(p instanceof Relation)) {
|
|---|
| 489 | continue;
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | Relation r = (Relation) p;
|
|---|
| 493 | if (relations.contains(r)) {
|
|---|
| 494 | continue;
|
|---|
| 495 | }
|
|---|
| 496 | if (isValid(r)) {
|
|---|
| 497 | relations.add(r);
|
|---|
| 498 | added = true;
|
|---|
| 499 | }
|
|---|
| 500 | }
|
|---|
| 501 | if (added) {
|
|---|
| 502 | List<IRelation<?>> sel = getSelectedRelations();
|
|---|
| 503 | sort();
|
|---|
| 504 | updateFilteredRelations();
|
|---|
| 505 | fireIntervalAdded(this, 0, getSize());
|
|---|
| 506 | setSelectedRelations(sel);
|
|---|
| 507 | }
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | /**
|
|---|
| 511 | * Removes all relations in <code>removedPrimitives</code> from the model
|
|---|
| 512 | *
|
|---|
| 513 | * @param removedPrimitives the removed primitives. May include nodes, ways,
|
|---|
| 514 | * and relations
|
|---|
| 515 | */
|
|---|
| 516 | public void removeRelations(Collection<? extends OsmPrimitive> removedPrimitives) {
|
|---|
| 517 | if (removedPrimitives == null) return;
|
|---|
| 518 | // extract the removed relations
|
|---|
| 519 | Set<Relation> removedRelations = removedPrimitives.stream()
|
|---|
| 520 | .filter(Relation.class::isInstance).map(Relation.class::cast)
|
|---|
| 521 | .collect(Collectors.toSet());
|
|---|
| 522 | if (removedRelations.isEmpty())
|
|---|
| 523 | return;
|
|---|
| 524 | int size = relations.size();
|
|---|
| 525 | relations.removeAll(removedRelations);
|
|---|
| 526 | if (filteredRelations != null) {
|
|---|
| 527 | filteredRelations.removeAll(removedRelations);
|
|---|
| 528 | }
|
|---|
| 529 | if (size != relations.size()) {
|
|---|
| 530 | List<IRelation<?>> sel = getSelectedRelations();
|
|---|
| 531 | sort();
|
|---|
| 532 | fireContentsChanged(this, 0, getSize());
|
|---|
| 533 | setSelectedRelations(sel);
|
|---|
| 534 | }
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | private void updateFilteredRelations() {
|
|---|
| 538 | if (filter != null) {
|
|---|
| 539 | filteredRelations = new ArrayList<>(SubclassFilteredCollection.filter(relations, filter::match));
|
|---|
| 540 | } else if (filteredRelations != null) {
|
|---|
| 541 | filteredRelations = null;
|
|---|
| 542 | }
|
|---|
| 543 | }
|
|---|
| 544 |
|
|---|
| 545 | public void setFilter(final SearchCompiler.Match filter) {
|
|---|
| 546 | this.filter = filter;
|
|---|
| 547 | updateFilteredRelations();
|
|---|
| 548 | List<IRelation<?>> sel = getSelectedRelations();
|
|---|
| 549 | fireContentsChanged(this, 0, getSize());
|
|---|
| 550 | setSelectedRelations(sel);
|
|---|
| 551 | updateTitle();
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | private List<IRelation<?>> getVisibleRelations() {
|
|---|
| 555 | return filteredRelations == null ? relations : filteredRelations;
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | private IRelation<?> getVisibleRelation(int index) {
|
|---|
| 559 | if (index < 0 || index >= getVisibleRelations().size()) return null;
|
|---|
| 560 | return getVisibleRelations().get(index);
|
|---|
| 561 | }
|
|---|
| 562 |
|
|---|
| 563 | @Override
|
|---|
| 564 | public IRelation<?> getElementAt(int index) {
|
|---|
| 565 | return getVisibleRelation(index);
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 | @Override
|
|---|
| 569 | public int getSize() {
|
|---|
| 570 | return getVisibleRelations().size();
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | /**
|
|---|
| 574 | * Replies the list of selected relations. Empty list,
|
|---|
| 575 | * if there are no selected relations.
|
|---|
| 576 | *
|
|---|
| 577 | * @return the list of selected, non-new relations.
|
|---|
| 578 | * @since 13957 (signature)
|
|---|
| 579 | */
|
|---|
| 580 | public List<IRelation<?>> getSelectedRelations() {
|
|---|
| 581 | return IntStream.range(0, getSize())
|
|---|
| 582 | .filter(selectionModel::isSelectedIndex)
|
|---|
| 583 | .mapToObj(this::getVisibleRelation)
|
|---|
| 584 | .collect(Collectors.toList());
|
|---|
| 585 | }
|
|---|
| 586 |
|
|---|
| 587 | /**
|
|---|
| 588 | * Sets the selected relations.
|
|---|
| 589 | *
|
|---|
| 590 | * @param sel the list of selected relations
|
|---|
| 591 | * @since 13957 (signature)
|
|---|
| 592 | */
|
|---|
| 593 | public void setSelectedRelations(Collection<? extends IRelation<?>> sel) {
|
|---|
| 594 | if (!Utils.isEmpty(sel)) {
|
|---|
| 595 | if (!new HashSet<>(getVisibleRelations()).containsAll(sel)) {
|
|---|
| 596 | resetFilter();
|
|---|
| 597 | }
|
|---|
| 598 | TableHelper.setSelectedIndices(selectionModel, sel.stream().mapToInt(getVisibleRelations()::indexOf));
|
|---|
| 599 | } else {
|
|---|
| 600 | TableHelper.setSelectedIndices(selectionModel, IntStream.empty());
|
|---|
| 601 | }
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | /**
|
|---|
| 605 | * Update the title for the relation list dialog
|
|---|
| 606 | */
|
|---|
| 607 | public void updateTitle() {
|
|---|
| 608 | if (!relations.isEmpty() && relations.size() != getSize()) {
|
|---|
| 609 | RelationListDialog.this.setTitle(tr("Relations: {0}/{1}", getSize(), relations.size()));
|
|---|
| 610 | } else if (getSize() > 0) {
|
|---|
| 611 | RelationListDialog.this.setTitle(tr("Relations: {0}", getSize()));
|
|---|
| 612 | } else {
|
|---|
| 613 | RelationListDialog.this.setTitle(tr("Relations"));
|
|---|
| 614 | }
|
|---|
| 615 | }
|
|---|
| 616 | }
|
|---|
| 617 |
|
|---|
| 618 | private void setupPopupMenuHandler() {
|
|---|
| 619 | List<JMenuItem> checkDisabled = new ArrayList<>();
|
|---|
| 620 |
|
|---|
| 621 | RelationPopupMenus.setupHandler(popupMenuHandler, SelectInRelationListAction.class);
|
|---|
| 622 |
|
|---|
| 623 | // -- export relation to gpx action
|
|---|
| 624 | popupMenuHandler.addSeparator();
|
|---|
| 625 | checkDisabled.add(popupMenuHandler.addAction(exportRelationFromFirstAction));
|
|---|
| 626 | checkDisabled.add(popupMenuHandler.addAction(exportRelationFromLastAction));
|
|---|
| 627 | popupMenuHandler.addSeparator();
|
|---|
| 628 | checkDisabled.add(popupMenuHandler.addAction(exportRelationFromFirstToLayerAction));
|
|---|
| 629 | checkDisabled.add(popupMenuHandler.addAction(exportRelationFromLastToLayerAction));
|
|---|
| 630 |
|
|---|
| 631 | popupMenuHandler.addSeparator();
|
|---|
| 632 | popupMenuHandler.addAction(editAction).setVisible(false);
|
|---|
| 633 | popupMenuHandler.addAction(duplicateAction).setVisible(false);
|
|---|
| 634 | popupMenuHandler.addAction(deleteRelationsAction).setVisible(false);
|
|---|
| 635 |
|
|---|
| 636 | ExpertToggleAction.addVisibilitySwitcher(popupMenuHandler.addAction(addSelectionToRelations));
|
|---|
| 637 |
|
|---|
| 638 | popupMenuHandler.addListener(new PopupMenuListener() {
|
|---|
| 639 | @Override
|
|---|
| 640 | public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
|
|---|
| 641 | for (JMenuItem mi: checkDisabled) {
|
|---|
| 642 | mi.setVisible(mi.getAction().isEnabled());
|
|---|
| 643 | Component sep = popupMenu.getComponent(Math.max(0, popupMenu.getComponentIndex(mi) - 1));
|
|---|
| 644 | if (!(sep instanceof JMenuItem)) {
|
|---|
| 645 | sep.setVisible(mi.isVisible());
|
|---|
| 646 | }
|
|---|
| 647 | }
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | @Override
|
|---|
| 651 | public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
|
|---|
| 652 | // Do nothing
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | @Override
|
|---|
| 656 | public void popupMenuCanceled(PopupMenuEvent e) {
|
|---|
| 657 | // Do nothing
|
|---|
| 658 | }
|
|---|
| 659 | });
|
|---|
| 660 |
|
|---|
| 661 | popupMenuHandler.addListener(new AbstractTag2LinkPopupListener() {
|
|---|
| 662 | @Override
|
|---|
| 663 | public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
|
|---|
| 664 | getSelectedRelations().forEach(relation ->
|
|---|
| 665 | relation.visitKeys((primitive, key, value) -> addLinks(popupMenu, key, value)));
|
|---|
| 666 | }
|
|---|
| 667 | });
|
|---|
| 668 | }
|
|---|
| 669 |
|
|---|
| 670 | /* ---------------------------------------------------------------------------------- */
|
|---|
| 671 | /* Methods that can be called from plugins */
|
|---|
| 672 | /* ---------------------------------------------------------------------------------- */
|
|---|
| 673 |
|
|---|
| 674 | /**
|
|---|
| 675 | * Replies the popup menu handler.
|
|---|
| 676 | * @return The popup menu handler
|
|---|
| 677 | */
|
|---|
| 678 | public PopupMenuHandler getPopupMenuHandler() {
|
|---|
| 679 | return popupMenuHandler;
|
|---|
| 680 | }
|
|---|
| 681 |
|
|---|
| 682 | /**
|
|---|
| 683 | * Replies the list of selected relations. Empty list, if there are no selected relations.
|
|---|
| 684 | * @return the list of selected, non-new relations.
|
|---|
| 685 | * @since 13957 (signature)
|
|---|
| 686 | */
|
|---|
| 687 | public Collection<IRelation<?>> getSelectedRelations() {
|
|---|
| 688 | return model.getSelectedRelations();
|
|---|
| 689 | }
|
|---|
| 690 |
|
|---|
| 691 | /* ---------------------------------------------------------------------------------- */
|
|---|
| 692 | /* DataSetListener */
|
|---|
| 693 | /* ---------------------------------------------------------------------------------- */
|
|---|
| 694 |
|
|---|
| 695 | @Override
|
|---|
| 696 | public void nodeMoved(NodeMovedEvent event) {
|
|---|
| 697 | /* irrelevant in this context */
|
|---|
| 698 | }
|
|---|
| 699 |
|
|---|
| 700 | @Override
|
|---|
| 701 | public void wayNodesChanged(WayNodesChangedEvent event) {
|
|---|
| 702 | /* irrelevant in this context */
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | @Override
|
|---|
| 706 | public void primitivesAdded(final PrimitivesAddedEvent event) {
|
|---|
| 707 | model.addRelations(event.getPrimitives());
|
|---|
| 708 | model.updateTitle();
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | @Override
|
|---|
| 712 | public void primitivesRemoved(final PrimitivesRemovedEvent event) {
|
|---|
| 713 | model.removeRelations(event.getPrimitives());
|
|---|
| 714 | model.updateTitle();
|
|---|
| 715 | }
|
|---|
| 716 |
|
|---|
| 717 | @Override
|
|---|
| 718 | public void relationMembersChanged(final RelationMembersChangedEvent event) {
|
|---|
| 719 | List<IRelation<?>> sel = model.getSelectedRelations();
|
|---|
| 720 | model.sort();
|
|---|
| 721 | model.setSelectedRelations(sel);
|
|---|
| 722 | displaylist.repaint();
|
|---|
| 723 | }
|
|---|
| 724 |
|
|---|
| 725 | @Override
|
|---|
| 726 | public void tagsChanged(TagsChangedEvent event) {
|
|---|
| 727 | OsmPrimitive prim = event.getPrimitive();
|
|---|
| 728 | if (!(prim instanceof Relation))
|
|---|
| 729 | return;
|
|---|
| 730 | // trigger a sort of the relation list because the display name may have changed
|
|---|
| 731 | List<IRelation<?>> sel = model.getSelectedRelations();
|
|---|
| 732 | model.sort();
|
|---|
| 733 | model.setSelectedRelations(sel);
|
|---|
| 734 | displaylist.repaint();
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | @Override
|
|---|
| 738 | public void dataChanged(DataChangedEvent event) {
|
|---|
| 739 | // I have no clue how it would be empty, but just in case use the original code.
|
|---|
| 740 | // {@code null} is used during initialization
|
|---|
| 741 | if (event == null || Utils.isEmpty(event.getEvents())) {
|
|---|
| 742 | initFromData(MainApplication.getLayerManager().getActiveData());
|
|---|
| 743 | } else {
|
|---|
| 744 | dataChangedIndividualEvents(event);
|
|---|
| 745 | }
|
|---|
| 746 | }
|
|---|
| 747 |
|
|---|
| 748 | @Override
|
|---|
| 749 | public void otherDatasetChange(AbstractDatasetChangedEvent event) {
|
|---|
| 750 | if (event.getType() == DatasetEventType.PRIMITIVE_FLAGS_CHANGED
|
|---|
| 751 | && event.getPrimitives().stream().anyMatch(Relation.class::isInstance)) {
|
|---|
| 752 | initFromData(MainApplication.getLayerManager().getActiveData());
|
|---|
| 753 | }
|
|---|
| 754 | }
|
|---|
| 755 |
|
|---|
| 756 | @Override
|
|---|
| 757 | public void zoomChanged() {
|
|---|
| 758 | // re-filter relations
|
|---|
| 759 | if (model.filter != null) {
|
|---|
| 760 | model.setFilter(model.filter);
|
|---|
| 761 | }
|
|---|
| 762 | }
|
|---|
| 763 | }
|
|---|