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