1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import java.awt.BorderLayout;
|
---|
5 | import java.awt.Component;
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.event.ActionListener;
|
---|
8 | import java.awt.event.ItemEvent;
|
---|
9 | import java.awt.event.ItemListener;
|
---|
10 | import java.awt.event.KeyAdapter;
|
---|
11 | import java.awt.event.KeyEvent;
|
---|
12 | import java.awt.event.MouseAdapter;
|
---|
13 | import java.awt.event.MouseEvent;
|
---|
14 | import java.util.ArrayList;
|
---|
15 | import java.util.Collection;
|
---|
16 | import java.util.Collections;
|
---|
17 | import java.util.EnumSet;
|
---|
18 | import java.util.HashSet;
|
---|
19 | import java.util.List;
|
---|
20 | import javax.swing.AbstractListModel;
|
---|
21 | import javax.swing.Action;
|
---|
22 | import javax.swing.BoxLayout;
|
---|
23 | import javax.swing.DefaultListCellRenderer;
|
---|
24 | import javax.swing.Icon;
|
---|
25 | import javax.swing.JCheckBox;
|
---|
26 | import javax.swing.JLabel;
|
---|
27 | import javax.swing.JList;
|
---|
28 | import javax.swing.JPanel;
|
---|
29 | import javax.swing.JScrollPane;
|
---|
30 | import javax.swing.event.DocumentEvent;
|
---|
31 | import javax.swing.event.DocumentListener;
|
---|
32 | import javax.swing.event.ListSelectionEvent;
|
---|
33 | import javax.swing.event.ListSelectionListener;
|
---|
34 | import org.openstreetmap.josm.Main;
|
---|
35 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
36 | import org.openstreetmap.josm.data.osm.Node;
|
---|
37 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
38 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
39 | import org.openstreetmap.josm.data.osm.Way;
|
---|
40 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
41 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference;
|
---|
42 | import org.openstreetmap.josm.gui.tagging.TaggingPresetItems.Key;
|
---|
43 | import org.openstreetmap.josm.gui.tagging.TaggingPresetItems.KeyedItem;
|
---|
44 | import org.openstreetmap.josm.gui.tagging.TaggingPresetItems.Role;
|
---|
45 | import org.openstreetmap.josm.gui.tagging.TaggingPresetItems.Roles;
|
---|
46 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
47 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * GUI component to select tagging preset: the list with filter and two checkboxes
|
---|
51 | * @since 6068
|
---|
52 | */
|
---|
53 | public class TaggingPresetSelector extends JPanel implements SelectionChangedListener {
|
---|
54 |
|
---|
55 | private static final int CLASSIFICATION_IN_FAVORITES = 300;
|
---|
56 | private static final int CLASSIFICATION_NAME_MATCH = 300;
|
---|
57 | private static final int CLASSIFICATION_GROUP_MATCH = 200;
|
---|
58 | private static final int CLASSIFICATION_TAGS_MATCH = 100;
|
---|
59 |
|
---|
60 | private static final BooleanProperty SEARCH_IN_TAGS = new BooleanProperty("taggingpreset.dialog.search-in-tags", true);
|
---|
61 | private static final BooleanProperty ONLY_APPLICABLE = new BooleanProperty("taggingpreset.dialog.only-applicable-to-selection", true);
|
---|
62 |
|
---|
63 |
|
---|
64 | private JosmTextField edSearchText;
|
---|
65 | private JList lsResult;
|
---|
66 | private JCheckBox ckOnlyApplicable;
|
---|
67 | private JCheckBox ckSearchInTags;
|
---|
68 | private final EnumSet<TaggingPresetType> typesInSelection = EnumSet.noneOf(TaggingPresetType.class);
|
---|
69 | private boolean typesInSelectionDirty = true;
|
---|
70 | private final List<PresetClassification> classifications = new ArrayList<PresetClassification>();
|
---|
71 | private ResultListModel lsResultModel = new ResultListModel();
|
---|
72 |
|
---|
73 | private ActionListener dblClickListener;
|
---|
74 | private ActionListener clickListener;
|
---|
75 |
|
---|
76 | private static class ResultListCellRenderer extends DefaultListCellRenderer {
|
---|
77 | @Override
|
---|
78 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
|
---|
79 | boolean cellHasFocus) {
|
---|
80 | JLabel result = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
---|
81 | TaggingPreset tp = (TaggingPreset)value;
|
---|
82 | result.setText(tp.getName());
|
---|
83 | result.setIcon((Icon) tp.getValue(Action.SMALL_ICON));
|
---|
84 | return result;
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | private static class ResultListModel extends AbstractListModel {
|
---|
89 |
|
---|
90 | private List<PresetClassification> presets = new ArrayList<PresetClassification>();
|
---|
91 |
|
---|
92 | public void setPresets(List<PresetClassification> presets) {
|
---|
93 | this.presets = presets;
|
---|
94 | fireContentsChanged(this, 0, Integer.MAX_VALUE);
|
---|
95 | }
|
---|
96 |
|
---|
97 | public List<PresetClassification> getPresets() {
|
---|
98 | return presets;
|
---|
99 | }
|
---|
100 |
|
---|
101 | @Override
|
---|
102 | public Object getElementAt(int index) {
|
---|
103 | return presets.get(index).preset;
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public int getSize() {
|
---|
108 | return presets.size();
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 | private static class PresetClassification implements Comparable<PresetClassification> {
|
---|
114 | public final TaggingPreset preset;
|
---|
115 | public int classification;
|
---|
116 | public int favoriteIndex;
|
---|
117 | private final Collection<String> groups = new HashSet<String>();
|
---|
118 | private final Collection<String> names = new HashSet<String>();
|
---|
119 | private final Collection<String> tags = new HashSet<String>();
|
---|
120 |
|
---|
121 | PresetClassification(TaggingPreset preset) {
|
---|
122 | this.preset = preset;
|
---|
123 | TaggingPreset group = preset.group;
|
---|
124 | while (group != null) {
|
---|
125 | for (String word: group.getLocaleName().toLowerCase().split("\\s")) {
|
---|
126 | groups.add(word);
|
---|
127 | }
|
---|
128 | group = group.group;
|
---|
129 | }
|
---|
130 | for (String word: preset.getLocaleName().toLowerCase().split("\\s")) {
|
---|
131 | names.add(word);
|
---|
132 | }
|
---|
133 | for (TaggingPresetItem item: preset.data) {
|
---|
134 | if (item instanceof KeyedItem) {
|
---|
135 | tags.add(((KeyedItem) item).key);
|
---|
136 | // Should combo values also be added?
|
---|
137 | if (item instanceof Key && ((Key) item).value != null) {
|
---|
138 | tags.add(((Key) item).value);
|
---|
139 | }
|
---|
140 | } else if (item instanceof Roles) {
|
---|
141 | for (Role role : ((Roles) item).roles) {
|
---|
142 | tags.add(role.key);
|
---|
143 | }
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | private int isMatching(Collection<String> values, String[] searchString) {
|
---|
149 | int sum = 0;
|
---|
150 | for (String word: searchString) {
|
---|
151 | boolean found = false;
|
---|
152 | boolean foundFirst = false;
|
---|
153 | for (String value: values) {
|
---|
154 | int index = value.indexOf(word);
|
---|
155 | if (index == 0) {
|
---|
156 | foundFirst = true;
|
---|
157 | break;
|
---|
158 | } else if (index > 0) {
|
---|
159 | found = true;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | if (foundFirst) {
|
---|
163 | sum += 2;
|
---|
164 | } else if (found) {
|
---|
165 | sum += 1;
|
---|
166 | } else
|
---|
167 | return 0;
|
---|
168 | }
|
---|
169 | return sum;
|
---|
170 | }
|
---|
171 |
|
---|
172 | int isMatchingGroup(String[] words) {
|
---|
173 | return isMatching(groups, words);
|
---|
174 | }
|
---|
175 |
|
---|
176 | int isMatchingName(String[] words) {
|
---|
177 | return isMatching(names, words);
|
---|
178 | }
|
---|
179 |
|
---|
180 | int isMatchingTags(String[] words) {
|
---|
181 | return isMatching(tags, words);
|
---|
182 | }
|
---|
183 |
|
---|
184 | @Override
|
---|
185 | public int compareTo(PresetClassification o) {
|
---|
186 | int result = o.classification - classification;
|
---|
187 | if (result == 0)
|
---|
188 | return preset.getName().compareTo(o.preset.getName());
|
---|
189 | else
|
---|
190 | return result;
|
---|
191 | }
|
---|
192 |
|
---|
193 | @Override
|
---|
194 | public String toString() {
|
---|
195 | return classification + " " + preset.toString();
|
---|
196 | }
|
---|
197 | }
|
---|
198 |
|
---|
199 | public TaggingPresetSelector() {
|
---|
200 | super(new BorderLayout());
|
---|
201 | if (TaggingPresetPreference.taggingPresets!=null) {
|
---|
202 | loadPresets(TaggingPresetPreference.taggingPresets);
|
---|
203 | }
|
---|
204 |
|
---|
205 | edSearchText = new JosmTextField();
|
---|
206 | edSearchText.getDocument().addDocumentListener(new DocumentListener() {
|
---|
207 | @Override public void removeUpdate(DocumentEvent e) { filterPresets(); }
|
---|
208 | @Override public void insertUpdate(DocumentEvent e) { filterPresets(); }
|
---|
209 | @Override public void changedUpdate(DocumentEvent e) { filterPresets(); }
|
---|
210 | });
|
---|
211 | edSearchText.addKeyListener(new KeyAdapter() {
|
---|
212 | @Override
|
---|
213 | public void keyPressed(KeyEvent e) {
|
---|
214 | switch (e.getKeyCode()) {
|
---|
215 | case KeyEvent.VK_DOWN:
|
---|
216 | selectPreset(lsResult.getSelectedIndex() + 1);
|
---|
217 | break;
|
---|
218 | case KeyEvent.VK_UP:
|
---|
219 | selectPreset(lsResult.getSelectedIndex() - 1);
|
---|
220 | break;
|
---|
221 | case KeyEvent.VK_PAGE_DOWN:
|
---|
222 | selectPreset(lsResult.getSelectedIndex() + 10);
|
---|
223 | break;
|
---|
224 | case KeyEvent.VK_PAGE_UP:
|
---|
225 | selectPreset(lsResult.getSelectedIndex() - 10);
|
---|
226 | break;
|
---|
227 | case KeyEvent.VK_HOME:
|
---|
228 | selectPreset(0);
|
---|
229 | break;
|
---|
230 | case KeyEvent.VK_END:
|
---|
231 | selectPreset(lsResultModel.getSize());
|
---|
232 | break;
|
---|
233 | }
|
---|
234 | }
|
---|
235 | });
|
---|
236 | add(edSearchText, BorderLayout.NORTH);
|
---|
237 |
|
---|
238 | lsResult = new JList();
|
---|
239 | lsResult.setModel(lsResultModel);
|
---|
240 | lsResult.setCellRenderer(new ResultListCellRenderer());
|
---|
241 | lsResult.addMouseListener(new MouseAdapter() {
|
---|
242 | @Override
|
---|
243 | public void mouseClicked(MouseEvent e) {
|
---|
244 | if (e.getClickCount()>1) {
|
---|
245 | if (dblClickListener!=null)
|
---|
246 | dblClickListener.actionPerformed(null);
|
---|
247 | } else {
|
---|
248 | if (clickListener!=null)
|
---|
249 | clickListener.actionPerformed(null);
|
---|
250 | }
|
---|
251 | }
|
---|
252 | });
|
---|
253 | add(new JScrollPane(lsResult), BorderLayout.CENTER);
|
---|
254 |
|
---|
255 | JPanel pnChecks = new JPanel();
|
---|
256 | pnChecks.setLayout(new BoxLayout(pnChecks, BoxLayout.Y_AXIS));
|
---|
257 |
|
---|
258 | ckOnlyApplicable = new JCheckBox();
|
---|
259 | ckOnlyApplicable.setText(tr("Show only applicable to selection"));
|
---|
260 | pnChecks.add(ckOnlyApplicable);
|
---|
261 | ckOnlyApplicable.addItemListener(new ItemListener() {
|
---|
262 | @Override
|
---|
263 | public void itemStateChanged(ItemEvent e) {
|
---|
264 | filterPresets();
|
---|
265 | }
|
---|
266 | });
|
---|
267 |
|
---|
268 | ckSearchInTags = new JCheckBox();
|
---|
269 | ckSearchInTags.setText(tr("Search in tags"));
|
---|
270 | ckSearchInTags.setSelected(SEARCH_IN_TAGS.get());
|
---|
271 | ckSearchInTags.addItemListener(new ItemListener() {
|
---|
272 | @Override
|
---|
273 | public void itemStateChanged(ItemEvent e) {
|
---|
274 | filterPresets();
|
---|
275 | }
|
---|
276 | });
|
---|
277 | pnChecks.add(ckSearchInTags);
|
---|
278 |
|
---|
279 | add(pnChecks, BorderLayout.SOUTH);
|
---|
280 |
|
---|
281 | setPreferredSize(new Dimension(400, 300));
|
---|
282 |
|
---|
283 | filterPresets();
|
---|
284 | }
|
---|
285 |
|
---|
286 | private void selectPreset(int newIndex) {
|
---|
287 | if (newIndex < 0) {
|
---|
288 | newIndex = 0;
|
---|
289 | }
|
---|
290 | if (newIndex > lsResultModel.getSize() - 1) {
|
---|
291 | newIndex = lsResultModel.getSize() - 1;
|
---|
292 | }
|
---|
293 | lsResult.setSelectedIndex(newIndex);
|
---|
294 | lsResult.ensureIndexIsVisible(newIndex);
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Search expression can be in form: "group1/group2/name" where names can contain multiple words
|
---|
299 | */
|
---|
300 | private void filterPresets() {
|
---|
301 | //TODO Save favorites to file
|
---|
302 | String text = edSearchText.getText().toLowerCase();
|
---|
303 |
|
---|
304 | String[] groupWords;
|
---|
305 | String[] nameWords;
|
---|
306 |
|
---|
307 | if (text.contains("/")) {
|
---|
308 | groupWords = text.substring(0, text.lastIndexOf('/')).split("[\\s/]");
|
---|
309 | nameWords = text.substring(text.indexOf('/') + 1).split("\\s");
|
---|
310 | } else {
|
---|
311 | groupWords = null;
|
---|
312 | nameWords = text.split("\\s");
|
---|
313 | }
|
---|
314 |
|
---|
315 | boolean onlyApplicable = ckOnlyApplicable.isSelected();
|
---|
316 | boolean inTags = ckSearchInTags.isSelected();
|
---|
317 |
|
---|
318 | List<PresetClassification> result = new ArrayList<PresetClassification>();
|
---|
319 | PRESET_LOOP:
|
---|
320 | for (PresetClassification presetClasification: classifications) {
|
---|
321 | TaggingPreset preset = presetClasification.preset;
|
---|
322 | presetClasification.classification = 0;
|
---|
323 |
|
---|
324 | if (onlyApplicable && preset.types != null) {
|
---|
325 | boolean found = false;
|
---|
326 | for (TaggingPresetType type: preset.types) {
|
---|
327 | if (getTypesInSelection().contains(type)) {
|
---|
328 | found = true;
|
---|
329 | break;
|
---|
330 | }
|
---|
331 | }
|
---|
332 | if (!found) {
|
---|
333 | continue;
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | if (groupWords != null && presetClasification.isMatchingGroup(groupWords) == 0) {
|
---|
338 | continue PRESET_LOOP;
|
---|
339 | }
|
---|
340 |
|
---|
341 | int matchName = presetClasification.isMatchingName(nameWords);
|
---|
342 |
|
---|
343 | if (matchName == 0) {
|
---|
344 | if (groupWords == null) {
|
---|
345 | int groupMatch = presetClasification.isMatchingGroup(nameWords);
|
---|
346 | if (groupMatch > 0) {
|
---|
347 | presetClasification.classification = CLASSIFICATION_GROUP_MATCH + groupMatch;
|
---|
348 | }
|
---|
349 | }
|
---|
350 | if (presetClasification.classification == 0 && inTags) {
|
---|
351 | int tagsMatch = presetClasification.isMatchingTags(nameWords);
|
---|
352 | if (tagsMatch > 0) {
|
---|
353 | presetClasification.classification = CLASSIFICATION_TAGS_MATCH + tagsMatch;
|
---|
354 | }
|
---|
355 | }
|
---|
356 | } else {
|
---|
357 | presetClasification.classification = CLASSIFICATION_NAME_MATCH + matchName;
|
---|
358 | }
|
---|
359 |
|
---|
360 | if (presetClasification.classification > 0) {
|
---|
361 | presetClasification.classification += presetClasification.favoriteIndex;
|
---|
362 | result.add(presetClasification);
|
---|
363 | }
|
---|
364 | }
|
---|
365 |
|
---|
366 | Collections.sort(result);
|
---|
367 | lsResultModel.setPresets(result);
|
---|
368 |
|
---|
369 | }
|
---|
370 |
|
---|
371 | private EnumSet<TaggingPresetType> getTypesInSelection() {
|
---|
372 | if (typesInSelectionDirty) {
|
---|
373 | synchronized (typesInSelection) {
|
---|
374 | typesInSelectionDirty = false;
|
---|
375 | typesInSelection.clear();
|
---|
376 | if (Main.main==null || Main.main.getCurrentDataSet() == null) return typesInSelection;
|
---|
377 | for (OsmPrimitive primitive : Main.main.getCurrentDataSet().getSelected()) {
|
---|
378 | if (primitive instanceof Node) {
|
---|
379 | typesInSelection.add(TaggingPresetType.NODE);
|
---|
380 | } else if (primitive instanceof Way) {
|
---|
381 | typesInSelection.add(TaggingPresetType.WAY);
|
---|
382 | if (((Way) primitive).isClosed()) {
|
---|
383 | typesInSelection.add(TaggingPresetType.CLOSEDWAY);
|
---|
384 | }
|
---|
385 | } else if (primitive instanceof Relation) {
|
---|
386 | typesInSelection.add(TaggingPresetType.RELATION);
|
---|
387 | }
|
---|
388 | }
|
---|
389 | }
|
---|
390 | }
|
---|
391 | return typesInSelection;
|
---|
392 | }
|
---|
393 |
|
---|
394 | @Override
|
---|
395 | public void selectionChanged(Collection<? extends OsmPrimitive> newSelection) {
|
---|
396 | typesInSelectionDirty = true;
|
---|
397 | }
|
---|
398 |
|
---|
399 | public void init() {
|
---|
400 | ckOnlyApplicable.setEnabled(!getTypesInSelection().isEmpty());
|
---|
401 | ckOnlyApplicable.setSelected(!getTypesInSelection().isEmpty() && ONLY_APPLICABLE.get());
|
---|
402 | edSearchText.setText("");
|
---|
403 | filterPresets();
|
---|
404 | }
|
---|
405 |
|
---|
406 | public void init(Collection<TaggingPreset> presets) {
|
---|
407 | classifications.clear();
|
---|
408 | loadPresets(presets);
|
---|
409 | init();
|
---|
410 | }
|
---|
411 |
|
---|
412 |
|
---|
413 | public void clearSelection() {
|
---|
414 | lsResult.getSelectionModel().clearSelection();
|
---|
415 | }
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Save checkbox values in preferences for future reuse
|
---|
419 | */
|
---|
420 | public void savePreferences() {
|
---|
421 | SEARCH_IN_TAGS.put(ckSearchInTags.isSelected());
|
---|
422 | if (ckOnlyApplicable.isEnabled()) {
|
---|
423 | ONLY_APPLICABLE.put(ckOnlyApplicable.isSelected());
|
---|
424 | }
|
---|
425 | }
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Determines, which preset is selected at the current moment
|
---|
429 | * @return selected preset (as action)
|
---|
430 | */
|
---|
431 | public TaggingPreset getSelectedPreset() {
|
---|
432 | int idx = lsResult.getSelectedIndex();
|
---|
433 | if (idx == -1) {
|
---|
434 | idx = 0;
|
---|
435 | }
|
---|
436 | TaggingPreset preset = lsResultModel.getPresets().get(idx).preset;
|
---|
437 | for (PresetClassification pc: classifications) {
|
---|
438 | if (pc.preset == preset) {
|
---|
439 | pc.favoriteIndex = CLASSIFICATION_IN_FAVORITES;
|
---|
440 | } else if (pc.favoriteIndex > 0) {
|
---|
441 | pc.favoriteIndex--;
|
---|
442 | }
|
---|
443 | }
|
---|
444 | return preset;
|
---|
445 | }
|
---|
446 |
|
---|
447 | private void loadPresets(Collection<TaggingPreset> presets) {
|
---|
448 | for (TaggingPreset preset: presets) {
|
---|
449 | if (preset instanceof TaggingPresetSeparator || preset instanceof TaggingPresetMenu) {
|
---|
450 | continue;
|
---|
451 | }
|
---|
452 | classifications.add(new PresetClassification(preset));
|
---|
453 | }
|
---|
454 | }
|
---|
455 |
|
---|
456 | public void setSelectedPreset(TaggingPreset p) {
|
---|
457 | lsResult.setSelectedValue(p, true);
|
---|
458 | }
|
---|
459 |
|
---|
460 | public int getItemCount() {
|
---|
461 | return lsResultModel.getSize();
|
---|
462 | }
|
---|
463 |
|
---|
464 | public void setDblClickListener(ActionListener dblClickListener) {
|
---|
465 | this.dblClickListener = dblClickListener;
|
---|
466 | }
|
---|
467 |
|
---|
468 | public void setClickListener(ActionListener clickListener) {
|
---|
469 | this.clickListener = clickListener;
|
---|
470 | }
|
---|
471 |
|
---|
472 | public void addSelectionListener(final ActionListener selectListener) {
|
---|
473 | lsResult.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
|
---|
474 | @Override
|
---|
475 | public void valueChanged(ListSelectionEvent e) {
|
---|
476 | if (!e.getValueIsAdjusting())
|
---|
477 | selectListener.actionPerformed(null);
|
---|
478 | }
|
---|
479 | });
|
---|
480 | }
|
---|
481 | }
|
---|