Changeset 3504 in josm for trunk/src/org/openstreetmap/josm/actions/ReverseWayAction.java
- Timestamp:
- 01.09.2010 10:48:28 (21 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/ReverseWayAction.java
r2633 r3504 7 7 import java.awt.event.ActionEvent; 8 8 import java.awt.event.KeyEvent; 9 import java.util.ArrayList; 9 10 import java.util.Collection; 10 11 import java.util.Collections; … … 24 25 import org.openstreetmap.josm.data.osm.Way; 25 26 import org.openstreetmap.josm.tools.Shortcut; 27 import org.openstreetmap.josm.tools.Utils; 26 28 27 29 public 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 } 28 65 29 66 public ReverseWayAction() { 30 67 super(tr("Reverse Ways"), "wayflip", tr("Reverse the direction of all selected ways."), 31 68 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")); 33 70 } 34 71 … … 51 88 52 89 boolean propertiesUpdated = false; 53 ReverseWayTagCorrector reverseWayTagCorrector = new ReverseWayTagCorrector();54 90 Collection<Command> c = new LinkedList<Command>(); 55 91 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; 72 97 } 73 c.add(new ChangeCommand(w, wnew)); 98 c.addAll(revResult.getCommands()); 99 propertiesUpdated |= !revResult.getTagCorrectionCommands().isEmpty(); 74 100 } 75 101 Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c)); … … 78 104 } 79 105 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)); 80 123 } 81 124 … … 102 145 @Override 103 146 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)); 115 148 } 116 149 }
Note: See TracChangeset
for help on using the changeset viewer.
