Changeset 2123 in josm
- Timestamp:
- 2009-09-13T22:42:43+02:00 (15 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java
r2070 r2123 25 25 import org.openstreetmap.josm.tools.GBC; 26 26 import org.openstreetmap.josm.tools.Shortcut; 27 import org.openstreetmap.josm.data.osm.Filter; 27 28 28 29 public class SearchAction extends JosmAction{ … … 31 32 32 33 public static enum SearchMode { 33 replace, add, remove 34 replace, add, remove, in_selection 34 35 } 35 36 … … 57 58 SearchSetting s = lastSearch; 58 59 if (s == null) { 59 s = new SearchSetting("", false, false, SearchMode.replace); 60 } 61 showSearchDialog(s); 62 } 63 64 public void showSearchDialog(SearchSetting initialValues) { 65 JLabel label = new JLabel(tr("Please enter a search string.")); 60 s = new SearchSetting("", SearchMode.replace, false, false); 61 } 62 SearchSetting se = showSearchDialog(s); 63 if(se != null) searchWithHistory(se); 64 } 65 66 public static SearchSetting showSearchDialog(SearchSetting initialValues) { 67 JLabel label = new JLabel( initialValues instanceof Filter ? tr("Please enter a filter string.") : tr("Please enter a search string.")); 66 68 final JTextField input = new JTextField(initialValues.text); 67 69 input.selectAll(); … … 70 72 JRadioButton add = new JRadioButton(tr("add to selection"), initialValues.mode == SearchMode.add); 71 73 JRadioButton remove = new JRadioButton(tr("remove from selection"), initialValues.mode == SearchMode.remove); 74 JRadioButton in_selection = new JRadioButton(tr("find in selection"), initialValues.mode == SearchMode.in_selection); 72 75 ButtonGroup bg = new ButtonGroup(); 73 76 bg.add(replace); 74 77 bg.add(add); 75 78 bg.add(remove); 79 bg.add(in_selection); 76 80 77 81 JCheckBox caseSensitive = new JCheckBox(tr("case sensitive"), initialValues.caseSensitive); … … 79 83 80 84 JPanel left = new JPanel(new GridBagLayout()); 85 86 JTextField finput = null; 87 if(initialValues instanceof Filter){ 88 JLabel fLabel = new JLabel(tr("Please enter a filter name.")); 89 finput = new JTextField(((Filter)initialValues).filterName); 90 left.add(fLabel, GBC.eop()); 91 left.add(finput, GBC.eop().fill(GBC.HORIZONTAL)); 92 } 93 81 94 left.add(label, GBC.eop()); 82 95 left.add(input, GBC.eop().fill(GBC.HORIZONTAL)); 83 96 left.add(replace, GBC.eol()); 84 97 left.add(add, GBC.eol()); 85 left.add(remove, GBC.eop()); 98 left.add(remove, GBC.eol()); 99 left.add(in_selection, GBC.eop()); 86 100 left.add(caseSensitive, GBC.eol()); 87 101 left.add(regexSearch, GBC.eol()); … … 120 134 ExtendedDialog dialog = new ExtendedDialog( 121 135 Main.parent, 122 tr("Search"), 123 new String[] {tr("Start Search"), tr("Cancel")} 136 initialValues instanceof Filter ? tr("Filter") : tr("Search"), 137 new String[] { 138 initialValues instanceof Filter ? tr("Make filter") : tr("Start Search"), 139 tr("Cancel")} 124 140 ); 125 141 dialog.setButtonIcons(new String[] {"dialogs/search.png", "cancel.png"}); … … 128 144 int result = dialog.getValue(); 129 145 130 if(result != 1) return ;146 if(result != 1) return null; 131 147 132 148 // User pressed OK - let's perform the search 133 149 SearchMode mode = replace.isSelected() ? SearchAction.SearchMode.replace 134 : (add.isSelected() ? SearchAction.SearchMode.add : SearchAction.SearchMode.remove); 135 SearchSetting setting = new SearchSetting(input.getText(), caseSensitive.isSelected(), regexSearch.isSelected(), mode); 136 searchWithHistory(setting); 150 : (add.isSelected() ? SearchAction.SearchMode.add 151 : (remove.isSelected() ? SearchAction.SearchMode.remove : SearchAction.SearchMode.in_selection)); 152 if(initialValues instanceof Filter){ 153 return new Filter(input.getText(), mode, caseSensitive.isSelected(), regexSearch.isSelected(), finput.getText()); 154 } else { 155 return new SearchSetting(input.getText(), mode, caseSensitive.isSelected(), regexSearch.isSelected()); 156 } 137 157 } 138 158 … … 151 171 } 152 172 lastSearch = s; 153 search(s .text, s.mode, s.caseSensitive, s.regexSearch);173 search(s); 154 174 } 155 175 156 176 public static void searchWithoutHistory(SearchSetting s) { 157 177 lastSearch = s; 158 search(s.text, s.mode, s.caseSensitive, s.regexSearch); 159 } 160 178 search(s); 179 } 180 181 public interface Function{ 182 public Boolean isSomething(OsmPrimitive o); 183 } 184 185 public static Integer getSelection(SearchSetting s, Collection<OsmPrimitive> sel, Function f) { 186 Integer foundMatches = 0; 187 try { 188 String searchText = s.text; 189 if(s instanceof Filter){ 190 searchText = ((Filter)s).inverted ? "-" : ""; 191 searchText = searchText + "(" + ((Filter)s).text + ")" + (((Filter)s).applyForChildren ? ("| child (" + ((Filter)s).text + ")"): ""); 192 } 193 /*System.out.println(searchText); */ 194 SearchCompiler.Match matcher = SearchCompiler.compile(searchText, s.caseSensitive, s.regexSearch); 195 foundMatches = 0; 196 for (OsmPrimitive osm : Main.main.getCurrentDataSet().allNonDeletedCompletePrimitives()) { 197 if (s.mode == SearchMode.replace) { 198 if (matcher.match(osm)) { 199 sel.add(osm); 200 ++foundMatches; 201 } else { 202 sel.remove(osm); 203 } 204 } else if (s.mode == SearchMode.add && !f.isSomething(osm) && matcher.match(osm)) { 205 sel.add(osm); 206 ++foundMatches; 207 } else if (s.mode == SearchMode.remove && f.isSomething(osm) && matcher.match(osm)) { 208 sel.remove(osm); 209 ++foundMatches; 210 } else if (s.mode == SearchMode.in_selection && f.isSomething(osm)&& !matcher.match(osm)) { 211 sel.remove(osm); 212 ++foundMatches; 213 } 214 } 215 } catch (SearchCompiler.ParseError e) { 216 JOptionPane.showMessageDialog( 217 Main.parent, 218 e.getMessage(), 219 tr("Error"), 220 JOptionPane.ERROR_MESSAGE 221 222 ); 223 } 224 return foundMatches; 225 } 226 161 227 public static void search(String search, SearchMode mode, boolean caseSensitive, boolean regexSearch) { 228 search(new SearchSetting(search, mode, caseSensitive, regexSearch)); 229 } 230 231 public static void search(SearchSetting s) { 162 232 // FIXME: This is confusing. The GUI says nothing about loading primitives from an URL. We'd like to *search* 163 233 // for URLs in the current data set. … … 172 242 // } 173 243 // } 174 try { 175 Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected(); 176 SearchCompiler.Match matcher = SearchCompiler.compile(search, caseSensitive, regexSearch); 177 int foundMatches = 0; 178 for (OsmPrimitive osm : Main.main.getCurrentDataSet().allNonDeletedCompletePrimitives()) { 179 if (mode == SearchMode.replace) { 180 if (matcher.match(osm)) { 181 sel.add(osm); 182 ++foundMatches; 183 } else { 184 sel.remove(osm); 185 } 186 } else if (mode == SearchMode.add && !osm.isSelected() && matcher.match(osm)) { 187 sel.add(osm); 188 ++foundMatches; 189 } else if (mode == SearchMode.remove && osm.isSelected() && matcher.match(osm)) { 190 sel.remove(osm); 191 ++foundMatches; 192 } 193 } 194 Main.main.getCurrentDataSet().setSelected(sel); 195 if (foundMatches == 0) { 196 String msg = null; 197 if (mode == SearchMode.replace) { 198 msg = tr("No match found for ''{0}''", search); 199 } else if (mode == SearchMode.add) { 200 msg = tr("Nothing added to selection by searching for ''{0}''", search); 201 } else if (mode == SearchMode.remove) { 202 msg = tr("Nothing removed from selection by searching for ''{0}''", search); 203 } 204 Main.map.statusLine.setHelpText(msg); 205 JOptionPane.showMessageDialog( 206 Main.parent, 207 msg, 208 tr("Warning"), 209 JOptionPane.WARNING_MESSAGE 210 ); 211 } else { 212 Main.map.statusLine.setHelpText(tr("Found {0} matches", foundMatches)); 213 } 214 } catch (SearchCompiler.ParseError e) { 215 JOptionPane.showMessageDialog( 216 Main.parent, 217 e.getMessage(), 218 tr("Error"), 219 JOptionPane.ERROR_MESSAGE 220 221 ); 222 } 244 245 Collection<OsmPrimitive> sel = Main.main.getCurrentDataSet().getSelected(); 246 int foundMatches = getSelection(s, sel, new Function(){ 247 public Boolean isSomething(OsmPrimitive o){ 248 return o.isSelected(); 249 } 250 }); 251 Main.main.getCurrentDataSet().setSelected(sel); 252 if (foundMatches == 0) { 253 String msg = null; 254 if (s.mode == SearchMode.replace) { 255 msg = tr("No match found for ''{0}''", s.text); 256 } else if (s.mode == SearchMode.add) { 257 msg = tr("Nothing added to selection by searching for ''{0}''", s.text); 258 } else if (s.mode == SearchMode.remove) { 259 msg = tr("Nothing removed from selection by searching for ''{0}''", s.text); 260 } else if (s.mode == SearchMode.in_selection) { 261 msg = tr("Nothing find in selection by searching for ''{0}''", s.text); 262 } 263 Main.map.statusLine.setHelpText(msg); 264 JOptionPane.showMessageDialog( 265 Main.parent, 266 msg, 267 tr("Warning"), 268 JOptionPane.WARNING_MESSAGE 269 ); 270 } else { 271 Main.map.statusLine.setHelpText(tr("Found {0} matches", foundMatches)); 272 } 223 273 } 224 274 225 275 public static class SearchSetting { 226 String text;227 SearchMode mode;228 boolean caseSensitive;229 boolean regexSearch;230 231 public SearchSetting(String text, boolean caseSensitive, boolean regexSearch, SearchMode mode) {276 public String text; 277 public SearchMode mode; 278 public boolean caseSensitive; 279 public boolean regexSearch; 280 281 public SearchSetting(String text, SearchMode mode, boolean caseSensitive, boolean regexSearch) { 232 282 super(); 233 283 this.caseSensitive = caseSensitive; -
trunk/src/org/openstreetmap/josm/gui/MapFrame.java
r2021 r2123 29 29 import org.openstreetmap.josm.gui.dialogs.RelationListDialog; 30 30 import org.openstreetmap.josm.gui.dialogs.SelectionListDialog; 31 import org.openstreetmap.josm.gui.dialogs.FilterDialog; 31 32 import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 32 33 import org.openstreetmap.josm.gui.dialogs.UserListDialog; … … 104 105 addToggleDialog(new HistoryDialog()); 105 106 addToggleDialog(new SelectionListDialog()); 107 if(Main.pref.getBoolean("displayfilter", false)) 108 addToggleDialog(new FilterDialog()); 106 109 addToggleDialog(new UserListDialog()); 107 110 addToggleDialog(conflictDialog = new ConflictDialog()); … … 251 254 * Replies the instance of a toggle dialog of type <code>type</code> managed by this 252 255 * map frame 253 * 256 * 254 257 * @param <T> 255 258 * @param type the class of the toggle dialog, i.e. UserListDialog.class 256 259 * @return the instance of a toggle dialog of type <code>type</code> managed by this 257 260 * map frame; null, if no such dialog exists 258 * 261 * 259 262 */ 260 263 public <T> T getToggleDialog(Class<T> type) {
Note:
See TracChangeset
for help on using the changeset viewer.