Changeset 3504 in josm


Ignore:
Timestamp:
2010-09-01T10:48:28+02:00 (14 years ago)
Author:
bastiK
Message:

reworked reverseWay and combineWay such that it can be used by other actions without hack (see #5179 - Join overlapping areas rewrite)

Location:
trunk/src/org/openstreetmap/josm
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/CombineWayAction.java

    r3445 r3504  
    5757    }
    5858
    59     protected boolean confirmChangeDirectionOfWays() {
     59    protected static boolean confirmChangeDirectionOfWays() {
    6060        ExtendedDialog ed = new ExtendedDialog(Main.parent,
    6161                tr("Change directions?"),
     
    6868    }
    6969
    70     protected void warnCombiningImpossible() {
     70    protected static void warnCombiningImpossible() {
    7171        String msg = tr("Could not combine ways "
    7272                + "(They could not be merged into a single string of nodes)");
     
    8080    }
    8181
    82     protected Way getTargetWay(Collection<Way> combinedWays) {
     82    protected static Way getTargetWay(Collection<Way> combinedWays) {
    8383        // init with an arbitrary way
    8484        Way targetWay = combinedWays.iterator().next();
     
    100100     * @return the set of referring relations
    101101     */
    102     protected Set<Relation> getParentRelations(Collection<Way> ways) {
     102    public static Set<Relation> getParentRelations(Collection<Way> ways) {
    103103        HashSet<Relation> ret = new HashSet<Relation>();
    104104        for (Way w: ways) {
     
    108108    }
    109109
    110     public Way combineWays(Collection<Way> ways) {
     110    @Deprecated
     111    public static Way combineWays(Collection<Way> ways) {
     112        Pair<Way, Command> combineResult;
     113        try {
     114            combineResult = combineWaysWorker(ways);
     115        } catch (UserCancelException ex) {
     116            return null;
     117        }
     118        return combineResult.a;
     119    }
     120
     121    /**
     122     * @param ways
     123     * @return null if ways cannot be combined. Otherwise returns the combined
     124     *              ways and the commands to combine
     125     * @throws UserCancelException
     126     */
     127    public static Pair<Way, Command> combineWaysWorker(Collection<Way> ways) throws UserCancelException {
    111128
    112129        // prepare and clean the list of ways to combine
     
    170187                    Way wnew = new Way(w);
    171188                    reversedTagWays.add(wnew);
    172                     try {
    173                         changePropertyCommands = reverseWayTagCorrector.execute(w, wnew);
    174                     }
    175                     catch(UserCancelException ex) {
    176                         return null;
    177                     }
     189                    changePropertyCommands = reverseWayTagCorrector.execute(w, wnew);
    178190                }
    179191                if ((changePropertyCommands != null) && !changePropertyCommands.isEmpty()) {
     
    214226            dialog.setVisible(true);
    215227            if (dialog.isCancelled())
    216                 return null;
     228                throw new UserCancelException();
    217229        }
    218230
     
    225237        cmds.add(new DeleteCommand(deletedWays));
    226238        final SequenceCommand sequenceCommand = new SequenceCommand(tr("Combine {0} ways", ways.size()), cmds);
    227         Main.main.undoRedo.add(sequenceCommand);
    228 
    229         return targetWay;
     239
     240        return new Pair<Way, Command>(targetWay, sequenceCommand);
    230241    }
    231242
     
    245256        }
    246257        // combine and update gui
    247         final Way selectedWay = combineWays(selectedWays);
     258        Pair<Way, Command> combineResult;
     259        try {
     260            combineResult = combineWaysWorker(selectedWays);
     261        } catch (UserCancelException ex) {
     262            return;
     263        }
     264
     265        if (combineResult == null)
     266            return;
     267        final Way selectedWay = combineResult.a;
     268        Main.main.undoRedo.add(combineResult.b);
    248269        if(selectedWay != null)
    249270        {
  • trunk/src/org/openstreetmap/josm/actions/JosmAction.java

    r3444 r3504  
    179179     * Override in subclasses to update the enabled state of the action if the
    180180     * collection of selected primitives changes. This method is called with the
    181      * new selection. Avoid calling getCurrentDataSet().getSelected() because this
    182      * loops over the complete data set.
    183      *
    184      * @param selection the collection of selected primitives
     181     * new selection.
     182     *
     183     * @param selection the collection of selected primitives; may be empty, but not null
    185184     *
    186185     * @see #updateEnabledState()
  • trunk/src/org/openstreetmap/josm/actions/ReverseWayAction.java

    r2633 r3504  
    77import java.awt.event.ActionEvent;
    88import java.awt.event.KeyEvent;
     9import java.util.ArrayList;
    910import java.util.Collection;
    1011import java.util.Collections;
     
    2425import org.openstreetmap.josm.data.osm.Way;
    2526import org.openstreetmap.josm.tools.Shortcut;
     27import org.openstreetmap.josm.tools.Utils;
    2628
    2729public final class ReverseWayAction extends JosmAction {
     30
     31    public static class ReverseWayResult {
     32        private Way newWay;
     33        private Collection<Command> tagCorrectionCommands;
     34        private Command reverseCommand;
     35
     36        public ReverseWayResult(Way newWay, Collection<Command> tagCorrectionCommands, Command reverseCommand) {
     37            this.newWay = newWay;
     38            this.tagCorrectionCommands = tagCorrectionCommands;
     39            this.reverseCommand = reverseCommand;
     40        }
     41
     42        public Way getNewWay() {
     43            return newWay;
     44        }
     45
     46        public Collection<Command> getCommands() {
     47            List<Command> c = new ArrayList<Command>();
     48            c.addAll(tagCorrectionCommands);
     49            c.add(reverseCommand);
     50            return c;
     51        }
     52
     53        public Command getAsSequenceCommand() {
     54            return new SequenceCommand(tr("Reverse way"), getCommands());
     55        }
     56
     57        public Command getReverseCommand() {
     58            return reverseCommand;
     59        }
     60
     61        public Collection<Command> getTagCorrectionCommands() {
     62            return tagCorrectionCommands;
     63        }
     64    }
    2865
    2966    public ReverseWayAction() {
    3067        super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."),
    3168                Shortcut.registerShortcut("tools:reverse", tr("Tool: {0}", tr("Reverse Ways")), KeyEvent.VK_R, Shortcut.GROUP_EDIT), true);
    32         putValue("help", ht("/Action/ReverseWay"));
     69        putValue("help", ht("/Action/ReverseWays"));
    3370    }
    3471
     
    5188
    5289        boolean propertiesUpdated = false;
    53         ReverseWayTagCorrector reverseWayTagCorrector = new ReverseWayTagCorrector();
    5490        Collection<Command> c = new LinkedList<Command>();
    5591        for (Way w : sel) {
    56             Way wnew = new Way(w);
    57             List<Node> nodesCopy = wnew.getNodes();
    58             Collections.reverse(nodesCopy);
    59             wnew.setNodes(nodesCopy);
    60             if (Main.pref.getBoolean("tag-correction.reverse-way", true)) {
    61                 try
    62                 {
    63                     final Collection<Command> changePropertyCommands = reverseWayTagCorrector.execute(w, wnew);
    64                     propertiesUpdated = propertiesUpdated
    65                     || (changePropertyCommands != null && !changePropertyCommands.isEmpty());
    66                     c.addAll(changePropertyCommands);
    67                 }
    68                 catch(UserCancelException ex)
    69                 {
    70                     return;
    71                 }
     92            ReverseWayResult revResult;
     93            try {
     94                revResult = reverseWay(w);
     95            } catch (UserCancelException ex) {
     96                return;
    7297            }
    73             c.add(new ChangeCommand(w, wnew));
     98            c.addAll(revResult.getCommands());
     99            propertiesUpdated |= !revResult.getTagCorrectionCommands().isEmpty();
    74100        }
    75101        Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c));
     
    78104        }
    79105        Main.map.repaint();
     106    }
     107
     108    /**
     109     * @param w the way
     110     * @return the reverse command and the tag correction commands
     111     */
     112    public static ReverseWayResult reverseWay(Way w) throws UserCancelException {
     113        Way wnew = new Way(w);
     114        List<Node> nodesCopy = wnew.getNodes();
     115        Collections.reverse(nodesCopy);
     116        wnew.setNodes(nodesCopy);
     117
     118        Collection<Command> corrCmds = Collections.<Command>emptyList();
     119        if (Main.pref.getBoolean("tag-correction.reverse-way", true)) {
     120            corrCmds = (new ReverseWayTagCorrector()).execute(w, wnew);
     121        }
     122        return new ReverseWayResult(wnew, corrCmds, new ChangeCommand(w, wnew));
    80123    }
    81124
     
    102145    @Override
    103146    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
    104         if (selection == null) {
    105             setEnabled(false);
    106             return;
    107         }
    108         int n = 0;
    109         for (OsmPrimitive primitive : selection) {
    110             if (primitive instanceof Way) {
    111                 n++;
    112             }
    113         }
    114         setEnabled(n > 0);
     147        setEnabled(Utils.exists(selection, OsmPrimitive.wayPredicate));
    115148    }
    116149}
  • trunk/src/org/openstreetmap/josm/data/osm/DataSet.java

    r3503 r3504  
    346346    /**
    347347     * Replies an unmodifiable collection of primitives currently selected
    348      * in this dataset
     348     * in this dataset. May be empty, but not null.
    349349     *
    350350     * @return unmodifiable collection of primitives
  • trunk/src/org/openstreetmap/josm/data/osm/Storage.java

    r3503 r3504  
    132132     *          modify, but the read-only iteration will happen on a copy
    133133     *          of the unmodified Storage.
     134     *          This is similar to CopyOnWriteArrayList.
    134135     */
    135136    public Storage(Hash<? super T, ? super T> ha, int capacity, boolean safeIterator) {
Note: See TracChangeset for help on using the changeset viewer.