1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions.search;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.Font;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 |
|
---|
17 | import javax.swing.ButtonGroup;
|
---|
18 | import javax.swing.JCheckBox;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JOptionPane;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 | import javax.swing.JRadioButton;
|
---|
23 |
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
26 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
27 | import org.openstreetmap.josm.data.osm.Filter;
|
---|
28 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
29 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
|
---|
31 | import org.openstreetmap.josm.tools.GBC;
|
---|
32 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
33 |
|
---|
34 | public class SearchAction extends JosmAction{
|
---|
35 |
|
---|
36 | public static final int DEFAULT_SEARCH_HISTORY_SIZE = 10;
|
---|
37 |
|
---|
38 | public static enum SearchMode {
|
---|
39 | replace, add, remove, in_selection
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static final LinkedList<SearchSetting> searchHistory = new LinkedList<SearchSetting>();
|
---|
43 |
|
---|
44 | private static SearchSetting lastSearch = null;
|
---|
45 |
|
---|
46 | public SearchAction() {
|
---|
47 | super(tr("Search..."), "dialogs/search", tr("Search for objects."),
|
---|
48 | Shortcut.registerShortcut("system:find", tr("Search..."), KeyEvent.VK_F, Shortcut.GROUP_HOTKEY), true);
|
---|
49 | putValue("help", ht("/Action/Search"));
|
---|
50 | }
|
---|
51 |
|
---|
52 | public void actionPerformed(ActionEvent e) {
|
---|
53 | if (!isEnabled())
|
---|
54 | return;
|
---|
55 | SearchSetting s = lastSearch;
|
---|
56 | if (s == null) {
|
---|
57 | s = new SearchSetting("", SearchMode.replace, false /* case insensitive */, false /* no regexp */);
|
---|
58 | }
|
---|
59 | SearchSetting se = showSearchDialog(s);
|
---|
60 | if(se != null) {
|
---|
61 | searchWithHistory(se);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public static List<String> getSearchExpressionHistory() {
|
---|
66 | ArrayList<String> ret = new ArrayList<String>(searchHistory.size());
|
---|
67 | for (SearchSetting ss: searchHistory) {
|
---|
68 | ret.add(ss.text);
|
---|
69 | }
|
---|
70 | return ret;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public static SearchSetting showSearchDialog(SearchSetting initialValues) {
|
---|
74 |
|
---|
75 | // -- prepare the combo box with the search expressions
|
---|
76 | //
|
---|
77 | JLabel label = new JLabel( initialValues instanceof Filter ? tr("Please enter a filter string.") : tr("Please enter a search string."));
|
---|
78 | final HistoryComboBox hcbSearchString = new HistoryComboBox();
|
---|
79 | hcbSearchString.setText(initialValues.text);
|
---|
80 | hcbSearchString.getEditor().selectAll();
|
---|
81 | hcbSearchString.getEditor().getEditorComponent().requestFocusInWindow();
|
---|
82 | hcbSearchString.setToolTipText(tr("Enter the search expression"));
|
---|
83 | // we have to reverse the history, because ComboBoxHistory will reverse it again
|
---|
84 | // in addElement()
|
---|
85 | //
|
---|
86 | List<String> searchExpressionHistory = getSearchExpressionHistory();
|
---|
87 | Collections.reverse(searchExpressionHistory);
|
---|
88 | hcbSearchString.setPossibleItems(searchExpressionHistory);
|
---|
89 |
|
---|
90 | JRadioButton replace = new JRadioButton(tr("replace selection"), initialValues.mode == SearchMode.replace);
|
---|
91 | JRadioButton add = new JRadioButton(tr("add to selection"), initialValues.mode == SearchMode.add);
|
---|
92 | JRadioButton remove = new JRadioButton(tr("remove from selection"), initialValues.mode == SearchMode.remove);
|
---|
93 | JRadioButton in_selection = new JRadioButton(tr("find in selection"), initialValues.mode == SearchMode.in_selection);
|
---|
94 | ButtonGroup bg = new ButtonGroup();
|
---|
95 | bg.add(replace);
|
---|
96 | bg.add(add);
|
---|
97 | bg.add(remove);
|
---|
98 | bg.add(in_selection);
|
---|
99 |
|
---|
100 | JCheckBox caseSensitive = new JCheckBox(tr("case sensitive"), initialValues.caseSensitive);
|
---|
101 | JCheckBox regexSearch = new JCheckBox(tr("regular expression"), initialValues.regexSearch);
|
---|
102 |
|
---|
103 | JPanel left = new JPanel(new GridBagLayout());
|
---|
104 | left.add(label, GBC.eop());
|
---|
105 | left.add(hcbSearchString, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
106 | left.add(replace, GBC.eol());
|
---|
107 | left.add(add, GBC.eol());
|
---|
108 | left.add(remove, GBC.eol());
|
---|
109 | left.add(in_selection, GBC.eop());
|
---|
110 | left.add(caseSensitive, GBC.eol());
|
---|
111 | left.add(regexSearch, GBC.eol());
|
---|
112 |
|
---|
113 | JPanel right = new JPanel();
|
---|
114 | JLabel description =
|
---|
115 | new JLabel("<html><ul>"
|
---|
116 | + "<li>"+tr("<b>Baker Street</b> - 'Baker' and 'Street' in any key or name.")+"</li>"
|
---|
117 | + "<li>"+tr("<b>\"Baker Street\"</b> - 'Baker Street' in any key or name.")+"</li>"
|
---|
118 | + "<li>"+tr("<b>name:Bak</b> - 'Bak' anywhere in the name.")+"</li>"
|
---|
119 | + "<li>"+tr("<b>type=route</b> - key 'type' with value exactly 'route'.") + "</li>"
|
---|
120 | + "<li>"+tr("<b>type=*</b> - key 'type' with any value. Try also <b>*=value</b>, <b>type=</b>, <b>*=*</b>, <b>*=</b>") + "</li>"
|
---|
121 | + "<li>"+tr("<b>-name:Bak</b> - not 'Bak' in the name.")+"</li>"
|
---|
122 | + "<li>"+tr("<b>foot:</b> - key=foot set to any value.")+"</li>"
|
---|
123 | + "<li>"+tr("<u>Special targets:</u>")+"</li>"
|
---|
124 | + "<li>"+tr("<b>type:</b> - type of the object (<b>node</b>, <b>way</b>, <b>relation</b>)")+"</li>"
|
---|
125 | + "<li>"+tr("<b>user:</b>... - all objects changed by user")+"</li>"
|
---|
126 | + "<li>"+tr("<b>user:anonymous</b> - all objects changed by anonymous users")+"</li>"
|
---|
127 | + "<li>"+tr("<b>id:</b>... - object with given ID (0 for new objects)")+"</li>"
|
---|
128 | + "<li>"+tr("<b>version:</b>... - object with given version (0 objects without an assigned version)")+"</li>"
|
---|
129 | + "<li>"+tr("<b>changeset:</b>... - object with given changeset id (0 objects without assigned changeset)")+"</li>"
|
---|
130 | + "<li>"+tr("<b>nodes:</b>... - object with given number of nodes (nodes:count or nodes:min-max)")+"</li>"
|
---|
131 | + "<li>"+tr("<b>tags:</b>... - object with given number of tags (tags:count or tags:min-max)")+"</li>"
|
---|
132 | + "<li>"+tr("<b>timestamp:</b>... - objects with this timestamp (<b>2009-11-12T14:51:09Z</b>, <b>2009-11-12</b> or <b>T14:51</b> ...)")+"</li>"
|
---|
133 | + "<li>"+tr("<b>modified</b> - all changed objects")+"</li>"
|
---|
134 | + "<li>"+tr("<b>selected</b> - all selected objects")+"</li>"
|
---|
135 | + "<li>"+tr("<b>incomplete</b> - all incomplete objects")+"</li>"
|
---|
136 | + "<li>"+tr("<b>untagged</b> - all untagged objects")+"</li>"
|
---|
137 | + "<li>"+tr("<b>child <i>expr</i></b> - all children of objects matching the expression")+"</li>"
|
---|
138 | + "<li>"+tr("<b>parent <i>expr</i></b> - all parents of objects matching the expression")+"</li>"
|
---|
139 | + "<li>"+tr("Use <b>|</b> or <b>OR</b> to combine with logical or")+"</li>"
|
---|
140 | + "<li>"+tr("Use <b>\"</b> to quote operators (e.g. if key contains :)")+"</li>"
|
---|
141 | + "<li>"+tr("Use <b>(</b> and <b>)</b> to group expressions")+"</li>"
|
---|
142 | + "</ul></html>");
|
---|
143 | description.setFont(description.getFont().deriveFont(Font.PLAIN));
|
---|
144 | right.add(description);
|
---|
145 |
|
---|
146 | final JPanel p = new JPanel();
|
---|
147 | p.add(left);
|
---|
148 | p.add(right);
|
---|
149 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
150 | Main.parent,
|
---|
151 | initialValues instanceof Filter ? tr("Filter") : tr("Search"),
|
---|
152 | new String[] {
|
---|
153 | initialValues instanceof Filter ? tr("Submit filter") : tr("Start Search"),
|
---|
154 | tr("Cancel")}
|
---|
155 | );
|
---|
156 | dialog.setButtonIcons(new String[] {"dialogs/search.png", "cancel.png"});
|
---|
157 | dialog.configureContextsensitiveHelp("/Action/Search", true /* show help button */);
|
---|
158 | dialog.setContent(p);
|
---|
159 | dialog.showDialog();
|
---|
160 | int result = dialog.getValue();
|
---|
161 |
|
---|
162 | if(result != 1) return null;
|
---|
163 |
|
---|
164 | // User pressed OK - let's perform the search
|
---|
165 | SearchMode mode = replace.isSelected() ? SearchAction.SearchMode.replace
|
---|
166 | : (add.isSelected() ? SearchAction.SearchMode.add
|
---|
167 | : (remove.isSelected() ? SearchAction.SearchMode.remove : SearchAction.SearchMode.in_selection));
|
---|
168 | initialValues.text = hcbSearchString.getText();
|
---|
169 | initialValues.mode = mode;
|
---|
170 | initialValues.caseSensitive = caseSensitive.isSelected();
|
---|
171 | initialValues.regexSearch = regexSearch.isSelected();
|
---|
172 | return initialValues;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Adds the search specified by the settings in <code>s</code> to the
|
---|
177 | * search history and performs the search.
|
---|
178 | *
|
---|
179 | * @param s
|
---|
180 | */
|
---|
181 | public static void searchWithHistory(SearchSetting s) {
|
---|
182 | if(searchHistory.isEmpty() || !s.equals(searchHistory.getFirst())) {
|
---|
183 | searchHistory.addFirst(new SearchSetting(s));
|
---|
184 | }
|
---|
185 | int maxsize = Main.pref.getInteger("search.history-size", DEFAULT_SEARCH_HISTORY_SIZE);
|
---|
186 | while (searchHistory.size() > maxsize) {
|
---|
187 | searchHistory.removeLast();
|
---|
188 | }
|
---|
189 | lastSearch = new SearchSetting(s);
|
---|
190 | search(s);
|
---|
191 | }
|
---|
192 |
|
---|
193 | public static void searchWithoutHistory(SearchSetting s) {
|
---|
194 | lastSearch = new SearchSetting(s);
|
---|
195 | search(s);
|
---|
196 | }
|
---|
197 |
|
---|
198 | public interface Function{
|
---|
199 | public Boolean isSomething(OsmPrimitive o);
|
---|
200 | }
|
---|
201 |
|
---|
202 | public static Integer getSelection(SearchSetting s, Collection<OsmPrimitive> sel, Function f) {
|
---|
203 | Integer foundMatches = 0;
|
---|
204 | try {
|
---|
205 | String searchText = s.text;
|
---|
206 | if(s instanceof Filter){
|
---|
207 | searchText = "(" + s.text + ")" + (((Filter)s).applyForChildren ? ("| child (" + s.text + ")"): "");
|
---|
208 | searchText = (((Filter)s).inverted ? "-" : "") + "(" + searchText + ")";
|
---|
209 | }
|
---|
210 | /*System.out.println(searchText);*/
|
---|
211 | SearchCompiler.Match matcher = SearchCompiler.compile(searchText, s.caseSensitive, s.regexSearch);
|
---|
212 | foundMatches = 0;
|
---|
213 | for (OsmPrimitive osm : Main.main.getCurrentDataSet().allNonDeletedCompletePrimitives()) {
|
---|
214 | if (s.mode == SearchMode.replace) {
|
---|
215 | if (matcher.match(osm)) {
|
---|
216 | sel.add(osm);
|
---|
217 | ++foundMatches;
|
---|
218 | } else {
|
---|
219 | sel.remove(osm);
|
---|
220 | }
|
---|
221 | } else if (s.mode == SearchMode.add && !f.isSomething(osm) && matcher.match(osm)) {
|
---|
222 | sel.add(osm);
|
---|
223 | ++foundMatches;
|
---|
224 | } else if (s.mode == SearchMode.remove && f.isSomething(osm) && matcher.match(osm)) {
|
---|
225 | sel.remove(osm);
|
---|
226 | ++foundMatches;
|
---|
227 | } else if (s.mode == SearchMode.in_selection && f.isSomething(osm)&& !matcher.match(osm)) {
|
---|
228 | sel.remove(osm);
|
---|
229 | ++foundMatches;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | } catch (SearchCompiler.ParseError e) {
|
---|
233 | JOptionPane.showMessageDialog(
|
---|
234 | Main.parent,
|
---|
235 | e.getMessage(),
|
---|
236 | tr("Error"),
|
---|
237 | JOptionPane.ERROR_MESSAGE
|
---|
238 |
|
---|
239 | );
|
---|
240 | }
|
---|
241 | return foundMatches;
|
---|
242 | }
|
---|
243 |
|
---|
244 | public static void search(String search, SearchMode mode, boolean caseSensitive, boolean regexSearch) {
|
---|
245 | search(new SearchSetting(search, mode, caseSensitive, regexSearch));
|
---|
246 | }
|
---|
247 |
|
---|
248 | public static void search(SearchSetting s) {
|
---|
249 | // FIXME: This is confusing. The GUI says nothing about loading primitives from an URL. We'd like to *search*
|
---|
250 | // for URLs in the current data set.
|
---|
251 | // Disabling until a better solution is in place
|
---|
252 | //
|
---|
253 | // if (search.startsWith("http://") || search.startsWith("ftp://") || search.startsWith("https://")
|
---|
254 | // || search.startsWith("file:/")) {
|
---|
255 | // SelectionWebsiteLoader loader = new SelectionWebsiteLoader(search, mode);
|
---|
256 | // if (loader.url != null && loader.url.getHost() != null) {
|
---|
257 | // Main.worker.execute(loader);
|
---|
258 | // return;
|
---|
259 | // }
|
---|
260 | // }
|
---|
261 |
|
---|
262 | final DataSet ds = Main.main.getCurrentDataSet();
|
---|
263 | Collection<OsmPrimitive> sel = ds.getSelected();
|
---|
264 | int foundMatches = getSelection(s, sel, new Function(){
|
---|
265 | public Boolean isSomething(OsmPrimitive o){
|
---|
266 | return ds.isSelected(o);
|
---|
267 | }
|
---|
268 | });
|
---|
269 | ds.setSelected(sel);
|
---|
270 | if (foundMatches == 0) {
|
---|
271 | String msg = null;
|
---|
272 | if (s.mode == SearchMode.replace) {
|
---|
273 | msg = tr("No match found for ''{0}''", s.text);
|
---|
274 | } else if (s.mode == SearchMode.add) {
|
---|
275 | msg = tr("Nothing added to selection by searching for ''{0}''", s.text);
|
---|
276 | } else if (s.mode == SearchMode.remove) {
|
---|
277 | msg = tr("Nothing removed from selection by searching for ''{0}''", s.text);
|
---|
278 | } else if (s.mode == SearchMode.in_selection) {
|
---|
279 | msg = tr("Nothing found in selection by searching for ''{0}''", s.text);
|
---|
280 | }
|
---|
281 | Main.map.statusLine.setHelpText(msg);
|
---|
282 | JOptionPane.showMessageDialog(
|
---|
283 | Main.parent,
|
---|
284 | msg,
|
---|
285 | tr("Warning"),
|
---|
286 | JOptionPane.WARNING_MESSAGE
|
---|
287 | );
|
---|
288 | } else {
|
---|
289 | Main.map.statusLine.setHelpText(tr("Found {0} matches", foundMatches));
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | public static class SearchSetting {
|
---|
294 | public String text;
|
---|
295 | public SearchMode mode;
|
---|
296 | public boolean caseSensitive;
|
---|
297 | public boolean regexSearch;
|
---|
298 |
|
---|
299 | public SearchSetting(String text, SearchMode mode, boolean caseSensitive, boolean regexSearch) {
|
---|
300 | super();
|
---|
301 | this.caseSensitive = caseSensitive;
|
---|
302 | this.regexSearch = regexSearch;
|
---|
303 | this.mode = mode;
|
---|
304 | this.text = text;
|
---|
305 | }
|
---|
306 |
|
---|
307 | public SearchSetting(SearchSetting original) {
|
---|
308 | super();
|
---|
309 | this.caseSensitive = original.caseSensitive;
|
---|
310 | this.regexSearch = original.regexSearch;
|
---|
311 | this.mode = original.mode;
|
---|
312 | this.text = original.text;
|
---|
313 | }
|
---|
314 |
|
---|
315 | @Override
|
---|
316 | public String toString() {
|
---|
317 | String cs = caseSensitive ? tr("CS") : tr("CI");
|
---|
318 | String rx = regexSearch ? (", " + tr("RX")) : "";
|
---|
319 | return "\"" + text + "\" (" + cs + rx + ", " + mode + ")";
|
---|
320 | }
|
---|
321 |
|
---|
322 | @Override
|
---|
323 | public boolean equals(Object other) {
|
---|
324 | if(!(other instanceof SearchSetting))
|
---|
325 | return false;
|
---|
326 | SearchSetting o = (SearchSetting) other;
|
---|
327 | return (o.caseSensitive == this.caseSensitive
|
---|
328 | && o.regexSearch == this.regexSearch
|
---|
329 | && o.mode.equals(this.mode)
|
---|
330 | && o.text.equals(this.text));
|
---|
331 | }
|
---|
332 |
|
---|
333 | @Override
|
---|
334 | public int hashCode() {
|
---|
335 | return text.hashCode();
|
---|
336 | }
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Refreshes the enabled state
|
---|
341 | *
|
---|
342 | */
|
---|
343 | @Override
|
---|
344 | protected void updateEnabledState() {
|
---|
345 | setEnabled(getEditLayer() != null);
|
---|
346 | }
|
---|
347 | }
|
---|