[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 |
|
---|
| 172 | // activate DEL in the list of relations
|
---|
[4957] | 173 | //displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE,0), "deleteRelation");
|
---|
| 174 | //displaylist.getActionMap().put("deleteRelation", deleteAction);
|
---|
[1677] | 175 |
|
---|
[5200] | 176 | InputMapUtils.unassignCtrlShiftUpDown(displaylist, JComponent.WHEN_FOCUSED);
|
---|
[6070] | 177 |
|
---|
[5200] | 178 | // Select relation on Ctrl-Enter
|
---|
[5793] | 179 | InputMapUtils.addEnterAction(displaylist, selectRelationAction);
|
---|
[5200] | 180 |
|
---|
| 181 | // Edit relation on Ctrl-Enter
|
---|
| 182 | displaylist.getActionMap().put("edit", editAction);
|
---|
| 183 | displaylist.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, KeyEvent.CTRL_MASK), "edit");
|
---|
[6070] | 184 |
|
---|
[5799] | 185 | updateActionsRelationLists();
|
---|
[2485] | 186 | }
|
---|
[6070] | 187 |
|
---|
[5799] | 188 | // inform all actions about list of relations they need
|
---|
| 189 | private void updateActionsRelationLists() {
|
---|
[6025] | 190 | List<Relation> sel = model.getSelectedRelations();
|
---|
| 191 | popupMenuHandler.setPrimitives(sel);
|
---|
[6070] | 192 |
|
---|
[6038] | 193 | Component focused = FocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
|
---|
[6070] | 194 |
|
---|
[6025] | 195 | //update highlights
|
---|
[6058] | 196 | if (highlightEnabled && focused==displaylist && Main.isDisplayingMapView()) {
|
---|
[6038] | 197 | if (highlightHelper.highlightOnly(sel)) {
|
---|
| 198 | Main.map.mapView.repaint();
|
---|
| 199 | }
|
---|
[6025] | 200 | }
|
---|
[5799] | 201 | }
|
---|
[6070] | 202 |
|
---|
[2485] | 203 | @Override public void showNotify() {
|
---|
[2621] | 204 | MapView.addLayerChangeListener(newAction);
|
---|
[2691] | 205 | newAction.updateEnabledState();
|
---|
[2741] | 206 | DatasetEventManager.getInstance().addDatasetListener(this, FireMode.IN_EDT);
|
---|
[5799] | 207 | DataSet.addSelectionListener(addSelectionToRelations);
|
---|
[2623] | 208 | dataChanged(null);
|
---|
[1169] | 209 | }
|
---|
[626] | 210 |
|
---|
[2485] | 211 | @Override public void hideNotify() {
|
---|
[2621] | 212 | MapView.removeLayerChangeListener(newAction);
|
---|
[2622] | 213 | DatasetEventManager.getInstance().removeDatasetListener(this);
|
---|
[5799] | 214 | DataSet.removeSelectionListener(addSelectionToRelations);
|
---|
[1169] | 215 | }
|
---|
[6070] | 216 |
|
---|
[5725] | 217 | private void resetFilter() {
|
---|
| 218 | filter.setText(null);
|
---|
| 219 | }
|
---|
[1023] | 220 |
|
---|
[2126] | 221 | /**
|
---|
[2487] | 222 | * Initializes the relation list dialog from a layer. If <code>layer</code> is null
|
---|
[5266] | 223 | * or if it isn't an {@link OsmDataLayer} the dialog is reset to an empty dialog.
|
---|
[2487] | 224 | * Otherwise it is initialized with the list of non-deleted and visible relations
|
---|
| 225 | * in the layer's dataset.
|
---|
[2512] | 226 | *
|
---|
[2487] | 227 | * @param layer the layer. May be null.
|
---|
[2126] | 228 | */
|
---|
[2487] | 229 | protected void initFromLayer(Layer layer) {
|
---|
| 230 | if (layer == null || ! (layer instanceof OsmDataLayer)) {
|
---|
[2126] | 231 | model.setRelations(null);
|
---|
[1897] | 232 | return;
|
---|
| 233 | }
|
---|
[2487] | 234 | OsmDataLayer l = (OsmDataLayer)layer;
|
---|
| 235 | model.setRelations(l.data.getRelations());
|
---|
[3020] | 236 | model.updateTitle();
|
---|
[5799] | 237 | updateActionsRelationLists();
|
---|
[1169] | 238 | }
|
---|
[1023] | 239 |
|
---|
[1169] | 240 | /**
|
---|
[1419] | 241 | * @return The selected relation in the list
|
---|
| 242 | */
|
---|
| 243 | private Relation getSelected() {
|
---|
[2126] | 244 | if(model.getSize() == 1) {
|
---|
[1419] | 245 | displaylist.setSelectedIndex(0);
|
---|
[1763] | 246 | }
|
---|
[1419] | 247 | return (Relation) displaylist.getSelectedValue();
|
---|
| 248 | }
|
---|
[1763] | 249 |
|
---|
| 250 | /**
|
---|
| 251 | * Selects the relation <code>relation</code> in the list of relations.
|
---|
[1897] | 252 | *
|
---|
[1763] | 253 | * @param relation the relation
|
---|
| 254 | */
|
---|
| 255 | public void selectRelation(Relation relation) {
|
---|
[4795] | 256 | selectRelations(Collections.singleton(relation));
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | /**
|
---|
| 260 | * Selects the relations in the list of relations.
|
---|
| 261 | * @param relations the relations to be selected
|
---|
| 262 | */
|
---|
| 263 | public void selectRelations(Collection<Relation> relations) {
|
---|
| 264 | if (relations == null || relations.isEmpty()) {
|
---|
[2487] | 265 | model.setSelectedRelations(null);
|
---|
[2126] | 266 | } else {
|
---|
[4795] | 267 | model.setSelectedRelations(relations);
|
---|
[5725] | 268 | Integer i = model.getVisibleRelationIndex(relations.iterator().next());
|
---|
[2914] | 269 | 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)
|
---|
| 270 | displaylist.scrollRectToVisible(displaylist.getCellBounds(i, i));
|
---|
| 271 | }
|
---|
[1780] | 272 | }
|
---|
[1763] | 273 | }
|
---|
[1778] | 274 |
|
---|
[5886] | 275 | private JosmTextField setupFilter() {
|
---|
| 276 | final JosmTextField f = new DisableShortcutsOnFocusGainedTextField();
|
---|
[5793] | 277 | f.setToolTipText(tr("Relation list filter"));
|
---|
| 278 | f.getDocument().addDocumentListener(new DocumentListener() {
|
---|
| 279 |
|
---|
| 280 | private void setFilter() {
|
---|
| 281 | try {
|
---|
| 282 | f.setBackground(UIManager.getColor("TextField.background"));
|
---|
| 283 | f.setToolTipText(tr("Relation list filter"));
|
---|
| 284 | model.setFilter(SearchCompiler.compile(filter.getText(), false, false));
|
---|
| 285 | } catch (SearchCompiler.ParseError ex) {
|
---|
| 286 | f.setBackground(new Color(255, 224, 224));
|
---|
| 287 | f.setToolTipText(ex.getMessage());
|
---|
| 288 | model.setFilter(new SearchCompiler.Always());
|
---|
| 289 | }
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | @Override
|
---|
| 293 | public void insertUpdate(DocumentEvent e) {
|
---|
| 294 | setFilter();
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | @Override
|
---|
| 298 | public void removeUpdate(DocumentEvent e) {
|
---|
| 299 | setFilter();
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | @Override
|
---|
| 303 | public void changedUpdate(DocumentEvent e) {
|
---|
| 304 | setFilter();
|
---|
| 305 | }
|
---|
| 306 | });
|
---|
| 307 | return f;
|
---|
| 308 | }
|
---|
| 309 |
|
---|
[5958] | 310 | class MouseEventHandler extends PopupMenuLauncher {
|
---|
[6070] | 311 |
|
---|
[5958] | 312 | public MouseEventHandler() {
|
---|
| 313 | super(popupMenu);
|
---|
| 314 | }
|
---|
[6025] | 315 |
|
---|
| 316 | @Override
|
---|
| 317 | public void mouseExited(MouseEvent me) {
|
---|
[6058] | 318 | if (highlightEnabled) highlightHelper.clear();
|
---|
[6025] | 319 | }
|
---|
[6070] | 320 |
|
---|
[1916] | 321 | protected void setCurrentRelationAsSelection() {
|
---|
| 322 | Main.main.getCurrentDataSet().setSelected((Relation)displaylist.getSelectedValue());
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | protected void editCurrentRelation() {
|
---|
[5793] | 326 | EditRelationAction.launchEditor(getSelected());
|
---|
[1916] | 327 | }
|
---|
[6070] | 328 |
|
---|
[1916] | 329 | @Override public void mouseClicked(MouseEvent e) {
|
---|
[3102] | 330 | if (Main.main.getEditLayer() == null) return;
|
---|
[5958] | 331 | if (isDoubleClick(e)) {
|
---|
[1916] | 332 | if (e.isControlDown()) {
|
---|
| 333 | editCurrentRelation();
|
---|
| 334 | } else {
|
---|
| 335 | setCurrentRelationAsSelection();
|
---|
| 336 | }
|
---|
| 337 | }
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
[6070] | 340 |
|
---|
[1778] | 341 | /**
|
---|
[2487] | 342 | * The action for creating a new relation
|
---|
[1866] | 343 | *
|
---|
| 344 | */
|
---|
[2632] | 345 | static class NewAction extends AbstractAction implements LayerChangeListener{
|
---|
[1866] | 346 | public NewAction() {
|
---|
| 347 | putValue(SHORT_DESCRIPTION,tr("Create a new relation"));
|
---|
[5028] | 348 | putValue(NAME, tr("New"));
|
---|
[1866] | 349 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "addrelation"));
|
---|
[2632] | 350 | updateEnabledState();
|
---|
[1866] | 351 | }
|
---|
| 352 |
|
---|
| 353 | public void run() {
|
---|
[2487] | 354 | RelationEditor.getEditor(Main.main.getEditLayer(),null, null).setVisible(true);
|
---|
[1866] | 355 | }
|
---|
| 356 |
|
---|
[5799] | 357 | @Override
|
---|
[1866] | 358 | public void actionPerformed(ActionEvent e) {
|
---|
| 359 | run();
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | protected void updateEnabledState() {
|
---|
| 363 | setEnabled(Main.main != null && Main.main.getEditLayer() != null);
|
---|
| 364 | }
|
---|
| 365 |
|
---|
[5799] | 366 | @Override
|
---|
[1866] | 367 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
| 368 | updateEnabledState();
|
---|
[5799] | 369 | }
|
---|
[1866] | 370 |
|
---|
[5799] | 371 | @Override
|
---|
[1866] | 372 | public void layerAdded(Layer newLayer) {
|
---|
| 373 | updateEnabledState();
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[5799] | 376 | @Override
|
---|
[1866] | 377 | public void layerRemoved(Layer oldLayer) {
|
---|
| 378 | updateEnabledState();
|
---|
| 379 | }
|
---|
| 380 | }
|
---|
[1916] | 381 |
|
---|
| 382 | /**
|
---|
[2487] | 383 | * The list model for the list of relations displayed in the relation list
|
---|
| 384 | * dialog.
|
---|
| 385 | *
|
---|
| 386 | */
|
---|
[3020] | 387 | private class RelationListModel extends AbstractListModel {
|
---|
[2504] | 388 | private final ArrayList<Relation> relations = new ArrayList<Relation>();
|
---|
[5616] | 389 | private ArrayList<Relation> filteredRelations;
|
---|
[2317] | 390 | private DefaultListSelectionModel selectionModel;
|
---|
[5720] | 391 | private SearchCompiler.Match filter;
|
---|
[2126] | 392 |
|
---|
[2317] | 393 | public RelationListModel(DefaultListSelectionModel selectionModel) {
|
---|
| 394 | this.selectionModel = selectionModel;
|
---|
| 395 | }
|
---|
| 396 |
|
---|
[2194] | 397 | public Relation getRelation(int idx) {
|
---|
| 398 | return relations.get(idx);
|
---|
| 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();
|
---|
| 418 | fireContentsChanged(this,0,getSize());
|
---|
| 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) {
|
---|
| 442 | if (! (p instanceof Relation)) {
|
---|
| 443 | continue;
|
---|
| 444 | }
|
---|
[2623] | 445 |
|
---|
| 446 | Relation r = (Relation)p;
|
---|
| 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 | //
|
---|
| 474 | Set<Relation> removedRelations = new HashSet<Relation>();
|
---|
| 475 | for (OsmPrimitive p: removedPrimitives) {
|
---|
| 476 | if (! (p instanceof Relation)) {
|
---|
| 477 | continue;
|
---|
| 478 | }
|
---|
| 479 | removedRelations.add((Relation)p);
|
---|
| 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) {
|
---|
| 498 | filteredRelations = new ArrayList<Relation>(Utils.filter(relations, new Predicate<Relation>() {
|
---|
| 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
|
---|
[2126] | 528 | public Object 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() {
|
---|
| 544 | ArrayList<Relation> ret = new ArrayList<Relation>();
|
---|
| 545 | for (int i=0; i<getSize();i++) {
|
---|
| 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) {
|
---|
| 569 | selectionModel.addSelectionInterval(i,i);
|
---|
[2487] | 570 | }
|
---|
| 571 | }
|
---|
| 572 | }
|
---|
[2894] | 573 |
|
---|
| 574 | /**
|
---|
| 575 | * Returns the index of the relation
|
---|
[5709] | 576 | * @param rel The relation to look for
|
---|
[2894] | 577 | *
|
---|
[2914] | 578 | * @return index of relation (null if it cannot be found)
|
---|
[2894] | 579 | */
|
---|
| 580 | public Integer getRelationIndex(Relation rel) {
|
---|
| 581 | int i = relations.indexOf(rel);
|
---|
[2914] | 582 | if (i<0)
|
---|
[2894] | 583 | return null;
|
---|
| 584 | return i;
|
---|
| 585 | }
|
---|
[6070] | 586 |
|
---|
[5725] | 587 | private Integer getVisibleRelationIndex(Relation rel) {
|
---|
| 588 | int i = getVisibleRelations().indexOf(rel);
|
---|
| 589 | if (i<0)
|
---|
| 590 | return null;
|
---|
| 591 | return i;
|
---|
| 592 | }
|
---|
[2914] | 593 |
|
---|
[3020] | 594 | public void updateTitle() {
|
---|
[5616] | 595 | if (relations.size() > 0 && relations.size() != getSize()) {
|
---|
| 596 | RelationListDialog.this.setTitle(tr("Relations: {0}/{1}", getSize(), relations.size()));
|
---|
| 597 | } else if (getSize() > 0) {
|
---|
[3020] | 598 | RelationListDialog.this.setTitle(tr("Relations: {0}", getSize()));
|
---|
| 599 | } else {
|
---|
| 600 | RelationListDialog.this.setTitle(tr("Relations"));
|
---|
| 601 | }
|
---|
| 602 | }
|
---|
[2126] | 603 | }
|
---|
[2317] | 604 |
|
---|
[5821] | 605 | private final void setupPopupMenuHandler() {
|
---|
[6070] | 606 |
|
---|
[5825] | 607 | // -- select action
|
---|
| 608 | popupMenuHandler.addAction(selectRelationAction);
|
---|
| 609 | popupMenuHandler.addAction(addRelationToSelectionAction);
|
---|
| 610 |
|
---|
| 611 | // -- select members action
|
---|
| 612 | popupMenuHandler.addAction(selectMembersAction);
|
---|
| 613 | popupMenuHandler.addAction(addMembersToSelectionAction);
|
---|
| 614 |
|
---|
| 615 | popupMenuHandler.addSeparator();
|
---|
[5821] | 616 | // -- download members action
|
---|
| 617 | popupMenuHandler.addAction(downloadMembersAction);
|
---|
[4459] | 618 |
|
---|
[5821] | 619 | // -- download incomplete members action
|
---|
| 620 | popupMenuHandler.addAction(downloadSelectedIncompleteMembersAction);
|
---|
[2317] | 621 |
|
---|
[5821] | 622 | popupMenuHandler.addSeparator();
|
---|
[5825] | 623 | popupMenuHandler.addAction(editAction).setVisible(false);
|
---|
| 624 | popupMenuHandler.addAction(duplicateAction).setVisible(false);
|
---|
| 625 | popupMenuHandler.addAction(deleteRelationsAction).setVisible(false);
|
---|
[6070] | 626 |
|
---|
[5821] | 627 | popupMenuHandler.addAction(addSelectionToRelations);
|
---|
[4459] | 628 | }
|
---|
[6070] | 629 |
|
---|
[5799] | 630 | /* ---------------------------------------------------------------------------------- */
|
---|
[5821] | 631 | /* Methods that can be called from plugins */
|
---|
[5799] | 632 | /* ---------------------------------------------------------------------------------- */
|
---|
| 633 |
|
---|
[5821] | 634 | /**
|
---|
| 635 | * Replies the popup menu handler.
|
---|
| 636 | * @return The popup menu handler
|
---|
| 637 | */
|
---|
| 638 | public PopupMenuHandler getPopupMenuHandler() {
|
---|
| 639 | return popupMenuHandler;
|
---|
[2317] | 640 | }
|
---|
[2439] | 641 |
|
---|
[4536] | 642 | public Collection<Relation> getSelectedRelations() {
|
---|
| 643 | return model.getSelectedRelations();
|
---|
| 644 | }
|
---|
| 645 |
|
---|
[2487] | 646 | /* ---------------------------------------------------------------------------------- */
|
---|
| 647 | /* DataSetListener */
|
---|
| 648 | /* ---------------------------------------------------------------------------------- */
|
---|
| 649 |
|
---|
[5799] | 650 | @Override
|
---|
[2622] | 651 | public void nodeMoved(NodeMovedEvent event) {/* irrelevant in this context */}
|
---|
[2487] | 652 |
|
---|
[5799] | 653 | @Override
|
---|
[2622] | 654 | public void wayNodesChanged(WayNodesChangedEvent event) {/* irrelevant in this context */}
|
---|
[2487] | 655 |
|
---|
[5799] | 656 | @Override
|
---|
[4223] | 657 | public void primitivesAdded(final PrimitivesAddedEvent event) {
|
---|
[2622] | 658 | model.addRelations(event.getPrimitives());
|
---|
[3020] | 659 | model.updateTitle();
|
---|
[2439] | 660 | }
|
---|
| 661 |
|
---|
[5799] | 662 | @Override
|
---|
[4223] | 663 | public void primitivesRemoved(final PrimitivesRemovedEvent event) {
|
---|
[2622] | 664 | model.removeRelations(event.getPrimitives());
|
---|
[3020] | 665 | model.updateTitle();
|
---|
[2439] | 666 | }
|
---|
| 667 |
|
---|
[5799] | 668 | @Override
|
---|
[2622] | 669 | public void relationMembersChanged(final RelationMembersChangedEvent event) {
|
---|
| 670 | List<Relation> sel = model.getSelectedRelations();
|
---|
| 671 | model.sort();
|
---|
| 672 | model.setSelectedRelations(sel);
|
---|
| 673 | displaylist.repaint();
|
---|
[2439] | 674 | }
|
---|
| 675 |
|
---|
[5799] | 676 | @Override
|
---|
[2622] | 677 | public void tagsChanged(TagsChangedEvent event) {
|
---|
| 678 | OsmPrimitive prim = event.getPrimitive();
|
---|
[2487] | 679 | if (prim == null || ! (prim instanceof Relation))
|
---|
| 680 | return;
|
---|
[2622] | 681 | // trigger a sort of the relation list because the display name may
|
---|
| 682 | // have changed
|
---|
| 683 | //
|
---|
| 684 | List<Relation> sel = model.getSelectedRelations();
|
---|
| 685 | model.sort();
|
---|
| 686 | model.setSelectedRelations(sel);
|
---|
| 687 | displaylist.repaint();
|
---|
[2439] | 688 | }
|
---|
[2488] | 689 |
|
---|
[5799] | 690 | @Override
|
---|
[2622] | 691 | public void dataChanged(DataChangedEvent event) {
|
---|
[2860] | 692 | initFromLayer(Main.main.getEditLayer());
|
---|
[2497] | 693 | }
|
---|
[2655] | 694 |
|
---|
[5799] | 695 | @Override
|
---|
[2655] | 696 | public void otherDatasetChange(AbstractDatasetChangedEvent event) {/* ignore */}
|
---|
[626] | 697 | }
|
---|