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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.BorderLayout;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.FlowLayout;
|
---|
11 | import java.awt.GridBagConstraints;
|
---|
12 | import java.awt.GridBagLayout;
|
---|
13 | import java.awt.event.ActionEvent;
|
---|
14 | import java.awt.event.FocusAdapter;
|
---|
15 | import java.awt.event.FocusEvent;
|
---|
16 | import java.awt.event.InputEvent;
|
---|
17 | import java.awt.event.KeyEvent;
|
---|
18 | import java.awt.event.MouseAdapter;
|
---|
19 | import java.awt.event.MouseEvent;
|
---|
20 | import java.awt.event.WindowAdapter;
|
---|
21 | import java.awt.event.WindowEvent;
|
---|
22 | import java.beans.PropertyChangeEvent;
|
---|
23 | import java.beans.PropertyChangeListener;
|
---|
24 | import java.util.ArrayList;
|
---|
25 | import java.util.Collection;
|
---|
26 | import java.util.Collections;
|
---|
27 | import java.util.EnumSet;
|
---|
28 | import java.util.HashSet;
|
---|
29 | import java.util.List;
|
---|
30 | import java.util.Set;
|
---|
31 |
|
---|
32 | import javax.swing.AbstractAction;
|
---|
33 | import javax.swing.BorderFactory;
|
---|
34 | import javax.swing.InputMap;
|
---|
35 | import javax.swing.JComponent;
|
---|
36 | import javax.swing.JLabel;
|
---|
37 | import javax.swing.JMenu;
|
---|
38 | import javax.swing.JMenuItem;
|
---|
39 | import javax.swing.JOptionPane;
|
---|
40 | import javax.swing.JPanel;
|
---|
41 | import javax.swing.JScrollPane;
|
---|
42 | import javax.swing.JSplitPane;
|
---|
43 | import javax.swing.JTabbedPane;
|
---|
44 | import javax.swing.JToolBar;
|
---|
45 | import javax.swing.KeyStroke;
|
---|
46 | import javax.swing.SwingUtilities;
|
---|
47 | import javax.swing.event.ChangeEvent;
|
---|
48 | import javax.swing.event.ChangeListener;
|
---|
49 | import javax.swing.event.DocumentEvent;
|
---|
50 | import javax.swing.event.DocumentListener;
|
---|
51 | import javax.swing.event.ListSelectionEvent;
|
---|
52 | import javax.swing.event.ListSelectionListener;
|
---|
53 | import javax.swing.event.TableModelEvent;
|
---|
54 | import javax.swing.event.TableModelListener;
|
---|
55 |
|
---|
56 | import org.openstreetmap.josm.Main;
|
---|
57 | import org.openstreetmap.josm.actions.CopyAction;
|
---|
58 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
59 | import org.openstreetmap.josm.command.AddCommand;
|
---|
60 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
61 | import org.openstreetmap.josm.command.Command;
|
---|
62 | import org.openstreetmap.josm.command.conflict.ConflictAddCommand;
|
---|
63 | import org.openstreetmap.josm.data.conflict.Conflict;
|
---|
64 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
65 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
66 | import org.openstreetmap.josm.data.osm.PrimitiveData;
|
---|
67 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
68 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
69 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
70 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
|
---|
71 | import org.openstreetmap.josm.gui.DefaultNameFormatter;
|
---|
72 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
73 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
74 | import org.openstreetmap.josm.gui.MainMenu;
|
---|
75 | import org.openstreetmap.josm.gui.SideButton;
|
---|
76 | import org.openstreetmap.josm.gui.help.ContextSensitiveHelpAction;
|
---|
77 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
78 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
79 | import org.openstreetmap.josm.gui.tagging.PresetHandler;
|
---|
80 | import org.openstreetmap.josm.gui.tagging.TagEditorModel;
|
---|
81 | import org.openstreetmap.josm.gui.tagging.TagEditorPanel;
|
---|
82 | import org.openstreetmap.josm.gui.tagging.TaggingPreset;
|
---|
83 | import org.openstreetmap.josm.gui.tagging.TaggingPresetType;
|
---|
84 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
85 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
86 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
87 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
88 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
89 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
90 | import org.openstreetmap.josm.tools.WindowGeometry;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * This dialog is for editing relations.
|
---|
94 | * @since 343
|
---|
95 | */
|
---|
96 | public class GenericRelationEditor extends RelationEditor {
|
---|
97 | /** the tag table and its model */
|
---|
98 | private TagEditorPanel tagEditorPanel;
|
---|
99 | private ReferringRelationsBrowser referrerBrowser;
|
---|
100 | private ReferringRelationsBrowserModel referrerModel;
|
---|
101 |
|
---|
102 | /** the member table */
|
---|
103 | private MemberTable memberTable;
|
---|
104 | private MemberTableModel memberTableModel;
|
---|
105 |
|
---|
106 | /** the model for the selection table */
|
---|
107 | private SelectionTable selectionTable;
|
---|
108 | private SelectionTableModel selectionTableModel;
|
---|
109 |
|
---|
110 | private AutoCompletingTextField tfRole;
|
---|
111 |
|
---|
112 | /** the menu item in the windows menu. Required to properly
|
---|
113 | * hide on dialog close.
|
---|
114 | */
|
---|
115 | private JMenuItem windowMenuItem;
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Creates a new relation editor for the given relation. The relation will be saved if the user
|
---|
119 | * selects "ok" in the editor.
|
---|
120 | *
|
---|
121 | * If no relation is given, will create an editor for a new relation.
|
---|
122 | *
|
---|
123 | * @param layer the {@link OsmDataLayer} the new or edited relation belongs to
|
---|
124 | * @param relation relation to edit, or null to create a new one.
|
---|
125 | * @param selectedMembers a collection of members which shall be selected initially
|
---|
126 | */
|
---|
127 | public GenericRelationEditor(OsmDataLayer layer, Relation relation, Collection<RelationMember> selectedMembers) {
|
---|
128 | super(layer, relation, selectedMembers);
|
---|
129 |
|
---|
130 | setRememberWindowGeometry(getClass().getName() + ".geometry",
|
---|
131 | WindowGeometry.centerInWindow(Main.parent, new Dimension(700, 650)));
|
---|
132 |
|
---|
133 | final PresetHandler presetHandler = new PresetHandler() {
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public void updateTags(List<Tag> tags) {
|
---|
137 | tagEditorPanel.getModel().updateTags(tags);
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public Collection<OsmPrimitive> getSelection() {
|
---|
142 | Relation relation = new Relation();
|
---|
143 | tagEditorPanel.getModel().applyToPrimitive(relation);
|
---|
144 | return Collections.<OsmPrimitive>singletonList(relation);
|
---|
145 | }
|
---|
146 | };
|
---|
147 |
|
---|
148 | // init the various models
|
---|
149 | //
|
---|
150 | memberTableModel = new MemberTableModel(getLayer(), presetHandler);
|
---|
151 | memberTableModel.register();
|
---|
152 | selectionTableModel = new SelectionTableModel(getLayer());
|
---|
153 | selectionTableModel.register();
|
---|
154 | referrerModel = new ReferringRelationsBrowserModel(relation);
|
---|
155 |
|
---|
156 | tagEditorPanel = new TagEditorPanel(presetHandler);
|
---|
157 |
|
---|
158 | // populate the models
|
---|
159 | //
|
---|
160 | if (relation != null) {
|
---|
161 | tagEditorPanel.getModel().initFromPrimitive(relation);
|
---|
162 | this.memberTableModel.populate(relation);
|
---|
163 | if (!getLayer().data.getRelations().contains(relation)) {
|
---|
164 | // treat it as a new relation if it doesn't exist in the
|
---|
165 | // data set yet.
|
---|
166 | setRelation(null);
|
---|
167 | }
|
---|
168 | } else {
|
---|
169 | tagEditorPanel.getModel().clear();
|
---|
170 | this.memberTableModel.populate(null);
|
---|
171 | }
|
---|
172 | tagEditorPanel.getModel().ensureOneTag();
|
---|
173 |
|
---|
174 | JSplitPane pane = buildSplitPane(relation);
|
---|
175 | pane.setPreferredSize(new Dimension(100, 100));
|
---|
176 |
|
---|
177 | JPanel pnl = new JPanel();
|
---|
178 | pnl.setLayout(new BorderLayout());
|
---|
179 | pnl.add(pane, BorderLayout.CENTER);
|
---|
180 | pnl.setBorder(BorderFactory.createRaisedBevelBorder());
|
---|
181 |
|
---|
182 | getContentPane().setLayout(new BorderLayout());
|
---|
183 | JTabbedPane tabbedPane = new JTabbedPane();
|
---|
184 | tabbedPane.add(tr("Tags and Members"), pnl);
|
---|
185 | referrerBrowser = new ReferringRelationsBrowser(getLayer(), referrerModel);
|
---|
186 | tabbedPane.add(tr("Parent Relations"), referrerBrowser);
|
---|
187 | tabbedPane.add(tr("Child Relations"), new ChildRelationBrowser(getLayer(), relation));
|
---|
188 | tabbedPane.addChangeListener(
|
---|
189 | new ChangeListener() {
|
---|
190 | @Override
|
---|
191 | public void stateChanged(ChangeEvent e) {
|
---|
192 | JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
|
---|
193 | int index = sourceTabbedPane.getSelectedIndex();
|
---|
194 | String title = sourceTabbedPane.getTitleAt(index);
|
---|
195 | if (title.equals(tr("Parent Relations"))) {
|
---|
196 | referrerBrowser.init();
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|
200 | );
|
---|
201 |
|
---|
202 | getContentPane().add(buildToolBar(), BorderLayout.NORTH);
|
---|
203 | getContentPane().add(tabbedPane, BorderLayout.CENTER);
|
---|
204 | getContentPane().add(buildOkCancelButtonPanel(), BorderLayout.SOUTH);
|
---|
205 |
|
---|
206 | setSize(findMaxDialogSize());
|
---|
207 |
|
---|
208 | addWindowListener(
|
---|
209 | new WindowAdapter() {
|
---|
210 | @Override
|
---|
211 | public void windowOpened(WindowEvent e) {
|
---|
212 | cleanSelfReferences();
|
---|
213 | }
|
---|
214 | }
|
---|
215 | );
|
---|
216 | registerCopyPasteAction(tagEditorPanel.getPasteAction(),
|
---|
217 | "PASTE_TAGS",
|
---|
218 | Shortcut.registerShortcut("system:pastestyle", tr("Edit: {0}", tr("Paste Tags")), KeyEvent.VK_V, Shortcut.CTRL_SHIFT).getKeyStroke());
|
---|
219 | registerCopyPasteAction(new PasteMembersAction(), "PASTE_MEMBERS", Shortcut.getPasteKeyStroke());
|
---|
220 | registerCopyPasteAction(new CopyMembersAction(), "COPY_MEMBERS", Shortcut.getCopyKeyStroke());
|
---|
221 |
|
---|
222 | tagEditorPanel.setNextFocusComponent(memberTable);
|
---|
223 | selectionTable.setFocusable(false);
|
---|
224 | memberTableModel.setSelectedMembers(selectedMembers);
|
---|
225 | HelpUtil.setHelpContext(getRootPane(),ht("/Dialog/RelationEditor"));
|
---|
226 | }
|
---|
227 |
|
---|
228 | /**
|
---|
229 | * Creates the toolbar
|
---|
230 | *
|
---|
231 | * @return the toolbar
|
---|
232 | */
|
---|
233 | protected JToolBar buildToolBar() {
|
---|
234 | JToolBar tb = new JToolBar();
|
---|
235 | tb.setFloatable(false);
|
---|
236 | tb.add(new ApplyAction());
|
---|
237 | tb.add(new DuplicateRelationAction());
|
---|
238 | DeleteCurrentRelationAction deleteAction = new DeleteCurrentRelationAction();
|
---|
239 | addPropertyChangeListener(deleteAction);
|
---|
240 | tb.add(deleteAction);
|
---|
241 | return tb;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * builds the panel with the OK and the Cancel button
|
---|
246 | *
|
---|
247 | * @return the panel with the OK and the Cancel button
|
---|
248 | */
|
---|
249 | protected JPanel buildOkCancelButtonPanel() {
|
---|
250 | JPanel pnl = new JPanel();
|
---|
251 | pnl.setLayout(new FlowLayout(FlowLayout.CENTER));
|
---|
252 |
|
---|
253 | pnl.add(new SideButton(new OKAction()));
|
---|
254 | pnl.add(new SideButton(new CancelAction()));
|
---|
255 | pnl.add(new SideButton(new ContextSensitiveHelpAction(ht("/Dialog/RelationEditor"))));
|
---|
256 | return pnl;
|
---|
257 | }
|
---|
258 |
|
---|
259 | /**
|
---|
260 | * builds the panel with the tag editor
|
---|
261 | *
|
---|
262 | * @return the panel with the tag editor
|
---|
263 | */
|
---|
264 | protected JPanel buildTagEditorPanel() {
|
---|
265 | JPanel pnl = new JPanel();
|
---|
266 | pnl.setLayout(new GridBagLayout());
|
---|
267 |
|
---|
268 | GridBagConstraints gc = new GridBagConstraints();
|
---|
269 | gc.gridx = 0;
|
---|
270 | gc.gridy = 0;
|
---|
271 | gc.gridheight = 1;
|
---|
272 | gc.gridwidth = 1;
|
---|
273 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
274 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
275 | gc.weightx = 1.0;
|
---|
276 | gc.weighty = 0.0;
|
---|
277 | pnl.add(new JLabel(tr("Tags")), gc);
|
---|
278 |
|
---|
279 | gc.gridx = 0;
|
---|
280 | gc.gridy = 1;
|
---|
281 | gc.fill = GridBagConstraints.BOTH;
|
---|
282 | gc.anchor = GridBagConstraints.CENTER;
|
---|
283 | gc.weightx = 1.0;
|
---|
284 | gc.weighty = 1.0;
|
---|
285 | pnl.add(tagEditorPanel, gc);
|
---|
286 | return pnl;
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * builds the panel for the relation member editor
|
---|
291 | *
|
---|
292 | * @return the panel for the relation member editor
|
---|
293 | */
|
---|
294 | protected JPanel buildMemberEditorPanel() {
|
---|
295 | final JPanel pnl = new JPanel(new GridBagLayout());
|
---|
296 | // setting up the member table
|
---|
297 | memberTable = new MemberTable(getLayer(), getRelation(), memberTableModel);
|
---|
298 | memberTable.addMouseListener(new MemberTableDblClickAdapter());
|
---|
299 | memberTableModel.addMemberModelListener(memberTable);
|
---|
300 |
|
---|
301 | final JScrollPane scrollPane = new JScrollPane(memberTable);
|
---|
302 |
|
---|
303 | GridBagConstraints gc = new GridBagConstraints();
|
---|
304 | gc.gridx = 0;
|
---|
305 | gc.gridy = 0;
|
---|
306 | gc.gridwidth = 2;
|
---|
307 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
308 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
309 | gc.weightx = 1.0;
|
---|
310 | gc.weighty = 0.0;
|
---|
311 | pnl.add(new JLabel(tr("Members")), gc);
|
---|
312 |
|
---|
313 | gc.gridx = 0;
|
---|
314 | gc.gridy = 1;
|
---|
315 | gc.gridheight = 2;
|
---|
316 | gc.gridwidth = 1;
|
---|
317 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
318 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
319 | gc.weightx = 0.0;
|
---|
320 | gc.weighty = 1.0;
|
---|
321 | pnl.add(buildLeftButtonPanel(), gc);
|
---|
322 |
|
---|
323 | gc.gridx = 1;
|
---|
324 | gc.gridy = 1;
|
---|
325 | gc.gridheight = 1;
|
---|
326 | gc.fill = GridBagConstraints.BOTH;
|
---|
327 | gc.anchor = GridBagConstraints.CENTER;
|
---|
328 | gc.weightx = 0.6;
|
---|
329 | gc.weighty = 1.0;
|
---|
330 | pnl.add(scrollPane, gc);
|
---|
331 |
|
---|
332 | // --- role editing
|
---|
333 | JPanel p3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
|
---|
334 | p3.add(new JLabel(tr("Apply Role:")));
|
---|
335 | tfRole = new AutoCompletingTextField(10);
|
---|
336 | tfRole.setToolTipText(tr("Enter a role and apply it to the selected relation members"));
|
---|
337 | tfRole.addFocusListener(new FocusAdapter() {
|
---|
338 | @Override
|
---|
339 | public void focusGained(FocusEvent e) {
|
---|
340 | tfRole.selectAll();
|
---|
341 | }
|
---|
342 | });
|
---|
343 | tfRole.setAutoCompletionList(new AutoCompletionList());
|
---|
344 | tfRole.addFocusListener(
|
---|
345 | new FocusAdapter() {
|
---|
346 | @Override
|
---|
347 | public void focusGained(FocusEvent e) {
|
---|
348 | AutoCompletionList list = tfRole.getAutoCompletionList();
|
---|
349 | if (list != null) {
|
---|
350 | list.clear();
|
---|
351 | getLayer().data.getAutoCompletionManager().populateWithMemberRoles(list, getRelation());
|
---|
352 | }
|
---|
353 | }
|
---|
354 | }
|
---|
355 | );
|
---|
356 | tfRole.setText(Main.pref.get("relation.editor.generic.lastrole", ""));
|
---|
357 | p3.add(tfRole);
|
---|
358 | SetRoleAction setRoleAction = new SetRoleAction();
|
---|
359 | memberTableModel.getSelectionModel().addListSelectionListener(setRoleAction);
|
---|
360 | tfRole.getDocument().addDocumentListener(setRoleAction);
|
---|
361 | tfRole.addActionListener(setRoleAction);
|
---|
362 | memberTableModel.getSelectionModel().addListSelectionListener(
|
---|
363 | new ListSelectionListener() {
|
---|
364 | @Override
|
---|
365 | public void valueChanged(ListSelectionEvent e) {
|
---|
366 | tfRole.setEnabled(memberTable.getSelectedRowCount() > 0);
|
---|
367 | }
|
---|
368 | }
|
---|
369 | );
|
---|
370 | tfRole.setEnabled(memberTable.getSelectedRowCount() > 0);
|
---|
371 | SideButton btnApply = new SideButton(setRoleAction);
|
---|
372 | btnApply.setPreferredSize(new Dimension(20,20));
|
---|
373 | btnApply.setText("");
|
---|
374 | p3.add(btnApply);
|
---|
375 |
|
---|
376 | gc.gridx = 1;
|
---|
377 | gc.gridy = 2;
|
---|
378 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
379 | gc.anchor = GridBagConstraints.LAST_LINE_START;
|
---|
380 | gc.weightx = 1.0;
|
---|
381 | gc.weighty = 0.0;
|
---|
382 | pnl.add(p3, gc);
|
---|
383 |
|
---|
384 | JPanel pnl2 = new JPanel();
|
---|
385 | pnl2.setLayout(new GridBagLayout());
|
---|
386 |
|
---|
387 | gc.gridx = 0;
|
---|
388 | gc.gridy = 0;
|
---|
389 | gc.gridheight = 1;
|
---|
390 | gc.gridwidth = 3;
|
---|
391 | gc.fill = GridBagConstraints.HORIZONTAL;
|
---|
392 | gc.anchor = GridBagConstraints.FIRST_LINE_START;
|
---|
393 | gc.weightx = 1.0;
|
---|
394 | gc.weighty = 0.0;
|
---|
395 | pnl2.add(new JLabel(tr("Selection")), gc);
|
---|
396 |
|
---|
397 | gc.gridx = 0;
|
---|
398 | gc.gridy = 1;
|
---|
399 | gc.gridheight = 1;
|
---|
400 | gc.gridwidth = 1;
|
---|
401 | gc.fill = GridBagConstraints.VERTICAL;
|
---|
402 | gc.anchor = GridBagConstraints.NORTHWEST;
|
---|
403 | gc.weightx = 0.0;
|
---|
404 | gc.weighty = 1.0;
|
---|
405 | pnl2.add(buildSelectionControlButtonPanel(), gc);
|
---|
406 |
|
---|
407 | gc.gridx = 1;
|
---|
408 | gc.gridy = 1;
|
---|
409 | gc.weightx = 1.0;
|
---|
410 | gc.weighty = 1.0;
|
---|
411 | gc.fill = GridBagConstraints.BOTH;
|
---|
412 | pnl2.add(buildSelectionTablePanel(), gc);
|
---|
413 |
|
---|
414 | final JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
|
---|
415 | splitPane.setLeftComponent(pnl);
|
---|
416 | splitPane.setRightComponent(pnl2);
|
---|
417 | splitPane.setOneTouchExpandable(false);
|
---|
418 | addWindowListener(new WindowAdapter() {
|
---|
419 | @Override
|
---|
420 | public void windowOpened(WindowEvent e) {
|
---|
421 | // has to be called when the window is visible, otherwise
|
---|
422 | // no effect
|
---|
423 | splitPane.setDividerLocation(0.6);
|
---|
424 | }
|
---|
425 | });
|
---|
426 |
|
---|
427 | JPanel pnl3 = new JPanel();
|
---|
428 | pnl3.setLayout(new BorderLayout());
|
---|
429 | pnl3.add(splitPane, BorderLayout.CENTER);
|
---|
430 |
|
---|
431 | return pnl3;
|
---|
432 | }
|
---|
433 |
|
---|
434 | /**
|
---|
435 | * builds the panel with the table displaying the currently selected primitives
|
---|
436 | *
|
---|
437 | * @return panel with current selection
|
---|
438 | */
|
---|
439 | protected JPanel buildSelectionTablePanel() {
|
---|
440 | JPanel pnl = new JPanel(new BorderLayout());
|
---|
441 | MemberRoleCellEditor ce = (MemberRoleCellEditor)memberTable.getColumnModel().getColumn(0).getCellEditor();
|
---|
442 | selectionTable = new SelectionTable(selectionTableModel, new SelectionTableColumnModel(memberTableModel));
|
---|
443 | selectionTable.setMemberTableModel(memberTableModel);
|
---|
444 | selectionTable.setRowHeight(ce.getEditor().getPreferredSize().height);
|
---|
445 | pnl.add(new JScrollPane(selectionTable), BorderLayout.CENTER);
|
---|
446 | return pnl;
|
---|
447 | }
|
---|
448 |
|
---|
449 | /**
|
---|
450 | * builds the {@link JSplitPane} which divides the editor in an upper and a lower half
|
---|
451 | *
|
---|
452 | * @return the split panel
|
---|
453 | */
|
---|
454 | protected JSplitPane buildSplitPane(Relation relation) {
|
---|
455 | final JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
|
---|
456 | pane.setTopComponent(buildTagEditorPanel());
|
---|
457 | pane.setBottomComponent(buildMemberEditorPanel());
|
---|
458 | pane.setOneTouchExpandable(true);
|
---|
459 | addWindowListener(new WindowAdapter() {
|
---|
460 | @Override
|
---|
461 | public void windowOpened(WindowEvent e) {
|
---|
462 | // has to be called when the window is visible, otherwise
|
---|
463 | // no effect
|
---|
464 | pane.setDividerLocation(0.3);
|
---|
465 | }
|
---|
466 | });
|
---|
467 | return pane;
|
---|
468 | }
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * build the panel with the buttons on the left
|
---|
472 | *
|
---|
473 | * @return left button panel
|
---|
474 | */
|
---|
475 | protected JToolBar buildLeftButtonPanel() {
|
---|
476 | JToolBar tb = new JToolBar();
|
---|
477 | tb.setOrientation(JToolBar.VERTICAL);
|
---|
478 | tb.setFloatable(false);
|
---|
479 |
|
---|
480 | // -- move up action
|
---|
481 | MoveUpAction moveUpAction = new MoveUpAction();
|
---|
482 | memberTableModel.getSelectionModel().addListSelectionListener(moveUpAction);
|
---|
483 | tb.add(moveUpAction);
|
---|
484 | memberTable.getActionMap().put("moveUp", moveUpAction);
|
---|
485 |
|
---|
486 | // -- move down action
|
---|
487 | MoveDownAction moveDownAction = new MoveDownAction();
|
---|
488 | memberTableModel.getSelectionModel().addListSelectionListener(moveDownAction);
|
---|
489 | tb.add(moveDownAction);
|
---|
490 | memberTable.getActionMap().put("moveDown", moveDownAction);
|
---|
491 |
|
---|
492 | tb.addSeparator();
|
---|
493 |
|
---|
494 | // -- edit action
|
---|
495 | EditAction editAction = new EditAction();
|
---|
496 | memberTableModel.getSelectionModel().addListSelectionListener(editAction);
|
---|
497 | tb.add(editAction);
|
---|
498 |
|
---|
499 | // -- delete action
|
---|
500 | RemoveAction removeSelectedAction = new RemoveAction();
|
---|
501 | memberTable.getSelectionModel().addListSelectionListener(removeSelectedAction);
|
---|
502 | tb.add(removeSelectedAction);
|
---|
503 | memberTable.getActionMap().put("removeSelected", removeSelectedAction);
|
---|
504 |
|
---|
505 | tb.addSeparator();
|
---|
506 | // -- sort action
|
---|
507 | SortAction sortAction = new SortAction();
|
---|
508 | memberTableModel.addTableModelListener(sortAction);
|
---|
509 | tb.add(sortAction);
|
---|
510 |
|
---|
511 | // -- reverse action
|
---|
512 | ReverseAction reverseAction = new ReverseAction();
|
---|
513 | memberTableModel.addTableModelListener(reverseAction);
|
---|
514 | tb.add(reverseAction);
|
---|
515 |
|
---|
516 | tb.addSeparator();
|
---|
517 |
|
---|
518 | // -- download action
|
---|
519 | DownloadIncompleteMembersAction downloadIncompleteMembersAction = new DownloadIncompleteMembersAction();
|
---|
520 | memberTable.getModel().addTableModelListener(downloadIncompleteMembersAction);
|
---|
521 | tb.add(downloadIncompleteMembersAction);
|
---|
522 | memberTable.getActionMap().put("downloadIncomplete", downloadIncompleteMembersAction);
|
---|
523 |
|
---|
524 | // -- download selected action
|
---|
525 | DownloadSelectedIncompleteMembersAction downloadSelectedIncompleteMembersAction = new DownloadSelectedIncompleteMembersAction();
|
---|
526 | memberTable.getModel().addTableModelListener(downloadSelectedIncompleteMembersAction);
|
---|
527 | memberTable.getSelectionModel().addListSelectionListener(downloadSelectedIncompleteMembersAction);
|
---|
528 | tb.add(downloadSelectedIncompleteMembersAction);
|
---|
529 |
|
---|
530 | InputMap inputMap = memberTable.getInputMap(MemberTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
---|
531 | inputMap.put((KeyStroke) removeSelectedAction.getValue(AbstractAction.ACCELERATOR_KEY),"removeSelected");
|
---|
532 | inputMap.put((KeyStroke) moveUpAction.getValue(AbstractAction.ACCELERATOR_KEY),"moveUp");
|
---|
533 | inputMap.put((KeyStroke) moveDownAction.getValue(AbstractAction.ACCELERATOR_KEY),"moveDown");
|
---|
534 | inputMap.put((KeyStroke) downloadIncompleteMembersAction.getValue(AbstractAction.ACCELERATOR_KEY),"downloadIncomplete");
|
---|
535 |
|
---|
536 | return tb;
|
---|
537 | }
|
---|
538 |
|
---|
539 | /**
|
---|
540 | * build the panel with the buttons for adding or removing the current selection
|
---|
541 | *
|
---|
542 | * @return control buttons panel for selection/members
|
---|
543 | */
|
---|
544 | protected JToolBar buildSelectionControlButtonPanel() {
|
---|
545 | JToolBar tb = new JToolBar(JToolBar.VERTICAL);
|
---|
546 | tb.setFloatable(false);
|
---|
547 |
|
---|
548 | // -- add at start action
|
---|
549 | AddSelectedAtStartAction addSelectionAction = new AddSelectedAtStartAction();
|
---|
550 | selectionTableModel.addTableModelListener(addSelectionAction);
|
---|
551 | tb.add(addSelectionAction);
|
---|
552 |
|
---|
553 | // -- add before selected action
|
---|
554 | AddSelectedBeforeSelection addSelectedBeforeSelectionAction = new AddSelectedBeforeSelection();
|
---|
555 | selectionTableModel.addTableModelListener(addSelectedBeforeSelectionAction);
|
---|
556 | memberTableModel.getSelectionModel().addListSelectionListener(addSelectedBeforeSelectionAction);
|
---|
557 | tb.add(addSelectedBeforeSelectionAction);
|
---|
558 |
|
---|
559 | // -- add after selected action
|
---|
560 | AddSelectedAfterSelection addSelectedAfterSelectionAction = new AddSelectedAfterSelection();
|
---|
561 | selectionTableModel.addTableModelListener(addSelectedAfterSelectionAction);
|
---|
562 | memberTableModel.getSelectionModel().addListSelectionListener(addSelectedAfterSelectionAction);
|
---|
563 | tb.add(addSelectedAfterSelectionAction);
|
---|
564 |
|
---|
565 | // -- add at end action
|
---|
566 | AddSelectedAtEndAction addSelectedAtEndAction = new AddSelectedAtEndAction();
|
---|
567 | selectionTableModel.addTableModelListener(addSelectedAtEndAction);
|
---|
568 | tb.add(addSelectedAtEndAction);
|
---|
569 |
|
---|
570 | tb.addSeparator();
|
---|
571 |
|
---|
572 | // -- select members action
|
---|
573 | SelectedMembersForSelectionAction selectMembersForSelectionAction = new SelectedMembersForSelectionAction();
|
---|
574 | selectionTableModel.addTableModelListener(selectMembersForSelectionAction);
|
---|
575 | memberTableModel.addTableModelListener(selectMembersForSelectionAction);
|
---|
576 | tb.add(selectMembersForSelectionAction);
|
---|
577 |
|
---|
578 | // -- select action
|
---|
579 | SelectPrimitivesForSelectedMembersAction selectAction = new SelectPrimitivesForSelectedMembersAction();
|
---|
580 | memberTable.getSelectionModel().addListSelectionListener(selectAction);
|
---|
581 | tb.add(selectAction);
|
---|
582 |
|
---|
583 | tb.addSeparator();
|
---|
584 |
|
---|
585 | // -- remove selected action
|
---|
586 | RemoveSelectedAction removeSelectedAction = new RemoveSelectedAction();
|
---|
587 | selectionTableModel.addTableModelListener(removeSelectedAction);
|
---|
588 | tb.add(removeSelectedAction);
|
---|
589 |
|
---|
590 | return tb;
|
---|
591 | }
|
---|
592 |
|
---|
593 | @Override
|
---|
594 | protected Dimension findMaxDialogSize() {
|
---|
595 | return new Dimension(700, 650);
|
---|
596 | }
|
---|
597 |
|
---|
598 | @Override
|
---|
599 | public void setVisible(boolean visible) {
|
---|
600 | if (visible) {
|
---|
601 | tagEditorPanel.initAutoCompletion(getLayer());
|
---|
602 | }
|
---|
603 | super.setVisible(visible);
|
---|
604 | if (visible) {
|
---|
605 | RelationDialogManager.getRelationDialogManager().positionOnScreen(this);
|
---|
606 | if(windowMenuItem == null) {
|
---|
607 | addToWindowMenu();
|
---|
608 | }
|
---|
609 | tagEditorPanel.requestFocusInWindow();
|
---|
610 | } else {
|
---|
611 | // make sure all registered listeners are unregistered
|
---|
612 | //
|
---|
613 | memberTable.stopHighlighting();
|
---|
614 | selectionTableModel.unregister();
|
---|
615 | memberTableModel.unregister();
|
---|
616 | memberTable.unlinkAsListener();
|
---|
617 | if(windowMenuItem != null) {
|
---|
618 | Main.main.menu.windowMenu.remove(windowMenuItem);
|
---|
619 | windowMenuItem = null;
|
---|
620 | }
|
---|
621 | dispose();
|
---|
622 | }
|
---|
623 | }
|
---|
624 |
|
---|
625 | /** adds current relation editor to the windows menu (in the "volatile" group) o*/
|
---|
626 | protected void addToWindowMenu() {
|
---|
627 | String name = getRelation() == null ? tr("New Relation") : getRelation().getLocalName();
|
---|
628 | final String tt = tr("Focus Relation Editor with relation ''{0}'' in layer ''{1}''",
|
---|
629 | name, getLayer().getName());
|
---|
630 | name = tr("Relation Editor: {0}", name == null ? getRelation().getId() : name);
|
---|
631 | final JMenu wm = Main.main.menu.windowMenu;
|
---|
632 | final JosmAction focusAction = new JosmAction(name, "dialogs/relationlist", tt, null, false, false) {
|
---|
633 | @Override
|
---|
634 | public void actionPerformed(ActionEvent e) {
|
---|
635 | final RelationEditor r = (RelationEditor) getValue("relationEditor");
|
---|
636 | r.setVisible(true);
|
---|
637 | }
|
---|
638 | };
|
---|
639 | focusAction.putValue("relationEditor", this);
|
---|
640 | windowMenuItem = MainMenu.add(wm, focusAction, MainMenu.WINDOW_MENU_GROUP.VOLATILE);
|
---|
641 | }
|
---|
642 |
|
---|
643 | /**
|
---|
644 | * checks whether the current relation has members referring to itself. If so,
|
---|
645 | * warns the users and provides an option for removing these members.
|
---|
646 | *
|
---|
647 | */
|
---|
648 | protected void cleanSelfReferences() {
|
---|
649 | List<OsmPrimitive> toCheck = new ArrayList<>();
|
---|
650 | toCheck.add(getRelation());
|
---|
651 | if (memberTableModel.hasMembersReferringTo(toCheck)) {
|
---|
652 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
653 | "clean_relation_self_references",
|
---|
654 | Main.parent,
|
---|
655 | tr("<html>There is at least one member in this relation referring<br>"
|
---|
656 | + "to the relation itself.<br>"
|
---|
657 | + "This creates circular dependencies and is discouraged.<br>"
|
---|
658 | + "How do you want to proceed with circular dependencies?</html>"),
|
---|
659 | tr("Warning"),
|
---|
660 | JOptionPane.YES_NO_OPTION,
|
---|
661 | JOptionPane.WARNING_MESSAGE,
|
---|
662 | new String[]{tr("Remove them, clean up relation"), tr("Ignore them, leave relation as is")},
|
---|
663 | tr("Remove them, clean up relation")
|
---|
664 | );
|
---|
665 | switch(ret) {
|
---|
666 | case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
|
---|
667 | case JOptionPane.CLOSED_OPTION:
|
---|
668 | case JOptionPane.NO_OPTION:
|
---|
669 | return;
|
---|
670 | case JOptionPane.YES_OPTION:
|
---|
671 | memberTableModel.removeMembersReferringTo(toCheck);
|
---|
672 | break;
|
---|
673 | }
|
---|
674 | }
|
---|
675 | }
|
---|
676 |
|
---|
677 |
|
---|
678 | private void registerCopyPasteAction(AbstractAction action, Object actionName, KeyStroke shortcut) {
|
---|
679 | int mods = shortcut.getModifiers();
|
---|
680 | int code = shortcut.getKeyCode();
|
---|
681 | if (code!=KeyEvent.VK_INSERT && (mods == 0 || mods == InputEvent.SHIFT_DOWN_MASK)) {
|
---|
682 | Main.info(tr("Sorry, shortcut \"{0}\" can not be enabled in Relation editor dialog"), shortcut);
|
---|
683 | return;
|
---|
684 | }
|
---|
685 | getRootPane().getActionMap().put(actionName, action);
|
---|
686 | getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(shortcut, actionName);
|
---|
687 | // Assign also to JTables because they have their own Copy&Paste implementation (which is disabled in this case but eats key shortcuts anyway)
|
---|
688 | memberTable.getInputMap(JComponent.WHEN_FOCUSED).put(shortcut, actionName);
|
---|
689 | memberTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(shortcut, actionName);
|
---|
690 | memberTable.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(shortcut, actionName);
|
---|
691 | selectionTable.getInputMap(JComponent.WHEN_FOCUSED).put(shortcut, actionName);
|
---|
692 | selectionTable.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(shortcut, actionName);
|
---|
693 | selectionTable.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(shortcut, actionName);
|
---|
694 | }
|
---|
695 |
|
---|
696 | static class AddAbortException extends Exception {
|
---|
697 | }
|
---|
698 |
|
---|
699 | static boolean confirmAddingPrimitive(OsmPrimitive primitive) throws AddAbortException {
|
---|
700 | String msg = tr("<html>This relation already has one or more members referring to<br>"
|
---|
701 | + "the object ''{0}''<br>"
|
---|
702 | + "<br>"
|
---|
703 | + "Do you really want to add another relation member?</html>",
|
---|
704 | primitive.getDisplayName(DefaultNameFormatter.getInstance())
|
---|
705 | );
|
---|
706 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
707 | "add_primitive_to_relation",
|
---|
708 | Main.parent,
|
---|
709 | msg,
|
---|
710 | tr("Multiple members referring to same object."),
|
---|
711 | JOptionPane.YES_NO_CANCEL_OPTION,
|
---|
712 | JOptionPane.WARNING_MESSAGE,
|
---|
713 | null,
|
---|
714 | null
|
---|
715 | );
|
---|
716 | switch(ret) {
|
---|
717 | case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
|
---|
718 | case JOptionPane.YES_OPTION:
|
---|
719 | return true;
|
---|
720 | case JOptionPane.NO_OPTION:
|
---|
721 | case JOptionPane.CLOSED_OPTION:
|
---|
722 | return false;
|
---|
723 | case JOptionPane.CANCEL_OPTION:
|
---|
724 | throw new AddAbortException();
|
---|
725 | }
|
---|
726 | // should not happen
|
---|
727 | return false;
|
---|
728 | }
|
---|
729 |
|
---|
730 | static void warnOfCircularReferences(OsmPrimitive primitive) {
|
---|
731 | String msg = tr("<html>You are trying to add a relation to itself.<br>"
|
---|
732 | + "<br>"
|
---|
733 | + "This creates circular references and is therefore discouraged.<br>"
|
---|
734 | + "Skipping relation ''{0}''.</html>",
|
---|
735 | primitive.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
736 | JOptionPane.showMessageDialog(
|
---|
737 | Main.parent,
|
---|
738 | msg,
|
---|
739 | tr("Warning"),
|
---|
740 | JOptionPane.WARNING_MESSAGE);
|
---|
741 | }
|
---|
742 |
|
---|
743 | /**
|
---|
744 | * Adds primitives to a given relation.
|
---|
745 | * @param orig The relation to modify
|
---|
746 | * @param primitivesToAdd The primitives to add as relation members
|
---|
747 | * @return The resulting command
|
---|
748 | * @throws IllegalArgumentException if orig is null
|
---|
749 | */
|
---|
750 | public static Command addPrimitivesToRelation(final Relation orig, Collection<? extends OsmPrimitive> primitivesToAdd) {
|
---|
751 | CheckParameterUtil.ensureParameterNotNull(orig, "orig");
|
---|
752 | try {
|
---|
753 | final Collection<TaggingPreset> presets = TaggingPreset.getMatchingPresets(
|
---|
754 | EnumSet.of(TaggingPresetType.RELATION), orig.getKeys(), false);
|
---|
755 | Relation relation = new Relation(orig);
|
---|
756 | boolean modified = false;
|
---|
757 | for (OsmPrimitive p : primitivesToAdd) {
|
---|
758 | if (p instanceof Relation && orig.equals(p)) {
|
---|
759 | warnOfCircularReferences(p);
|
---|
760 | continue;
|
---|
761 | } else if (MemberTableModel.hasMembersReferringTo(relation.getMembers(), Collections.singleton(p))
|
---|
762 | && !confirmAddingPrimitive(p)) {
|
---|
763 | continue;
|
---|
764 | }
|
---|
765 | final Set<String> roles = findSuggestedRoles(presets, p);
|
---|
766 | relation.addMember(new RelationMember(roles.size() == 1 ? roles.iterator().next() : "", p));
|
---|
767 | modified = true;
|
---|
768 | }
|
---|
769 | return modified ? new ChangeCommand(orig, relation) : null;
|
---|
770 | } catch (AddAbortException ign) {
|
---|
771 | return null;
|
---|
772 | }
|
---|
773 | }
|
---|
774 |
|
---|
775 | protected static Set<String> findSuggestedRoles(final Collection<TaggingPreset> presets, OsmPrimitive p) {
|
---|
776 | final Set<String> roles = new HashSet<>();
|
---|
777 | for (TaggingPreset preset : presets) {
|
---|
778 | String role = preset.suggestRoleForOsmPrimitive(p);
|
---|
779 | if (role != null && !role.isEmpty()) {
|
---|
780 | roles.add(role);
|
---|
781 | }
|
---|
782 | }
|
---|
783 | return roles;
|
---|
784 | }
|
---|
785 |
|
---|
786 | abstract class AddFromSelectionAction extends AbstractAction {
|
---|
787 | protected boolean isPotentialDuplicate(OsmPrimitive primitive) {
|
---|
788 | return memberTableModel.hasMembersReferringTo(Collections.singleton(primitive));
|
---|
789 | }
|
---|
790 |
|
---|
791 | protected List<OsmPrimitive> filterConfirmedPrimitives(List<OsmPrimitive> primitives) throws AddAbortException {
|
---|
792 | if (primitives == null || primitives.isEmpty())
|
---|
793 | return primitives;
|
---|
794 | List<OsmPrimitive> ret = new ArrayList<>();
|
---|
795 | ConditionalOptionPaneUtil.startBulkOperation("add_primitive_to_relation");
|
---|
796 | for (OsmPrimitive primitive : primitives) {
|
---|
797 | if (primitive instanceof Relation && getRelation() != null && getRelation().equals(primitive)) {
|
---|
798 | warnOfCircularReferences(primitive);
|
---|
799 | continue;
|
---|
800 | }
|
---|
801 | if (isPotentialDuplicate(primitive)) {
|
---|
802 | if (confirmAddingPrimitive(primitive)) {
|
---|
803 | ret.add(primitive);
|
---|
804 | }
|
---|
805 | continue;
|
---|
806 | } else {
|
---|
807 | ret.add(primitive);
|
---|
808 | }
|
---|
809 | }
|
---|
810 | ConditionalOptionPaneUtil.endBulkOperation("add_primitive_to_relation");
|
---|
811 | return ret;
|
---|
812 | }
|
---|
813 | }
|
---|
814 |
|
---|
815 | class AddSelectedAtStartAction extends AddFromSelectionAction implements TableModelListener {
|
---|
816 | public AddSelectedAtStartAction() {
|
---|
817 | putValue(SHORT_DESCRIPTION,
|
---|
818 | tr("Add all objects selected in the current dataset before the first member"));
|
---|
819 | putValue(SMALL_ICON, ImageProvider.get("dialogs/conflict", "copystartright"));
|
---|
820 | refreshEnabled();
|
---|
821 | }
|
---|
822 |
|
---|
823 | protected void refreshEnabled() {
|
---|
824 | setEnabled(selectionTableModel.getRowCount() > 0);
|
---|
825 | }
|
---|
826 |
|
---|
827 | @Override
|
---|
828 | public void actionPerformed(ActionEvent e) {
|
---|
829 | try {
|
---|
830 | List<OsmPrimitive> toAdd = filterConfirmedPrimitives(selectionTableModel.getSelection());
|
---|
831 | memberTableModel.addMembersAtBeginning(toAdd);
|
---|
832 | } catch(AddAbortException ex) {
|
---|
833 | // do nothing
|
---|
834 | }
|
---|
835 | }
|
---|
836 |
|
---|
837 | @Override
|
---|
838 | public void tableChanged(TableModelEvent e) {
|
---|
839 | refreshEnabled();
|
---|
840 | }
|
---|
841 | }
|
---|
842 |
|
---|
843 | class AddSelectedAtEndAction extends AddFromSelectionAction implements TableModelListener {
|
---|
844 | public AddSelectedAtEndAction() {
|
---|
845 | putValue(SHORT_DESCRIPTION, tr("Add all objects selected in the current dataset after the last member"));
|
---|
846 | putValue(SMALL_ICON, ImageProvider.get("dialogs/conflict", "copyendright"));
|
---|
847 | refreshEnabled();
|
---|
848 | }
|
---|
849 |
|
---|
850 | protected void refreshEnabled() {
|
---|
851 | setEnabled(selectionTableModel.getRowCount() > 0);
|
---|
852 | }
|
---|
853 |
|
---|
854 | @Override
|
---|
855 | public void actionPerformed(ActionEvent e) {
|
---|
856 | try {
|
---|
857 | List<OsmPrimitive> toAdd = filterConfirmedPrimitives(selectionTableModel.getSelection());
|
---|
858 | memberTableModel.addMembersAtEnd(toAdd);
|
---|
859 | } catch(AddAbortException ex) {
|
---|
860 | // do nothing
|
---|
861 | }
|
---|
862 | }
|
---|
863 |
|
---|
864 | @Override
|
---|
865 | public void tableChanged(TableModelEvent e) {
|
---|
866 | refreshEnabled();
|
---|
867 | }
|
---|
868 | }
|
---|
869 |
|
---|
870 | class AddSelectedBeforeSelection extends AddFromSelectionAction implements TableModelListener, ListSelectionListener {
|
---|
871 | public AddSelectedBeforeSelection() {
|
---|
872 | putValue(SHORT_DESCRIPTION,
|
---|
873 | tr("Add all objects selected in the current dataset before the first selected member"));
|
---|
874 | putValue(SMALL_ICON, ImageProvider.get("dialogs/conflict", "copybeforecurrentright"));
|
---|
875 | refreshEnabled();
|
---|
876 | }
|
---|
877 |
|
---|
878 | protected void refreshEnabled() {
|
---|
879 | setEnabled(selectionTableModel.getRowCount() > 0
|
---|
880 | && memberTableModel.getSelectionModel().getMinSelectionIndex() >= 0);
|
---|
881 | }
|
---|
882 |
|
---|
883 | @Override
|
---|
884 | public void actionPerformed(ActionEvent e) {
|
---|
885 | try {
|
---|
886 | List<OsmPrimitive> toAdd = filterConfirmedPrimitives(selectionTableModel.getSelection());
|
---|
887 | memberTableModel.addMembersBeforeIdx(toAdd, memberTableModel
|
---|
888 | .getSelectionModel().getMinSelectionIndex());
|
---|
889 | } catch(AddAbortException ex) {
|
---|
890 | // do nothing
|
---|
891 | }
|
---|
892 |
|
---|
893 | }
|
---|
894 |
|
---|
895 | @Override
|
---|
896 | public void tableChanged(TableModelEvent e) {
|
---|
897 | refreshEnabled();
|
---|
898 | }
|
---|
899 |
|
---|
900 | @Override
|
---|
901 | public void valueChanged(ListSelectionEvent e) {
|
---|
902 | refreshEnabled();
|
---|
903 | }
|
---|
904 | }
|
---|
905 |
|
---|
906 | class AddSelectedAfterSelection extends AddFromSelectionAction implements TableModelListener, ListSelectionListener {
|
---|
907 | public AddSelectedAfterSelection() {
|
---|
908 | putValue(SHORT_DESCRIPTION,
|
---|
909 | tr("Add all objects selected in the current dataset after the last selected member"));
|
---|
910 | putValue(SMALL_ICON, ImageProvider.get("dialogs/conflict", "copyaftercurrentright"));
|
---|
911 | refreshEnabled();
|
---|
912 | }
|
---|
913 |
|
---|
914 | protected void refreshEnabled() {
|
---|
915 | setEnabled(selectionTableModel.getRowCount() > 0
|
---|
916 | && memberTableModel.getSelectionModel().getMinSelectionIndex() >= 0);
|
---|
917 | }
|
---|
918 |
|
---|
919 | @Override
|
---|
920 | public void actionPerformed(ActionEvent e) {
|
---|
921 | try {
|
---|
922 | List<OsmPrimitive> toAdd = filterConfirmedPrimitives(selectionTableModel.getSelection());
|
---|
923 | memberTableModel.addMembersAfterIdx(toAdd, memberTableModel
|
---|
924 | .getSelectionModel().getMaxSelectionIndex());
|
---|
925 | } catch(AddAbortException ex) {
|
---|
926 | // do nothing
|
---|
927 | }
|
---|
928 | }
|
---|
929 |
|
---|
930 | @Override
|
---|
931 | public void tableChanged(TableModelEvent e) {
|
---|
932 | refreshEnabled();
|
---|
933 | }
|
---|
934 |
|
---|
935 | @Override
|
---|
936 | public void valueChanged(ListSelectionEvent e) {
|
---|
937 | refreshEnabled();
|
---|
938 | }
|
---|
939 | }
|
---|
940 |
|
---|
941 | class RemoveSelectedAction extends AbstractAction implements TableModelListener {
|
---|
942 | public RemoveSelectedAction() {
|
---|
943 | putValue(SHORT_DESCRIPTION, tr("Remove all members referring to one of the selected objects"));
|
---|
944 | putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "deletemembers"));
|
---|
945 | updateEnabledState();
|
---|
946 | }
|
---|
947 |
|
---|
948 | protected void updateEnabledState() {
|
---|
949 | DataSet ds = getLayer().data;
|
---|
950 | if (ds == null || ds.getSelected().isEmpty()) {
|
---|
951 | setEnabled(false);
|
---|
952 | return;
|
---|
953 | }
|
---|
954 | // only enable the action if we have members referring to the
|
---|
955 | // selected primitives
|
---|
956 | //
|
---|
957 | setEnabled(memberTableModel.hasMembersReferringTo(ds.getSelected()));
|
---|
958 | }
|
---|
959 |
|
---|
960 | @Override
|
---|
961 | public void actionPerformed(ActionEvent e) {
|
---|
962 | memberTableModel.removeMembersReferringTo(selectionTableModel.getSelection());
|
---|
963 | }
|
---|
964 |
|
---|
965 | @Override
|
---|
966 | public void tableChanged(TableModelEvent e) {
|
---|
967 | updateEnabledState();
|
---|
968 | }
|
---|
969 | }
|
---|
970 |
|
---|
971 | /**
|
---|
972 | * Selects members in the relation editor which refer to primitives in the current
|
---|
973 | * selection of the context layer.
|
---|
974 | *
|
---|
975 | */
|
---|
976 | class SelectedMembersForSelectionAction extends AbstractAction implements TableModelListener {
|
---|
977 | public SelectedMembersForSelectionAction() {
|
---|
978 | putValue(SHORT_DESCRIPTION, tr("Select relation members which refer to objects in the current selection"));
|
---|
979 | putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "selectmembers"));
|
---|
980 | updateEnabledState();
|
---|
981 | }
|
---|
982 |
|
---|
983 | protected void updateEnabledState() {
|
---|
984 | boolean enabled = selectionTableModel.getRowCount() > 0
|
---|
985 | && !memberTableModel.getChildPrimitives(getLayer().data.getSelected()).isEmpty();
|
---|
986 |
|
---|
987 | if (enabled) {
|
---|
988 | putValue(SHORT_DESCRIPTION, tr("Select relation members which refer to {0} objects in the current selection",memberTableModel.getChildPrimitives(getLayer().data.getSelected()).size()));
|
---|
989 | } else {
|
---|
990 | putValue(SHORT_DESCRIPTION, tr("Select relation members which refer to objects in the current selection"));
|
---|
991 | }
|
---|
992 | setEnabled(enabled);
|
---|
993 | }
|
---|
994 |
|
---|
995 | @Override
|
---|
996 | public void actionPerformed(ActionEvent e) {
|
---|
997 | memberTableModel.selectMembersReferringTo(getLayer().data.getSelected());
|
---|
998 | }
|
---|
999 |
|
---|
1000 | @Override
|
---|
1001 | public void tableChanged(TableModelEvent e) {
|
---|
1002 | updateEnabledState();
|
---|
1003 |
|
---|
1004 | }
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | /**
|
---|
1008 | * Selects primitives in the layer this editor belongs to. The selected primitives are
|
---|
1009 | * equal to the set of primitives the currently selected relation members refer to.
|
---|
1010 | *
|
---|
1011 | */
|
---|
1012 | class SelectPrimitivesForSelectedMembersAction extends AbstractAction implements ListSelectionListener {
|
---|
1013 | public SelectPrimitivesForSelectedMembersAction() {
|
---|
1014 | putValue(SHORT_DESCRIPTION, tr("Select objects for selected relation members"));
|
---|
1015 | putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "selectprimitives"));
|
---|
1016 | updateEnabledState();
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | protected void updateEnabledState() {
|
---|
1020 | setEnabled(memberTable.getSelectedRowCount() > 0);
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | @Override
|
---|
1024 | public void actionPerformed(ActionEvent e) {
|
---|
1025 | getLayer().data.setSelected(memberTableModel.getSelectedChildPrimitives());
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | @Override
|
---|
1029 | public void valueChanged(ListSelectionEvent e) {
|
---|
1030 | updateEnabledState();
|
---|
1031 | }
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | class SortAction extends AbstractAction implements TableModelListener {
|
---|
1035 | public SortAction() {
|
---|
1036 | String tooltip = tr("Sort the relation members");
|
---|
1037 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "sort"));
|
---|
1038 | putValue(NAME, tr("Sort"));
|
---|
1039 | Shortcut sc = Shortcut.registerShortcut("relationeditor:sort", tr("Relation Editor: Sort"),
|
---|
1040 | KeyEvent.VK_END, Shortcut.ALT);
|
---|
1041 | sc.setAccelerator(this);
|
---|
1042 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
|
---|
1043 | updateEnabledState();
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | @Override
|
---|
1047 | public void actionPerformed(ActionEvent e) {
|
---|
1048 | memberTableModel.sort();
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | protected void updateEnabledState() {
|
---|
1052 | setEnabled(memberTableModel.getRowCount() > 0);
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | @Override
|
---|
1056 | public void tableChanged(TableModelEvent e) {
|
---|
1057 | updateEnabledState();
|
---|
1058 | }
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | class ReverseAction extends AbstractAction implements TableModelListener {
|
---|
1062 | public ReverseAction() {
|
---|
1063 | putValue(SHORT_DESCRIPTION, tr("Reverse the order of the relation members"));
|
---|
1064 | putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "reverse"));
|
---|
1065 | putValue(NAME, tr("Reverse"));
|
---|
1066 | // Shortcut.register Shortcut("relationeditor:reverse", tr("Relation Editor: Reverse"),
|
---|
1067 | // KeyEvent.VK_END, Shortcut.ALT)
|
---|
1068 | updateEnabledState();
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | @Override
|
---|
1072 | public void actionPerformed(ActionEvent e) {
|
---|
1073 | memberTableModel.reverse();
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | protected void updateEnabledState() {
|
---|
1077 | setEnabled(memberTableModel.getRowCount() > 0);
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | @Override
|
---|
1081 | public void tableChanged(TableModelEvent e) {
|
---|
1082 | updateEnabledState();
|
---|
1083 | }
|
---|
1084 | }
|
---|
1085 |
|
---|
1086 | class MoveUpAction extends AbstractAction implements ListSelectionListener {
|
---|
1087 | public MoveUpAction() {
|
---|
1088 | String tooltip = tr("Move the currently selected members up");
|
---|
1089 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "moveup"));
|
---|
1090 | Shortcut sc = Shortcut.registerShortcut("relationeditor:moveup", tr("Relation Editor: Move Up"),
|
---|
1091 | KeyEvent.VK_UP, Shortcut.ALT);
|
---|
1092 | sc.setAccelerator(this);
|
---|
1093 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
|
---|
1094 | setEnabled(false);
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | @Override
|
---|
1098 | public void actionPerformed(ActionEvent e) {
|
---|
1099 | memberTableModel.moveUp(memberTable.getSelectedRows());
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | @Override
|
---|
1103 | public void valueChanged(ListSelectionEvent e) {
|
---|
1104 | setEnabled(memberTableModel.canMoveUp(memberTable.getSelectedRows()));
|
---|
1105 | }
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | class MoveDownAction extends AbstractAction implements ListSelectionListener {
|
---|
1109 | public MoveDownAction() {
|
---|
1110 | String tooltip = tr("Move the currently selected members down");
|
---|
1111 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "movedown"));
|
---|
1112 | Shortcut sc = Shortcut.registerShortcut("relationeditor:movedown", tr("Relation Editor: Move Down"),
|
---|
1113 | KeyEvent.VK_DOWN, Shortcut.ALT);
|
---|
1114 | sc.setAccelerator(this);
|
---|
1115 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
|
---|
1116 | setEnabled(false);
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | @Override
|
---|
1120 | public void actionPerformed(ActionEvent e) {
|
---|
1121 | memberTableModel.moveDown(memberTable.getSelectedRows());
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | @Override
|
---|
1125 | public void valueChanged(ListSelectionEvent e) {
|
---|
1126 | setEnabled(memberTableModel.canMoveDown(memberTable.getSelectedRows()));
|
---|
1127 | }
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | class RemoveAction extends AbstractAction implements ListSelectionListener {
|
---|
1131 | public RemoveAction() {
|
---|
1132 | String tooltip = tr("Remove the currently selected members from this relation");
|
---|
1133 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
|
---|
1134 | putValue(NAME, tr("Remove"));
|
---|
1135 | Shortcut sc = Shortcut.registerShortcut("relationeditor:remove", tr("Relation Editor: Remove"),
|
---|
1136 | KeyEvent.VK_DELETE, Shortcut.ALT);
|
---|
1137 | sc.setAccelerator(this);
|
---|
1138 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
|
---|
1139 | setEnabled(false);
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | @Override
|
---|
1143 | public void actionPerformed(ActionEvent e) {
|
---|
1144 | memberTableModel.remove(memberTable.getSelectedRows());
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | @Override
|
---|
1148 | public void valueChanged(ListSelectionEvent e) {
|
---|
1149 | setEnabled(memberTableModel.canRemove(memberTable.getSelectedRows()));
|
---|
1150 | }
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | class DeleteCurrentRelationAction extends AbstractAction implements PropertyChangeListener{
|
---|
1154 | public DeleteCurrentRelationAction() {
|
---|
1155 | putValue(SHORT_DESCRIPTION, tr("Delete the currently edited relation"));
|
---|
1156 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
|
---|
1157 | putValue(NAME, tr("Delete"));
|
---|
1158 | updateEnabledState();
|
---|
1159 | }
|
---|
1160 |
|
---|
1161 | public void run() {
|
---|
1162 | Relation toDelete = getRelation();
|
---|
1163 | if (toDelete == null)
|
---|
1164 | return;
|
---|
1165 | org.openstreetmap.josm.actions.mapmode.DeleteAction.deleteRelation(
|
---|
1166 | getLayer(),
|
---|
1167 | toDelete
|
---|
1168 | );
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | @Override
|
---|
1172 | public void actionPerformed(ActionEvent e) {
|
---|
1173 | run();
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | protected void updateEnabledState() {
|
---|
1177 | setEnabled(getRelationSnapshot() != null);
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | @Override
|
---|
1181 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
1182 | if (evt.getPropertyName().equals(RELATION_SNAPSHOT_PROP)) {
|
---|
1183 | updateEnabledState();
|
---|
1184 | }
|
---|
1185 | }
|
---|
1186 | }
|
---|
1187 |
|
---|
1188 | abstract class SavingAction extends AbstractAction {
|
---|
1189 | /**
|
---|
1190 | * apply updates to a new relation
|
---|
1191 | */
|
---|
1192 | protected void applyNewRelation() {
|
---|
1193 | final Relation newRelation = new Relation();
|
---|
1194 | tagEditorPanel.getModel().applyToPrimitive(newRelation);
|
---|
1195 | memberTableModel.applyToRelation(newRelation);
|
---|
1196 | List<RelationMember> newMembers = new ArrayList<>();
|
---|
1197 | for (RelationMember rm: newRelation.getMembers()) {
|
---|
1198 | if (!rm.getMember().isDeleted()) {
|
---|
1199 | newMembers.add(rm);
|
---|
1200 | }
|
---|
1201 | }
|
---|
1202 | if (newRelation.getMembersCount() != newMembers.size()) {
|
---|
1203 | newRelation.setMembers(newMembers);
|
---|
1204 | String msg = tr("One or more members of this new relation have been deleted while the relation editor\n" +
|
---|
1205 | "was open. They have been removed from the relation members list.");
|
---|
1206 | JOptionPane.showMessageDialog(Main.parent, msg, tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
1207 | }
|
---|
1208 | // If the user wanted to create a new relation, but hasn't added any members or
|
---|
1209 | // tags, don't add an empty relation
|
---|
1210 | if (newRelation.getMembersCount() == 0 && !newRelation.hasKeys())
|
---|
1211 | return;
|
---|
1212 | Main.main.undoRedo.add(new AddCommand(getLayer(),newRelation));
|
---|
1213 |
|
---|
1214 | // make sure everybody is notified about the changes
|
---|
1215 | //
|
---|
1216 | getLayer().data.fireSelectionChanged();
|
---|
1217 | GenericRelationEditor.this.setRelation(newRelation);
|
---|
1218 | RelationDialogManager.getRelationDialogManager().updateContext(
|
---|
1219 | getLayer(),
|
---|
1220 | getRelation(),
|
---|
1221 | GenericRelationEditor.this
|
---|
1222 | );
|
---|
1223 | SwingUtilities.invokeLater(new Runnable() {
|
---|
1224 | @Override
|
---|
1225 | public void run() {
|
---|
1226 | // Relation list gets update in EDT so selecting my be postponed to following EDT run
|
---|
1227 | Main.map.relationListDialog.selectRelation(newRelation);
|
---|
1228 | }
|
---|
1229 | });
|
---|
1230 | }
|
---|
1231 |
|
---|
1232 | /**
|
---|
1233 | * Apply the updates for an existing relation which has been changed
|
---|
1234 | * outside of the relation editor.
|
---|
1235 | *
|
---|
1236 | */
|
---|
1237 | protected void applyExistingConflictingRelation() {
|
---|
1238 | Relation editedRelation = new Relation(getRelation());
|
---|
1239 | tagEditorPanel.getModel().applyToPrimitive(editedRelation);
|
---|
1240 | memberTableModel.applyToRelation(editedRelation);
|
---|
1241 | Conflict<Relation> conflict = new Conflict<>(getRelation(), editedRelation);
|
---|
1242 | Main.main.undoRedo.add(new ConflictAddCommand(getLayer(),conflict));
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | /**
|
---|
1246 | * Apply the updates for an existing relation which has not been changed
|
---|
1247 | * outside of the relation editor.
|
---|
1248 | *
|
---|
1249 | */
|
---|
1250 | protected void applyExistingNonConflictingRelation() {
|
---|
1251 | Relation editedRelation = new Relation(getRelation());
|
---|
1252 | tagEditorPanel.getModel().applyToPrimitive(editedRelation);
|
---|
1253 | memberTableModel.applyToRelation(editedRelation);
|
---|
1254 | Main.main.undoRedo.add(new ChangeCommand(getRelation(), editedRelation));
|
---|
1255 | getLayer().data.fireSelectionChanged();
|
---|
1256 | // this will refresh the snapshot and update the dialog title
|
---|
1257 | //
|
---|
1258 | setRelation(getRelation());
|
---|
1259 | }
|
---|
1260 |
|
---|
1261 | protected boolean confirmClosingBecauseOfDirtyState() {
|
---|
1262 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
1263 | new ButtonSpec(
|
---|
1264 | tr("Yes, create a conflict and close"),
|
---|
1265 | ImageProvider.get("ok"),
|
---|
1266 | tr("Click to create a conflict and close this relation editor") ,
|
---|
1267 | null /* no specific help topic */
|
---|
1268 | ),
|
---|
1269 | new ButtonSpec(
|
---|
1270 | tr("No, continue editing"),
|
---|
1271 | ImageProvider.get("cancel"),
|
---|
1272 | tr("Click to return to the relation editor and to resume relation editing") ,
|
---|
1273 | null /* no specific help topic */
|
---|
1274 | )
|
---|
1275 | };
|
---|
1276 |
|
---|
1277 | int ret = HelpAwareOptionPane.showOptionDialog(
|
---|
1278 | Main.parent,
|
---|
1279 | tr("<html>This relation has been changed outside of the editor.<br>"
|
---|
1280 | + "You cannot apply your changes and continue editing.<br>"
|
---|
1281 | + "<br>"
|
---|
1282 | + "Do you want to create a conflict and close the editor?</html>"),
|
---|
1283 | tr("Conflict in data"),
|
---|
1284 | JOptionPane.WARNING_MESSAGE,
|
---|
1285 | null,
|
---|
1286 | options,
|
---|
1287 | options[0], // OK is default
|
---|
1288 | "/Dialog/RelationEditor#RelationChangedOutsideOfEditor"
|
---|
1289 | );
|
---|
1290 | return ret == 0;
|
---|
1291 | }
|
---|
1292 |
|
---|
1293 | protected void warnDoubleConflict() {
|
---|
1294 | JOptionPane.showMessageDialog(
|
---|
1295 | Main.parent,
|
---|
1296 | tr("<html>Layer ''{0}'' already has a conflict for object<br>"
|
---|
1297 | + "''{1}''.<br>"
|
---|
1298 | + "Please resolve this conflict first, then try again.</html>",
|
---|
1299 | getLayer().getName(),
|
---|
1300 | getRelation().getDisplayName(DefaultNameFormatter.getInstance())
|
---|
1301 | ),
|
---|
1302 | tr("Double conflict"),
|
---|
1303 | JOptionPane.WARNING_MESSAGE
|
---|
1304 | );
|
---|
1305 | }
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 | class ApplyAction extends SavingAction {
|
---|
1309 | public ApplyAction() {
|
---|
1310 | putValue(SHORT_DESCRIPTION, tr("Apply the current updates"));
|
---|
1311 | putValue(SMALL_ICON, ImageProvider.get("save"));
|
---|
1312 | putValue(NAME, tr("Apply"));
|
---|
1313 | setEnabled(true);
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | public void run() {
|
---|
1317 | if (getRelation() == null) {
|
---|
1318 | applyNewRelation();
|
---|
1319 | } else if (!memberTableModel.hasSameMembersAs(getRelationSnapshot())
|
---|
1320 | || tagEditorPanel.getModel().isDirty()) {
|
---|
1321 | if (isDirtyRelation()) {
|
---|
1322 | if (confirmClosingBecauseOfDirtyState()) {
|
---|
1323 | if (getLayer().getConflicts().hasConflictForMy(getRelation())) {
|
---|
1324 | warnDoubleConflict();
|
---|
1325 | return;
|
---|
1326 | }
|
---|
1327 | applyExistingConflictingRelation();
|
---|
1328 | setVisible(false);
|
---|
1329 | }
|
---|
1330 | } else {
|
---|
1331 | applyExistingNonConflictingRelation();
|
---|
1332 | }
|
---|
1333 | }
|
---|
1334 | }
|
---|
1335 |
|
---|
1336 | @Override
|
---|
1337 | public void actionPerformed(ActionEvent e) {
|
---|
1338 | run();
|
---|
1339 | }
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | class OKAction extends SavingAction {
|
---|
1343 | public OKAction() {
|
---|
1344 | putValue(SHORT_DESCRIPTION, tr("Apply the updates and close the dialog"));
|
---|
1345 | putValue(SMALL_ICON, ImageProvider.get("ok"));
|
---|
1346 | putValue(NAME, tr("OK"));
|
---|
1347 | setEnabled(true);
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 | public void run() {
|
---|
1351 | Main.pref.put("relation.editor.generic.lastrole", tfRole.getText());
|
---|
1352 | memberTable.stopHighlighting();
|
---|
1353 | if (getRelation() == null) {
|
---|
1354 | applyNewRelation();
|
---|
1355 | } else if (!memberTableModel.hasSameMembersAs(getRelationSnapshot())
|
---|
1356 | || tagEditorPanel.getModel().isDirty()) {
|
---|
1357 | if (isDirtyRelation()) {
|
---|
1358 | if (confirmClosingBecauseOfDirtyState()) {
|
---|
1359 | if (getLayer().getConflicts().hasConflictForMy(getRelation())) {
|
---|
1360 | warnDoubleConflict();
|
---|
1361 | return;
|
---|
1362 | }
|
---|
1363 | applyExistingConflictingRelation();
|
---|
1364 | } else
|
---|
1365 | return;
|
---|
1366 | } else {
|
---|
1367 | applyExistingNonConflictingRelation();
|
---|
1368 | }
|
---|
1369 | }
|
---|
1370 | setVisible(false);
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | @Override
|
---|
1374 | public void actionPerformed(ActionEvent e) {
|
---|
1375 | run();
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | class CancelAction extends SavingAction {
|
---|
1380 | public CancelAction() {
|
---|
1381 | putValue(SHORT_DESCRIPTION, tr("Cancel the updates and close the dialog"));
|
---|
1382 | putValue(SMALL_ICON, ImageProvider.get("cancel"));
|
---|
1383 | putValue(NAME, tr("Cancel"));
|
---|
1384 |
|
---|
1385 | getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
|
---|
1386 | .put(KeyStroke.getKeyStroke("ESCAPE"), "ESCAPE");
|
---|
1387 | getRootPane().getActionMap().put("ESCAPE", this);
|
---|
1388 | setEnabled(true);
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | @Override
|
---|
1392 | public void actionPerformed(ActionEvent e) {
|
---|
1393 | memberTable.stopHighlighting();
|
---|
1394 | TagEditorModel tagModel = tagEditorPanel.getModel();
|
---|
1395 | Relation snapshot = getRelationSnapshot();
|
---|
1396 | if ((!memberTableModel.hasSameMembersAs(snapshot) || tagModel.isDirty())
|
---|
1397 | && !(snapshot == null && tagModel.getTags().isEmpty())) {
|
---|
1398 | //give the user a chance to save the changes
|
---|
1399 | int ret = confirmClosingByCancel();
|
---|
1400 | if (ret == 0) { //Yes, save the changes
|
---|
1401 | //copied from OKAction.run()
|
---|
1402 | Main.pref.put("relation.editor.generic.lastrole", tfRole.getText());
|
---|
1403 | if (getRelation() == null) {
|
---|
1404 | applyNewRelation();
|
---|
1405 | } else if (!memberTableModel.hasSameMembersAs(snapshot) || tagModel.isDirty()) {
|
---|
1406 | if (isDirtyRelation()) {
|
---|
1407 | if (confirmClosingBecauseOfDirtyState()) {
|
---|
1408 | if (getLayer().getConflicts().hasConflictForMy(getRelation())) {
|
---|
1409 | warnDoubleConflict();
|
---|
1410 | return;
|
---|
1411 | }
|
---|
1412 | applyExistingConflictingRelation();
|
---|
1413 | } else
|
---|
1414 | return;
|
---|
1415 | } else {
|
---|
1416 | applyExistingNonConflictingRelation();
|
---|
1417 | }
|
---|
1418 | }
|
---|
1419 | } else if (ret == 2) //Cancel, continue editing
|
---|
1420 | return;
|
---|
1421 | //in case of "No, discard", there is no extra action to be performed here.
|
---|
1422 | }
|
---|
1423 | setVisible(false);
|
---|
1424 | }
|
---|
1425 |
|
---|
1426 | protected int confirmClosingByCancel() {
|
---|
1427 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
1428 | new ButtonSpec(
|
---|
1429 | tr("Yes, save the changes and close"),
|
---|
1430 | ImageProvider.get("ok"),
|
---|
1431 | tr("Click to save the changes and close this relation editor") ,
|
---|
1432 | null /* no specific help topic */
|
---|
1433 | ),
|
---|
1434 | new ButtonSpec(
|
---|
1435 | tr("No, discard the changes and close"),
|
---|
1436 | ImageProvider.get("cancel"),
|
---|
1437 | tr("Click to discard the changes and close this relation editor") ,
|
---|
1438 | null /* no specific help topic */
|
---|
1439 | ),
|
---|
1440 | new ButtonSpec(
|
---|
1441 | tr("Cancel, continue editing"),
|
---|
1442 | ImageProvider.get("cancel"),
|
---|
1443 | tr("Click to return to the relation editor and to resume relation editing") ,
|
---|
1444 | null /* no specific help topic */
|
---|
1445 | )
|
---|
1446 | };
|
---|
1447 |
|
---|
1448 | return HelpAwareOptionPane.showOptionDialog(
|
---|
1449 | Main.parent,
|
---|
1450 | tr("<html>The relation has been changed.<br>"
|
---|
1451 | + "<br>"
|
---|
1452 | + "Do you want to save your changes?</html>"),
|
---|
1453 | tr("Unsaved changes"),
|
---|
1454 | JOptionPane.WARNING_MESSAGE,
|
---|
1455 | null,
|
---|
1456 | options,
|
---|
1457 | options[0], // OK is default,
|
---|
1458 | "/Dialog/RelationEditor#DiscardChanges"
|
---|
1459 | );
|
---|
1460 | }
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | class AddTagAction extends AbstractAction {
|
---|
1464 | public AddTagAction() {
|
---|
1465 | putValue(SHORT_DESCRIPTION, tr("Add an empty tag"));
|
---|
1466 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "add"));
|
---|
1467 | setEnabled(true);
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | @Override
|
---|
1471 | public void actionPerformed(ActionEvent e) {
|
---|
1472 | tagEditorPanel.getModel().appendNewTag();
|
---|
1473 | }
|
---|
1474 | }
|
---|
1475 |
|
---|
1476 | class DownloadIncompleteMembersAction extends AbstractAction implements TableModelListener {
|
---|
1477 | public DownloadIncompleteMembersAction() {
|
---|
1478 | String tooltip = tr("Download all incomplete members");
|
---|
1479 | putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "downloadincomplete"));
|
---|
1480 | putValue(NAME, tr("Download Members"));
|
---|
1481 | Shortcut sc = Shortcut.registerShortcut("relationeditor:downloadincomplete", tr("Relation Editor: Download Members"),
|
---|
1482 | KeyEvent.VK_HOME, Shortcut.ALT);
|
---|
1483 | sc.setAccelerator(this);
|
---|
1484 | putValue(SHORT_DESCRIPTION, Main.platform.makeTooltip(tooltip, sc));
|
---|
1485 | updateEnabledState();
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | @Override
|
---|
1489 | public void actionPerformed(ActionEvent e) {
|
---|
1490 | if (!isEnabled())
|
---|
1491 | return;
|
---|
1492 | Main.worker.submit(new DownloadRelationMemberTask(
|
---|
1493 | getRelation(),
|
---|
1494 | memberTableModel.getIncompleteMemberPrimitives(),
|
---|
1495 | getLayer(),
|
---|
1496 | GenericRelationEditor.this)
|
---|
1497 | );
|
---|
1498 | }
|
---|
1499 |
|
---|
1500 | protected void updateEnabledState() {
|
---|
1501 | setEnabled(memberTableModel.hasIncompleteMembers() && !Main.isOffline(OnlineResource.OSM_API));
|
---|
1502 | }
|
---|
1503 |
|
---|
1504 | @Override
|
---|
1505 | public void tableChanged(TableModelEvent e) {
|
---|
1506 | updateEnabledState();
|
---|
1507 | }
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | class DownloadSelectedIncompleteMembersAction extends AbstractAction implements ListSelectionListener, TableModelListener{
|
---|
1511 | public DownloadSelectedIncompleteMembersAction() {
|
---|
1512 | putValue(SHORT_DESCRIPTION, tr("Download selected incomplete members"));
|
---|
1513 | putValue(SMALL_ICON, ImageProvider.get("dialogs/relation", "downloadincompleteselected"));
|
---|
1514 | putValue(NAME, tr("Download Members"));
|
---|
1515 | // Shortcut.register Shortcut("relationeditor:downloadincomplete", tr("Relation Editor: Download Members"),
|
---|
1516 | // KeyEvent.VK_K, Shortcut.ALT)
|
---|
1517 | updateEnabledState();
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | @Override
|
---|
1521 | public void actionPerformed(ActionEvent e) {
|
---|
1522 | if (!isEnabled())
|
---|
1523 | return;
|
---|
1524 | Main.worker.submit(new DownloadRelationMemberTask(
|
---|
1525 | getRelation(),
|
---|
1526 | memberTableModel.getSelectedIncompleteMemberPrimitives(),
|
---|
1527 | getLayer(),
|
---|
1528 | GenericRelationEditor.this)
|
---|
1529 | );
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | protected void updateEnabledState() {
|
---|
1533 | setEnabled(memberTableModel.hasIncompleteSelectedMembers() && !Main.isOffline(OnlineResource.OSM_API));
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 | @Override
|
---|
1537 | public void valueChanged(ListSelectionEvent e) {
|
---|
1538 | updateEnabledState();
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | @Override
|
---|
1542 | public void tableChanged(TableModelEvent e) {
|
---|
1543 | updateEnabledState();
|
---|
1544 | }
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | class SetRoleAction extends AbstractAction implements ListSelectionListener, DocumentListener {
|
---|
1548 | public SetRoleAction() {
|
---|
1549 | putValue(SHORT_DESCRIPTION, tr("Sets a role for the selected members"));
|
---|
1550 | putValue(SMALL_ICON, ImageProvider.get("apply"));
|
---|
1551 | putValue(NAME, tr("Apply Role"));
|
---|
1552 | refreshEnabled();
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | protected void refreshEnabled() {
|
---|
1556 | setEnabled(memberTable.getSelectedRowCount() > 0);
|
---|
1557 | }
|
---|
1558 |
|
---|
1559 | protected boolean isEmptyRole() {
|
---|
1560 | return tfRole.getText() == null || tfRole.getText().trim().isEmpty();
|
---|
1561 | }
|
---|
1562 |
|
---|
1563 | protected boolean confirmSettingEmptyRole(int onNumMembers) {
|
---|
1564 | String message = "<html>"
|
---|
1565 | + trn("You are setting an empty role on {0} object.",
|
---|
1566 | "You are setting an empty role on {0} objects.", onNumMembers, onNumMembers)
|
---|
1567 | + "<br>"
|
---|
1568 | + tr("This is equal to deleting the roles of these objects.") +
|
---|
1569 | "<br>"
|
---|
1570 | + tr("Do you really want to apply the new role?") + "</html>";
|
---|
1571 | String[] options = new String[] {
|
---|
1572 | tr("Yes, apply it"),
|
---|
1573 | tr("No, do not apply")
|
---|
1574 | };
|
---|
1575 | int ret = ConditionalOptionPaneUtil.showOptionDialog(
|
---|
1576 | "relation_editor.confirm_applying_empty_role",
|
---|
1577 | Main.parent,
|
---|
1578 | message,
|
---|
1579 | tr("Confirm empty role"),
|
---|
1580 | JOptionPane.YES_NO_OPTION,
|
---|
1581 | JOptionPane.WARNING_MESSAGE,
|
---|
1582 | options,
|
---|
1583 | options[0]
|
---|
1584 | );
|
---|
1585 | switch(ret) {
|
---|
1586 | case JOptionPane.YES_OPTION:
|
---|
1587 | case ConditionalOptionPaneUtil.DIALOG_DISABLED_OPTION:
|
---|
1588 | return true;
|
---|
1589 | default:
|
---|
1590 | return false;
|
---|
1591 | }
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | @Override
|
---|
1595 | public void actionPerformed(ActionEvent e) {
|
---|
1596 | if (isEmptyRole()) {
|
---|
1597 | if (!confirmSettingEmptyRole(memberTable.getSelectedRowCount()))
|
---|
1598 | return;
|
---|
1599 | }
|
---|
1600 | memberTableModel.updateRole(memberTable.getSelectedRows(), tfRole.getText());
|
---|
1601 | }
|
---|
1602 |
|
---|
1603 | @Override
|
---|
1604 | public void valueChanged(ListSelectionEvent e) {
|
---|
1605 | refreshEnabled();
|
---|
1606 | }
|
---|
1607 |
|
---|
1608 | @Override
|
---|
1609 | public void changedUpdate(DocumentEvent e) {
|
---|
1610 | refreshEnabled();
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | @Override
|
---|
1614 | public void insertUpdate(DocumentEvent e) {
|
---|
1615 | refreshEnabled();
|
---|
1616 | }
|
---|
1617 |
|
---|
1618 | @Override
|
---|
1619 | public void removeUpdate(DocumentEvent e) {
|
---|
1620 | refreshEnabled();
|
---|
1621 | }
|
---|
1622 | }
|
---|
1623 |
|
---|
1624 | /**
|
---|
1625 | * Creates a new relation with a copy of the current editor state.
|
---|
1626 | */
|
---|
1627 | class DuplicateRelationAction extends AbstractAction {
|
---|
1628 | public DuplicateRelationAction() {
|
---|
1629 | putValue(SHORT_DESCRIPTION, tr("Create a copy of this relation and open it in another editor window"));
|
---|
1630 | // FIXME provide an icon
|
---|
1631 | putValue(SMALL_ICON, ImageProvider.get("duplicate"));
|
---|
1632 | putValue(NAME, tr("Duplicate"));
|
---|
1633 | setEnabled(true);
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | @Override
|
---|
1637 | public void actionPerformed(ActionEvent e) {
|
---|
1638 | Relation copy = new Relation();
|
---|
1639 | tagEditorPanel.getModel().applyToPrimitive(copy);
|
---|
1640 | memberTableModel.applyToRelation(copy);
|
---|
1641 | RelationEditor editor = RelationEditor.getEditor(getLayer(), copy, memberTableModel.getSelectedMembers());
|
---|
1642 | editor.setVisible(true);
|
---|
1643 | }
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 | /**
|
---|
1647 | * Action for editing the currently selected relation.
|
---|
1648 | */
|
---|
1649 | class EditAction extends AbstractAction implements ListSelectionListener {
|
---|
1650 | public EditAction() {
|
---|
1651 | putValue(SHORT_DESCRIPTION, tr("Edit the relation the currently selected relation member refers to"));
|
---|
1652 | putValue(SMALL_ICON, ImageProvider.get("dialogs", "edit"));
|
---|
1653 | refreshEnabled();
|
---|
1654 | }
|
---|
1655 |
|
---|
1656 | protected void refreshEnabled() {
|
---|
1657 | setEnabled(memberTable.getSelectedRowCount() == 1
|
---|
1658 | && memberTableModel.isEditableRelation(memberTable.getSelectedRow()));
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | protected Collection<RelationMember> getMembersForCurrentSelection(Relation r) {
|
---|
1662 | Collection<RelationMember> members = new HashSet<>();
|
---|
1663 | Collection<OsmPrimitive> selection = getLayer().data.getSelected();
|
---|
1664 | for (RelationMember member: r.getMembers()) {
|
---|
1665 | if (selection.contains(member.getMember())) {
|
---|
1666 | members.add(member);
|
---|
1667 | }
|
---|
1668 | }
|
---|
1669 | return members;
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | public void run() {
|
---|
1673 | int idx = memberTable.getSelectedRow();
|
---|
1674 | if (idx < 0)
|
---|
1675 | return;
|
---|
1676 | OsmPrimitive primitive = memberTableModel.getReferredPrimitive(idx);
|
---|
1677 | if (!(primitive instanceof Relation))
|
---|
1678 | return;
|
---|
1679 | Relation r = (Relation) primitive;
|
---|
1680 | if (r.isIncomplete())
|
---|
1681 | return;
|
---|
1682 |
|
---|
1683 | RelationEditor editor = RelationEditor.getEditor(getLayer(), r, getMembersForCurrentSelection(r));
|
---|
1684 | editor.setVisible(true);
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | @Override
|
---|
1688 | public void actionPerformed(ActionEvent e) {
|
---|
1689 | if (!isEnabled())
|
---|
1690 | return;
|
---|
1691 | run();
|
---|
1692 | }
|
---|
1693 |
|
---|
1694 | @Override
|
---|
1695 | public void valueChanged(ListSelectionEvent e) {
|
---|
1696 | refreshEnabled();
|
---|
1697 | }
|
---|
1698 | }
|
---|
1699 |
|
---|
1700 | class PasteMembersAction extends AddFromSelectionAction {
|
---|
1701 |
|
---|
1702 | @Override
|
---|
1703 | public void actionPerformed(ActionEvent e) {
|
---|
1704 | try {
|
---|
1705 | List<PrimitiveData> primitives = Main.pasteBuffer.getDirectlyAdded();
|
---|
1706 | DataSet ds = getLayer().data;
|
---|
1707 | List<OsmPrimitive> toAdd = new ArrayList<>();
|
---|
1708 | boolean hasNewInOtherLayer = false;
|
---|
1709 |
|
---|
1710 | for (PrimitiveData primitive: primitives) {
|
---|
1711 | OsmPrimitive primitiveInDs = ds.getPrimitiveById(primitive);
|
---|
1712 | if (primitiveInDs != null) {
|
---|
1713 | toAdd.add(primitiveInDs);
|
---|
1714 | } else if (!primitive.isNew()) {
|
---|
1715 | OsmPrimitive p = primitive.getType().newInstance(primitive.getUniqueId(), true);
|
---|
1716 | ds.addPrimitive(p);
|
---|
1717 | toAdd.add(p);
|
---|
1718 | } else {
|
---|
1719 | hasNewInOtherLayer = true;
|
---|
1720 | break;
|
---|
1721 | }
|
---|
1722 | }
|
---|
1723 |
|
---|
1724 | if (hasNewInOtherLayer) {
|
---|
1725 | JOptionPane.showMessageDialog(Main.parent, tr("Members from paste buffer cannot be added because they are not included in current layer"));
|
---|
1726 | return;
|
---|
1727 | }
|
---|
1728 |
|
---|
1729 | toAdd = filterConfirmedPrimitives(toAdd);
|
---|
1730 | int index = memberTableModel.getSelectionModel().getMaxSelectionIndex();
|
---|
1731 | if (index == -1) {
|
---|
1732 | index = memberTableModel.getRowCount() - 1;
|
---|
1733 | }
|
---|
1734 | memberTableModel.addMembersAfterIdx(toAdd, index);
|
---|
1735 |
|
---|
1736 | tfRole.requestFocusInWindow();
|
---|
1737 |
|
---|
1738 | } catch (AddAbortException ex) {
|
---|
1739 | // Do nothing
|
---|
1740 | }
|
---|
1741 | }
|
---|
1742 | }
|
---|
1743 |
|
---|
1744 | class CopyMembersAction extends AbstractAction {
|
---|
1745 | @Override
|
---|
1746 | public void actionPerformed(ActionEvent e) {
|
---|
1747 | Set<OsmPrimitive> primitives = new HashSet<>();
|
---|
1748 | for (RelationMember rm: memberTableModel.getSelectedMembers()) {
|
---|
1749 | primitives.add(rm.getMember());
|
---|
1750 | }
|
---|
1751 | if (!primitives.isEmpty()) {
|
---|
1752 | CopyAction.copy(getLayer(), primitives);
|
---|
1753 | }
|
---|
1754 | }
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 | class MemberTableDblClickAdapter extends MouseAdapter {
|
---|
1758 | @Override
|
---|
1759 | public void mouseClicked(MouseEvent e) {
|
---|
1760 | if (e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2) {
|
---|
1761 | new EditAction().run();
|
---|
1762 | }
|
---|
1763 | }
|
---|
1764 | }
|
---|
1765 | }
|
---|