Ignore:
Timestamp:
2012-04-22T13:41:29+02:00 (13 years ago)
Author:
joshdoe
Message:

[josm_utilsplugin2] return command with ReplaceGeometryUtils instead of executing them directly

Also add icon

Location:
applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/replacegeometry
Files:
2 added
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/replacegeometry/ReplaceGeometryAction.java

    r28028 r28335  
    4646       
    4747        try {
    48             ReplaceGeometryUtils.replaceWithNew(firstObject, secondObject);
     48            ReplaceGeometryCommand replaceCommand =
     49                    ReplaceGeometryUtils.buildReplaceWithNewCommand(firstObject, secondObject);
     50           
     51            // action was canceled
     52            if (replaceCommand == null)
     53                return;
     54           
     55            Main.main.undoRedo.add(replaceCommand);
    4956        } catch (IllegalArgumentException ex) {
    5057            JOptionPane.showMessageDialog(Main.parent,
    5158                    ex.getMessage(), TITLE, JOptionPane.INFORMATION_MESSAGE);
     59        } catch (ReplaceGeometryException ex) {
     60            JOptionPane.showMessageDialog(Main.parent,
     61                    ex.getMessage(), TITLE, JOptionPane.INFORMATION_MESSAGE);
    5262        }
    53          
    5463    }
    5564
  • applications/editors/josm/plugins/utilsplugin2/src/org/openstreetmap/josm/plugins/utilsplugin2/replacegeometry/ReplaceGeometryUtils.java

    r28298 r28335  
    2121public final class ReplaceGeometryUtils {
    2222    private static final String TITLE = tr("Replace Geometry");
    23    
    2423    /**
    2524     * Replace new or uploaded object with new object
     
    2928     * @return
    3029     */
    31     public static boolean replaceWithNew(OsmPrimitive firstObject, OsmPrimitive secondObject) {
     30    public static ReplaceGeometryCommand buildReplaceWithNewCommand(OsmPrimitive firstObject, OsmPrimitive secondObject) {
    3231        if (firstObject instanceof Node && secondObject instanceof Node) {
    33             return replaceNodeWithNew((Node) firstObject, (Node) secondObject);
     32            return buildReplaceNodeWithNewCommand((Node) firstObject, (Node) secondObject);
    3433        } else if (firstObject instanceof Way && secondObject instanceof Way) {
    35             return replaceWayWithNew(Arrays.asList((Way) firstObject, (Way) secondObject));
     34            return buildReplaceWayWithNewCommand(Arrays.asList((Way) firstObject, (Way) secondObject));
    3635        } else if (firstObject instanceof Node) {
    37             return upgradeNode((Node) firstObject, secondObject);
     36            return createUpgradeNodeCommand((Node) firstObject, secondObject);
    3837        } else if (secondObject instanceof Node) {
    39             return upgradeNode((Node) secondObject, firstObject);
     38            return createUpgradeNodeCommand((Node) secondObject, firstObject);
    4039        } else {
    4140            throw new IllegalArgumentException(tr("This tool can only replace a node, upgrade a node to a way or a multipolygon, or replace a way with a way."));
     
    5150     * @return
    5251     */
    53     public static boolean replace(OsmPrimitive subjectObject, OsmPrimitive referenceSubject) {
     52    public static ReplaceGeometryCommand buildReplaceCommand(OsmPrimitive subjectObject, OsmPrimitive referenceSubject) {
    5453        if (subjectObject instanceof Node && referenceSubject instanceof Node) {
    55             return replaceNode((Node) subjectObject, (Node) referenceSubject);
     54            return buildReplaceNodeCommand((Node) subjectObject, (Node) referenceSubject);
    5655        } else if (subjectObject instanceof Way && referenceSubject instanceof Way) {
    57             return replaceWay((Way) subjectObject, (Way) referenceSubject);
     56            return buildReplaceWayCommand((Way) subjectObject, (Way) referenceSubject);
    5857        } else if (subjectObject instanceof Node) {
    59             return upgradeNode((Node) subjectObject, referenceSubject);
     58            return createUpgradeNodeCommand((Node) subjectObject, referenceSubject);
    6059        } else if (referenceSubject instanceof Node) {
    6160            // TODO: fix this illogical reversal?
    62             return upgradeNode((Node) referenceSubject, subjectObject);
     61            return createUpgradeNodeCommand((Node) referenceSubject, subjectObject);
    6362        } else {
    6463            throw new IllegalArgumentException(tr("This tool can only replace a node, upgrade a node to a way or a multipolygon, or replace a way with a way."));
     
    7271     * @return
    7372     */
    74     public static boolean replaceNodeWithNew(Node firstNode, Node secondNode) {
     73    public static ReplaceGeometryCommand buildReplaceNodeWithNewCommand(Node firstNode, Node secondNode) {
    7574        if (firstNode.isNew() && !secondNode.isNew())
    76             return replaceNode(secondNode, firstNode);
     75            return buildReplaceNodeCommand(secondNode, firstNode);
    7776        else if (!firstNode.isNew() && secondNode.isNew())
    78             return replaceNode(firstNode, secondNode);
     77            return buildReplaceNodeCommand(firstNode, secondNode);
    7978        else
    8079            // both nodes are new OR uploaded, act like MergeNodes, moving first
    8180            // node to second
    82             return replaceNode(firstNode, secondNode);
     81            return buildReplaceNodeCommand(firstNode, secondNode);
    8382    }
    8483   
     
    9089     * @return
    9190     */
    92     public static boolean replaceNode(Node subjectNode, Node referenceNode) {
     91    public static ReplaceGeometryCommand buildReplaceNodeCommand(Node subjectNode, Node referenceNode) {
    9392        if (!OsmPrimitive.getFilteredList(subjectNode.getReferrers(), Way.class).isEmpty()) {
    94             JOptionPane.showMessageDialog(Main.parent, tr("Node belongs to way(s), cannot replace."),
    95                     TITLE, JOptionPane.INFORMATION_MESSAGE);
    96             return false;
     93            throw new ReplaceGeometryException(tr("Node belongs to way(s), cannot replace."));
    9794        }
    9895        // FIXME: handle different layers
     
    10097        commands.add(MergeNodesAction.mergeNodes(Main.main.getEditLayer(), Arrays.asList(subjectNode, referenceNode), referenceNode));
    10198
    102         Main.main.undoRedo.add(new SequenceCommand(
     99        return new ReplaceGeometryCommand(
    103100                tr("Replace geometry for node {0}", subjectNode.getDisplayName(DefaultNameFormatter.getInstance())),
    104                 commands));
    105         return true;
     101                commands);
    106102    }
    107103   
     
    112108     * @param referenceObject object with greater spatial quality
    113109     */
    114     public static boolean upgradeNode(Node subjectNode, OsmPrimitive referenceObject) {
     110    public static ReplaceGeometryCommand createUpgradeNodeCommand(Node subjectNode, OsmPrimitive referenceObject) {
    115111        if (!OsmPrimitive.getFilteredList(subjectNode.getReferrers(), Way.class).isEmpty()) {
    116             JOptionPane.showMessageDialog(Main.parent, tr("Node belongs to way(s), cannot replace."),
    117                     TITLE, JOptionPane.INFORMATION_MESSAGE);
    118             return false;
     112            throw new ReplaceGeometryException(tr("Node belongs to way(s), cannot replace."));
    119113        }
    120114
    121115        if (referenceObject instanceof Relation && !((Relation) referenceObject).isMultipolygon()) {
    122             JOptionPane.showMessageDialog(Main.parent, tr("Relation is not a multipolygon, cannot be used as a replacement."),
    123                     TITLE, JOptionPane.INFORMATION_MESSAGE);
    124             return false;
     116            throw new ReplaceGeometryException(tr("Relation is not a multipolygon, cannot be used as a replacement."));
    125117        }
    126118
     
    154146        if (tagResolutionCommands == null) {
    155147            // user canceled tag merge dialog
    156             return false;
     148            return null;
    157149        }
    158150        commands.addAll(tagResolutionCommands);
     
    187179        Main.main.getCurrentDataSet().setSelected(referenceObject);
    188180
    189         Main.main.undoRedo.add(new SequenceCommand(
     181        return new ReplaceGeometryCommand(
    190182                tr("Replace geometry for node {0}", subjectNode.getDisplayName(DefaultNameFormatter.getInstance())),
    191                 commands));
    192         return true;
    193     }
    194    
    195     public static boolean replaceWayWithNew(List<Way> selection) {
     183                commands);
     184    }
     185   
     186    public static ReplaceGeometryCommand buildReplaceWayWithNewCommand(List<Way> selection) {
    196187        // determine which way will be replaced and which will provide the geometry
    197188        boolean overrideNewCheck = false;
     
    217208       
    218209        if( !overrideNewCheck && (subjectWay.isNew() || !referenceWay.isNew()) ) {
    219             JOptionPane.showMessageDialog(Main.parent,
    220                     tr("Please select one way that exists in the database and one new way with correct geometry."),
    221                     TITLE, JOptionPane.WARNING_MESSAGE);
    222             return false;
    223         }
    224         return replaceWay(subjectWay, referenceWay);
    225     }
    226    
    227     public static boolean replaceWay(Way subjectWay, Way referenceWay) {
     210            throw new ReplaceGeometryException(
     211                    tr("Please select one way that exists in the database and one new way with correct geometry."));
     212        }
     213        return buildReplaceWayCommand(subjectWay, referenceWay);
     214    }
     215   
     216    public static ReplaceGeometryCommand buildReplaceWayCommand(Way subjectWay, Way referenceWay) {
    228217
    229218        Area a = Main.main.getCurrentDataSet().getDataSourceArea();
    230219        if (!isInArea(subjectWay, a) || !isInArea(referenceWay, a)) {
    231             JOptionPane.showMessageDialog(Main.parent,
    232                     tr("The ways must be entirely within the downloaded area."),
    233                     TITLE, JOptionPane.WARNING_MESSAGE);
    234             return false;
     220            throw new ReplaceGeometryException(tr("The ways must be entirely within the downloaded area."));
    235221        }
    236222       
    237223        if (hasImportantNode(referenceWay, subjectWay)) {
    238             JOptionPane.showMessageDialog(Main.parent,
    239                     tr("The way to be replaced cannot have any nodes with properties or relation memberships unless they belong to both ways."),
    240                     TITLE, JOptionPane.WARNING_MESSAGE);
    241             return false;
     224            throw new ReplaceGeometryException(
     225                    tr("The way to be replaced cannot have any nodes with properties or relation memberships unless they belong to both ways."));
    242226        }
    243227
     
    248232        if (tagResolutionCommands == null) {
    249233            // user canceled tag merge dialog
    250             return false;
     234            return null;
    251235        }
    252236        commands.addAll(tagResolutionCommands);
     
    357341
    358342        // Two items in undo stack: change original way and delete geometry way
    359         Main.main.undoRedo.add(new SequenceCommand(
     343        return new ReplaceGeometryCommand(
    360344                tr("Replace geometry for way {0}", subjectWay.getDisplayName(DefaultNameFormatter.getInstance())),
    361                 commands));
    362         return true;
     345                commands);
    363346    }
    364347
Note: See TracChangeset for help on using the changeset viewer.