1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.FlowLayout;
|
---|
11 | import java.awt.GridBagConstraints;
|
---|
12 | import java.awt.GridBagLayout;
|
---|
13 | import java.awt.Window;
|
---|
14 | import java.awt.datatransfer.Clipboard;
|
---|
15 | import java.awt.datatransfer.FlavorListener;
|
---|
16 | import java.awt.event.ActionEvent;
|
---|
17 | import java.awt.event.FocusAdapter;
|
---|
18 | import java.awt.event.FocusEvent;
|
---|
19 | import java.awt.event.InputEvent;
|
---|
20 | import java.awt.event.KeyEvent;
|
---|
21 | import java.awt.event.MouseAdapter;
|
---|
22 | import java.awt.event.MouseEvent;
|
---|
23 | import java.awt.event.WindowAdapter;
|
---|
24 | import java.awt.event.WindowEvent;
|
---|
25 | import java.util.ArrayList;
|
---|
26 | import java.util.Arrays;
|
---|
27 | import java.util.Collection;
|
---|
28 | import java.util.Collections;
|
---|
29 | import java.util.EnumSet;
|
---|
30 | import java.util.List;
|
---|
31 | import java.util.Set;
|
---|
32 | import java.util.stream.Collectors;
|
---|
33 |
|
---|
34 | import javax.swing.AbstractAction;
|
---|
35 | import javax.swing.BorderFactory;
|
---|
36 | import javax.swing.InputMap;
|
---|
37 | import javax.swing.JButton;
|
---|
38 | import javax.swing.JComponent;
|
---|
39 | import javax.swing.JLabel;
|
---|
40 | import javax.swing.JMenuItem;
|
---|
41 | import javax.swing.JOptionPane;
|
---|
42 | import javax.swing.JPanel;
|
---|
43 | import javax.swing.JRootPane;
|
---|
44 | import javax.swing.JScrollPane;
|
---|
45 | import javax.swing.JSplitPane;
|
---|
46 | import javax.swing.JTabbedPane;
|
---|
47 | import javax.swing.JTable;
|
---|
48 | import javax.swing.JToolBar;
|
---|
49 | import javax.swing.KeyStroke;
|
---|
50 | import javax.swing.event.TableModelListener;
|
---|
51 |
|
---|
52 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
53 | import org.openstreetmap.josm.command.ChangeMembersCommand;
|
---|
54 | import org.openstreetmap.josm.command.Command;
|
---|
55 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
---|
56 | import org.openstreetmap.josm.data.UndoRedoHandler.CommandQueueListener;
|
---|
57 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
58 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
59 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
60 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
61 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
62 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
63 | import org.openstreetmap.josm.data.validation.tests.RelationChecker;
|
---|
64 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
65 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
66 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
67 | import org.openstreetmap.josm.gui.ScrollViewport;
|
---|
68 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
69 | import org.openstreetmap.josm.gui.dialogs.relation.actions.AbstractRelationEditorAction;
|
---|
70 | import org.openstreetmap.josm.gui.dialogs.relation.actions.AddSelectedAfterSelection;
|
---|
71 | import org.openstreetmap.josm.gui.dialogs.relation.actions.AddSelectedAtEndAction;
|
---|
72 | import org.openstreetmap.josm.gui.dialogs.relation.actions.AddSelectedAtStartAction;
|
---|
73 | import org.openstreetmap.josm.gui.dialogs.relation.actions.AddSelectedBeforeSelection;
|
---|
74 | import org.openstreetmap.josm.gui.dialogs.relation.actions.ApplyAction;
|
---|
75 | import org.openstreetmap.josm.gui.dialogs.relation.actions.CancelAction;
|
---|
76 | import org.openstreetmap.josm.gui.dialogs.relation.actions.CopyMembersAction;
|
---|
77 | import org.openstreetmap.josm.gui.dialogs.relation.actions.DeleteCurrentRelationAction;
|
---|
78 | import org.openstreetmap.josm.gui.dialogs.relation.actions.DownloadIncompleteMembersAction;
|
---|
79 | import org.openstreetmap.josm.gui.dialogs.relation.actions.DownloadSelectedIncompleteMembersAction;
|
---|
80 | import org.openstreetmap.josm.gui.dialogs.relation.actions.DuplicateRelationAction;
|
---|
81 | import org.openstreetmap.josm.gui.dialogs.relation.actions.EditAction;
|
---|
82 | import org.openstreetmap.josm.gui.dialogs.relation.actions.IRelationEditorActionAccess;
|
---|
83 | import org.openstreetmap.josm.gui.dialogs.relation.actions.IRelationEditorActionGroup;
|
---|
84 | import org.openstreetmap.josm.gui.dialogs.relation.actions.MoveDownAction;
|
---|
85 | import org.openstreetmap.josm.gui.dialogs.relation.actions.MoveUpAction;
|
---|
86 | import org.openstreetmap.josm.gui.dialogs.relation.actions.OKAction;
|
---|
87 | import org.openstreetmap.josm.gui.dialogs.relation.actions.PasteMembersAction;
|
---|
88 | import org.openstreetmap.josm.gui.dialogs.relation.actions.RefreshAction;
|
---|
89 | import org.openstreetmap.josm.gui.dialogs.relation.actions.RemoveAction;
|
---|
90 | import org.openstreetmap.josm.gui.dialogs.relation.actions.RemoveSelectedAction;
|
---|
91 | import org.openstreetmap.josm.gui.dialogs.relation.actions.ReverseAction;
|
---|
92 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SelectAction;
|
---|
93 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SelectPrimitivesForSelectedMembersAction;
|
---|
94 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SelectedMembersForSelectionAction;
|
---|
95 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SetRoleAction;
|
---|
96 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SortAction;
|
---|
97 | import org.openstreetmap.josm.gui.dialogs.relation.actions.SortBelowAction;
|
---|
98 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
99 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
100 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
101 | import org.openstreetmap.josm.gui.tagging.TagEditorModel;
|
---|
102 | import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
|
---|
103 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
104 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
105 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionManager;
|
---|
106 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPreset;
|
---|
107 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetHandler;
|
---|
108 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetType;
|
---|
109 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
110 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
---|
111 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
112 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
113 | import org.openstreetmap.josm.tools.InputMapUtils;
|
---|
114 | import org.openstreetmap.josm.tools.Logging;
|
---|
115 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
116 | import org.openstreetmap.josm.tools.Utils;
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * This dialog is for editing relations.
|
---|
120 | * @since 343
|
---|
121 | */
|
---|
122 | public class GenericRelationEditor extends RelationEditor implements CommandQueueListener {
|
---|
123 | /** the tag table and its model */
|
---|
124 | private final TagEditorPanel tagEditorPanel;
|
---|
125 | private final ReferringRelationsBrowser referrerBrowser;
|
---|
126 |
|
---|
127 | /** the member table and its model */
|
---|
128 | private final MemberTable memberTable;
|
---|
129 | private final MemberTableModel memberTableModel;
|
---|
130 |
|
---|
131 | /** the selection table and its model */
|
---|
132 | private final SelectionTable selectionTable;
|
---|
133 | private final SelectionTableModel selectionTableModel;
|
---|
134 |
|
---|
135 | private final AutoCompletingTextField tfRole;
|
---|
136 | private final RelationEditorActionAccess actionAccess;
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * the menu item in the windows menu. Required to properly hide on dialog close.
|
---|
140 | */
|
---|
141 | private JMenuItem windowMenuItem;
|
---|
142 | /**
|
---|
143 | * Action for performing the {@link RefreshAction}
|
---|
144 | */
|
---|
145 | private final RefreshAction refreshAction;
|
---|
146 | /**
|
---|
147 | * Action for performing the {@link ApplyAction}
|
---|
148 | */
|
---|
149 | private final ApplyAction applyAction;
|
---|
150 | /**
|
---|
151 | * Action for performing the {@link SelectAction}
|
---|
152 | */
|
---|
153 | private final SelectAction selectAction;
|
---|
154 | /**
|
---|
155 | * Action for performing the {@link CancelAction}
|
---|
156 | */
|
---|
157 | private final CancelAction cancelAction;
|
---|
158 | /**
|
---|
159 | * Action for performing the {@link DeleteCurrentRelationAction}
|
---|
160 | */
|
---|
161 | private final DeleteCurrentRelationAction deleteAction;
|
---|
162 | /**
|
---|
163 | * A list of listeners that need to be notified on clipboard content changes.
|
---|
164 | */
|
---|
165 | private final ArrayList<FlavorListener> clipboardListeners = new ArrayList<>();
|
---|
166 |
|
---|
167 | private Component selectedTabPane;
|
---|
168 | private final JTabbedPane tabbedPane;
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Creates a new relation editor for the given relation. The relation will be saved if the user
|
---|
172 | * selects "ok" in the editor.
|
---|
173 | * <p>
|
---|
174 | * If no relation is given, will create an editor for a new relation.
|
---|
175 | *
|
---|
176 | * @param layer the {@link OsmDataLayer} the new or edited relation belongs to
|
---|
177 | * @param relation relation to edit, or null to create a new one.
|
---|
178 | * @param selectedMembers a collection of members which shall be selected initially
|
---|
179 | */
|
---|
180 | public GenericRelationEditor(OsmDataLayer layer, Relation relation, Collection<RelationMember> selectedMembers) {
|
---|
181 | super(layer, relation);
|
---|
182 |
|
---|
183 | setRememberWindowGeometry(getClass().getName() + ".geometry",
|
---|
184 | WindowGeometry.centerInWindow(MainApplication.getMainFrame(), new Dimension(700, 650)));
|
---|
185 |
|
---|
186 | final TaggingPresetHandler presetHandler = new TaggingPresetHandler() {
|
---|
187 |
|
---|
188 | @Override
|
---|
189 | public void updateTags(List<Tag> tags) {
|
---|
190 | tagEditorPanel.getModel().updateTags(tags);
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public Collection<OsmPrimitive> getSelection() {
|
---|
195 | // Creating a new relation will open the window. The relation, in that case, will be null.
|
---|
196 | if (getRelation() == null) {
|
---|
197 | Relation relation = new Relation();
|
---|
198 | tagEditorPanel.getModel().applyToPrimitive(relation);
|
---|
199 | memberTableModel.applyToRelation(relation);
|
---|
200 | return Collections.singletonList(relation);
|
---|
201 | }
|
---|
202 | return Collections.singletonList(getRelation());
|
---|
203 | }
|
---|
204 | };
|
---|
205 |
|
---|
206 | // init the various models
|
---|
207 | //
|
---|
208 | memberTableModel = new MemberTableModel(relation, getLayer(), presetHandler);
|
---|
209 | memberTableModel.register();
|
---|
210 | selectionTableModel = new SelectionTableModel(getLayer());
|
---|
211 | selectionTableModel.register();
|
---|
212 | ReferringRelationsBrowserModel referrerModel = new ReferringRelationsBrowserModel(relation);
|
---|
213 |
|
---|
214 | tagEditorPanel = new TagEditorPanel(relation, presetHandler);
|
---|
215 | populateModels(relation);
|
---|
216 | tagEditorPanel.getModel().ensureOneTag();
|
---|
217 |
|
---|
218 | // setting up the member table
|
---|
219 | memberTable = new MemberTable(getLayer(), getRelation(), memberTableModel);
|
---|
220 | memberTable.addMouseListener(new MemberTableDblClickAdapter());
|
---|
221 | memberTableModel.addMemberModelListener(memberTable);
|
---|
222 |
|
---|
223 | MemberRoleCellEditor ce = (MemberRoleCellEditor) memberTable.getColumnModel().getColumn(0).getCellEditor();
|
---|
224 | selectionTable = new SelectionTable(selectionTableModel, memberTableModel);
|
---|
225 | selectionTable.setRowHeight(ce.getEditor().getPreferredSize().height);
|
---|
226 |
|
---|
227 | LeftButtonToolbar leftButtonToolbar = new LeftButtonToolbar(new RelationEditorActionAccess());
|
---|
228 | tfRole = buildRoleTextField(this);
|
---|
229 |
|
---|
230 | JSplitPane pane = buildSplitPane(
|
---|
231 | buildTagEditorPanel(tagEditorPanel),
|
---|
232 | buildMemberEditorPanel(leftButtonToolbar, new RelationEditorActionAccess()),
|
---|
233 | this);
|
---|
234 | pane.setPreferredSize(new Dimension(100, 100));
|
---|
235 |
|
---|
236 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
237 | pnl.add(pane, BorderLayout.CENTER);
|
---|
238 | pnl.setBorder(BorderFactory.createRaisedBevelBorder());
|
---|
239 |
|
---|
240 | getContentPane().setLayout(new BorderLayout());
|
---|
241 | tabbedPane = new JTabbedPane();
|
---|
242 | tabbedPane.add(tr("Tags and Members"), pnl);
|
---|
243 | referrerBrowser = new ReferringRelationsBrowser(getLayer(), referrerModel);
|
---|
244 | tabbedPane.add(tr("Parent Relations"), referrerBrowser);
|
---|
245 | tabbedPane.add(tr("Child Relations"), new ChildRelationBrowser(getLayer(), relation));
|
---|
246 | selectedTabPane = tabbedPane.getSelectedComponent();
|
---|
247 | tabbedPane.addChangeListener(e -> {
|
---|
248 | JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
|
---|
249 | int index = sourceTabbedPane.getSelectedIndex();
|
---|
250 | String title = sourceTabbedPane.getTitleAt(index);
|
---|
251 | if (title.equals(tr("Parent Relations"))) {
|
---|
252 | referrerBrowser.init();
|
---|
253 | }
|
---|
254 | // see #20228
|
---|
255 | boolean selIsTagsAndMembers = sourceTabbedPane.getSelectedComponent() == pnl;
|
---|
256 | if (selectedTabPane == pnl && !selIsTagsAndMembers) {
|
---|
257 | unregisterMain();
|
---|
258 | } else if (selectedTabPane != pnl && selIsTagsAndMembers) {
|
---|
259 | registerMain();
|
---|
260 | }
|
---|
261 | selectedTabPane = sourceTabbedPane.getSelectedComponent();
|
---|
262 | });
|
---|
263 |
|
---|
264 | actionAccess = new RelationEditorActionAccess();
|
---|
265 |
|
---|
266 | refreshAction = new RefreshAction(actionAccess);
|
---|
267 | applyAction = new ApplyAction(actionAccess);
|
---|
268 | selectAction = new SelectAction(actionAccess);
|
---|
269 | // Action for performing the {@link DuplicateRelationAction}
|
---|
270 | final DuplicateRelationAction duplicateAction = new DuplicateRelationAction(actionAccess);
|
---|
271 | // Action for performing the {@link DeleteCurrentRelationAction}
|
---|
272 | deleteAction = new DeleteCurrentRelationAction(actionAccess);
|
---|
273 |
|
---|
274 | this.memberTableModel.addTableModelListener(applyAction);
|
---|
275 | this.tagEditorPanel.getModel().addTableModelListener(applyAction);
|
---|
276 |
|
---|
277 | addPropertyChangeListener(deleteAction);
|
---|
278 |
|
---|
279 | // Action for performing the {@link OKAction}
|
---|
280 | final OKAction okAction = new OKAction(actionAccess);
|
---|
281 | cancelAction = new CancelAction(actionAccess);
|
---|
282 |
|
---|
283 | getContentPane().add(buildToolBar(refreshAction, applyAction, selectAction, duplicateAction, deleteAction), BorderLayout.NORTH);
|
---|
284 | getContentPane().add(tabbedPane, BorderLayout.CENTER);
|
---|
285 | getContentPane().add(buildOkCancelButtonPanel(okAction, deleteAction, cancelAction), BorderLayout.SOUTH);
|
---|
286 |
|
---|
287 | setSize(findMaxDialogSize());
|
---|
288 |
|
---|
289 | setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
---|
290 | addWindowListener(
|
---|
291 | new WindowAdapter() {
|
---|
292 | @Override
|
---|
293 | public void windowOpened(WindowEvent e) {
|
---|
294 | cleanSelfReferences(memberTableModel, getRelation());
|
---|
295 | }
|
---|
296 |
|
---|
297 | @Override
|
---|
298 | public void windowClosing(WindowEvent e) {
|
---|
299 | cancel();
|
---|
300 | }
|
---|
301 | }
|
---|
302 | );
|
---|
303 | InputMapUtils.addCtrlEnterAction(getRootPane(), okAction);
|
---|
304 | // CHECKSTYLE.OFF: LineLength
|
---|
305 | registerCopyPasteAction(tagEditorPanel.getPasteAction(), "PASTE_TAGS",
|
---|
306 | Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")), KeyEvent.VK_V, Shortcut.CTRL_SHIFT).getKeyStroke(),
|
---|
307 | getRootPane(), memberTable, selectionTable);
|
---|
308 | // CHECKSTYLE.ON: LineLength
|
---|
309 |
|
---|
310 | KeyStroke key = Shortcut.getPasteKeyStroke();
|
---|
311 | if (key != null) {
|
---|
312 | // handle uncommon situation, that user has no keystroke assigned to paste
|
---|
313 | registerCopyPasteAction(new PasteMembersAction(actionAccess) {
|
---|
314 | private static final long serialVersionUID = 1L;
|
---|
315 |
|
---|
316 | @Override
|
---|
317 | public void actionPerformed(ActionEvent e) {
|
---|
318 | super.actionPerformed(e);
|
---|
319 | tfRole.requestFocusInWindow();
|
---|
320 | }
|
---|
321 | }, "PASTE_MEMBERS", key, getRootPane(), memberTable, selectionTable);
|
---|
322 | }
|
---|
323 | key = Shortcut.getCopyKeyStroke();
|
---|
324 | if (key != null) {
|
---|
325 | // handle uncommon situation, that user has no keystroke assigned to copy
|
---|
326 | registerCopyPasteAction(new CopyMembersAction(actionAccess),
|
---|
327 | "COPY_MEMBERS", key, getRootPane(), memberTable, selectionTable);
|
---|
328 | }
|
---|
329 | tagEditorPanel.setNextFocusComponent(memberTable);
|
---|
330 | selectionTable.setFocusable(false);
|
---|
331 | memberTableModel.setSelectedMembers(selectedMembers);
|
---|
332 | HelpUtil.setHelpContext(getRootPane(), ht("/Dialog/RelationEditor"));
|
---|
333 | UndoRedoHandler.getInstance().addCommandQueueListener(this);
|
---|
334 | }
|
---|
335 |
|
---|
336 | private void registerMain() {
|
---|
337 | selectionTableModel.register();
|
---|
338 | memberTableModel.register();
|
---|
339 | memberTable.registerListeners();
|
---|
340 | }
|
---|
341 |
|
---|
342 | private void unregisterMain() {
|
---|
343 | selectionTableModel.unregister();
|
---|
344 | memberTableModel.unregister();
|
---|
345 | memberTable.unregisterListeners();
|
---|
346 | }
|
---|
347 |
|
---|
348 | @Override
|
---|
349 | public void reloadDataFromRelation() {
|
---|
350 | setRelation(getRelation());
|
---|
351 | populateModels(getRelation());
|
---|
352 | refreshAction.updateEnabledState();
|
---|
353 | }
|
---|
354 |
|
---|
355 | private void populateModels(Relation relation) {
|
---|
356 | if (relation != null) {
|
---|
357 | tagEditorPanel.getModel().initFromPrimitive(relation);
|
---|
358 | memberTableModel.populate(relation);
|
---|
359 | if (!getLayer().data.getRelations().contains(relation)) {
|
---|
360 | // treat it as a new relation if it doesn't exist in the data set yet.
|
---|
361 | setRelation(null);
|
---|
362 | }
|
---|
363 | } else {
|
---|
364 | tagEditorPanel.getModel().clear();
|
---|
365 | memberTableModel.populate(null);
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Apply changes.
|
---|
371 | * @see ApplyAction
|
---|
372 | */
|
---|
373 | public void apply() {
|
---|
374 | applyAction.actionPerformed(null);
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Select relation.
|
---|
379 | * @see SelectAction
|
---|
380 | * @since 12933
|
---|
381 | */
|
---|
382 | public void select() {
|
---|
383 | selectAction.actionPerformed(null);
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Cancel changes.
|
---|
388 | * @see CancelAction
|
---|
389 | */
|
---|
390 | public void cancel() {
|
---|
391 | cancelAction.actionPerformed(null);
|
---|
392 | }
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * Creates the toolbar
|
---|
396 | * @param actions relation toolbar actions
|
---|
397 | * @return the toolbar
|
---|
398 | * @since 12933
|
---|
399 | */
|
---|
400 | protected static JToolBar buildToolBar(AbstractRelationEditorAction... actions) {
|
---|
401 | JToolBar tb = new JToolBar();
|
---|
402 | tb.setFloatable(false);
|
---|
403 | for (AbstractRelationEditorAction action : actions) {
|
---|
404 | tb.add(action);
|
---|
405 | }
|
---|
406 | return tb;
|
---|
407 | }
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * builds the panel with the OK and the Cancel button
|
---|
411 | * @param okAction OK action
|
---|
412 | * @param deleteAction Delete action
|
---|
413 | * @param cancelAction Cancel action
|
---|
414 | *
|
---|
415 | * @return the panel with the OK and the Cancel button
|
---|
416 | */
|
---|
417 | protected final JPanel buildOkCancelButtonPanel(OKAction okAction, DeleteCurrentRelationAction deleteAction,
|
---|
418 | CancelAction cancelAction) {
|
---|
419 | final JPanel pnl = new JPanel(new FlowLayout(FlowLayout.CENTER));
|
---|
420 | final JButton okButton = new JButton(okAction);
|
---|
421 | final JButton deleteButton = new JButton(deleteAction);
|
---|
422 | okButton.setPreferredSize(deleteButton.getPreferredSize());
|
---|
423 | pnl.add(okButton);
|
---|
424 | pnl.add(deleteButton);
|
---|
425 | pnl.add(new JButton(cancelAction));
|
---|
426 | pnl.add(new JButton(new ContextSensitiveHelpAction(ht("/Dialog/RelationEditor"))));
|
---|
427 | // Keep users from saving invalid relations -- a relation MUST have at least a tag with the key "type"
|
---|
428 | // AND must contain at least one other OSM object.
|
---|
429 | final TableModelListener listener = l -> updateOkPanel(this.actionAccess.getChangedRelation(), okButton, deleteButton);
|
---|
430 | listener.tableChanged(null);
|
---|
431 | this.memberTableModel.addTableModelListener(listener);
|
---|
432 | this.tagEditorPanel.getModel().addTableModelListener(listener);
|
---|
433 | return pnl;
|
---|
434 | }
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Update the OK panel area
|
---|
438 | * @param newRelation What the new relation would "look" like if it were to be saved now
|
---|
439 | * @param okButton The OK button
|
---|
440 | * @param deleteButton The delete button
|
---|
441 | */
|
---|
442 | private void updateOkPanel(IRelation<?> newRelation, JButton okButton, JButton deleteButton) {
|
---|
443 | okButton.setVisible(newRelation.isUseful() || this.getRelationSnapshot() == null);
|
---|
444 | deleteButton.setVisible(!newRelation.isUseful() && this.getRelationSnapshot() != null);
|
---|
445 | if (this.getRelationSnapshot() == null && !newRelation.isUseful()) {
|
---|
446 | okButton.setText(tr("Delete"));
|
---|
447 | } else {
|
---|
448 | okButton.setText(tr("OK"));
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | /**
|
---|
453 | * builds the panel with the tag editor
|
---|
454 | * @param tagEditorPanel tag editor panel
|
---|
455 | *
|
---|
456 | * @return the panel with the tag editor
|
---|
457 | */
|
---|
458 | protected static JPanel buildTagEditorPanel(TagEditorPanel tagEditorPanel) {
|
---|
459 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
460 |
|
---|
461 | GridBagConstraints gc = new GridBagConstraints();
|
---|
462 | gc.gridx = 0;
|
---|
463 | gc.gridy = 0;
|
---|
464 | gc.gridheight = 1;
|
---|
465 | gc.gridwidth = 1;
|
---|
466 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
467 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
468 | gc.weightx = 1.0;
|
---|
469 | gc.weighty = 0.0;
|
---|
470 | pnl.add(new JLabel(tr("Tags")), gc);
|
---|
471 |
|
---|
472 | gc.gridx = 0;
|
---|
473 | gc.gridy = 1;
|
---|
474 | gc.fill = GridBagConstraints.BOTH;
|
---|
475 | gc.anchor = GridBagConstraints.CENTER;
|
---|
476 | gc.weightx = 1.0;
|
---|
477 | gc.weighty = 1.0;
|
---|
478 | pnl.add(tagEditorPanel, gc);
|
---|
479 | return pnl;
|
---|
480 | }
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * builds the role text field
|
---|
484 | * @param re relation editor
|
---|
485 | * @return the role text field
|
---|
486 | */
|
---|
487 | protected static AutoCompletingTextField buildRoleTextField(final IRelationEditor re) {
|
---|
488 | final AutoCompletingTextField tfRole = new AutoCompletingTextField(10);
|
---|
489 | tfRole.setToolTipText(tr("Enter a role and apply it to the selected relation members"));
|
---|
490 | tfRole.addFocusListener(new FocusAdapter() {
|
---|
491 | @Override
|
---|
492 | public void focusGained(FocusEvent e) {
|
---|
493 | tfRole.selectAll();
|
---|
494 | }
|
---|
495 | });
|
---|
496 | tfRole.setAutoCompletionList(new AutoCompletionList());
|
---|
497 | tfRole.addFocusListener(
|
---|
498 | new FocusAdapter() {
|
---|
499 | @Override
|
---|
500 | public void focusGained(FocusEvent e) {
|
---|
501 | AutoCompletionList list = tfRole.getAutoCompletionList();
|
---|
502 | if (list != null) {
|
---|
503 | list.clear();
|
---|
504 | AutoCompletionManager.of(re.getLayer().data).populateWithMemberRoles(list, re.getRelation());
|
---|
505 | }
|
---|
506 | }
|
---|
507 | }
|
---|
508 | );
|
---|
509 | tfRole.setText(Config.getPref().get("relation.editor.generic.lastrole", ""));
|
---|
510 | return tfRole;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * builds the panel for the relation member editor
|
---|
515 | * @param leftButtonToolbar left button toolbar
|
---|
516 | * @param editorAccess The relation editor
|
---|
517 | *
|
---|
518 | * @return the panel for the relation member editor
|
---|
519 | */
|
---|
520 | static JPanel buildMemberEditorPanel(
|
---|
521 | LeftButtonToolbar leftButtonToolbar, IRelationEditorActionAccess editorAccess) {
|
---|
522 | final JPanel pnl = new JPanel(new GridBagLayout());
|
---|
523 | final JScrollPane scrollPane = new JScrollPane(editorAccess.getMemberTable());
|
---|
524 |
|
---|
525 | GridBagConstraints gc = new GridBagConstraints();
|
---|
526 | gc.gridx = 0;
|
---|
527 | gc.gridy = 0;
|
---|
528 | gc.gridwidth = 2;
|
---|
529 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
530 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
531 | gc.weightx = 1.0;
|
---|
532 | gc.weighty = 0.0;
|
---|
533 | pnl.add(new JLabel(tr("Members")), gc);
|
---|
534 |
|
---|
535 | gc.gridx = 0;
|
---|
536 | gc.gridy = 1;
|
---|
537 | gc.gridheight = 2;
|
---|
538 | gc.gridwidth = 1;
|
---|
539 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
540 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
541 | gc.weightx = 0.0;
|
---|
542 | gc.weighty = 1.0;
|
---|
543 | pnl.add(new ScrollViewport(leftButtonToolbar, ScrollViewport.VERTICAL_DIRECTION), gc);
|
---|
544 |
|
---|
545 | gc.gridx = 1;
|
---|
546 | gc.gridy = 1;
|
---|
547 | gc.gridheight = 1;
|
---|
548 | gc.fill = GridBagConstraints.BOTH;
|
---|
549 | gc.anchor = GridBagConstraints.CENTER;
|
---|
550 | gc.weightx = 0.6;
|
---|
551 | gc.weighty = 1.0;
|
---|
552 | pnl.add(scrollPane, gc);
|
---|
553 |
|
---|
554 | // --- role editing
|
---|
555 | JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
556 | p3.add(new JLabel(tr("Apply Role:")));
|
---|
557 | p3.add(editorAccess.getTextFieldRole());
|
---|
558 | SetRoleAction setRoleAction = new SetRoleAction(editorAccess);
|
---|
559 | editorAccess.getMemberTableModel().getSelectionModel().addListSelectionListener(setRoleAction);
|
---|
560 | editorAccess.getTextFieldRole().getDocument().addDocumentListener(setRoleAction);
|
---|
561 | editorAccess.getTextFieldRole().addActionListener(setRoleAction);
|
---|
562 | editorAccess.getMemberTableModel().getSelectionModel().addListSelectionListener(
|
---|
563 | e -> editorAccess.getTextFieldRole().setEnabled(editorAccess.getMemberTable().getSelectedRowCount() > 0)
|
---|
564 | );
|
---|
565 | editorAccess.getTextFieldRole().setEnabled(editorAccess.getMemberTable().getSelectedRowCount() > 0);
|
---|
566 | JButton btnApply = new JButton(setRoleAction);
|
---|
567 | btnApply.setPreferredSize(new Dimension(20, 20));
|
---|
568 | btnApply.setText("");
|
---|
569 | p3.add(btnApply);
|
---|
570 |
|
---|
571 | gc.gridx = 1;
|
---|
572 | gc.gridy = 2;
|
---|
573 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
574 | gc.anchor = GridBagConstraints.LAST_LINE_START;
|
---|
575 | gc.weightx = 1.0;
|
---|
576 | gc.weighty = 0.0;
|
---|
577 | pnl.add(p3, gc);
|
---|
578 |
|
---|
579 | JPanel pnl2 = new JPanel(new GridBagLayout());
|
---|
580 |
|
---|
581 | gc.gridx = 0;
|
---|
582 | gc.gridy = 0;
|
---|
583 | gc.gridheight = 1;
|
---|
584 | gc.gridwidth = 3;
|
---|
585 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
586 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
587 | gc.weightx = 1.0;
|
---|
588 | gc.weighty = 0.0;
|
---|
589 | pnl2.add(new JLabel(tr("Selection")), gc);
|
---|
590 |
|
---|
591 | gc.gridx = 0;
|
---|
592 | gc.gridy = 1;
|
---|
593 | gc.gridheight = 1;
|
---|
594 | gc.gridwidth = 1;
|
---|
595 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
596 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
597 | gc.weightx = 0.0;
|
---|
598 | gc.weighty = 1.0;
|
---|
599 | pnl2.add(new ScrollViewport(buildSelectionControlButtonToolbar(editorAccess),
|
---|
600 | ScrollViewport.VERTICAL_DIRECTION), gc);
|
---|
601 |
|
---|
602 | gc.gridx = 1;
|
---|
603 | gc.gridy = 1;
|
---|
604 | gc.weightx = 1.0;
|
---|
605 | gc.weighty = 1.0;
|
---|
606 | gc.fill = GridBagConstraints.BOTH;
|
---|
607 | pnl2.add(buildSelectionTablePanel(editorAccess.getSelectionTable()), gc);
|
---|
608 |
|
---|
609 | final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
|
---|
610 | splitPane.setLeftComponent(pnl);
|
---|
611 | splitPane.setRightComponent(pnl2);
|
---|
612 | splitPane.setOneTouchExpandable(false);
|
---|
613 | if (editorAccess.getEditor() instanceof Window) {
|
---|
614 | ((Window) editorAccess.getEditor()).addWindowListener(new WindowAdapter() {
|
---|
615 | @Override
|
---|
616 | public void windowOpened(WindowEvent e) {
|
---|
617 | // has to be called when the window is visible, otherwise no effect
|
---|
618 | splitPane.setDividerLocation(0.6);
|
---|
619 | }
|
---|
620 | });
|
---|
621 | }
|
---|
622 |
|
---|
623 | JPanel pnl3 = new JPanel(new BorderLayout());
|
---|
624 | pnl3.add(splitPane, BorderLayout.CENTER);
|
---|
625 |
|
---|
626 | return pnl3;
|
---|
627 | }
|
---|
628 |
|
---|
629 | /**
|
---|
630 | * builds the panel with the table displaying the currently selected primitives
|
---|
631 | * @param selectionTable selection table
|
---|
632 | *
|
---|
633 | * @return panel with current selection
|
---|
634 | */
|
---|
635 | protected static JPanel buildSelectionTablePanel(SelectionTable selectionTable) {
|
---|
636 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
637 | pnl.add(new JScrollPane(selectionTable), BorderLayout.CENTER);
|
---|
638 | return pnl;
|
---|
639 | }
|
---|
640 |
|
---|
641 | /**
|
---|
642 | * builds the {@link JSplitPane} which divides the editor in an upper and a lower half
|
---|
643 | * @param top top panel
|
---|
644 | * @param bottom bottom panel
|
---|
645 | * @param re relation editor
|
---|
646 | *
|
---|
647 | * @return the split panel
|
---|
648 | */
|
---|
649 | protected static JSplitPane buildSplitPane(JPanel top, JPanel bottom, IRelationEditor re) {
|
---|
650 | final JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
|
---|
651 | pane.setTopComponent(top);
|
---|
652 | pane.setBottomComponent(bottom);
|
---|
653 | pane.setOneTouchExpandable(true);
|
---|
654 | if (re instanceof Window) {
|
---|
655 | ((Window) re).addWindowListener(new WindowAdapter() {
|
---|
656 | @Override
|
---|
657 | public void windowOpened(WindowEvent e) {
|
---|
658 | // has to be called when the window is visible, otherwise no effect
|
---|
659 | pane.setDividerLocation(0.3);
|
---|
660 | }
|
---|
661 | });
|
---|
662 | }
|
---|
663 | return pane;
|
---|
664 | }
|
---|
665 |
|
---|
666 | /**
|
---|
667 | * The toolbar with the buttons on the left
|
---|
668 | */
|
---|
669 | static class LeftButtonToolbar extends JToolBar {
|
---|
670 | private static final long serialVersionUID = 1L;
|
---|
671 |
|
---|
672 | /**
|
---|
673 | * Constructs a new {@code LeftButtonToolbar}.
|
---|
674 | * @param editorAccess relation editor
|
---|
675 | */
|
---|
676 | LeftButtonToolbar(IRelationEditorActionAccess editorAccess) {
|
---|
677 | setOrientation(JToolBar.VERTICAL);
|
---|
678 | setFloatable(false);
|
---|
679 |
|
---|
680 | List<IRelationEditorActionGroup> groups = new ArrayList<>();
|
---|
681 | // Move
|
---|
682 | groups.add(buildNativeGroup(10,
|
---|
683 | new MoveUpAction(editorAccess, "moveUp"),
|
---|
684 | new MoveDownAction(editorAccess, "moveDown")
|
---|
685 | ));
|
---|
686 | // Edit
|
---|
687 | groups.add(buildNativeGroup(20,
|
---|
688 | new EditAction(editorAccess),
|
---|
689 | new RemoveAction(editorAccess, "removeSelected")
|
---|
690 | ));
|
---|
691 | // Sort
|
---|
692 | groups.add(buildNativeGroup(30,
|
---|
693 | new SortAction(editorAccess),
|
---|
694 | new SortBelowAction(editorAccess)
|
---|
695 | ));
|
---|
696 | // Reverse
|
---|
697 | groups.add(buildNativeGroup(40,
|
---|
698 | new ReverseAction(editorAccess)
|
---|
699 | ));
|
---|
700 | // Download
|
---|
701 | groups.add(buildNativeGroup(50,
|
---|
702 | new DownloadIncompleteMembersAction(editorAccess, "downloadIncomplete"),
|
---|
703 | new DownloadSelectedIncompleteMembersAction(editorAccess)
|
---|
704 | ));
|
---|
705 | groups.addAll(RelationEditorHooks.getMemberActions());
|
---|
706 |
|
---|
707 | IRelationEditorActionGroup.fillToolbar(this, groups, editorAccess);
|
---|
708 |
|
---|
709 |
|
---|
710 | InputMap inputMap = editorAccess.getMemberTable().getInputMap(MemberTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
---|
711 | inputMap.put((KeyStroke) new RemoveAction(editorAccess, "removeSelected")
|
---|
712 | .getValue(AbstractAction.ACCELERATOR_KEY), "removeSelected");
|
---|
713 | inputMap.put((KeyStroke) new MoveUpAction(editorAccess, "moveUp")
|
---|
714 | .getValue(AbstractAction.ACCELERATOR_KEY), "moveUp");
|
---|
715 | inputMap.put((KeyStroke) new MoveDownAction(editorAccess, "moveDown")
|
---|
716 | .getValue(AbstractAction.ACCELERATOR_KEY), "moveDown");
|
---|
717 | inputMap.put((KeyStroke) new DownloadIncompleteMembersAction(
|
---|
718 | editorAccess, "downloadIncomplete").getValue(AbstractAction.ACCELERATOR_KEY), "downloadIncomplete");
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 | /**
|
---|
723 | * build the toolbar with the buttons for adding or removing the current selection
|
---|
724 | * @param editorAccess relation editor
|
---|
725 | *
|
---|
726 | * @return control buttons panel for selection/members
|
---|
727 | */
|
---|
728 | protected static JToolBar buildSelectionControlButtonToolbar(IRelationEditorActionAccess editorAccess) {
|
---|
729 | JToolBar tb = new JToolBar(JToolBar.VERTICAL);
|
---|
730 | tb.setFloatable(false);
|
---|
731 |
|
---|
732 | List<IRelationEditorActionGroup> groups = new ArrayList<>();
|
---|
733 | groups.add(buildNativeGroup(10,
|
---|
734 | new AddSelectedAtStartAction(editorAccess),
|
---|
735 | new AddSelectedBeforeSelection(editorAccess),
|
---|
736 | new AddSelectedAfterSelection(editorAccess),
|
---|
737 | new AddSelectedAtEndAction(editorAccess)
|
---|
738 | ));
|
---|
739 | groups.add(buildNativeGroup(20,
|
---|
740 | new SelectedMembersForSelectionAction(editorAccess),
|
---|
741 | new SelectPrimitivesForSelectedMembersAction(editorAccess)
|
---|
742 | ));
|
---|
743 | groups.add(buildNativeGroup(30,
|
---|
744 | new RemoveSelectedAction(editorAccess)
|
---|
745 | ));
|
---|
746 | groups.addAll(RelationEditorHooks.getSelectActions());
|
---|
747 |
|
---|
748 | IRelationEditorActionGroup.fillToolbar(tb, groups, editorAccess);
|
---|
749 | return tb;
|
---|
750 | }
|
---|
751 |
|
---|
752 | private static IRelationEditorActionGroup buildNativeGroup(int order, AbstractRelationEditorAction... actions) {
|
---|
753 | return new IRelationEditorActionGroup() {
|
---|
754 | @Override
|
---|
755 | public int order() {
|
---|
756 | return order;
|
---|
757 | }
|
---|
758 |
|
---|
759 | @Override
|
---|
760 | public List<AbstractRelationEditorAction> getActions(IRelationEditorActionAccess editorAccess) {
|
---|
761 | return Arrays.asList(actions);
|
---|
762 | }
|
---|
763 | };
|
---|
764 | }
|
---|
765 |
|
---|
766 | @Override
|
---|
767 | protected Dimension findMaxDialogSize() {
|
---|
768 | return new Dimension(700, 650);
|
---|
769 | }
|
---|
770 |
|
---|
771 | @Override
|
---|
772 | public void setVisible(boolean visible) {
|
---|
773 | if (isVisible() == visible) {
|
---|
774 | return;
|
---|
775 | }
|
---|
776 | if (visible) {
|
---|
777 | tagEditorPanel.initAutoCompletion(getLayer());
|
---|
778 | }
|
---|
779 | super.setVisible(visible);
|
---|
780 | Clipboard clipboard = ClipboardUtils.getClipboard();
|
---|
781 | if (visible) {
|
---|
782 | RelationDialogManager.getRelationDialogManager().positionOnScreen(this);
|
---|
783 | if (windowMenuItem == null) {
|
---|
784 | windowMenuItem = addToWindowMenu(this, getLayer().getName());
|
---|
785 | }
|
---|
786 | tagEditorPanel.requestFocusInWindow();
|
---|
787 | for (FlavorListener listener : clipboardListeners) {
|
---|
788 | clipboard.addFlavorListener(listener);
|
---|
789 | }
|
---|
790 | } else {
|
---|
791 | // make sure all registered listeners are unregistered
|
---|
792 | //
|
---|
793 | memberTable.stopHighlighting();
|
---|
794 | if (tabbedPane != null && tr("Tags and Members").equals(tabbedPane.getTitleAt(tabbedPane.getSelectedIndex()))) {
|
---|
795 | unregisterMain();
|
---|
796 | }
|
---|
797 | if (windowMenuItem != null) {
|
---|
798 | MainApplication.getMenu().windowMenu.remove(windowMenuItem);
|
---|
799 | windowMenuItem = null;
|
---|
800 | }
|
---|
801 | for (FlavorListener listener : clipboardListeners) {
|
---|
802 | clipboard.removeFlavorListener(listener);
|
---|
803 | }
|
---|
804 | dispose();
|
---|
805 | }
|
---|
806 | }
|
---|
807 |
|
---|
808 | /**
|
---|
809 | * Adds current relation editor to the windows menu (in the "volatile" group)
|
---|
810 | * @param re relation editor
|
---|
811 | * @param layerName layer name
|
---|
812 | * @return created menu item
|
---|
813 | */
|
---|
814 | protected static JMenuItem addToWindowMenu(IRelationEditor re, String layerName) {
|
---|
815 | Relation r = re.getRelation();
|
---|
816 | String name = r == null ? tr("New relation") : r.getLocalName();
|
---|
817 | JosmAction focusAction = new JosmAction(
|
---|
818 | tr("Relation Editor: {0}", name == null && r != null ? r.getId() : name),
|
---|
819 | "dialogs/relationlist",
|
---|
820 | tr("Focus Relation Editor with relation ''{0}'' in layer ''{1}''", name, layerName),
|
---|
821 | null, false, false) {
|
---|
822 | private static final long serialVersionUID = 1L;
|
---|
823 |
|
---|
824 | @Override
|
---|
825 | public void actionPerformed(ActionEvent e) {
|
---|
826 | ((RelationEditor) getValue("relationEditor")).setVisible(true);
|
---|
827 | }
|
---|
828 | };
|
---|
829 | focusAction.putValue("relationEditor", re);
|
---|
830 | return MainMenu.add(MainApplication.getMenu().windowMenu, focusAction, MainMenu.WINDOW_MENU_GROUP.VOLATILE);
|
---|
831 | }
|
---|
832 |
|
---|
833 | /**
|
---|
834 | * checks whether the current relation has members referring to itself. If so,
|
---|
835 | * warns the users and provides an option for removing these members.
|
---|
836 | * @param memberTableModel member table model
|
---|
837 | * @param relation relation
|
---|
838 | */
|
---|
839 | protected static void cleanSelfReferences(MemberTableModel memberTableModel, Relation relation) {
|
---|
840 | List<OsmPrimitive> toCheck = new ArrayList<>();
|
---|
841 | toCheck.add(relation);
|
---|
842 | if (memberTableModel.hasMembersReferringTo(toCheck)) {
|
---|
843 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
844 | "clean_relation_self_references",
|
---|
845 | MainApplication.getMainFrame(),
|
---|
846 | tr("<html>There is at least one member in this relation referring<br>"
|
---|
847 | + "to the relation itself.<br>"
|
---|
848 | + "This creates circular dependencies and is discouraged.<br>"
|
---|
849 | + "How do you want to proceed with circular dependencies?</html>"),
|
---|
850 | tr("Warning"),
|
---|
851 | JOptionPane.YES_NO_OPTION,
|
---|
852 | JOptionPane.WARNING_MESSAGE,
|
---|
853 | new String[]{tr("Remove them, clean up relation"), tr("Ignore them, leave relation as is")},
|
---|
854 | tr("Remove them, clean up relation")
|
---|
855 | );
|
---|
856 | switch(ret) {
|
---|
857 | case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
|
---|
858 | case JOptionPane.CLOSED_OPTION:
|
---|
859 | case JOptionPane.NO_OPTION:
|
---|
860 | return;
|
---|
861 | case JOptionPane.YES_OPTION:
|
---|
862 | memberTableModel.removeMembersReferringTo(toCheck);
|
---|
863 | break;
|
---|
864 | default: // Do nothing
|
---|
865 | }
|
---|
866 | }
|
---|
867 | }
|
---|
868 |
|
---|
869 | private void registerCopyPasteAction(AbstractAction action, Object actionName, KeyStroke shortcut,
|
---|
870 | JRootPane rootPane, JTable... tables) {
|
---|
871 | if (shortcut == null) {
|
---|
872 | Logging.warn("No shortcut provided for the Paste action in Relation editor dialog");
|
---|
873 | } else {
|
---|
874 | int mods = shortcut.getModifiers();
|
---|
875 | int code = shortcut.getKeyCode();
|
---|
876 | if (code != KeyEvent.VK_INSERT && (mods == 0 || mods == InputEvent.SHIFT_DOWN_MASK)) {
|
---|
877 | Logging.info(tr("Sorry, shortcut \"{0}\" can not be enabled in Relation editor dialog"), shortcut);
|
---|
878 | return;
|
---|
879 | }
|
---|
880 | }
|
---|
881 | rootPane.getActionMap().put(actionName, action);
|
---|
882 | if (shortcut != null) {
|
---|
883 | rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(shortcut, actionName);
|
---|
884 | // Assign also to JTables because they have their own Copy&Paste implementation
|
---|
885 | // (which is disabled in this case but eats key shortcuts anyway)
|
---|
886 | for (JTable table : tables) {
|
---|
887 | table.getInputMap(JComponent.WHEN_FOCUSED).put(shortcut, actionName);
|
---|
888 | table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(shortcut, actionName);
|
---|
889 | table.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(shortcut, actionName);
|
---|
890 | }
|
---|
891 | }
|
---|
892 | if (action instanceof FlavorListener) {
|
---|
893 | clipboardListeners.add((FlavorListener) action);
|
---|
894 | }
|
---|
895 | }
|
---|
896 |
|
---|
897 | @Override
|
---|
898 | public void dispose() {
|
---|
899 | refreshAction.destroy();
|
---|
900 | UndoRedoHandler.getInstance().removeCommandQueueListener(this);
|
---|
901 | super.dispose(); // call before setting relation to null, see #20304
|
---|
902 | setRelation(null);
|
---|
903 | selectedTabPane = null;
|
---|
904 | }
|
---|
905 |
|
---|
906 | /**
|
---|
907 | * Exception thrown when user aborts add operation.
|
---|
908 | */
|
---|
909 | public static class AddAbortException extends Exception {
|
---|
910 | }
|
---|
911 |
|
---|
912 | /**
|
---|
913 | * Asks confirmation before adding a primitive.
|
---|
914 | * @param primitive primitive to add
|
---|
915 | * @return {@code true} is user confirms the operation, {@code false} otherwise
|
---|
916 | * @throws AddAbortException if user aborts operation
|
---|
917 | */
|
---|
918 | public static boolean confirmAddingPrimitive(OsmPrimitive primitive) throws AddAbortException {
|
---|
919 | String msg = tr("<html>This relation already has one or more members referring to<br>"
|
---|
920 | + "the object ''{0}''<br>"
|
---|
921 | + "<br>"
|
---|
922 | + "Do you really want to add another relation member?</html>",
|
---|
923 | Utils.escapeReservedCharactersHTML(primitive.getDisplayName(DefaultNameFormatter.getInstance()))
|
---|
924 | );
|
---|
925 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
926 | "add_primitive_to_relation",
|
---|
927 | MainApplication.getMainFrame(),
|
---|
928 | msg,
|
---|
929 | tr("Multiple members referring to same object."),
|
---|
930 | JOptionPane.YES_NO_CANCEL_OPTION,
|
---|
931 | JOptionPane.WARNING_MESSAGE,
|
---|
932 | null,
|
---|
933 | null
|
---|
934 | );
|
---|
935 | switch(ret) {
|
---|
936 | case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
|
---|
937 | case JOptionPane.YES_OPTION:
|
---|
938 | return true;
|
---|
939 | case JOptionPane.NO_OPTION:
|
---|
940 | case JOptionPane.CLOSED_OPTION:
|
---|
941 | return false;
|
---|
942 | case JOptionPane.CANCEL_OPTION:
|
---|
943 | default:
|
---|
944 | throw new AddAbortException();
|
---|
945 | }
|
---|
946 | }
|
---|
947 |
|
---|
948 | /**
|
---|
949 | * Warn about circular references.
|
---|
950 | * @param primitive the concerned primitive
|
---|
951 | */
|
---|
952 | public static void warnOfCircularReferences(OsmPrimitive primitive) {
|
---|
953 | warnOfCircularReferences(primitive, Collections.emptyList());
|
---|
954 | }
|
---|
955 |
|
---|
956 | /**
|
---|
957 | * Warn about circular references.
|
---|
958 | * @param primitive the concerned primitive
|
---|
959 | * @param loop list of relation that form the circular dependencies.
|
---|
960 | * Only used to report the loop if more than one relation is involved.
|
---|
961 | * @since 16651
|
---|
962 | */
|
---|
963 | public static void warnOfCircularReferences(OsmPrimitive primitive, List<Relation> loop) {
|
---|
964 | final String msg;
|
---|
965 | DefaultNameFormatter df = DefaultNameFormatter.getInstance();
|
---|
966 | if (loop.size() <= 2) {
|
---|
967 | msg = tr("<html>You are trying to add a relation to itself.<br>"
|
---|
968 | + "<br>"
|
---|
969 | + "This generates a circular dependency of parent/child elements and is therefore discouraged.<br>"
|
---|
970 | + "Skipping relation ''{0}''.</html>",
|
---|
971 | Utils.escapeReservedCharactersHTML(primitive.getDisplayName(df)));
|
---|
972 | } else {
|
---|
973 | msg = tr("<html>You are trying to add a child relation which refers to the parent relation.<br>"
|
---|
974 | + "<br>"
|
---|
975 | + "This generates a circular dependency of parent/child elements and is therefore discouraged.<br>"
|
---|
976 | + "Skipping relation ''{0}''." + "<br>"
|
---|
977 | + "Relations that would generate the circular dependency:<br>{1}</html>",
|
---|
978 | Utils.escapeReservedCharactersHTML(primitive.getDisplayName(df)),
|
---|
979 | loop.stream().map(p -> Utils.escapeReservedCharactersHTML(p.getDisplayName(df)))
|
---|
980 | .collect(Collectors.joining(" -> <br>")));
|
---|
981 | }
|
---|
982 | JOptionPane.showMessageDialog(
|
---|
983 | MainApplication.getMainFrame(),
|
---|
984 | msg,
|
---|
985 | tr("Warning"),
|
---|
986 | JOptionPane.WARNING_MESSAGE);
|
---|
987 | }
|
---|
988 |
|
---|
989 | /**
|
---|
990 | * Adds primitives to a given relation.
|
---|
991 | * @param orig The relation to modify
|
---|
992 | * @param primitivesToAdd The primitives to add as relation members
|
---|
993 | * @return The resulting command
|
---|
994 | * @throws IllegalArgumentException if orig is null
|
---|
995 | */
|
---|
996 | public static Command addPrimitivesToRelation(final Relation orig, Collection<? extends OsmPrimitive> primitivesToAdd) {
|
---|
997 | CheckParameterUtil.ensureParameterNotNull(orig, "orig");
|
---|
998 | try {
|
---|
999 | final Collection<TaggingPreset> presets = TaggingPresets.getMatchingPresets(
|
---|
1000 | EnumSet.of(TaggingPresetType.forPrimitive(orig)), orig.getKeys(), false);
|
---|
1001 | Relation target = new Relation(orig);
|
---|
1002 | boolean modified = false;
|
---|
1003 | for (OsmPrimitive p : primitivesToAdd) {
|
---|
1004 | if (p instanceof Relation) {
|
---|
1005 | List<Relation> loop = RelationChecker.checkAddMember(target, (Relation) p);
|
---|
1006 | if (!loop.isEmpty() && loop.get(0).equals(loop.get(loop.size() - 1))) {
|
---|
1007 | warnOfCircularReferences(p, loop);
|
---|
1008 | continue;
|
---|
1009 | }
|
---|
1010 | } else if (MemberTableModel.hasMembersReferringTo(target.getMembers(), Collections.singleton(p))
|
---|
1011 | && !confirmAddingPrimitive(p)) {
|
---|
1012 | continue;
|
---|
1013 | }
|
---|
1014 | final Set<String> roles = findSuggestedRoles(presets, p);
|
---|
1015 | target.addMember(new RelationMember(roles.size() == 1 ? roles.iterator().next() : "", p));
|
---|
1016 | modified = true;
|
---|
1017 | }
|
---|
1018 | List<RelationMember> members = new ArrayList<>(target.getMembers());
|
---|
1019 | target.setMembers(null); // see #19885
|
---|
1020 | return modified ? new ChangeMembersCommand(orig, members) : null;
|
---|
1021 | } catch (AddAbortException ign) {
|
---|
1022 | Logging.trace(ign);
|
---|
1023 | return null;
|
---|
1024 | }
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | protected static Set<String> findSuggestedRoles(final Collection<TaggingPreset> presets, OsmPrimitive p) {
|
---|
1028 | return presets.stream()
|
---|
1029 | .map(preset -> preset.suggestRoleForOsmPrimitive(p))
|
---|
1030 | .filter(role -> !Utils.isEmpty(role))
|
---|
1031 | .collect(Collectors.toSet());
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | class MemberTableDblClickAdapter extends MouseAdapter {
|
---|
1035 | @Override
|
---|
1036 | public void mouseClicked(MouseEvent e) {
|
---|
1037 | if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
|
---|
1038 | new EditAction(actionAccess).actionPerformed(null);
|
---|
1039 | }
|
---|
1040 | }
|
---|
1041 | }
|
---|
1042 |
|
---|
1043 | private class RelationEditorActionAccess implements IRelationEditorActionAccess {
|
---|
1044 |
|
---|
1045 | @Override
|
---|
1046 | public MemberTable getMemberTable() {
|
---|
1047 | return memberTable;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | @Override
|
---|
1051 | public MemberTableModel getMemberTableModel() {
|
---|
1052 | return memberTableModel;
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | @Override
|
---|
1056 | public SelectionTable getSelectionTable() {
|
---|
1057 | return selectionTable;
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | @Override
|
---|
1061 | public SelectionTableModel getSelectionTableModel() {
|
---|
1062 | return selectionTableModel;
|
---|
1063 | }
|
---|
1064 |
|
---|
1065 | @Override
|
---|
1066 | public IRelationEditor getEditor() {
|
---|
1067 | return GenericRelationEditor.this;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | @Override
|
---|
1071 | public TagEditorModel getTagModel() {
|
---|
1072 | return tagEditorPanel.getModel();
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | @Override
|
---|
1076 | public AutoCompletingTextField getTextFieldRole() {
|
---|
1077 | return tfRole;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | @Override
|
---|
1083 | public void commandChanged(int queueSize, int redoSize) {
|
---|
1084 | deleteAction.updateEnabledState();
|
---|
1085 | Relation r = getRelation();
|
---|
1086 | if (r != null && r.getDataSet() == null) {
|
---|
1087 | // see #19915
|
---|
1088 | setRelation(null);
|
---|
1089 | applyAction.updateEnabledState();
|
---|
1090 | }
|
---|
1091 | }
|
---|
1092 | }
|
---|