[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;
|
---|
[4070] | 64 | import org.openstreetmap.josm.gui.MapView.LayerChangeListener;
|
---|
[9264] | 65 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
[626] | 66 | import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
|
---|
[5821] | 67 | import org.openstreetmap.josm.gui.PopupMenuHandler;
|
---|
[2710] | 68 | import org.openstreetmap.josm.gui.SideButton;
|
---|
[1599] | 69 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
[626] | 70 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 71 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[7547] | 72 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
[6025] | 73 | import org.openstreetmap.josm.gui.util.HighlightHelper;
|
---|
[8979] | 74 | import org.openstreetmap.josm.gui.widgets.CompileSearchTextDecorator;
|
---|
[5696] | 75 | import org.openstreetmap.josm.gui.widgets.DisableShortcutsOnFocusGainedTextField;
|
---|
[5958] | 76 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
| 77 | import org.openstreetmap.josm.gui.widgets.PopupMenuLauncher;
|
---|
[1778] | 78 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[5200] | 79 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
[5616] | 80 | import org.openstreetmap.josm.tools.Predicate;
|
---|
[1084] | 81 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[5616] | 82 | import org.openstreetmap.josm.tools.Utils;
|
---|
[626] | 83 |
|
---|
| 84 | /**
|
---|
| 85 | * A dialog showing all known relations, with buttons to add, edit, and
|
---|
[1023] | 86 | * delete them.
|
---|
| 87 | *
|
---|
[1554] | 88 | * We don't have such dialogs for nodes, segments, and ways, because those
|
---|
[626] | 89 | * objects are visible on the map and can be selected there. Relations are not.
|
---|
| 90 | */
|
---|
[9365] | 91 | public class RelationListDialog extends ToggleDialog
|
---|
| 92 | implements DataSetListener, NavigatableComponent.ZoomChangeListener, ExpertToggleAction.ExpertModeChangeListener {
|
---|
[2126] | 93 | /** The display list. */
|
---|
[7001] | 94 | private final JList<Relation> displaylist;
|
---|
[2126] | 95 | /** the list model used */
|
---|
[5725] | 96 | private final RelationListModel model;
|
---|
[626] | 97 |
|
---|
[5725] | 98 | private final NewAction newAction;
|
---|
[6070] | 99 |
|
---|
[5821] | 100 | /** the popup menu and its handler */
|
---|
| 101 | private final JPopupMenu popupMenu = new JPopupMenu();
|
---|
[8308] | 102 | private final transient PopupMenuHandler popupMenuHandler = new PopupMenuHandler(popupMenu);
|
---|
[1677] | 103 |
|
---|
[5886] | 104 | private final JosmTextField filter;
|
---|
[6070] | 105 |
|
---|
[5793] | 106 | // Actions
|
---|
| 107 | /** the edit action */
|
---|
[5799] | 108 | private final EditRelationAction editAction = new EditRelationAction();
|
---|
| 109 | /** the delete action */
|
---|
| 110 | private final DeleteRelationsAction deleteRelationsAction = new DeleteRelationsAction();
|
---|
| 111 | /** the duplicate action */
|
---|
| 112 | private final DuplicateRelationAction duplicateAction = new DuplicateRelationAction();
|
---|
[5793] | 113 | private final DownloadMembersAction downloadMembersAction = new DownloadMembersAction();
|
---|
[8540] | 114 | private final DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction =
|
---|
| 115 | new DownloadSelectedIncompleteMembersAction();
|
---|
[5821] | 116 | private final SelectMembersAction selectMembersAction = new SelectMembersAction(false);
|
---|
[5793] | 117 | private final SelectMembersAction addMembersToSelectionAction = new SelectMembersAction(true);
|
---|
| 118 | private final SelectRelationAction selectRelationAction = new SelectRelationAction(false);
|
---|
| 119 | private final SelectRelationAction addRelationToSelectionAction = new SelectRelationAction(true);
|
---|
[5821] | 120 | /** add all selected primitives to the given relations */
|
---|
[5799] | 121 | private final AddSelectionToRelations addSelectionToRelations = new AddSelectionToRelations();
|
---|
[9365] | 122 | private transient JMenuItem addSelectionToRelationMenuItem;
|
---|
[6070] | 123 |
|
---|
[8308] | 124 | private final transient HighlightHelper highlightHelper = new HighlightHelper();
|
---|
[9078] | 125 | private final boolean highlightEnabled = Main.pref.getBoolean("draw.target-highlight", true);
|
---|
[9668] | 126 | private RecentRelationsAction recentRelationsAction;
|
---|
[7539] | 127 |
|
---|
[1778] | 128 | /**
|
---|
[5799] | 129 | * Constructs <code>RelationListDialog</code>
|
---|
[1778] | 130 | */
|
---|
[1169] | 131 | public RelationListDialog() {
|
---|
| 132 | super(tr("Relations"), "relationlist", tr("Open a list of all relations."),
|
---|
[4957] | 133 | Shortcut.registerShortcut("subwindow:relations", tr("Toggle: {0}", tr("Relations")),
|
---|
[6726] | 134 | KeyEvent.VK_R, Shortcut.ALT_SHIFT), 150, true);
|
---|
[1778] | 135 |
|
---|
| 136 | // create the list of relations
|
---|
| 137 | //
|
---|
[2317] | 138 | DefaultListSelectionModel selectionModel = new DefaultListSelectionModel();
|
---|
| 139 | model = new RelationListModel(selectionModel);
|
---|
[7005] | 140 | displaylist = new JList<>(model);
|
---|
[2317] | 141 | displaylist.setSelectionModel(selectionModel);
|
---|
[2885] | 142 | displaylist.setCellRenderer(new OsmPrimitivRenderer() {
|
---|
| 143 | /**
|
---|
| 144 | * Don't show the default tooltip in the relation list.
|
---|
| 145 | */
|
---|
| 146 | @Override
|
---|
| 147 | protected String getComponentToolTipText(OsmPrimitive value) {
|
---|
| 148 | return null;
|
---|
| 149 | }
|
---|
| 150 | });
|
---|
[2194] | 151 | displaylist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
[2317] | 152 | displaylist.addMouseListener(new MouseEventHandler());
|
---|
[626] | 153 |
|
---|
[1866] | 154 | // the new action
|
---|
| 155 | //
|
---|
[2485] | 156 | newAction = new NewAction();
|
---|
[1866] | 157 |
|
---|
[5793] | 158 | filter = setupFilter();
|
---|
[5616] | 159 |
|
---|
[5793] | 160 | displaylist.addListSelectionListener(new ListSelectionListener() {
|
---|
[5616] | 161 | @Override
|
---|
[5793] | 162 | public void valueChanged(ListSelectionEvent e) {
|
---|
[5799] | 163 | updateActionsRelationLists();
|
---|
[5616] | 164 | }
|
---|
| 165 | });
|
---|
[5821] | 166 |
|
---|
| 167 | // Setup popup menu handler
|
---|
| 168 | setupPopupMenuHandler();
|
---|
[6070] | 169 |
|
---|
[5616] | 170 | JPanel pane = new JPanel(new BorderLayout());
|
---|
| 171 | pane.add(filter, BorderLayout.NORTH);
|
---|
| 172 | pane.add(new JScrollPane(displaylist), BorderLayout.CENTER);
|
---|
[9668] | 173 |
|
---|
| 174 | SideButton editButton = new SideButton(editAction, false);
|
---|
| 175 | recentRelationsAction = new RecentRelationsAction(editButton);
|
---|
| 176 |
|
---|
[5616] | 177 | createLayout(pane, false, Arrays.asList(new SideButton[]{
|
---|
[5028] | 178 | new SideButton(newAction, false),
|
---|
[9668] | 179 | editButton,
|
---|
[5028] | 180 | new SideButton(duplicateAction, false),
|
---|
[5799] | 181 | new SideButton(deleteRelationsAction, false),
|
---|
[5793] | 182 | new SideButton(selectRelationAction, false)
|
---|
[4353] | 183 | }));
|
---|
[2487] | 184 |
|
---|
[5200] | 185 | InputMapUtils.unassignCtrlShiftUpDown(displaylist, JComponent.WHEN_FOCUSED);
|
---|
[6070] | 186 |
|
---|
[7547] | 187 | // Select relation on Enter
|
---|
[5793] | 188 | InputMapUtils.addEnterAction(displaylist, selectRelationAction);
|
---|
[5200] | 189 |
|
---|
| 190 | // Edit relation on Ctrl-Enter
|
---|
| 191 | displaylist.getActionMap().put("edit", editAction);
|
---|
| 192 | displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK), "edit");
|
---|
[6070] | 193 |
|
---|
[7547] | 194 | // Do not hide copy action because of default JList override (fix #9815)
|
---|
| 195 | displaylist.getActionMap().put("copy", Main.main.menu.copy);
|
---|
| 196 | displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_C, GuiHelper.getMenuShortcutKeyMaskEx()), "copy");
|
---|
| 197 |
|
---|
[5799] | 198 | updateActionsRelationLists();
|
---|
[2485] | 199 | }
|
---|
[6070] | 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() {
|
---|
[2621] | 222 | MapView.addLayerChangeListener(newAction);
|
---|
[9264] | 223 | MapView.addZoomChangeListener(this);
|
---|
[2691] | 224 | newAction.updateEnabledState();
|
---|
[2741] | 225 | DatasetEventManager.getInstance().addDatasetListener(this, FireMode.IN_EDT);
|
---|
[5799] | 226 | DataSet.addSelectionListener(addSelectionToRelations);
|
---|
[2623] | 227 | dataChanged(null);
|
---|
[9365] | 228 | ExpertToggleAction.addExpertModeChangeListener(this);
|
---|
| 229 | expertChanged(ExpertToggleAction.isExpert());
|
---|
[1169] | 230 | }
|
---|
[626] | 231 |
|
---|
[7539] | 232 | @Override
|
---|
| 233 | public void hideNotify() {
|
---|
[2621] | 234 | MapView.removeLayerChangeListener(newAction);
|
---|
[9264] | 235 | MapView.removeZoomChangeListener(this);
|
---|
[2622] | 236 | DatasetEventManager.getInstance().removeDatasetListener(this);
|
---|
[5799] | 237 | DataSet.removeSelectionListener(addSelectionToRelations);
|
---|
[9365] | 238 | ExpertToggleAction.removeExpertModeChangeListener(this);
|
---|
[1169] | 239 | }
|
---|
[6070] | 240 |
|
---|
[5725] | 241 | private void resetFilter() {
|
---|
| 242 | filter.setText(null);
|
---|
| 243 | }
|
---|
[1023] | 244 |
|
---|
[2126] | 245 | /**
|
---|
[2487] | 246 | * Initializes the relation list dialog from a layer. If <code>layer</code> is null
|
---|
[5266] | 247 | * or if it isn't an {@link OsmDataLayer} the dialog is reset to an empty dialog.
|
---|
[2487] | 248 | * Otherwise it is initialized with the list of non-deleted and visible relations
|
---|
| 249 | * in the layer's dataset.
|
---|
[2512] | 250 | *
|
---|
[2487] | 251 | * @param layer the layer. May be null.
|
---|
[2126] | 252 | */
|
---|
[2487] | 253 | protected void initFromLayer(Layer layer) {
|
---|
[6105] | 254 | if (!(layer instanceof OsmDataLayer)) {
|
---|
[2126] | 255 | model.setRelations(null);
|
---|
[1897] | 256 | return;
|
---|
| 257 | }
|
---|
[8510] | 258 | OsmDataLayer l = (OsmDataLayer) layer;
|
---|
[2487] | 259 | model.setRelations(l.data.getRelations());
|
---|
[3020] | 260 | model.updateTitle();
|
---|
[5799] | 261 | updateActionsRelationLists();
|
---|
[1169] | 262 | }
|
---|
[1023] | 263 |
|
---|
[1169] | 264 | /**
|
---|
[1419] | 265 | * @return The selected relation in the list
|
---|
| 266 | */
|
---|
| 267 | private Relation getSelected() {
|
---|
[7001] | 268 | if (model.getSize() == 1) {
|
---|
[1419] | 269 | displaylist.setSelectedIndex(0);
|
---|
[1763] | 270 | }
|
---|
[7001] | 271 | return displaylist.getSelectedValue();
|
---|
[1419] | 272 | }
|
---|
[1763] | 273 |
|
---|
| 274 | /**
|
---|
| 275 | * Selects the relation <code>relation</code> in the list of relations.
|
---|
[1897] | 276 | *
|
---|
[1763] | 277 | * @param relation the relation
|
---|
| 278 | */
|
---|
| 279 | public void selectRelation(Relation relation) {
|
---|
[4795] | 280 | selectRelations(Collections.singleton(relation));
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | /**
|
---|
| 284 | * Selects the relations in the list of relations.
|
---|
| 285 | * @param relations the relations to be selected
|
---|
| 286 | */
|
---|
| 287 | public void selectRelations(Collection<Relation> relations) {
|
---|
| 288 | if (relations == null || relations.isEmpty()) {
|
---|
[2487] | 289 | model.setSelectedRelations(null);
|
---|
[2126] | 290 | } else {
|
---|
[4795] | 291 | model.setSelectedRelations(relations);
|
---|
[5725] | 292 | Integer i = model.getVisibleRelationIndex(relations.iterator().next());
|
---|
[8509] | 293 | if (i != null) {
|
---|
| 294 | // Not all relations have to be in the list
|
---|
| 295 | // (for example when the relation list is hidden, it's not updated with new relations)
|
---|
[2914] | 296 | displaylist.scrollRectToVisible(displaylist.getCellBounds(i, i));
|
---|
| 297 | }
|
---|
[1780] | 298 | }
|
---|
[1763] | 299 | }
|
---|
[1778] | 300 |
|
---|
[5886] | 301 | private JosmTextField setupFilter() {
|
---|
| 302 | final JosmTextField f = new DisableShortcutsOnFocusGainedTextField();
|
---|
[5793] | 303 | f.setToolTipText(tr("Relation list filter"));
|
---|
[8979] | 304 | final CompileSearchTextDecorator decorator = CompileSearchTextDecorator.decorate(f);
|
---|
| 305 | f.addPropertyChangeListener("filter", new PropertyChangeListener() {
|
---|
[5793] | 306 | @Override
|
---|
[8979] | 307 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
| 308 | model.setFilter(decorator.getMatch());
|
---|
[5793] | 309 | }
|
---|
| 310 | });
|
---|
| 311 | return f;
|
---|
| 312 | }
|
---|
| 313 |
|
---|
[5958] | 314 | class MouseEventHandler extends PopupMenuLauncher {
|
---|
[6070] | 315 |
|
---|
[8836] | 316 | MouseEventHandler() {
|
---|
[5958] | 317 | super(popupMenu);
|
---|
| 318 | }
|
---|
[6025] | 319 |
|
---|
| 320 | @Override
|
---|
| 321 | public void mouseExited(MouseEvent me) {
|
---|
[6058] | 322 | if (highlightEnabled) highlightHelper.clear();
|
---|
[6025] | 323 | }
|
---|
[6070] | 324 |
|
---|
[1916] | 325 | protected void setCurrentRelationAsSelection() {
|
---|
[7021] | 326 | Main.main.getCurrentDataSet().setSelected(displaylist.getSelectedValue());
|
---|
[1916] | 327 | }
|
---|
| 328 |
|
---|
| 329 | protected void editCurrentRelation() {
|
---|
[5793] | 330 | EditRelationAction.launchEditor(getSelected());
|
---|
[1916] | 331 | }
|
---|
[6070] | 332 |
|
---|
[7539] | 333 | @Override
|
---|
| 334 | public void mouseClicked(MouseEvent e) {
|
---|
[6336] | 335 | if (!Main.main.hasEditLayer()) return;
|
---|
[5958] | 336 | if (isDoubleClick(e)) {
|
---|
[1916] | 337 | if (e.isControlDown()) {
|
---|
| 338 | editCurrentRelation();
|
---|
| 339 | } else {
|
---|
| 340 | setCurrentRelationAsSelection();
|
---|
| 341 | }
|
---|
| 342 | }
|
---|
| 343 | }
|
---|
| 344 | }
|
---|
[6070] | 345 |
|
---|
[1778] | 346 | /**
|
---|
[2487] | 347 | * The action for creating a new relation
|
---|
[1866] | 348 | *
|
---|
| 349 | */
|
---|
[9059] | 350 | static class NewAction extends AbstractAction implements LayerChangeListener {
|
---|
[8836] | 351 | NewAction() {
|
---|
[8510] | 352 | putValue(SHORT_DESCRIPTION, tr("Create a new relation"));
|
---|
[5028] | 353 | putValue(NAME, tr("New"));
|
---|
[1866] | 354 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "addrelation"));
|
---|
[2632] | 355 | updateEnabledState();
|
---|
[1866] | 356 | }
|
---|
| 357 |
|
---|
| 358 | public void run() {
|
---|
[8510] | 359 | RelationEditor.getEditor(Main.main.getEditLayer(), null, null).setVisible(true);
|
---|
[1866] | 360 | }
|
---|
| 361 |
|
---|
[5799] | 362 | @Override
|
---|
[1866] | 363 | public void actionPerformed(ActionEvent e) {
|
---|
| 364 | run();
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | protected void updateEnabledState() {
|
---|
[6336] | 368 | setEnabled(Main.main != null && Main.main.hasEditLayer());
|
---|
[1866] | 369 | }
|
---|
| 370 |
|
---|
[5799] | 371 | @Override
|
---|
[1866] | 372 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
| 373 | updateEnabledState();
|
---|
[7001] | 374 | }
|
---|
[1866] | 375 |
|
---|
[5799] | 376 | @Override
|
---|
[1866] | 377 | public void layerAdded(Layer newLayer) {
|
---|
| 378 | updateEnabledState();
|
---|
| 379 | }
|
---|
| 380 |
|
---|
[5799] | 381 | @Override
|
---|
[1866] | 382 | public void layerRemoved(Layer oldLayer) {
|
---|
| 383 | updateEnabledState();
|
---|
| 384 | }
|
---|
| 385 | }
|
---|
[1916] | 386 |
|
---|
| 387 | /**
|
---|
[7001] | 388 | * The list model for the list of relations displayed in the relation list dialog.
|
---|
[2487] | 389 | *
|
---|
| 390 | */
|
---|
[7001] | 391 | private class RelationListModel extends AbstractListModel<Relation> {
|
---|
[8308] | 392 | private final transient List<Relation> relations = new ArrayList<>();
|
---|
| 393 | private transient List<Relation> filteredRelations;
|
---|
[9078] | 394 | private final DefaultListSelectionModel selectionModel;
|
---|
[8308] | 395 | private transient SearchCompiler.Match filter;
|
---|
[2126] | 396 |
|
---|
[8836] | 397 | RelationListModel(DefaultListSelectionModel selectionModel) {
|
---|
[2317] | 398 | this.selectionModel = selectionModel;
|
---|
| 399 | }
|
---|
| 400 |
|
---|
[2741] | 401 | public void sort() {
|
---|
[2623] | 402 | Collections.sort(
|
---|
| 403 | relations,
|
---|
[4070] | 404 | DefaultNameFormatter.getInstance().getRelationComparator()
|
---|
[5028] | 405 | );
|
---|
[2623] | 406 | }
|
---|
| 407 |
|
---|
| 408 | private boolean isValid(Relation r) {
|
---|
| 409 | return !r.isDeleted() && r.isVisible() && !r.isIncomplete();
|
---|
| 410 | }
|
---|
| 411 |
|
---|
[2741] | 412 | public void setRelations(Collection<Relation> relations) {
|
---|
[2488] | 413 | List<Relation> sel = getSelectedRelations();
|
---|
[2504] | 414 | this.relations.clear();
|
---|
[5619] | 415 | this.filteredRelations = null;
|
---|
[2506] | 416 | if (relations == null) {
|
---|
| 417 | selectionModel.clearSelection();
|
---|
[8510] | 418 | fireContentsChanged(this, 0, getSize());
|
---|
[2506] | 419 | return;
|
---|
| 420 | }
|
---|
[2504] | 421 | for (Relation r: relations) {
|
---|
[2623] | 422 | if (isValid(r)) {
|
---|
[2504] | 423 | this.relations.add(r);
|
---|
[2487] | 424 | }
|
---|
| 425 | }
|
---|
| 426 | sort();
|
---|
[5744] | 427 | updateFilteredRelations();
|
---|
[2126] | 428 | fireIntervalAdded(this, 0, getSize());
|
---|
[2488] | 429 | setSelectedRelations(sel);
|
---|
[2126] | 430 | }
|
---|
| 431 |
|
---|
[2487] | 432 | /**
|
---|
| 433 | * Add all relations in <code>addedPrimitives</code> to the model for the
|
---|
| 434 | * relation list dialog
|
---|
[2512] | 435 | *
|
---|
[2487] | 436 | * @param addedPrimitives the collection of added primitives. May include nodes,
|
---|
| 437 | * ways, and relations.
|
---|
| 438 | */
|
---|
[2741] | 439 | public void addRelations(Collection<? extends OsmPrimitive> addedPrimitives) {
|
---|
[2484] | 440 | boolean added = false;
|
---|
| 441 | for (OsmPrimitive p: addedPrimitives) {
|
---|
[8444] | 442 | if (!(p instanceof Relation)) {
|
---|
[2484] | 443 | continue;
|
---|
| 444 | }
|
---|
[2623] | 445 |
|
---|
[8510] | 446 | Relation r = (Relation) p;
|
---|
[2623] | 447 | if (relations.contains(r)) {
|
---|
[2484] | 448 | continue;
|
---|
| 449 | }
|
---|
[2623] | 450 | if (isValid(r)) {
|
---|
| 451 | relations.add(r);
|
---|
| 452 | added = true;
|
---|
| 453 | }
|
---|
[2484] | 454 | }
|
---|
| 455 | if (added) {
|
---|
[2487] | 456 | List<Relation> sel = getSelectedRelations();
|
---|
| 457 | sort();
|
---|
[5744] | 458 | updateFilteredRelations();
|
---|
[2484] | 459 | fireIntervalAdded(this, 0, getSize());
|
---|
[2487] | 460 | setSelectedRelations(sel);
|
---|
[2484] | 461 | }
|
---|
| 462 | }
|
---|
| 463 |
|
---|
[2487] | 464 | /**
|
---|
| 465 | * Removes all relations in <code>removedPrimitives</code> from the model
|
---|
[2512] | 466 | *
|
---|
[2487] | 467 | * @param removedPrimitives the removed primitives. May include nodes, ways,
|
---|
| 468 | * and relations
|
---|
| 469 | */
|
---|
[2741] | 470 | public void removeRelations(Collection<? extends OsmPrimitive> removedPrimitives) {
|
---|
[2487] | 471 | if (removedPrimitives == null) return;
|
---|
| 472 | // extract the removed relations
|
---|
| 473 | //
|
---|
[7005] | 474 | Set<Relation> removedRelations = new HashSet<>();
|
---|
[2487] | 475 | for (OsmPrimitive p: removedPrimitives) {
|
---|
[8444] | 476 | if (!(p instanceof Relation)) {
|
---|
[2487] | 477 | continue;
|
---|
| 478 | }
|
---|
[8510] | 479 | removedRelations.add((Relation) p);
|
---|
[2487] | 480 | }
|
---|
| 481 | if (removedRelations.isEmpty())
|
---|
| 482 | return;
|
---|
| 483 | int size = relations.size();
|
---|
| 484 | relations.removeAll(removedRelations);
|
---|
[5709] | 485 | if (filteredRelations != null) {
|
---|
| 486 | filteredRelations.removeAll(removedRelations);
|
---|
| 487 | }
|
---|
[2487] | 488 | if (size != relations.size()) {
|
---|
| 489 | List<Relation> sel = getSelectedRelations();
|
---|
| 490 | sort();
|
---|
| 491 | fireContentsChanged(this, 0, getSize());
|
---|
| 492 | setSelectedRelations(sel);
|
---|
| 493 | }
|
---|
| 494 | }
|
---|
[6070] | 495 |
|
---|
[5720] | 496 | private void updateFilteredRelations() {
|
---|
| 497 | if (filter != null) {
|
---|
[7005] | 498 | filteredRelations = new ArrayList<>(Utils.filter(relations, new Predicate<Relation>() {
|
---|
[5720] | 499 | @Override
|
---|
| 500 | public boolean evaluate(Relation r) {
|
---|
| 501 | return filter.match(r);
|
---|
| 502 | }
|
---|
| 503 | }));
|
---|
| 504 | } else if (filteredRelations != null) {
|
---|
| 505 | filteredRelations = null;
|
---|
| 506 | }
|
---|
| 507 | }
|
---|
[3102] | 508 |
|
---|
[5616] | 509 | public void setFilter(final SearchCompiler.Match filter) {
|
---|
[5720] | 510 | this.filter = filter;
|
---|
| 511 | updateFilteredRelations();
|
---|
[5616] | 512 | List<Relation> sel = getSelectedRelations();
|
---|
| 513 | fireContentsChanged(this, 0, getSize());
|
---|
| 514 | setSelectedRelations(sel);
|
---|
| 515 | updateTitle();
|
---|
| 516 | }
|
---|
| 517 |
|
---|
| 518 | private List<Relation> getVisibleRelations() {
|
---|
| 519 | return filteredRelations == null ? relations : filteredRelations;
|
---|
| 520 | }
|
---|
[6070] | 521 |
|
---|
[5709] | 522 | private Relation getVisibleRelation(int index) {
|
---|
| 523 | if (index < 0 || index >= getVisibleRelations().size()) return null;
|
---|
| 524 | return getVisibleRelations().get(index);
|
---|
| 525 | }
|
---|
[5616] | 526 |
|
---|
| 527 | @Override
|
---|
[7001] | 528 | public Relation getElementAt(int index) {
|
---|
[5709] | 529 | return getVisibleRelation(index);
|
---|
[2126] | 530 | }
|
---|
| 531 |
|
---|
[5616] | 532 | @Override
|
---|
[2126] | 533 | public int getSize() {
|
---|
[5616] | 534 | return getVisibleRelations().size();
|
---|
[2126] | 535 | }
|
---|
| 536 |
|
---|
[2317] | 537 | /**
|
---|
| 538 | * Replies the list of selected relations. Empty list,
|
---|
| 539 | * if there are no selected relations.
|
---|
[2512] | 540 | *
|
---|
[2317] | 541 | * @return the list of selected, non-new relations.
|
---|
| 542 | */
|
---|
| 543 | public List<Relation> getSelectedRelations() {
|
---|
[7005] | 544 | List<Relation> ret = new ArrayList<>();
|
---|
[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) {
|
---|
[2860] | 686 | initFromLayer(Main.main.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 | }
|
---|