Hi,

here is a small update of the TagCorrector. Some code cleanup and
addition of the key prefixes/suffixes "forward" and "backward".

Robin

Index: src/org/openstreetmap/josm/actions/ReverseWayAction.java
===================================================================
--- src/org/openstreetmap/josm/actions/ReverseWayAction.java    (revision 743)
+++ src/org/openstreetmap/josm/actions/ReverseWayAction.java    (working copy)
@@ -16,6 +16,7 @@
 import org.openstreetmap.josm.command.Command;
 import org.openstreetmap.josm.command.SequenceCommand;
 import org.openstreetmap.josm.corrector.ReverseWayTagCorrector;
+import org.openstreetmap.josm.data.osm.DataSet;
 import org.openstreetmap.josm.data.osm.Relation;
 import org.openstreetmap.josm.data.osm.Node;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
@@ -60,7 +61,7 @@
                }
                Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), 
c));
                if (propertiesUpdated)
-                       Main.ds.fireSelectionChanged(Main.ds.getSelected());
+                       DataSet.fireSelectionChanged(Main.ds.getSelected());
                Main.map.repaint();
     }
 }
Index: src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java
===================================================================
--- src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java    
(revision 743)
+++ src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java    
(working copy)
@@ -9,15 +9,48 @@
 
 import org.openstreetmap.josm.data.osm.OsmUtils;
 import org.openstreetmap.josm.data.osm.Way;
+import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
 
 public class ReverseWayTagCorrector extends TagCorrector<Way> {
 
-       private static final Pattern leftRightStartRegex = Pattern.compile(
-               "^(left|right):.*", Pattern.CASE_INSENSITIVE);
+       private static class PrefixSuffixSwitcher {
 
-       private static final Pattern leftRightEndRegex = Pattern.compile(
-               ".*:(left|right)$", Pattern.CASE_INSENSITIVE);
+               private final String a;
+               private final String b;
 
+               private final Pattern startPattern;
+               private final Pattern endPattern;
+
+               public PrefixSuffixSwitcher(String a, String b) {
+                       this.a = a;
+                       this.b = b;
+                       startPattern = Pattern.compile("^(" + a + "|" + b + 
"):.*",
+                               Pattern.CASE_INSENSITIVE);
+                       endPattern = Pattern.compile(".*:(" + a + "|" + b + 
")$",
+                               Pattern.CASE_INSENSITIVE);
+               }
+
+               public String apply(String text) {
+                       Matcher m = startPattern.matcher(text);
+                       if (!m.matches())
+                               m = endPattern.matcher(text);
+
+                       if (m.matches()) {
+                               String leftRight = m.group(1).toLowerCase();
+
+                               return text.substring(0, m.start(1)).concat(
+                                       leftRight.equals(a) ? b : a).concat(
+                                       text.substring(m.end(1)));
+                       }
+                       return text;
+               }
+       }
+
+       private static PrefixSuffixSwitcher[] prefixSuffixSwitchers = new 
PrefixSuffixSwitcher[] {
+               new PrefixSuffixSwitcher("left", "right"),
+               new PrefixSuffixSwitcher("forward", "backward")
+       };
+       
        @Override public boolean execute(Way way) {
 
                ArrayList<TagCorrection> tagCorrections = new 
ArrayList<TagCorrection>();
@@ -37,27 +70,26 @@
                                        }
                                }
                        } else {
-                               Matcher m = leftRightStartRegex.matcher(key);
-                               if (!m.matches())
-                                       m = leftRightEndRegex.matcher(key);
-
-                               if (m.matches()) {
-                                       String leftRight = 
m.group(1).toLowerCase();
-
-                                       newKey = key.substring(0, 
m.start(1)).concat(
-                                               leftRight.equals("left") ? 
"right" : "left")
-                                               
.concat(key.substring(m.end(1)));
-                               }
+                               for (PrefixSuffixSwitcher prefixSuffixSwitcher 
: prefixSuffixSwitchers) {
+                                       newKey = 
prefixSuffixSwitcher.apply(key);
+                                       if (!key.equals(newKey))
+                                               break;
+                }                              
                        }
 
-                       if (key != newKey || value != newValue)
+                       if (!key.equals(newKey) || !value.equals(newValue))
                                tagCorrections.add(new TagCorrection(key, 
value, newKey,
                                        newValue));
                }
 
-               return applyCorrections(tagCorrections, way,
-                       tr("When reverting this way, following changes to the "
-                               + "properties are suggested in order to 
maintain "
-                               + "data consistency."));
+               NameVisitor nameVisitor = new NameVisitor();
+               nameVisitor.visit(way);
+               
+               return applyCorrections(
+                       tagCorrections,
+                       way,
+                       tr("When reverting this way " + nameVisitor.name
+                               + ", following changes to the properties are 
suggested "
+                               + "in order to maintain data consistency."));
        }
 }
Index: src/org/openstreetmap/josm/corrector/TagCorrector.java
===================================================================
--- src/org/openstreetmap/josm/corrector/TagCorrector.java      (revision 743)
+++ src/org/openstreetmap/josm/corrector/TagCorrector.java      (working copy)
@@ -3,11 +3,9 @@
 
 import static org.openstreetmap.josm.tools.I18n.tr;
 
-import java.awt.Dimension;
 import java.awt.GridBagLayout;
 import java.util.List;
 
-import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
_______________________________________________
josm-dev mailing list
[email protected]
http://lists.openstreetmap.org/listinfo/josm-dev

Reply via email to