Ignore:
Timestamp:
01.09.2010 10:48:28 (21 months 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)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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} 
Note: See TracChangeset for help on using the changeset viewer.