Changeset 1213 in josm for trunk/src


Ignore:
Timestamp:
2009-01-08T15:29:55+01:00 (15 years ago)
Author:
stoecker
Message:

fixed bugs #1864 and #1979, patches by Ævar Arnfjörð Bjarmason and markb _ ordern _ com

Location:
trunk/src/org/openstreetmap/josm
Files:
4 edited

Legend:

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

    r1205 r1213  
    450450        if (args.containsKey("selection"))
    451451            for (String s : args.get("selection"))
    452                 SearchAction.search(s, SearchAction.SearchMode.add, false);
     452                SearchAction.search(s, SearchAction.SearchMode.add, false, false);
    453453    }
    454454
  • trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java

    r1212 r1213  
    4848        SearchSetting s = lastSearch;
    4949        if (s == null)
    50             s = new SearchSetting("", false, SearchMode.replace);
     50            s = new SearchSetting("", false, false, SearchMode.replace);
    5151        showSearchDialog(s);
    5252    }
     
    8181
    8282        JCheckBox caseSensitive = new JCheckBox(tr("case sensitive"), initialValues.caseSensitive);
     83        JCheckBox regexSearch   = new JCheckBox(tr("regular expression"), initialValues.regexSearch);
    8384
    8485        JPanel p = new JPanel(new GridBagLayout());
     
    8990        p.add(remove, GBC.eop());
    9091        p.add(caseSensitive, GBC.eol());
     92        p.add(regexSearch, GBC.eol());
    9193        JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null) {
    9294            @Override
     
    102104        SearchMode mode = replace.isSelected() ? SearchAction.SearchMode.replace
    103105                : (add.isSelected() ? SearchAction.SearchMode.add : SearchAction.SearchMode.remove);
    104         SearchSetting setting = new SearchSetting(input.getText(), caseSensitive.isSelected(), mode);
     106        SearchSetting setting = new SearchSetting(input.getText(), caseSensitive.isSelected(), regexSearch.isSelected(), mode);
    105107        searchWithHistory(setting);
    106108    }
     
    117119            searchHistory.removeLast();
    118120        lastSearch = s;
    119         search(s.text, s.mode, s.caseSensitive);
     121        search(s.text, s.mode, s.caseSensitive, s.regexSearch);
    120122    }
    121123
    122124    public static void searchWithoutHistory(SearchSetting s) {
    123125        lastSearch = s;
    124         search(s.text, s.mode, s.caseSensitive);
     126        search(s.text, s.mode, s.caseSensitive, s.regexSearch);
    125127    }
    126128
    127     public static void search(String search, SearchMode mode, boolean caseSensitive) {
     129    public static void search(String search, SearchMode mode, boolean caseSensitive, boolean regexSearch) {
    128130        if (search.startsWith("http://") || search.startsWith("ftp://") || search.startsWith("https://")
    129131                || search.startsWith("file:/")) {
     
    136138        try {
    137139            Collection<OsmPrimitive> sel = Main.ds.getSelected();
    138             SearchCompiler.Match matcher = SearchCompiler.compile(search, caseSensitive);
     140            SearchCompiler.Match matcher = SearchCompiler.compile(search, caseSensitive, regexSearch);
    139141            int foundMatches = 0;
    140142            for (OsmPrimitive osm : Main.ds.allNonDeletedCompletePrimitives()) {
     
    175177        SearchMode mode;
    176178        boolean caseSensitive;
     179        boolean regexSearch;
    177180
    178         public SearchSetting(String text, boolean caseSensitive, SearchMode mode) {
     181        public SearchSetting(String text, boolean caseSensitive, boolean regexSearch, SearchMode mode) {
    179182            super();
    180183            this.caseSensitive = caseSensitive;
     184            this.regexSearch = regexSearch;
    181185            this.mode = mode;
    182186            this.text = text;
     
    186190        public String toString() {
    187191            String cs = caseSensitive ? tr("CI") : tr("CS");
    188             return "\"" + text + "\" (" + cs + ", " + mode + ")";
     192            String rx = regexSearch ? (", " + tr("RX")) : "";
     193            return "\"" + text + "\" (" + cs + rx + ", " + mode + ")";
    189194        }
    190195
  • trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java

    r1169 r1213  
    22package org.openstreetmap.josm.actions.search;
    33
     4import static org.openstreetmap.josm.tools.I18n.marktr;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    56
     
    78import java.io.StringReader;
    89import java.util.Map.Entry;
     10import java.util.regex.Matcher;
     11import java.util.regex.Pattern;
     12import java.util.regex.PatternSyntaxException;
    913
    1014import org.openstreetmap.josm.data.osm.Node;
     
    2125
    2226    private boolean caseSensitive = false;
     27    private boolean regexSearch = false;
     28    private String  rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}");
    2329    private PushbackTokenizer tokenizer;
    2430
    25     public SearchCompiler(boolean caseSensitive, PushbackTokenizer tokenizer) {
     31    public SearchCompiler(boolean caseSensitive, boolean regexSearch, PushbackTokenizer tokenizer) {
    2632        this.caseSensitive = caseSensitive;
     33        this.regexSearch = regexSearch;
    2734        this.tokenizer = tokenizer;
    2835    }
    2936
    3037    abstract public static class Match {
    31         abstract public boolean match(OsmPrimitive osm);
     38        abstract public boolean match(OsmPrimitive osm) throws ParseError;
    3239    }
    3340
     
    4148        private final Match match;
    4249        public Not(Match match) {this.match = match;}
    43         @Override public boolean match(OsmPrimitive osm) {
     50        @Override public boolean match(OsmPrimitive osm) throws ParseError {
    4451            return !match.match(osm);
    4552        }
     
    5158        private Match rhs;
    5259        public And(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
    53         @Override public boolean match(OsmPrimitive osm) {
     60        @Override public boolean match(OsmPrimitive osm) throws ParseError {
    5461            return lhs.match(osm) && rhs.match(osm);
    5562        }
     
    6168        private Match rhs;
    6269        public Or(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
    63         @Override public boolean match(OsmPrimitive osm) {
     70        @Override public boolean match(OsmPrimitive osm) throws ParseError {
    6471            return lhs.match(osm) || rhs.match(osm);
    6572        }
     
    8087        private String value;
    8188        public KeyValue(String key, String value) {this.key = key; this.value = value; }
    82         @Override public boolean match(OsmPrimitive osm) {
    83             String value = null;
    84             if (key.equals("timestamp"))
    85                 value = osm.getTimeStr();
    86             else
    87                 value = osm.get(key);
    88             if (value == null)
    89                 return false;
    90             String v1 = caseSensitive ? value : value.toLowerCase();
    91             String v2 = caseSensitive ? this.value : this.value.toLowerCase();
    92             // is not Java 1.5
    93             //v1 = java.text.Normalizer.normalize(v1, java.text.Normalizer.Form.NFC);
    94             //v2 = java.text.Normalizer.normalize(v2, java.text.Normalizer.Form.NFC);
    95             return v1.indexOf(v2) != -1;
     89        @Override public boolean match(OsmPrimitive osm) throws ParseError {
     90
     91            if (regexSearch) {
     92                if (osm.keys == null)
     93                    return false;
     94
     95                /* The string search will just get a key like
     96                 * 'highway' and look that up as osm.get(key). But
     97                 * since we're doing a regex match we'll have to loop
     98                 * over all the keys to see if they match our regex,
     99                 * and only then try to match against the value
     100                 */
     101
     102                Pattern searchKey   = null;
     103                Pattern searchValue = null;
     104
     105                if (caseSensitive) {
     106                    try {
     107                        searchKey = Pattern.compile(key);
     108                    } catch (PatternSyntaxException e) {
     109                        throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
     110                    }
     111                    try {
     112                        searchValue = Pattern.compile(value);
     113                    } catch (PatternSyntaxException e) {
     114                        throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
     115                    }
     116                } else {
     117                    try {
     118                        searchKey = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
     119                    } catch (PatternSyntaxException e) {
     120                        throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
     121                    }
     122                    try {
     123                        searchValue = Pattern.compile(value, Pattern.CASE_INSENSITIVE);
     124                    } catch (PatternSyntaxException e) {
     125                        throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
     126                    }
     127                }
     128
     129                for (Entry<String, String> e : osm.keys.entrySet()) {
     130                    String k = e.getKey();
     131                    String v = e.getValue();
     132
     133                    Matcher matcherKey = searchKey.matcher(k);
     134                    boolean matchedKey = matcherKey.find();
     135
     136                    if (matchedKey) {
     137                        Matcher matcherValue = searchValue.matcher(v);
     138                        boolean matchedValue = matcherValue.find();
     139
     140                        if (matchedValue)
     141                            return true;
     142                    }
     143                }
     144            } else {
     145                String value = null;
     146
     147                if (key.equals("timestamp"))
     148                    value = osm.getTimeStr();
     149                else
     150                    value = osm.get(key);
     151
     152                if (value == null)
     153                    return false;
     154
     155                String v1 = caseSensitive ? value : value.toLowerCase();
     156                String v2 = caseSensitive ? this.value : this.value.toLowerCase();
     157
     158                // is not Java 1.5
     159                //v1 = java.text.Normalizer.normalize(v1, java.text.Normalizer.Form.NFC);
     160                //v2 = java.text.Normalizer.normalize(v2, java.text.Normalizer.Form.NFC);
     161                return v1.indexOf(v2) != -1;
     162            }
     163
     164            return false;
    96165        }
    97166        @Override public String toString() {return key+"="+value;}
     
    101170        private String s;
    102171        public Any(String s) {this.s = s;}
    103         @Override public boolean match(OsmPrimitive osm) {
     172        @Override public boolean match(OsmPrimitive osm) throws ParseError {
    104173            if (osm.keys == null)
    105174                return s.equals("");
    106             String search = caseSensitive ? s : s.toLowerCase();
     175
     176            String search;
     177            Pattern searchRegex = null;
     178
     179            if (regexSearch) {
     180                search = s;
     181                if (caseSensitive) {
     182                    try {
     183                        searchRegex = Pattern.compile(search);
     184                    } catch (PatternSyntaxException e) {
     185                        throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
     186                    }
     187                } else {
     188                    try {
     189                        searchRegex = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
     190                    } catch (PatternSyntaxException e) {
     191                        throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
     192                    }
     193                }
     194            } else {
     195                search = caseSensitive ? s : s.toLowerCase();
     196            }
     197
    107198            // is not Java 1.5
    108199            //search = java.text.Normalizer.normalize(search, java.text.Normalizer.Form.NFC);
    109200            for (Entry<String, String> e : osm.keys.entrySet()) {
    110                 String key = caseSensitive ? e.getKey() : e.getKey().toLowerCase();
    111                 String value = caseSensitive ? e.getValue() : e.getValue().toLowerCase();
    112                 // is not Java 1.5
    113                 //value = java.text.Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
    114                 if (key.indexOf(search) != -1 || value.indexOf(search) != -1)
    115                     return true;
     201                if (regexSearch) {
     202                    String key = e.getKey();
     203                    String value = e.getValue();
     204
     205                    // is not Java 1.5
     206                    //value = java.text.Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
     207
     208                    Matcher keyMatcher = searchRegex.matcher(key);
     209                    Matcher valMatcher = searchRegex.matcher(value);
     210
     211                    boolean keyMatchFound = keyMatcher.find();
     212                    boolean valMatchFound = valMatcher.find();
     213
     214                    if (keyMatchFound || valMatchFound)
     215                        return true;
     216                } else {
     217                    String key = caseSensitive ? e.getKey() : e.getKey().toLowerCase();
     218                    String value = caseSensitive ? e.getValue() : e.getValue().toLowerCase();
     219
     220                    // is not Java 1.5
     221                    //value = java.text.Normalizer.normalize(value, java.text.Normalizer.Form.NFC);
     222
     223                    if (key.indexOf(search) != -1 || value.indexOf(search) != -1)
     224                        return true;
     225                }
    116226            }
    117227            if (osm.user != null) {
     
    189299    }
    190300
    191     public static Match compile(String searchStr, boolean caseSensitive)
     301    public static Match compile(String searchStr, boolean caseSensitive, boolean regexSearch)
    192302            throws ParseError {
    193         return new SearchCompiler(caseSensitive,
     303        return new SearchCompiler(caseSensitive, regexSearch,
    194304                new PushbackTokenizer(
    195305                    new PushbackReader(new StringReader(searchStr))))
  • trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java

    r1169 r1213  
    200200        instance.addMouseListener(new MouseAdapter(){
    201201            private void openPopup(MouseEvent e) {
    202                 Point p = listScrollPane.getMousePosition();
    203                 if (p == null)
    204                     return; // user is faster than swing with mouse movement
    205                 int index = instance.locationToIndex(e.getPoint());
     202                Point p = e.getPoint();
     203                int index = instance.locationToIndex(p);
    206204                Layer layer = (Layer)instance.getModel().getElementAt(index);
    207205                LayerListPopup menu = new LayerListPopup(instance, layer);
Note: See TracChangeset for help on using the changeset viewer.