Changeset 14320 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2018-10-13T14:37:04+02:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/UnGlueAction.java
r14153 r14320 6 6 import static org.openstreetmap.josm.tools.I18n.trn; 7 7 8 import java.awt.GridBagLayout;9 8 import java.awt.event.ActionEvent; 10 9 import java.awt.event.KeyEvent; … … 19 18 import java.util.Set; 20 19 21 import javax.swing.AbstractButton;22 import javax.swing.ButtonGroup;23 import javax.swing.JLabel;24 20 import javax.swing.JOptionPane; 25 21 import javax.swing.JPanel; 26 import javax.swing.JToggleButton;27 22 28 23 import org.openstreetmap.josm.command.AddCommand; … … 38 33 import org.openstreetmap.josm.data.osm.RelationMember; 39 34 import org.openstreetmap.josm.data.osm.Way; 40 import org.openstreetmap.josm.gui.ExtendedDialog;41 35 import org.openstreetmap.josm.gui.MainApplication; 42 36 import org.openstreetmap.josm.gui.MapView; 43 37 import org.openstreetmap.josm.gui.Notification; 44 import org.openstreetmap.josm. tools.GBC;45 import org.openstreetmap.josm. tools.ImageProvider;38 import org.openstreetmap.josm.gui.dialogs.PropertiesMembershipChoiceDialog; 39 import org.openstreetmap.josm.gui.dialogs.PropertiesMembershipChoiceDialog.ExistingBothNewChoice; 46 40 import org.openstreetmap.josm.tools.Logging; 47 41 import org.openstreetmap.josm.tools.Shortcut; … … 181 175 } 182 176 183 /** 184 * Provides toggle buttons to allow the user choose the existing node, the new nodes, or all of them. 185 */ 186 private static class ExistingBothNewChoice { 187 final AbstractButton oldNode = new JToggleButton(tr("Existing node"), ImageProvider.get("dialogs/conflict/tagkeeptheir")); 188 final AbstractButton bothNodes = new JToggleButton(tr("Both nodes"), ImageProvider.get("dialogs/conflict/tagundecide")); 189 final AbstractButton newNode = new JToggleButton(tr("New node"), ImageProvider.get("dialogs/conflict/tagkeepmine")); 190 191 ExistingBothNewChoice(final boolean preselectNew) { 192 final ButtonGroup tagsGroup = new ButtonGroup(); 193 tagsGroup.add(oldNode); 194 tagsGroup.add(bothNodes); 195 tagsGroup.add(newNode); 196 tagsGroup.setSelected((preselectNew ? newNode : oldNode).getModel(), true); 197 } 198 } 199 200 /** 201 * A dialog allowing the user decide whether the tags/memberships of the existing node should afterwards be at 202 * the existing node, the new nodes, or all of them. 203 */ 204 static final class PropertiesMembershipDialog extends ExtendedDialog { 205 206 final transient ExistingBothNewChoice tags; 207 final transient ExistingBothNewChoice memberships; 208 209 private PropertiesMembershipDialog(boolean preselectNew, boolean queryTags, boolean queryMemberships) { 210 super(MainApplication.getMainFrame(), tr("Tags/Memberships"), tr("Unglue"), tr("Cancel")); 211 setButtonIcons("unglueways", "cancel"); 212 213 final JPanel content = new JPanel(new GridBagLayout()); 214 215 if (queryTags) { 216 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)); 217 tags = new ExistingBothNewChoice(preselectNew); 218 content.add(tags.oldNode, GBC.std(1, 2)); 219 content.add(tags.bothNodes, GBC.std(2, 2)); 220 content.add(tags.newNode, GBC.std(3, 2)); 221 } else { 222 tags = null; 223 } 224 225 if (queryMemberships) { 226 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)); 227 memberships = new ExistingBothNewChoice(preselectNew); 228 content.add(memberships.oldNode, GBC.std(1, 4)); 229 content.add(memberships.bothNodes, GBC.std(2, 4)); 230 content.add(memberships.newNode, GBC.std(3, 4)); 231 } else { 232 memberships = null; 233 } 234 235 setContent(content); 236 setResizable(false); 237 } 238 239 static PropertiesMembershipDialog showIfNecessary(Collection<Node> selectedNodes, boolean preselectNew) throws UserCancelException { 240 final boolean tagged = isTagged(selectedNodes); 241 final boolean usedInRelations = isUsedInRelations(selectedNodes); 242 if (tagged || usedInRelations) { 243 final PropertiesMembershipDialog dialog = new PropertiesMembershipDialog(preselectNew, tagged, usedInRelations); 244 dialog.showDialog(); 245 if (dialog.getValue() != 1) { 246 throw new UserCancelException(); 247 } 248 return dialog; 249 } 250 return null; 251 } 252 253 private static boolean isTagged(final Collection<Node> existingNodes) { 254 return existingNodes.stream().anyMatch(Node::hasKeys); 255 } 256 257 private static boolean isUsedInRelations(final Collection<Node> existingNodes) { 258 return existingNodes.stream().anyMatch( 259 selectedNode -> selectedNode.getReferrers().stream().anyMatch(Relation.class::isInstance)); 260 } 261 262 void update(final Node existingNode, final List<Node> newNodes, final Collection<Command> cmds) { 263 updateMemberships(existingNode, newNodes, cmds); 264 updateProperties(existingNode, newNodes, cmds); 265 } 266 267 private void updateProperties(final Node existingNode, final Iterable<Node> newNodes, final Collection<Command> cmds) { 268 if (tags != null && tags.newNode.isSelected()) { 269 final Node newSelectedNode = new Node(existingNode); 270 newSelectedNode.removeAll(); 271 cmds.add(new ChangeCommand(existingNode, newSelectedNode)); 272 } else if (tags != null && tags.oldNode.isSelected()) { 273 for (Node newNode : newNodes) { 274 newNode.removeAll(); 275 } 276 } 277 } 278 279 private void updateMemberships(final Node existingNode, final List<Node> newNodes, final Collection<Command> cmds) { 280 if (memberships != null && memberships.bothNodes.isSelected()) { 281 fixRelations(existingNode, cmds, newNodes, false); 282 } else if (memberships != null && memberships.newNode.isSelected()) { 283 fixRelations(existingNode, cmds, newNodes, true); 284 } 177 void update(PropertiesMembershipChoiceDialog dialog, Node existingNode, List<Node> newNodes, Collection<Command> cmds) { 178 updateMemberships(dialog.getMemberships(), existingNode, newNodes, cmds); 179 updateProperties(dialog.getTags(), existingNode, newNodes, cmds); 180 } 181 182 private void updateProperties(ExistingBothNewChoice tags, Node existingNode, Iterable<Node> newNodes, Collection<Command> cmds) { 183 if (tags != null && tags.newNode.isSelected()) { 184 final Node newSelectedNode = new Node(existingNode); 185 newSelectedNode.removeAll(); 186 cmds.add(new ChangeCommand(existingNode, newSelectedNode)); 187 } else if (tags != null && tags.oldNode.isSelected()) { 188 for (Node newNode : newNodes) { 189 newNode.removeAll(); 190 } 191 } 192 } 193 194 private void updateMemberships(ExistingBothNewChoice memberships, Node existingNode, List<Node> newNodes, Collection<Command> cmds) { 195 if (memberships != null && memberships.bothNodes.isSelected()) { 196 fixRelations(existingNode, cmds, newNodes, false); 197 } else if (memberships != null && memberships.newNode.isSelected()) { 198 fixRelations(existingNode, cmds, newNodes, true); 285 199 } 286 200 } … … 292 206 */ 293 207 private void unglueOneNodeAtMostOneWay(ActionEvent e) { 294 final PropertiesMembership Dialog dialog;208 final PropertiesMembershipChoiceDialog dialog; 295 209 try { 296 dialog = PropertiesMembership Dialog.showIfNecessary(Collections.singleton(selectedNode), true);210 dialog = PropertiesMembershipChoiceDialog.showIfNecessary(Collections.singleton(selectedNode), true); 297 211 } catch (UserCancelException ex) { 298 212 Logging.trace(ex); … … 305 219 cmds.add(new AddCommand(selectedNode.getDataSet(), n)); 306 220 if (dialog != null) { 307 dialog.update(selectedNode, Collections.singletonList(n), cmds);221 update(dialog, selectedNode, Collections.singletonList(n), cmds); 308 222 } 309 223 … … 505 419 */ 506 420 private void unglueWays() { 507 final PropertiesMembership Dialog dialog;421 final PropertiesMembershipChoiceDialog dialog; 508 422 try { 509 dialog = PropertiesMembership Dialog.showIfNecessary(Collections.singleton(selectedNode), false);423 dialog = PropertiesMembershipChoiceDialog.showIfNecessary(Collections.singleton(selectedNode), false); 510 424 } catch (UserCancelException e) { 511 425 Logging.trace(e); … … 541 455 542 456 if (dialog != null) { 543 dialog.update(selectedNode, newNodes, cmds);457 update(dialog, selectedNode, newNodes, cmds); 544 458 } 545 459 … … 601 515 notifyWayPartOfRelation(Collections.singleton(way)); 602 516 try { 603 final PropertiesMembershipDialog dialog = PropertiesMembershipDialog.showIfNecessary(Collections.singleton(selectedNode), false); 517 final PropertiesMembershipChoiceDialog dialog = PropertiesMembershipChoiceDialog.showIfNecessary( 518 Collections.singleton(selectedNode), false); 604 519 if (dialog != null) { 605 dialog.update(selectedNode, addNodes, cmds);520 update(dialog, selectedNode, addNodes, cmds); 606 521 } 607 522 execCommands(cmds, addNodes); … … 620 535 Way tmpWay = selectedWay; 621 536 622 final PropertiesMembership Dialog dialog;537 final PropertiesMembershipChoiceDialog dialog; 623 538 try { 624 dialog = PropertiesMembership Dialog.showIfNecessary(selectedNodes, false);539 dialog = PropertiesMembershipChoiceDialog.showIfNecessary(selectedNodes, false); 625 540 } catch (UserCancelException e) { 626 541 Logging.trace(e); … … 634 549 tmpWay = modifyWay(n, tmpWay, cmds, newNodes); 635 550 if (dialog != null) { 636 dialog.update(n, newNodes, cmds);551 update(dialog, n, newNodes, cmds); 637 552 } 638 553 allNewNodes.addAll(newNodes);
Note:
See TracChangeset
for help on using the changeset viewer.