Changeset 1213 in josm
- Timestamp:
- 2009-01-08T15:29:55+01:00 (16 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/Main.java
r1205 r1213 450 450 if (args.containsKey("selection")) 451 451 for (String s : args.get("selection")) 452 SearchAction.search(s, SearchAction.SearchMode.add, false );452 SearchAction.search(s, SearchAction.SearchMode.add, false, false); 453 453 } 454 454 -
trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java
r1212 r1213 48 48 SearchSetting s = lastSearch; 49 49 if (s == null) 50 s = new SearchSetting("", false, SearchMode.replace);50 s = new SearchSetting("", false, false, SearchMode.replace); 51 51 showSearchDialog(s); 52 52 } … … 81 81 82 82 JCheckBox caseSensitive = new JCheckBox(tr("case sensitive"), initialValues.caseSensitive); 83 JCheckBox regexSearch = new JCheckBox(tr("regular expression"), initialValues.regexSearch); 83 84 84 85 JPanel p = new JPanel(new GridBagLayout()); … … 89 90 p.add(remove, GBC.eop()); 90 91 p.add(caseSensitive, GBC.eol()); 92 p.add(regexSearch, GBC.eol()); 91 93 JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null) { 92 94 @Override … … 102 104 SearchMode mode = replace.isSelected() ? SearchAction.SearchMode.replace 103 105 : (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); 105 107 searchWithHistory(setting); 106 108 } … … 117 119 searchHistory.removeLast(); 118 120 lastSearch = s; 119 search(s.text, s.mode, s.caseSensitive );121 search(s.text, s.mode, s.caseSensitive, s.regexSearch); 120 122 } 121 123 122 124 public static void searchWithoutHistory(SearchSetting s) { 123 125 lastSearch = s; 124 search(s.text, s.mode, s.caseSensitive );126 search(s.text, s.mode, s.caseSensitive, s.regexSearch); 125 127 } 126 128 127 public static void search(String search, SearchMode mode, boolean caseSensitive ) {129 public static void search(String search, SearchMode mode, boolean caseSensitive, boolean regexSearch) { 128 130 if (search.startsWith("http://") || search.startsWith("ftp://") || search.startsWith("https://") 129 131 || search.startsWith("file:/")) { … … 136 138 try { 137 139 Collection<OsmPrimitive> sel = Main.ds.getSelected(); 138 SearchCompiler.Match matcher = SearchCompiler.compile(search, caseSensitive );140 SearchCompiler.Match matcher = SearchCompiler.compile(search, caseSensitive, regexSearch); 139 141 int foundMatches = 0; 140 142 for (OsmPrimitive osm : Main.ds.allNonDeletedCompletePrimitives()) { … … 175 177 SearchMode mode; 176 178 boolean caseSensitive; 179 boolean regexSearch; 177 180 178 public SearchSetting(String text, boolean caseSensitive, SearchMode mode) {181 public SearchSetting(String text, boolean caseSensitive, boolean regexSearch, SearchMode mode) { 179 182 super(); 180 183 this.caseSensitive = caseSensitive; 184 this.regexSearch = regexSearch; 181 185 this.mode = mode; 182 186 this.text = text; … … 186 190 public String toString() { 187 191 String cs = caseSensitive ? tr("CI") : tr("CS"); 188 return "\"" + text + "\" (" + cs + ", " + mode + ")"; 192 String rx = regexSearch ? (", " + tr("RX")) : ""; 193 return "\"" + text + "\" (" + cs + rx + ", " + mode + ")"; 189 194 } 190 195 -
trunk/src/org/openstreetmap/josm/actions/search/SearchCompiler.java
r1169 r1213 2 2 package org.openstreetmap.josm.actions.search; 3 3 4 import static org.openstreetmap.josm.tools.I18n.marktr; 4 5 import static org.openstreetmap.josm.tools.I18n.tr; 5 6 … … 7 8 import java.io.StringReader; 8 9 import java.util.Map.Entry; 10 import java.util.regex.Matcher; 11 import java.util.regex.Pattern; 12 import java.util.regex.PatternSyntaxException; 9 13 10 14 import org.openstreetmap.josm.data.osm.Node; … … 21 25 22 26 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}"); 23 29 private PushbackTokenizer tokenizer; 24 30 25 public SearchCompiler(boolean caseSensitive, PushbackTokenizer tokenizer) {31 public SearchCompiler(boolean caseSensitive, boolean regexSearch, PushbackTokenizer tokenizer) { 26 32 this.caseSensitive = caseSensitive; 33 this.regexSearch = regexSearch; 27 34 this.tokenizer = tokenizer; 28 35 } 29 36 30 37 abstract public static class Match { 31 abstract public boolean match(OsmPrimitive osm) ;38 abstract public boolean match(OsmPrimitive osm) throws ParseError; 32 39 } 33 40 … … 41 48 private final Match match; 42 49 public Not(Match match) {this.match = match;} 43 @Override public boolean match(OsmPrimitive osm) {50 @Override public boolean match(OsmPrimitive osm) throws ParseError { 44 51 return !match.match(osm); 45 52 } … … 51 58 private Match rhs; 52 59 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 { 54 61 return lhs.match(osm) && rhs.match(osm); 55 62 } … … 61 68 private Match rhs; 62 69 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 { 64 71 return lhs.match(osm) || rhs.match(osm); 65 72 } … … 80 87 private String value; 81 88 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; 96 165 } 97 166 @Override public String toString() {return key+"="+value;} … … 101 170 private String s; 102 171 public Any(String s) {this.s = s;} 103 @Override public boolean match(OsmPrimitive osm) {172 @Override public boolean match(OsmPrimitive osm) throws ParseError { 104 173 if (osm.keys == null) 105 174 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 107 198 // is not Java 1.5 108 199 //search = java.text.Normalizer.normalize(search, java.text.Normalizer.Form.NFC); 109 200 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 } 116 226 } 117 227 if (osm.user != null) { … … 189 299 } 190 300 191 public static Match compile(String searchStr, boolean caseSensitive )301 public static Match compile(String searchStr, boolean caseSensitive, boolean regexSearch) 192 302 throws ParseError { 193 return new SearchCompiler(caseSensitive, 303 return new SearchCompiler(caseSensitive, regexSearch, 194 304 new PushbackTokenizer( 195 305 new PushbackReader(new StringReader(searchStr)))) -
trunk/src/org/openstreetmap/josm/gui/dialogs/LayerListDialog.java
r1169 r1213 200 200 instance.addMouseListener(new MouseAdapter(){ 201 201 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); 206 204 Layer layer = (Layer)instance.getModel().getElementAt(index); 207 205 LayerListPopup menu = new LayerListPopup(instance, layer);
Note:
See TracChangeset
for help on using the changeset viewer.