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