- Timestamp:
- 2008-08-11T00:40:31+02:00 (16 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/ReverseWayAction.java
r733 r764 14 14 import org.openstreetmap.josm.Main; 15 15 import org.openstreetmap.josm.command.ChangeCommand; 16 import org.openstreetmap.josm.command.ChangePropertyCommand; 16 17 import org.openstreetmap.josm.command.Command; 17 18 import org.openstreetmap.josm.command.SequenceCommand; 18 19 import org.openstreetmap.josm.corrector.ReverseWayTagCorrector; 20 import org.openstreetmap.josm.data.osm.DataSet; 19 21 import org.openstreetmap.josm.data.osm.Relation; 20 22 import org.openstreetmap.josm.data.osm.Node; … … 25 27 public final class ReverseWayAction extends JosmAction { 26 28 27 28 29 30 31 29 public ReverseWayAction() { 30 super(tr("Reverse ways"), "wayflip", 31 tr("Reverse the direction of all selected ways."), 32 KeyEvent.VK_R, 0, true); 33 } 32 34 33 35 public void actionPerformed(ActionEvent e) { 34 final Collection<Way> sel = new LinkedList<Way>(); 35 new Visitor(){ 36 public void visit(Node n) {} 37 public void visit(Way w) {sel.add(w);} 38 public void visit(Relation e){} 36 final Collection<Way> sel = new LinkedList<Way>(); 37 new Visitor() { 38 public void visit(Node n) { 39 } 40 41 public void visit(Way w) { 42 sel.add(w); 43 } 44 45 public void visit(Relation e) { 46 } 47 39 48 public void visitAll() { 40 49 for (OsmPrimitive osm : Main.ds.getSelected()) 41 50 osm.visit(this); 42 51 } 43 52 }.visitAll(); 44 53 45 46 47 48 49 54 if (sel.isEmpty()) { 55 JOptionPane.showMessageDialog(Main.parent, 56 tr("Please select at least one way.")); 57 return; 58 } 50 59 51 60 boolean propertiesUpdated = false; 52 61 ReverseWayTagCorrector reverseWayTagCorrector = new ReverseWayTagCorrector(); 53 62 Collection<Command> c = new LinkedList<Command>(); … … 55 64 Way wnew = new Way(w); 56 65 Collections.reverse(wnew.nodes); 57 if (Main.pref.getBoolean("tag-correction.reverse-way", true)) 58 propertiesUpdated = reverseWayTagCorrector.execute(wnew) || propertiesUpdated; 66 if (Main.pref.getBoolean("tag-correction.reverse-way", true)) { 67 final Collection<ChangePropertyCommand> changePropertyCommands = reverseWayTagCorrector 68 .execute(wnew); 69 propertiesUpdated = propertiesUpdated 70 || (changePropertyCommands != null && !changePropertyCommands 71 .isEmpty()); 72 c.addAll(changePropertyCommands); 73 } 59 74 c.add(new ChangeCommand(w, wnew)); 60 75 } 61 76 Main.main.undoRedo.add(new SequenceCommand(tr("Reverse ways"), c)); 62 77 if (propertiesUpdated) 63 Main.ds.fireSelectionChanged(Main.ds.getSelected());78 DataSet.fireSelectionChanged(Main.ds.getSelected()); 64 79 Main.map.repaint(); 65 80 } 66 81 } -
trunk/src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java
r729 r764 5 5 6 6 import java.util.ArrayList; 7 import java.util.Collection; 8 import java.util.HashMap; 9 import java.util.List; 10 import java.util.Map; 7 11 import java.util.regex.Matcher; 8 12 import java.util.regex.Pattern; 9 13 14 import org.openstreetmap.josm.command.ChangePropertyCommand; 15 import org.openstreetmap.josm.data.osm.OsmPrimitive; 10 16 import org.openstreetmap.josm.data.osm.OsmUtils; 11 17 import org.openstreetmap.josm.data.osm.Way; … … 13 19 public class ReverseWayTagCorrector extends TagCorrector<Way> { 14 20 15 private static final Pattern leftRightStartRegex = Pattern.compile( 16 "^(left|right):.*", Pattern.CASE_INSENSITIVE); 21 private static class PrefixSuffixSwitcher { 17 22 18 private static final Pattern leftRightEndRegex = Pattern.compile( 19 ".*:(left|right)$", Pattern.CASE_INSENSITIVE); 23 private final String a; 20 24 21 @Override public boolean execute(Way way) {25 private final String b; 22 26 23 ArrayList<TagCorrection> tagCorrections = new ArrayList<TagCorrection>();27 private final Pattern startPattern; 24 28 25 for (String key : way.keySet()) { 26 String newKey = key; 27 String value = way.get(key); 28 String newValue = value; 29 private final Pattern endPattern; 29 30 30 if (key.equals("oneway")) { 31 if (value.equals("-1")) 32 newValue = OsmUtils.trueval; 33 else { 34 Boolean boolValue = OsmUtils.getOsmBoolean(value); 35 if (boolValue != null && boolValue.booleanValue()) { 36 newValue = "-1"; 31 public PrefixSuffixSwitcher(String a, String b) { 32 this.a = a; 33 this.b = b; 34 startPattern = Pattern.compile("^(" + a + "|" + b + "):.*", 35 Pattern.CASE_INSENSITIVE); 36 endPattern = Pattern.compile(".*:(" + a + "|" + b + ")$", 37 Pattern.CASE_INSENSITIVE); 38 } 39 40 public String apply(String text) { 41 Matcher m = startPattern.matcher(text); 42 if (!m.matches()) 43 m = endPattern.matcher(text); 44 45 if (m.matches()) { 46 String leftRight = m.group(1).toLowerCase(); 47 48 return text.substring(0, m.start(1)).concat( 49 leftRight.equals(a) ? b : a).concat( 50 text.substring(m.end(1))); 51 } 52 return text; 53 } 54 } 55 56 private static PrefixSuffixSwitcher[] prefixSuffixSwitchers = new PrefixSuffixSwitcher[] { 57 new PrefixSuffixSwitcher("left", "right"), 58 new PrefixSuffixSwitcher("forward", "backward") }; 59 60 @Override public Collection<ChangePropertyCommand> execute(Way way) { 61 62 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap = new HashMap<OsmPrimitive, List<TagCorrection>>(); 63 64 ArrayList<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>(); 65 primitives.add(way); 66 primitives.addAll(way.nodes); 67 68 for (OsmPrimitive primitive : primitives) { 69 70 tagCorrectionsMap.put(primitive, new ArrayList<TagCorrection>()); 71 72 for (String key : primitive.keySet()) { 73 String newKey = key; 74 String value = primitive.get(key); 75 String newValue = value; 76 77 if (key.equals("oneway")) { 78 if (value.equals("-1")) 79 newValue = OsmUtils.trueval; 80 else { 81 Boolean boolValue = OsmUtils.getOsmBoolean(value); 82 if (boolValue != null && boolValue.booleanValue()) { 83 newValue = "-1"; 84 } 85 } 86 } else { 87 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) { 88 newKey = prefixSuffixSwitcher.apply(key); 89 if (!key.equals(newKey)) 90 break; 37 91 } 38 92 } 39 } else {40 Matcher m = leftRightStartRegex.matcher(key);41 if (!m.matches())42 m = leftRightEndRegex.matcher(key);43 93 44 if (m.matches()) { 45 String leftRight = m.group(1).toLowerCase(); 46 47 newKey = key.substring(0, m.start(1)).concat( 48 leftRight.equals("left") ? "right" : "left") 49 .concat(key.substring(m.end(1))); 50 } 94 if (!key.equals(newKey) || !value.equals(newValue)) 95 tagCorrectionsMap.get(primitive).add( 96 new TagCorrection(key, value, newKey, newValue)); 51 97 } 52 53 if (key != newKey || value != newValue)54 tagCorrections.add(new TagCorrection(key, value, newKey,55 newValue));56 98 } 57 99 58 return applyCorrections(tagCorrections , way,59 tr("When reverting this way, following changes to the"60 + " properties are suggested in order to maintain"61 + " data consistency."));100 return applyCorrections(tagCorrectionsMap, 101 tr("When reverting this way, following changes to properties " 102 + "of the way and its nodes are suggested in order " 103 + "to maintain data consistency.")); 62 104 } 63 105 } -
trunk/src/org/openstreetmap/josm/corrector/TagCorrectionTable.java
r729 r764 13 13 public class TagCorrectionTable extends JTable { 14 14 15 public class BoldRenderer extends JLabel implements TableCellRenderer {15 public static class BoldRenderer extends JLabel implements TableCellRenderer { 16 16 17 17 public Component getTableCellRendererComponent(JTable table, … … 30 30 private static TableCellRenderer boldRenderer = null; 31 31 32 private static TagCorrectionTableModel tagCorrectionTableModel;33 34 32 public static TagCorrectionTable create(List<TagCorrection> tagCorrections) { 35 36 tagCorrectionTableModel = new TagCorrectionTableModel(tagCorrections); 33 TagCorrectionTableModel tagCorrectionTableModel = new TagCorrectionTableModel(tagCorrections); 37 34 TagCorrectionTable table = new TagCorrectionTable( 38 35 tagCorrectionTableModel); … … 46 43 47 44 public TableCellRenderer getCellRenderer(int row, int column) { 48 TagCorrection tagCorrection = tagCorrectionTableModel.tagCorrections45 TagCorrection tagCorrection = getTagCorrectionTableModel().tagCorrections 49 46 .get(row); 50 47 if ((column == 2 && tagCorrection.isKeyChanged()) … … 62 59 63 60 public TagCorrectionTableModel getTagCorrectionTableModel() { 64 return tagCorrectionTableModel;61 return (TagCorrectionTableModel) getModel(); 65 62 } 66 63 -
trunk/src/org/openstreetmap/josm/corrector/TagCorrector.java
r729 r764 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.Dimension;7 6 import java.awt.GridBagLayout; 7 import java.util.ArrayList; 8 import java.util.Collection; 9 import java.util.Collections; 10 import java.util.HashMap; 8 11 import java.util.List; 12 import java.util.Map; 9 13 10 14 import javax.swing.JLabel; … … 14 18 15 19 import org.openstreetmap.josm.Main; 20 import org.openstreetmap.josm.command.ChangePropertyCommand; 16 21 import org.openstreetmap.josm.data.osm.OsmPrimitive; 22 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 17 23 import org.openstreetmap.josm.gui.JMultilineLabel; 18 24 import org.openstreetmap.josm.tools.GBC; … … 20 26 public abstract class TagCorrector<P extends OsmPrimitive> { 21 27 22 public abstract booleanexecute(P primitive);28 public abstract Collection<ChangePropertyCommand> execute(P primitive); 23 29 24 protected boolean applyCorrections(List<TagCorrection> tagCorrections, 25 P primitive, String description) { 30 protected Collection<ChangePropertyCommand> applyCorrections( 31 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap, 32 String description) { 26 33 27 boolean updated = false; 34 boolean hasCorrections = false; 35 for (List<TagCorrection> tagCorrectionList : tagCorrectionsMap.values()) { 36 if (!tagCorrectionList.isEmpty()) { 37 hasCorrections = true; 38 break; 39 } 40 } 28 41 29 if (tagCorrections != null && tagCorrections.size() > 0) { 42 if (hasCorrections) { 43 Collection<ChangePropertyCommand> changePropertyCommands = new ArrayList<ChangePropertyCommand>(); 44 Map<OsmPrimitive, TagCorrectionTable> tableMap = new HashMap<OsmPrimitive, TagCorrectionTable>(); 45 NameVisitor nameVisitor = new NameVisitor(); 30 46 31 final TagCorrectionTable table = TagCorrectionTable 32 .create(tagCorrections); 33 final JScrollPane scrollPane = new JScrollPane(table); 34 35 final JPanel p = new JPanel(new GridBagLayout()); 47 final JPanel p = new JPanel(new GridBagLayout()); 36 48 37 49 final JMultilineLabel label1 = new JMultilineLabel(description); 38 50 label1.setMaxWidth(400); 39 51 p.add(label1, GBC.eop()); 40 41 final JMultilineLabel label2 = new JMultilineLabel(tr("Please select which property changes you want to apply.")); 52 53 final JMultilineLabel label2 = new JMultilineLabel( 54 tr("Please select which property changes you want to apply.")); 42 55 label2.setMaxWidth(400); 43 56 p.add(label2, GBC.eop()); 44 p.add(scrollPane, GBC.eol()); 45 57 58 for (OsmPrimitive primitive : tagCorrectionsMap.keySet()) { 59 final List<TagCorrection> tagCorrections = tagCorrectionsMap 60 .get(primitive); 61 62 if (tagCorrections.isEmpty()) 63 continue; 64 65 final TagCorrectionTable table = TagCorrectionTable 66 .create(tagCorrections); 67 final JScrollPane scrollPane = new JScrollPane(table); 68 tableMap.put(primitive, table); 69 70 primitive.visit(nameVisitor); 71 72 final JLabel label3 = new JLabel(nameVisitor.name + ":", 73 nameVisitor.icon, JLabel.LEFT); 74 75 p.add(label3, GBC.eol()); 76 p.add(scrollPane, GBC.eop()); 77 } 78 46 79 int answer = JOptionPane.showConfirmDialog(Main.parent, p, 47 80 tr("Automatic tag correction"), … … 49 82 50 83 if (answer == JOptionPane.OK_OPTION) { 51 for (int i = 0; i < tagCorrections.size(); i++) { 52 if (table.getTagCorrectionTableModel().getApply(i)) { 53 TagCorrection tagCorrection = tagCorrections.get(i); 54 if (tagCorrection.isKeyChanged()) 55 primitive.remove(tagCorrection.oldKey); 56 primitive.put(tagCorrection.newKey, tagCorrection 57 .newValue); 58 updated = true; 84 for (OsmPrimitive primitive : tagCorrectionsMap.keySet()) { 85 List<TagCorrection> tagCorrections = tagCorrectionsMap 86 .get(primitive); 87 for (int i = 0; i < tagCorrections.size(); i++) { 88 if (tableMap.get(primitive) 89 .getTagCorrectionTableModel().getApply(i)) { 90 TagCorrection tagCorrection = tagCorrections.get(i); 91 if (tagCorrection.isKeyChanged()) 92 changePropertyCommands 93 .add(new ChangePropertyCommand( 94 primitive, 95 tagCorrection.oldKey, null)); 96 changePropertyCommands 97 .add(new ChangePropertyCommand(primitive, 98 tagCorrection.newKey, 99 tagCorrection.newValue)); 100 } 59 101 } 60 102 } 61 103 } 104 return changePropertyCommands; 62 105 } 63 106 64 return updated;107 return Collections.emptyList(); 65 108 } 66 67 109 }
Note:
See TracChangeset
for help on using the changeset viewer.