Changeset 1000 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2008-09-21T14:09:25+02:00 (16 years ago)
Author:
stoecker
Message:

applied correction patch by Robin Rattay

Location:
trunk/src/org/openstreetmap/josm/corrector
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/corrector/ReverseWayTagCorrector.java

    r764 r1000  
    1212import java.util.regex.Pattern;
    1313
    14 import org.openstreetmap.josm.command.ChangePropertyCommand;
     14import org.openstreetmap.josm.Main;
     15import org.openstreetmap.josm.command.Command;
    1516import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1617import org.openstreetmap.josm.data.osm.OsmUtils;
     18import org.openstreetmap.josm.data.osm.Relation;
     19import org.openstreetmap.josm.data.osm.RelationMember;
    1720import org.openstreetmap.josm.data.osm.Way;
    1821
     
    2225
    2326                private final String a;
    24 
    2527                private final String b;
    26 
    2728                private final Pattern startPattern;
    28 
    2929                private final Pattern endPattern;
    3030
     31                private final String SEPARATOR = "[:_]?";
     32               
    3133                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);
    3842                }
    3943
    4044                public String apply(String text) {
    4145                        Matcher m = startPattern.matcher(text);
    42                         if (!m.matches())
     46                        if (!m.lookingAt())
    4347                                m = endPattern.matcher(text);
    4448
    45                         if (m.matches()) {
     49                        if (m.lookingAt()) {
    4650                                String leftRight = m.group(1).toLowerCase();
    4751
    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();
    5158                        }
    5259                        return text;
     
    5461        }
    5562
    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                };
    5968
    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>>();
    6373
    6474                ArrayList<OsmPrimitive> primitives = new ArrayList<OsmPrimitive>();
     
    6777
    6878                for (OsmPrimitive primitive : primitives) {
    69 
    7079                        tagCorrectionsMap.put(primitive, new ArrayList<TagCorrection>());
    7180
     
    98107                }
    99108
    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,
    101136                        tr("When reverting this way, following changes to properties "
    102137                                + "of the way and its nodes are suggested in order "
  • trunk/src/org/openstreetmap/josm/corrector/TagCorrection.java

    r729 r1000  
    22package org.openstreetmap.josm.corrector;
    33
    4 public class TagCorrection {
     4public class TagCorrection implements Correction {
    55
    66        public final String oldKey;
     
    99        public final String newValue;
    1010
    11         public TagCorrection(String oldKey, String oldValue, String newKey, String newValue) {
     11        public TagCorrection(String oldKey, String oldValue, String newKey,
     12            String newValue) {
    1213                this.oldKey = oldKey;
    1314                this.oldValue = oldValue;
  • trunk/src/org/openstreetmap/josm/corrector/TagCorrectionTable.java

    r764 r1000  
    22package org.openstreetmap.josm.corrector;
    33
    4 import java.awt.Dimension;
    54import java.util.List;
    65
    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;
     6public class TagCorrectionTable extends
     7        CorrectionTable<TagCorrectionTableModel> {
    128
    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));
    6211        }
    6312
  • trunk/src/org/openstreetmap/josm/corrector/TagCorrectionTableModel.java

    r732 r1000  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.util.Arrays;
    76import java.util.List;
    87
    9 import javax.swing.table.AbstractTableModel;
    10 
    11 public class TagCorrectionTableModel extends AbstractTableModel {
    12 
    13         List<TagCorrection> tagCorrections;
    14 
    15         private boolean[] apply;
     8public class TagCorrectionTableModel extends CorrectionTableModel<TagCorrection> {
    169
    1710        public TagCorrectionTableModel(List<TagCorrection> tagCorrections) {
    18                 this.tagCorrections = tagCorrections;
    19                 apply = new boolean[this.tagCorrections.size()];
    20                 Arrays.fill(apply, true);
     11                super(tagCorrections);
    2112        }
    2213
     14        @Override
    2315        public int getColumnCount() {
    2416                return 5;
    2517        }
    2618
    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) {
    3421                switch (colIndex) {
    3522                case 0:
     
    4128                case 3:
    4229                        return tr("New value");
    43                 case 4:
    44                         return tr("Apply?");
    4530                }
    4631                return null;
    4732        }
    4833
    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);
    5636
    5737                switch (colIndex) {
     
    6444                case 3:
    6545                        return tagCorrection.newValue;
    66                 case 4:
    67                         return apply[rowIndex];
    6846                }
    6947                return null;
    7048        }
    7149
    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());
    7454        }
    7555
    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         }
    8556}
  • trunk/src/org/openstreetmap/josm/corrector/TagCorrector.java

    r764 r1000  
    1818
    1919import org.openstreetmap.josm.Main;
     20import org.openstreetmap.josm.command.ChangeCommand;
    2021import org.openstreetmap.josm.command.ChangePropertyCommand;
     22import org.openstreetmap.josm.command.Command;
    2123import org.openstreetmap.josm.data.osm.OsmPrimitive;
     24import org.openstreetmap.josm.data.osm.Relation;
     25import org.openstreetmap.josm.data.osm.RelationMember;
    2226import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
    2327import org.openstreetmap.josm.gui.JMultilineLabel;
     
    2630public abstract class TagCorrector<P extends OsmPrimitive> {
    2731
    28         public abstract Collection<ChangePropertyCommand> execute(P primitive);
     32        public abstract Collection<Command> execute(P primitive)
     33            throws UserCancelException;
    2934
    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(
    3142                Map<OsmPrimitive, List<TagCorrection>> tagCorrectionsMap,
    32                 String description) {
     43                Map<OsmPrimitive, List<RoleCorrection>> roleCorrectionMap,
     44                String description) throws UserCancelException {
    3345
    3446                boolean hasCorrections = false;
     
    4052                }
    4153
     54                if (!hasCorrections)
     55                        for (List<RoleCorrection> roleCorrectionList : roleCorrectionMap
     56                                .values()) {
     57                                if (!roleCorrectionList.isEmpty()) {
     58                                        hasCorrections = true;
     59                                        break;
     60                                }
     61                        }
     62
    4263                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
    4570                        NameVisitor nameVisitor = new NameVisitor();
    4671
     
    6388                                        continue;
    6489
    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);
    67101                                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;
    69112
    70113                                primitive.visit(nameVisitor);
    71114
    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());
    74118
    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);
    76126                                p.add(scrollPane, GBC.eop());
     127
     128                                roleTableMap.put(primitive, table);
    77129                        }
    78130
    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]);
    82135
    83                         if (answer == JOptionPane.OK_OPTION) {
     136                        if (answer == JOptionPane.YES_OPTION) {
    84137                                for (OsmPrimitive primitive : tagCorrectionsMap.keySet()) {
    85138                                        List<TagCorrection> tagCorrections = tagCorrectionsMap
    86139                                                .get(primitive);
    87140                                        for (int i = 0; i < tagCorrections.size(); i++) {
    88                                                 if (tableMap.get(primitive)
    89                                                         .getTagCorrectionTableModel().getApply(i)) {
     141                                                if (tagTableMap.get(primitive)
     142                                                        .getCorrectionTableModel().getApply(i)) {
    90143                                                        TagCorrection tagCorrection = tagCorrections.get(i);
    91144                                                        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));
    100150                                                }
    101151                                        }
    102152                                }
     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();
    103173                        }
    104                         return changePropertyCommands;
     174                        return commands;
    105175                }
    106176
Note: See TracChangeset for help on using the changeset viewer.