Changeset 1000 in josm
- Timestamp:
- 2008-09-21T14:09:25+02:00 (16 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/corrector
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java
r764 r1000 12 12 import java.util.regex.Pattern; 13 13 14 import org.openstreetmap.josm.command.ChangePropertyCommand; 14 import org.openstreetmap.josm.Main; 15 import org.openstreetmap.josm.command.Command; 15 16 import org.openstreetmap.josm.data.osm.OsmPrimitive; 16 17 import org.openstreetmap.josm.data.osm.OsmUtils; 18 import org.openstreetmap.josm.data.osm.Relation; 19 import org.openstreetmap.josm.data.osm.RelationMember; 17 20 import org.openstreetmap.josm.data.osm.Way; 18 21 … … 22 25 23 26 private final String a; 24 25 27 private final String b; 26 27 28 private final Pattern startPattern; 28 29 29 private final Pattern endPattern; 30 30 31 private final String SEPARATOR = "[:_]?"; 32 31 33 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); 34 this.a = a; 35 this.b = b; 36 startPattern = Pattern.compile( 37 "^(" + a + "|" + b + ")" + SEPARATOR, 38 Pattern.CASE_INSENSITIVE); 39 endPattern = Pattern.compile( 40 SEPARATOR + "(" + a + "|" + b + ")$", 41 Pattern.CASE_INSENSITIVE); 38 42 } 39 43 40 44 public String apply(String text) { 41 45 Matcher m = startPattern.matcher(text); 42 if (!m. matches())46 if (!m.lookingAt()) 43 47 m = endPattern.matcher(text); 44 48 45 if (m. matches()) {49 if (m.lookingAt()) { 46 50 String leftRight = m.group(1).toLowerCase(); 47 51 48 return text.substring(0, m.start(1)).concat( 49 leftRight.equals(a) ? b : a).concat( 50 text.substring(m.end(1))); 52 StringBuilder result = new StringBuilder(); 53 result.append(text.substring(0, m.start(1))); 54 result.append(leftRight.equals(a) ? b : a); 55 result.append(text.substring(m.end(1))); 56 57 return result.toString(); 51 58 } 52 59 return text; … … 54 61 } 55 62 56 private static PrefixSuffixSwitcher[] prefixSuffixSwitchers = new PrefixSuffixSwitcher[] { 57 new PrefixSuffixSwitcher("left", "right"), 58 new PrefixSuffixSwitcher("forward", "backward") }; 63 private static PrefixSuffixSwitcher[] prefixSuffixSwitchers = 64 new PrefixSuffixSwitcher[] { 65 new PrefixSuffixSwitcher("left", "right"), 66 new PrefixSuffixSwitcher("forward", "backward") 67 }; 59 68 60 @Override public Collection<ChangePropertyCommand> execute(Way way) { 61 62 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap = new HashMap<OsmPrimitive, List<TagCorrection>>(); 69 @Override 70 public Collection<Command> execute(Way way) throws UserCancelException { 71 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap = 72 new HashMap<OsmPrimitive, List<TagCorrection>>(); 63 73 64 74 ArrayList<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>(); … … 67 77 68 78 for (OsmPrimitive primitive : primitives) { 69 70 79 tagCorrectionsMap.put(primitive, new ArrayList<TagCorrection>()); 71 80 … … 98 107 } 99 108 100 return applyCorrections(tagCorrectionsMap, 109 Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap = 110 new HashMap<OsmPrimitive, List<RoleCorrection>>(); 111 roleCorrectionMap.put(way, new ArrayList<RoleCorrection>()); 112 113 for (Relation relation : Main.ds.relations) { 114 for (RelationMember member : relation.members) { 115 if (!member.member.realEqual(way, true) 116 || member.role.isEmpty()) 117 continue; 118 119 boolean found = false; 120 String newRole = null; 121 for (PrefixSuffixSwitcher prefixSuffixSwitcher : prefixSuffixSwitchers) { 122 newRole = prefixSuffixSwitcher.apply(member.role); 123 if (!newRole.equals(member.role)) { 124 found = true; 125 break; 126 } 127 } 128 129 if (found) 130 roleCorrectionMap.get(way).add( 131 new RoleCorrection(relation, member, newRole)); 132 } 133 } 134 135 return applyCorrections(tagCorrectionsMap, roleCorrectionMap, 101 136 tr("When reverting this way, following changes to properties " 102 137 + "of the way and its nodes are suggested in order " -
trunk/src/org/openstreetmap/josm/corrector/TagCorrection.java
r729 r1000 2 2 package org.openstreetmap.josm.corrector; 3 3 4 public class TagCorrection {4 public class TagCorrection implements Correction { 5 5 6 6 public final String oldKey; … … 9 9 public final String newValue; 10 10 11 public TagCorrection(String oldKey, String oldValue, String newKey, String newValue) { 11 public TagCorrection(String oldKey, String oldValue, String newKey, 12 String newValue) { 12 13 this.oldKey = oldKey; 13 14 this.oldValue = oldValue; -
trunk/src/org/openstreetmap/josm/corrector/TagCorrectionTable.java
r764 r1000 2 2 package org.openstreetmap.josm.corrector; 3 3 4 import java.awt.Dimension;5 4 import java.util.List; 6 5 7 import javax.swing.JTable; 8 import javax.swing.JLabel; 9 import javax.swing.table.TableCellRenderer; 10 import java.awt.Component; 11 import java.awt.Font; 6 public class TagCorrectionTable extends 7 CorrectionTable<TagCorrectionTableModel> { 12 8 13 public class TagCorrectionTable extends JTable { 14 15 public static class BoldRenderer extends JLabel implements TableCellRenderer { 16 17 public Component getTableCellRendererComponent(JTable table, 18 Object value, boolean isSelected, boolean hasFocus, int row, 19 int column) { 20 21 Font f = getFont(); 22 setFont(new Font(f.getName(), f.getStyle() | Font.BOLD, f.getSize())); 23 24 setText((String)value); 25 26 return this; 27 } 28 } 29 30 private static TableCellRenderer boldRenderer = null; 31 32 public static TagCorrectionTable create(List<TagCorrection> tagCorrections) { 33 TagCorrectionTableModel tagCorrectionTableModel = new TagCorrectionTableModel(tagCorrections); 34 TagCorrectionTable table = new TagCorrectionTable( 35 tagCorrectionTableModel); 36 int lines = tagCorrections.size() > 10 ? 10 : tagCorrections.size(); 37 table.setPreferredScrollableViewportSize(new Dimension(400, lines * table.getRowHeight())); 38 table.getColumnModel().getColumn(4).setPreferredWidth(40); 39 table.setRowSelectionAllowed(false); 40 41 return table; 42 } 43 44 public TableCellRenderer getCellRenderer(int row, int column) { 45 TagCorrection tagCorrection = getTagCorrectionTableModel().tagCorrections 46 .get(row); 47 if ((column == 2 && tagCorrection.isKeyChanged()) 48 || (column == 3 && tagCorrection.isValueChanged())) { 49 if (boldRenderer == null) 50 boldRenderer = new BoldRenderer(); 51 return boldRenderer; 52 } 53 return super.getCellRenderer(row, column); 54 } 55 56 private TagCorrectionTable(TagCorrectionTableModel tagCorrectionTableModel) { 57 super(tagCorrectionTableModel); 58 } 59 60 public TagCorrectionTableModel getTagCorrectionTableModel() { 61 return (TagCorrectionTableModel) getModel(); 9 public TagCorrectionTable(List<TagCorrection> tagCorrections) { 10 super(new TagCorrectionTableModel(tagCorrections)); 62 11 } 63 12 -
trunk/src/org/openstreetmap/josm/corrector/TagCorrectionTableModel.java
r732 r1000 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.util.Arrays;7 6 import java.util.List; 8 7 9 import javax.swing.table.AbstractTableModel; 10 11 public class TagCorrectionTableModel extends AbstractTableModel { 12 13 List<TagCorrection> tagCorrections; 14 15 private boolean[] apply; 8 public class TagCorrectionTableModel extends CorrectionTableModel<TagCorrection> { 16 9 17 10 public TagCorrectionTableModel(List<TagCorrection> tagCorrections) { 18 this.tagCorrections = tagCorrections; 19 apply = new boolean[this.tagCorrections.size()]; 20 Arrays.fill(apply, true); 11 super(tagCorrections); 21 12 } 22 13 14 @Override 23 15 public int getColumnCount() { 24 16 return 5; 25 17 } 26 18 27 @Override public Class<?> getColumnClass(int columnIndex) { 28 if (columnIndex == 4) 29 return Boolean.class; 30 return String.class; 31 } 32 33 @Override public String getColumnName(int colIndex) { 19 @Override 20 public String getCorrectionColumnName(int colIndex) { 34 21 switch (colIndex) { 35 22 case 0: … … 41 28 case 3: 42 29 return tr("New value"); 43 case 4:44 return tr("Apply?");45 30 } 46 31 return null; 47 32 } 48 33 49 public int getRowCount() { 50 return tagCorrections.size(); 51 } 52 53 public Object getValueAt(int rowIndex, int colIndex) { 54 55 TagCorrection tagCorrection = tagCorrections.get(rowIndex); 34 public Object getCorrectionValueAt(int rowIndex, int colIndex) { 35 TagCorrection tagCorrection = getCorrections().get(rowIndex); 56 36 57 37 switch (colIndex) { … … 64 44 case 3: 65 45 return tagCorrection.newValue; 66 case 4:67 return apply[rowIndex];68 46 } 69 47 return null; 70 48 } 71 49 72 @Override public boolean isCellEditable(int rowIndex, int columnIndex) { 73 return columnIndex == 4; 50 protected boolean isBoldCell(int row, int column) { 51 TagCorrection tagCorrection = getCorrections().get(row); 52 return (column == 2 && tagCorrection.isKeyChanged()) 53 || (column == 3 && tagCorrection.isValueChanged()); 74 54 } 75 55 76 @Override public void setValueAt(Object aValue, int rowIndex,77 int columnIndex) {78 if (columnIndex == 4 && aValue instanceof Boolean)79 apply[rowIndex] = (Boolean)aValue;80 }81 82 public boolean getApply(int i) {83 return apply[i];84 }85 56 } -
trunk/src/org/openstreetmap/josm/corrector/TagCorrector.java
r764 r1000 18 18 19 19 import org.openstreetmap.josm.Main; 20 import org.openstreetmap.josm.command.ChangeCommand; 20 21 import org.openstreetmap.josm.command.ChangePropertyCommand; 22 import org.openstreetmap.josm.command.Command; 21 23 import org.openstreetmap.josm.data.osm.OsmPrimitive; 24 import org.openstreetmap.josm.data.osm.Relation; 25 import org.openstreetmap.josm.data.osm.RelationMember; 22 26 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 23 27 import org.openstreetmap.josm.gui.JMultilineLabel; … … 26 30 public abstract class TagCorrector<P extends OsmPrimitive> { 27 31 28 public abstract Collection<ChangePropertyCommand> execute(P primitive); 32 public abstract Collection<Command> execute(P primitive) 33 throws UserCancelException; 29 34 30 protected Collection<ChangePropertyCommand> applyCorrections( 35 private String[] applicationOptions = new String[] { 36 tr("Apply selected changes"), 37 tr("Don't apply changes"), 38 tr("Cancel") 39 }; 40 41 protected Collection<Command> applyCorrections( 31 42 Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap, 32 String description) { 43 Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap, 44 String description) throws UserCancelException { 33 45 34 46 boolean hasCorrections = false; … … 40 52 } 41 53 54 if (!hasCorrections) 55 for (List<RoleCorrection> roleCorrectionList : roleCorrectionMap 56 .values()) { 57 if (!roleCorrectionList.isEmpty()) { 58 hasCorrections = true; 59 break; 60 } 61 } 62 42 63 if (hasCorrections) { 43 Collection<ChangePropertyCommand> changePropertyCommands = new ArrayList<ChangePropertyCommand>(); 44 Map<OsmPrimitive, TagCorrectionTable> tableMap = new HashMap<OsmPrimitive, TagCorrectionTable>(); 64 Collection<Command> commands = new ArrayList<Command>(); 65 Map<OsmPrimitive, TagCorrectionTable> tagTableMap = 66 new HashMap<OsmPrimitive, TagCorrectionTable>(); 67 Map<OsmPrimitive, RoleCorrectionTable> roleTableMap = 68 new HashMap<OsmPrimitive, RoleCorrectionTable>(); 69 45 70 NameVisitor nameVisitor = new NameVisitor(); 46 71 … … 63 88 continue; 64 89 65 final TagCorrectionTable table = TagCorrectionTable 66 .create(tagCorrections); 90 primitive.visit(nameVisitor); 91 92 final JLabel propertiesLabel = new JLabel(tr("Properties of ")); 93 p.add(propertiesLabel, GBC.std()); 94 95 final JLabel primitiveLabel = new JLabel( 96 nameVisitor.name + ":", nameVisitor.icon, JLabel.LEFT); 97 p.add(primitiveLabel, GBC.eol()); 98 99 final TagCorrectionTable table = new TagCorrectionTable( 100 tagCorrections); 67 101 final JScrollPane scrollPane = new JScrollPane(table); 68 tableMap.put(primitive, table); 102 p.add(scrollPane, GBC.eop()); 103 104 tagTableMap.put(primitive, table); 105 } 106 107 for (OsmPrimitive primitive : roleCorrectionMap.keySet()) { 108 final List<RoleCorrection> roleCorrections = roleCorrectionMap 109 .get(primitive); 110 if (roleCorrections.isEmpty()) 111 continue; 69 112 70 113 primitive.visit(nameVisitor); 71 114 72 final JLabel label3 = new JLabel(nameVisitor.name + ":", 73 nameVisitor.icon, JLabel.LEFT); 115 final JLabel rolesLabel = new JLabel( 116 tr("Roles in relations refering to")); 117 p.add(rolesLabel, GBC.std()); 74 118 75 p.add(label3, GBC.eol()); 119 final JLabel primitiveLabel = new JLabel( 120 nameVisitor.name + ":", nameVisitor.icon, JLabel.LEFT); 121 p.add(primitiveLabel, GBC.eol()); 122 123 final RoleCorrectionTable table = new RoleCorrectionTable( 124 roleCorrections); 125 final JScrollPane scrollPane = new JScrollPane(table); 76 126 p.add(scrollPane, GBC.eop()); 127 128 roleTableMap.put(primitive, table); 77 129 } 78 130 79 int answer = JOptionPane.showConfirmDialog(Main.parent, p, 80 tr("Automatic tag correction"), 81 JOptionPane.OK_CANCEL_OPTION); 131 int answer = JOptionPane.showOptionDialog(Main.parent, p, 132 tr("Automatic tag correction"), JOptionPane.YES_NO_CANCEL_OPTION, 133 JOptionPane.PLAIN_MESSAGE, null, 134 applicationOptions, applicationOptions[0]); 82 135 83 if (answer == JOptionPane. OK_OPTION) {136 if (answer == JOptionPane.YES_OPTION) { 84 137 for (OsmPrimitive primitive : tagCorrectionsMap.keySet()) { 85 138 List<TagCorrection> tagCorrections = tagCorrectionsMap 86 139 .get(primitive); 87 140 for (int i = 0; i < tagCorrections.size(); i++) { 88 if (ta bleMap.get(primitive)89 .get TagCorrectionTableModel().getApply(i)) {141 if (tagTableMap.get(primitive) 142 .getCorrectionTableModel().getApply(i)) { 90 143 TagCorrection tagCorrection = tagCorrections.get(i); 91 144 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)); 145 commands.add(new ChangePropertyCommand( 146 primitive, tagCorrection.oldKey, null)); 147 commands.add(new ChangePropertyCommand(primitive, 148 tagCorrection.newKey, 149 tagCorrection.newValue)); 100 150 } 101 151 } 102 152 } 153 for (OsmPrimitive primitive : roleCorrectionMap.keySet()) { 154 List<RoleCorrection> roleCorrections = roleCorrectionMap 155 .get(primitive); 156 for (int i = 0; i < roleCorrections.size(); i++) { 157 if (roleTableMap.get(primitive) 158 .getCorrectionTableModel().getApply(i)) { 159 RoleCorrection roleCorrection = roleCorrections 160 .get(i); 161 Relation newRelation = new Relation( 162 roleCorrection.relation); 163 for (RelationMember member : newRelation.members) 164 if (member.equals(roleCorrection.member)) 165 member.role = roleCorrection.newRole; 166 commands.add(new ChangeCommand( 167 roleCorrection.relation, newRelation)); 168 } 169 } 170 } 171 } else if (answer != JOptionPane.NO_OPTION) { 172 throw new UserCancelException(); 103 173 } 104 return c hangePropertyCommands;174 return commands; 105 175 } 106 176
Note:
See TracChangeset
for help on using the changeset viewer.