Robin Rattay schrieb:
> I've already continued the development

I've finished extending the tag corrector, so that it can change more
that one element. This is in preperation for changing relations sometime
in future. For now it includes changing node tags, when reverting a way
(as discussed on talk-de).

You can ignore yesterdays patch und go with this one.

Robin
Index: src/org/openstreetmap/josm/actions/ReverseWayAction.java
===================================================================
--- src/org/openstreetmap/josm/actions/ReverseWayAction.java    (revision 745)
+++ src/org/openstreetmap/josm/actions/ReverseWayAction.java    (working copy)
@@ -13,9 +13,11 @@
 
 import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.command.ChangeCommand;
+import org.openstreetmap.josm.command.ChangePropertyCommand;
 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;
@@ -24,43 +26,56 @@
 
 public final class ReverseWayAction extends JosmAction {
 
-    public ReverseWayAction() {
-       super(tr("Reverse ways"), "wayflip",
-                       tr("Reverse the direction of all selected ways."),
-                       KeyEvent.VK_R, 0, true);
-    }
+       public ReverseWayAction() {
+               super(tr("Reverse ways"), "wayflip",
+                       tr("Reverse the direction of all selected ways."),
+                       KeyEvent.VK_R, 0, true);
+       }
 
        public void actionPerformed(ActionEvent e) {
-       final Collection<Way> sel = new LinkedList<Way>();
-       new Visitor(){
-                       public void visit(Node n)    {}
-                       public void visit(Way w)     {sel.add(w);}
-                       public void visit(Relation e){}
+               final Collection<Way> sel = new LinkedList<Way>();
+               new Visitor() {
+                       public void visit(Node n) {
+                       }
+
+                       public void visit(Way w) {
+                               sel.add(w);
+                       }
+
+                       public void visit(Relation e) {
+                       }
+
                        public void visitAll() {
                                for (OsmPrimitive osm : Main.ds.getSelected())
                                        osm.visit(this);
                        }
-       }.visitAll();
+               }.visitAll();
 
-       if (sel.isEmpty()) {
-               JOptionPane.showMessageDialog(Main.parent,
-                               tr("Please select at least one way."));
-               return;
-       }
+               if (sel.isEmpty()) {
+                       JOptionPane.showMessageDialog(Main.parent,
+                               tr("Please select at least one way."));
+                       return;
+               }
 
-       boolean propertiesUpdated = false;
+               boolean propertiesUpdated = false;
                ReverseWayTagCorrector reverseWayTagCorrector = new 
ReverseWayTagCorrector();
                Collection<Command> c = new LinkedList<Command>();
                for (Way w : sel) {
                        Way wnew = new Way(w);
                        Collections.reverse(wnew.nodes);
-                       if (Main.pref.getBoolean("tag-correction.reverse-way", 
true))
-                               propertiesUpdated = 
reverseWayTagCorrector.execute(wnew) || propertiesUpdated;
+                       if (Main.pref.getBoolean("tag-correction.reverse-way", 
true)) {
+                               final Collection<ChangePropertyCommand> 
changePropertyCommands = reverseWayTagCorrector
+                                       .execute(wnew);
+                               propertiesUpdated = propertiesUpdated
+                                       || (changePropertyCommands != null && 
!changePropertyCommands
+                                               .isEmpty());
+                               c.addAll(changePropertyCommands);
+                       }
                        c.add(new ChangeCommand(w, wnew));
                }
                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 745)
+++ src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java    
(working copy)
@@ -4,60 +4,102 @@
 import static org.openstreetmap.josm.tools.I18n.tr;
 
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.openstreetmap.josm.command.ChangePropertyCommand;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
 import org.openstreetmap.josm.data.osm.OsmUtils;
 import org.openstreetmap.josm.data.osm.Way;
 
 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;
 
-       @Override public boolean execute(Way way) {
+               private final String b;
 
-               ArrayList<TagCorrection> tagCorrections = new 
ArrayList<TagCorrection>();
+               private final Pattern startPattern;
 
-               for (String key : way.keySet()) {
-                       String newKey = key;
-                       String value = way.get(key);
-                       String newValue = value;
+               private final Pattern endPattern;
 
-                       if (key.equals("oneway")) {
-                               if (value.equals("-1"))
-                                       newValue = OsmUtils.trueval;
-                               else {
-                                       Boolean boolValue = 
OsmUtils.getOsmBoolean(value);
-                                       if (boolValue != null && 
boolValue.booleanValue()) {
-                                               newValue = "-1";
+               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 Collection<ChangePropertyCommand> execute(Way way) {
+
+               Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap = new 
HashMap<OsmPrimitive, List<TagCorrection>>();
+
+               ArrayList<OsmPrimitive> primitives = new 
ArrayList<OsmPrimitive>();
+               primitives.add(way);
+               primitives.addAll(way.nodes);
+
+               for (OsmPrimitive primitive : primitives) {
+
+                       tagCorrectionsMap.put(primitive, new 
ArrayList<TagCorrection>());
+
+                       for (String key : primitive.keySet()) {
+                               String newKey = key;
+                               String value = primitive.get(key);
+                               String newValue = value;
+
+                               if (key.equals("oneway")) {
+                                       if (value.equals("-1"))
+                                               newValue = OsmUtils.trueval;
+                                       else {
+                                               Boolean boolValue = 
OsmUtils.getOsmBoolean(value);
+                                               if (boolValue != null && 
boolValue.booleanValue()) {
+                                                       newValue = "-1";
+                                               }
                                        }
+                               } else {
+                                       for (PrefixSuffixSwitcher 
prefixSuffixSwitcher : prefixSuffixSwitchers) {
+                                               newKey = 
prefixSuffixSwitcher.apply(key);
+                                               if (!key.equals(newKey))
+                                                       break;
+                                       }
                                }
-                       } 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)));
-                               }
+                               if (!key.equals(newKey) || 
!value.equals(newValue))
+                                       tagCorrectionsMap.get(primitive).add(
+                                               new TagCorrection(key, value, 
newKey, newValue));
                        }
-
-                       if (key != newKey || value != 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."));
+               return applyCorrections(tagCorrectionsMap,
+                       tr("When reverting this way, following changes to 
properties "
+                               + "of the way and its nodes are suggested in 
order "
+                               + "to maintain data consistency."));
        }
 }
Index: src/org/openstreetmap/josm/corrector/TagCorrectionTable.java
===================================================================
--- src/org/openstreetmap/josm/corrector/TagCorrectionTable.java        
(revision 745)
+++ src/org/openstreetmap/josm/corrector/TagCorrectionTable.java        
(working copy)
@@ -12,7 +12,7 @@
 
 public class TagCorrectionTable extends JTable {
 
-       public class BoldRenderer extends JLabel implements TableCellRenderer {
+       public static class BoldRenderer extends JLabel implements 
TableCellRenderer {
 
                public Component getTableCellRendererComponent(JTable table,
                        Object value, boolean isSelected, boolean hasFocus, int 
row,
@@ -29,11 +29,8 @@
 
        private static TableCellRenderer boldRenderer = null;
 
-       private static TagCorrectionTableModel tagCorrectionTableModel;
-
        public static TagCorrectionTable create(List<TagCorrection> 
tagCorrections) {
-
-               tagCorrectionTableModel = new 
TagCorrectionTableModel(tagCorrections);
+               TagCorrectionTableModel tagCorrectionTableModel = new 
TagCorrectionTableModel(tagCorrections);
                TagCorrectionTable table = new TagCorrectionTable(
                        tagCorrectionTableModel);
                int lines = tagCorrections.size() > 10 ? 10 : 
tagCorrections.size();  
@@ -45,7 +42,7 @@
        }
 
        public TableCellRenderer getCellRenderer(int row, int column) {
-               TagCorrection tagCorrection = 
tagCorrectionTableModel.tagCorrections
+               TagCorrection tagCorrection = 
getTagCorrectionTableModel().tagCorrections
                        .get(row);
                if ((column == 2 && tagCorrection.isKeyChanged())
                        || (column == 3 && tagCorrection.isValueChanged())) {
@@ -61,7 +58,7 @@
        }
 
        public TagCorrectionTableModel getTagCorrectionTableModel() {
-               return tagCorrectionTableModel;
+               return (TagCorrectionTableModel) getModel();
        }
 
 }
Index: src/org/openstreetmap/josm/corrector/TagCorrector.java
===================================================================
--- src/org/openstreetmap/josm/corrector/TagCorrector.java      (revision 745)
+++ src/org/openstreetmap/josm/corrector/TagCorrector.java      (working copy)
@@ -3,9 +3,13 @@
 
 import static org.openstreetmap.josm.tools.I18n.tr;
 
-import java.awt.Dimension;
 import java.awt.GridBagLayout;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
@@ -13,55 +17,93 @@
 import javax.swing.JScrollPane;
 
 import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.command.ChangePropertyCommand;
 import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
 import org.openstreetmap.josm.gui.JMultilineLabel;
 import org.openstreetmap.josm.tools.GBC;
 
 public abstract class TagCorrector<P extends OsmPrimitive> {
 
-       public abstract boolean execute(P primitive);
+       public abstract Collection<ChangePropertyCommand> execute(P primitive);
 
-       protected boolean applyCorrections(List<TagCorrection> tagCorrections,
-               P primitive, String description) {
+       protected Collection<ChangePropertyCommand> applyCorrections(
+               Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap,
+               String description) {
 
-               boolean updated = false;
+               boolean hasCorrections = false;
+               for (List<TagCorrection> tagCorrectionList : 
tagCorrectionsMap.values()) {
+                       if (!tagCorrectionList.isEmpty()) {
+                               hasCorrections = true;
+                               break;
+                       }
+               }
 
-               if (tagCorrections != null && tagCorrections.size() > 0) {
+               if (hasCorrections) {
+                       Collection<ChangePropertyCommand> 
changePropertyCommands = new ArrayList<ChangePropertyCommand>();
+                       Map<OsmPrimitive, TagCorrectionTable> tableMap = new 
HashMap<OsmPrimitive, TagCorrectionTable>();
+                       NameVisitor nameVisitor = new NameVisitor();
 
-                       final TagCorrectionTable table = TagCorrectionTable
-                               .create(tagCorrections);
-                       final JScrollPane scrollPane = new JScrollPane(table);
+                       final JPanel p = new JPanel(new GridBagLayout());
 
-               final JPanel p = new JPanel(new GridBagLayout());
-
                        final JMultilineLabel label1 = new 
JMultilineLabel(description);
                        label1.setMaxWidth(400);
                        p.add(label1, GBC.eop());
-                       
-                       final JMultilineLabel label2 = new 
JMultilineLabel(tr("Please select which property changes you want to apply."));
+
+                       final JMultilineLabel label2 = new JMultilineLabel(
+                               tr("Please select which property changes you 
want to apply."));
                        label2.setMaxWidth(400);
                        p.add(label2, GBC.eop());
-               p.add(scrollPane, GBC.eol());
-                       
+
+                       for (OsmPrimitive primitive : 
tagCorrectionsMap.keySet()) {
+                               final List<TagCorrection> tagCorrections = 
tagCorrectionsMap
+                                       .get(primitive);
+
+                               if (tagCorrections.isEmpty())
+                                       continue;
+
+                               final TagCorrectionTable table = 
TagCorrectionTable
+                                       .create(tagCorrections);
+                               final JScrollPane scrollPane = new 
JScrollPane(table);
+                               tableMap.put(primitive, table);
+
+                               primitive.visit(nameVisitor);
+
+                               final JLabel label3 = new 
JLabel(nameVisitor.name + ":",
+                                       nameVisitor.icon, JLabel.LEFT);
+
+                               p.add(label3, GBC.eol());
+                               p.add(scrollPane, GBC.eop());
+                       }
+
                        int answer = JOptionPane.showConfirmDialog(Main.parent, 
p,
                                tr("Automatic tag correction"),
                                JOptionPane.OK_CANCEL_OPTION);
 
                        if (answer == JOptionPane.OK_OPTION) {
-                               for (int i = 0; i < tagCorrections.size(); i++) 
{
-                                       if 
(table.getTagCorrectionTableModel().getApply(i)) {
-                                               TagCorrection tagCorrection = 
tagCorrections.get(i);
-                                               if 
(tagCorrection.isKeyChanged())
-                                                       
primitive.remove(tagCorrection.oldKey);
-                                               
primitive.put(tagCorrection.newKey, tagCorrection
-                                                       .newValue);
-                                               updated = true;
+                               for (OsmPrimitive primitive : 
tagCorrectionsMap.keySet()) {
+                                       List<TagCorrection> tagCorrections = 
tagCorrectionsMap
+                                               .get(primitive);
+                                       for (int i = 0; i < 
tagCorrections.size(); i++) {
+                                               if (tableMap.get(primitive)
+                                                       
.getTagCorrectionTableModel().getApply(i)) {
+                                                       TagCorrection 
tagCorrection = tagCorrections.get(i);
+                                                       if 
(tagCorrection.isKeyChanged())
+                                                               
changePropertyCommands
+                                                                       
.add(new ChangePropertyCommand(
+                                                                               
primitive,
+                                                                               
tagCorrection.oldKey, null));
+                                                       changePropertyCommands
+                                                               .add(new 
ChangePropertyCommand(primitive,
+                                                                       
tagCorrection.newKey,
+                                                                       
tagCorrection.newValue));
+                                               }
                                        }
                                }
                        }
+                       return changePropertyCommands;
                }
 
-               return updated;
+               return Collections.emptyList();
        }
-
 }
_______________________________________________
josm-dev mailing list
[email protected]
http://lists.openstreetmap.org/listinfo/josm-dev

Reply via email to