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