1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.util.Collection;
|
---|
8 |
|
---|
9 | import javax.swing.AbstractButton;
|
---|
10 | import javax.swing.ButtonGroup;
|
---|
11 | import javax.swing.JLabel;
|
---|
12 | import javax.swing.JPanel;
|
---|
13 | import javax.swing.JToggleButton;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
17 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
19 | import org.openstreetmap.josm.tools.GBC;
|
---|
20 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
21 | import org.openstreetmap.josm.tools.UserCancelException;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * A dialog allowing the user decide whether the tags/memberships of the existing node should afterwards be at
|
---|
25 | * the existing node, the new nodes, or all of them.
|
---|
26 | * @since 14320 (extracted from UnglueAction)
|
---|
27 | */
|
---|
28 | public final class PropertiesMembershipChoiceDialog extends ExtendedDialog {
|
---|
29 |
|
---|
30 | private final transient ExistingBothNewChoice tags;
|
---|
31 | private final transient ExistingBothNewChoice memberships;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Provides toggle buttons to allow the user choose the existing node, the new nodes, or all of them.
|
---|
35 | */
|
---|
36 | public static class ExistingBothNewChoice {
|
---|
37 | /** The "Existing node" button */
|
---|
38 | public final AbstractButton oldNode = new JToggleButton(tr("Existing node"), ImageProvider.get("dialogs/conflict/tagkeeptheir"));
|
---|
39 | /** The "Both nodes" button */
|
---|
40 | public final AbstractButton bothNodes = new JToggleButton(tr("Both nodes"), ImageProvider.get("dialogs/conflict/tagundecide"));
|
---|
41 | /** The "New node" button */
|
---|
42 | public final AbstractButton newNode = new JToggleButton(tr("New node"), ImageProvider.get("dialogs/conflict/tagkeepmine"));
|
---|
43 |
|
---|
44 | ExistingBothNewChoice(final boolean preselectNew) {
|
---|
45 | final ButtonGroup tagsGroup = new ButtonGroup();
|
---|
46 | tagsGroup.add(oldNode);
|
---|
47 | tagsGroup.add(bothNodes);
|
---|
48 | tagsGroup.add(newNode);
|
---|
49 | tagsGroup.setSelected((preselectNew ? newNode : oldNode).getModel(), true);
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | private PropertiesMembershipChoiceDialog(boolean preselectNew, boolean queryTags, boolean queryMemberships) {
|
---|
54 | super(MainApplication.getMainFrame(), tr("Tags/Memberships"), tr("Unglue"), tr("Cancel"));
|
---|
55 | setButtonIcons("unglueways", "cancel");
|
---|
56 |
|
---|
57 | final JPanel content = new JPanel(new GridBagLayout());
|
---|
58 |
|
---|
59 | if (queryTags) {
|
---|
60 | content.add(new JLabel(tr("Where should the tags of the node be put?")), GBC.std(1, 1).span(3).insets(0, 20, 0, 0));
|
---|
61 | tags = new ExistingBothNewChoice(preselectNew);
|
---|
62 | content.add(tags.oldNode, GBC.std(1, 2));
|
---|
63 | content.add(tags.bothNodes, GBC.std(2, 2));
|
---|
64 | content.add(tags.newNode, GBC.std(3, 2));
|
---|
65 | } else {
|
---|
66 | tags = null;
|
---|
67 | }
|
---|
68 |
|
---|
69 | if (queryMemberships) {
|
---|
70 | content.add(new JLabel(tr("Where should the memberships of this node be put?")), GBC.std(1, 3).span(3).insets(0, 20, 0, 0));
|
---|
71 | memberships = new ExistingBothNewChoice(preselectNew);
|
---|
72 | content.add(memberships.oldNode, GBC.std(1, 4));
|
---|
73 | content.add(memberships.bothNodes, GBC.std(2, 4));
|
---|
74 | content.add(memberships.newNode, GBC.std(3, 4));
|
---|
75 | } else {
|
---|
76 | memberships = null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | setContent(content);
|
---|
80 | setResizable(false);
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Returns the tags choice.
|
---|
85 | * @return the tags choice (can be null)
|
---|
86 | */
|
---|
87 | public ExistingBothNewChoice getTags() {
|
---|
88 | return tags;
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Returns the memberships choice.
|
---|
93 | * @return the memberships choice (can be null)
|
---|
94 | */
|
---|
95 | public ExistingBothNewChoice getMemberships() {
|
---|
96 | return memberships;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Creates and shows a new {@code PropertiesMembershipChoiceDialog} if necessary. Otherwise does nothing.
|
---|
101 | * @param selectedNodes selected nodes
|
---|
102 | * @param preselectNew if {@code true}, pre-select "new node" as default choice
|
---|
103 | * @return A new {@code PropertiesMembershipChoiceDialog} that has been shown to user, or {@code null}
|
---|
104 | * @throws UserCancelException if user cancels choice
|
---|
105 | */
|
---|
106 | public static PropertiesMembershipChoiceDialog showIfNecessary(Collection<Node> selectedNodes, boolean preselectNew)
|
---|
107 | throws UserCancelException {
|
---|
108 | final boolean queryTags = isTagged(selectedNodes);
|
---|
109 | final boolean queryMemberships = isUsedInRelations(selectedNodes);
|
---|
110 | if (queryTags || queryMemberships) {
|
---|
111 | final PropertiesMembershipChoiceDialog dialog = new PropertiesMembershipChoiceDialog(preselectNew, queryTags, queryMemberships);
|
---|
112 | dialog.showDialog();
|
---|
113 | if (dialog.getValue() != 1) {
|
---|
114 | throw new UserCancelException();
|
---|
115 | }
|
---|
116 | return dialog;
|
---|
117 | }
|
---|
118 | return null;
|
---|
119 | }
|
---|
120 |
|
---|
121 | private static boolean isTagged(final Collection<Node> existingNodes) {
|
---|
122 | return existingNodes.stream().anyMatch(Node::hasKeys);
|
---|
123 | }
|
---|
124 |
|
---|
125 | private static boolean isUsedInRelations(final Collection<Node> existingNodes) {
|
---|
126 | return existingNodes.stream().anyMatch(
|
---|
127 | selectedNode -> selectedNode.getReferrers().stream().anyMatch(Relation.class::isInstance));
|
---|
128 | }
|
---|
129 | }
|
---|