Changeset 14320 in josm


Ignore:
Timestamp:
2018-10-13T14:37:04+02:00 (6 years ago)
Author:
Don-vip
Message:

see #16820 - extract GUI from UnglueAction

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  
    66import static org.openstreetmap.josm.tools.I18n.trn;
    77
    8 import java.awt.GridBagLayout;
    98import java.awt.event.ActionEvent;
    109import java.awt.event.KeyEvent;
     
    1918import java.util.Set;
    2019
    21 import javax.swing.AbstractButton;
    22 import javax.swing.ButtonGroup;
    23 import javax.swing.JLabel;
    2420import javax.swing.JOptionPane;
    2521import javax.swing.JPanel;
    26 import javax.swing.JToggleButton;
    2722
    2823import org.openstreetmap.josm.command.AddCommand;
     
    3833import org.openstreetmap.josm.data.osm.RelationMember;
    3934import org.openstreetmap.josm.data.osm.Way;
    40 import org.openstreetmap.josm.gui.ExtendedDialog;
    4135import org.openstreetmap.josm.gui.MainApplication;
    4236import org.openstreetmap.josm.gui.MapView;
    4337import org.openstreetmap.josm.gui.Notification;
    44 import org.openstreetmap.josm.tools.GBC;
    45 import org.openstreetmap.josm.tools.ImageProvider;
     38import org.openstreetmap.josm.gui.dialogs.PropertiesMembershipChoiceDialog;
     39import org.openstreetmap.josm.gui.dialogs.PropertiesMembershipChoiceDialog.ExistingBothNewChoice;
    4640import org.openstreetmap.josm.tools.Logging;
    4741import org.openstreetmap.josm.tools.Shortcut;
     
    181175    }
    182176
    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);
    285199        }
    286200    }
     
    292206     */
    293207    private void unglueOneNodeAtMostOneWay(ActionEvent e) {
    294         final PropertiesMembershipDialog dialog;
     208        final PropertiesMembershipChoiceDialog dialog;
    295209        try {
    296             dialog = PropertiesMembershipDialog.showIfNecessary(Collections.singleton(selectedNode), true);
     210            dialog = PropertiesMembershipChoiceDialog.showIfNecessary(Collections.singleton(selectedNode), true);
    297211        } catch (UserCancelException ex) {
    298212            Logging.trace(ex);
     
    305219        cmds.add(new AddCommand(selectedNode.getDataSet(), n));
    306220        if (dialog != null) {
    307             dialog.update(selectedNode, Collections.singletonList(n), cmds);
     221            update(dialog, selectedNode, Collections.singletonList(n), cmds);
    308222        }
    309223
     
    505419     */
    506420    private void unglueWays() {
    507         final PropertiesMembershipDialog dialog;
     421        final PropertiesMembershipChoiceDialog dialog;
    508422        try {
    509             dialog = PropertiesMembershipDialog.showIfNecessary(Collections.singleton(selectedNode), false);
     423            dialog = PropertiesMembershipChoiceDialog.showIfNecessary(Collections.singleton(selectedNode), false);
    510424        } catch (UserCancelException e) {
    511425            Logging.trace(e);
     
    541455
    542456        if (dialog != null) {
    543             dialog.update(selectedNode, newNodes, cmds);
     457            update(dialog, selectedNode, newNodes, cmds);
    544458        }
    545459
     
    601515        notifyWayPartOfRelation(Collections.singleton(way));
    602516        try {
    603             final PropertiesMembershipDialog dialog = PropertiesMembershipDialog.showIfNecessary(Collections.singleton(selectedNode), false);
     517            final PropertiesMembershipChoiceDialog dialog = PropertiesMembershipChoiceDialog.showIfNecessary(
     518                    Collections.singleton(selectedNode), false);
    604519            if (dialog != null) {
    605                 dialog.update(selectedNode, addNodes, cmds);
     520                update(dialog, selectedNode, addNodes, cmds);
    606521            }
    607522            execCommands(cmds, addNodes);
     
    620535        Way tmpWay = selectedWay;
    621536
    622         final PropertiesMembershipDialog dialog;
     537        final PropertiesMembershipChoiceDialog dialog;
    623538        try {
    624             dialog = PropertiesMembershipDialog.showIfNecessary(selectedNodes, false);
     539            dialog = PropertiesMembershipChoiceDialog.showIfNecessary(selectedNodes, false);
    625540        } catch (UserCancelException e) {
    626541            Logging.trace(e);
     
    634549            tmpWay = modifyWay(n, tmpWay, cmds, newNodes);
    635550            if (dialog != null) {
    636                 dialog.update(n, newNodes, cmds);
     551                update(dialog, n, newNodes, cmds);
    637552            }
    638553            allNewNodes.addAll(newNodes);
Note: See TracChangeset for help on using the changeset viewer.