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 | public enum ExistingBothNew {
|
---|
34 | OLD, BOTH, NEW
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Provides toggle buttons to allow the user choose the existing node, the new nodes, or all of them.
|
---|
39 | */
|
---|
40 | private static class ExistingBothNewChoice {
|
---|
41 | /** The "Existing node" button */
|
---|
42 | final AbstractButton oldNode = new JToggleButton(tr("Existing node"), ImageProvider.get("dialogs/conflict/tagkeeptheir"));
|
---|
43 | /** The "Both nodes" button */
|
---|
44 | final AbstractButton bothNodes = new JToggleButton(tr("Both nodes"), ImageProvider.get("dialogs/conflict/tagundecide"));
|
---|
45 | /** The "New node" button */
|
---|
46 | final AbstractButton newNode = new JToggleButton(tr("New node"), ImageProvider.get("dialogs/conflict/tagkeepmine"));
|
---|
47 |
|
---|
48 | ExistingBothNewChoice(final boolean preselectNew) {
|
---|
49 | final ButtonGroup tagsGroup = new ButtonGroup();
|
---|
50 | tagsGroup.add(oldNode);
|
---|
51 | tagsGroup.add(bothNodes);
|
---|
52 | tagsGroup.add(newNode);
|
---|
53 | tagsGroup.setSelected((preselectNew ? newNode : oldNode).getModel(), true);
|
---|
54 | }
|
---|
55 |
|
---|
56 | void add(JPanel content, int gridy) {
|
---|
57 | content.add(oldNode, GBC.std(1, gridy));
|
---|
58 | content.add(bothNodes, GBC.std(2, gridy));
|
---|
59 | content.add(newNode, GBC.std(3, gridy));
|
---|
60 | }
|
---|
61 |
|
---|
62 | ExistingBothNew getSelected() {
|
---|
63 | if (oldNode.isSelected()) {
|
---|
64 | return ExistingBothNew.OLD;
|
---|
65 | } else if (bothNodes.isSelected()) {
|
---|
66 | return ExistingBothNew.BOTH;
|
---|
67 | } else if (newNode.isSelected()) {
|
---|
68 | return ExistingBothNew.NEW;
|
---|
69 | } else {
|
---|
70 | throw new IllegalStateException();
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | private PropertiesMembershipChoiceDialog(boolean preselectNew, boolean queryTags, boolean queryMemberships) {
|
---|
76 | super(MainApplication.getMainFrame(), tr("Tags/Memberships"), tr("Unglue"), tr("Cancel"));
|
---|
77 | setButtonIcons("unglueways", "cancel");
|
---|
78 |
|
---|
79 | final JPanel content = new JPanel(new GridBagLayout());
|
---|
80 |
|
---|
81 | if (queryTags) {
|
---|
82 | 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));
|
---|
83 | tags = new ExistingBothNewChoice(preselectNew);
|
---|
84 | tags.add(content, 2);
|
---|
85 | } else {
|
---|
86 | tags = null;
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (queryMemberships) {
|
---|
90 | 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));
|
---|
91 | memberships = new ExistingBothNewChoice(preselectNew);
|
---|
92 | memberships.add(content, 4);
|
---|
93 | } else {
|
---|
94 | memberships = null;
|
---|
95 | }
|
---|
96 |
|
---|
97 | setContent(content);
|
---|
98 | setResizable(false);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Returns the tags choice.
|
---|
103 | * @return the tags choice (can be null)
|
---|
104 | */
|
---|
105 | public ExistingBothNew getTags() {
|
---|
106 | return tags.getSelected();
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Returns the memberships choice.
|
---|
111 | * @return the memberships choice (can be null)
|
---|
112 | */
|
---|
113 | public ExistingBothNew getMemberships() {
|
---|
114 | return memberships.getSelected();
|
---|
115 | }
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Creates and shows a new {@code PropertiesMembershipChoiceDialog} if necessary. Otherwise does nothing.
|
---|
119 | * @param selectedNodes selected nodes
|
---|
120 | * @param preselectNew if {@code true}, pre-select "new node" as default choice
|
---|
121 | * @return A new {@code PropertiesMembershipChoiceDialog} that has been shown to user, or {@code null}
|
---|
122 | * @throws UserCancelException if user cancels choice
|
---|
123 | */
|
---|
124 | public static PropertiesMembershipChoiceDialog showIfNecessary(Collection<Node> selectedNodes, boolean preselectNew)
|
---|
125 | throws UserCancelException {
|
---|
126 | final boolean queryTags = isTagged(selectedNodes);
|
---|
127 | final boolean queryMemberships = isUsedInRelations(selectedNodes);
|
---|
128 | if (queryTags || queryMemberships) {
|
---|
129 | final PropertiesMembershipChoiceDialog dialog = new PropertiesMembershipChoiceDialog(preselectNew, queryTags, queryMemberships);
|
---|
130 | dialog.showDialog();
|
---|
131 | if (dialog.getValue() != 1) {
|
---|
132 | throw new UserCancelException();
|
---|
133 | }
|
---|
134 | return dialog;
|
---|
135 | }
|
---|
136 | return null;
|
---|
137 | }
|
---|
138 |
|
---|
139 | private static boolean isTagged(final Collection<Node> existingNodes) {
|
---|
140 | return existingNodes.stream().anyMatch(Node::hasKeys);
|
---|
141 | }
|
---|
142 |
|
---|
143 | private static boolean isUsedInRelations(final Collection<Node> existingNodes) {
|
---|
144 | return existingNodes.stream().anyMatch(
|
---|
145 | selectedNode -> selectedNode.getReferrers().stream().anyMatch(Relation.class::isInstance));
|
---|
146 | }
|
---|
147 | }
|
---|