1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui.tagging;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.Image;
|
---|
11 | import java.awt.event.ActionEvent;
|
---|
12 | import java.io.BufferedReader;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.io.InputStreamReader;
|
---|
15 | import java.io.Reader;
|
---|
16 | import java.io.UnsupportedEncodingException;
|
---|
17 | import java.util.Arrays;
|
---|
18 | import java.util.Collection;
|
---|
19 | import java.util.HashMap;
|
---|
20 | import java.util.LinkedHashMap;
|
---|
21 | import java.util.LinkedList;
|
---|
22 | import java.util.List;
|
---|
23 | import java.util.TreeSet;
|
---|
24 |
|
---|
25 | import javax.swing.AbstractAction;
|
---|
26 | import javax.swing.Action;
|
---|
27 | import javax.swing.ImageIcon;
|
---|
28 | import javax.swing.JComboBox;
|
---|
29 | import javax.swing.JComponent;
|
---|
30 | import javax.swing.JLabel;
|
---|
31 | import javax.swing.JOptionPane;
|
---|
32 | import javax.swing.JPanel;
|
---|
33 | import javax.swing.JTextField;
|
---|
34 |
|
---|
35 | import org.openstreetmap.josm.Main;
|
---|
36 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
37 | import org.openstreetmap.josm.command.Command;
|
---|
38 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
39 | import org.openstreetmap.josm.data.osm.Node;
|
---|
40 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
41 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
42 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
43 | import org.openstreetmap.josm.data.osm.Way;
|
---|
44 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
45 | import org.openstreetmap.josm.gui.QuadStateCheckBox;
|
---|
46 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
47 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionCache;
|
---|
48 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPritority;
|
---|
49 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
50 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
51 | import org.openstreetmap.josm.tools.GBC;
|
---|
52 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
53 | import org.openstreetmap.josm.tools.UrlLabel;
|
---|
54 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
55 | import org.xml.sax.SAXException;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * This class read encapsulate one tagging preset. A class method can
|
---|
59 | * read in all predefined presets, either shipped with JOSM or that are
|
---|
60 | * in the config directory.
|
---|
61 | *
|
---|
62 | * It is also able to construct dialogs out of preset definitions.
|
---|
63 | */
|
---|
64 | public class TaggingPreset extends AbstractAction {
|
---|
65 |
|
---|
66 | public TaggingPresetMenu group = null;
|
---|
67 | public String name;
|
---|
68 | public String locale_name;
|
---|
69 |
|
---|
70 | public static abstract class Item {
|
---|
71 | protected void initAutoCompletionField(AutoCompletingTextField field, String key) {
|
---|
72 | OsmDataLayer layer = Main.main.getEditLayer();
|
---|
73 | if (layer == null) return;
|
---|
74 | AutoCompletionList list = new AutoCompletionList();
|
---|
75 | List<String> values = AutoCompletionCache.getCacheForLayer(Main.main.getEditLayer()).getValues(key);
|
---|
76 | list.add(values,AutoCompletionItemPritority.IS_IN_DATASET);
|
---|
77 | field.setAutoCompletionList(list);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public boolean focus = false;
|
---|
81 | abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel);
|
---|
82 | abstract void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds);
|
---|
83 | boolean requestFocusInWindow() {return false;}
|
---|
84 | }
|
---|
85 |
|
---|
86 | public static class Usage {
|
---|
87 | TreeSet<String> values;
|
---|
88 | boolean hadKeys = false;
|
---|
89 | boolean hadEmpty = false;
|
---|
90 | public boolean hasUniqueValue() {
|
---|
91 | return values.size() == 1 && !hadEmpty;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public boolean unused() {
|
---|
95 | return values.size() == 0;
|
---|
96 | }
|
---|
97 | public String getFirst() {
|
---|
98 | return values.first();
|
---|
99 | }
|
---|
100 |
|
---|
101 | public boolean hadKeys() {
|
---|
102 | return hadKeys;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static final String DIFFERENT = tr("<different>");
|
---|
107 |
|
---|
108 | static Usage determineTextUsage(Collection<OsmPrimitive> sel, String key) {
|
---|
109 | Usage returnValue = new Usage();
|
---|
110 | returnValue.values = new TreeSet<String>();
|
---|
111 | for (OsmPrimitive s : sel) {
|
---|
112 | String v = s.get(key);
|
---|
113 | if (v != null) {
|
---|
114 | returnValue.values.add(v);
|
---|
115 | } else {
|
---|
116 | returnValue.hadEmpty = true;
|
---|
117 | }
|
---|
118 | returnValue.hadKeys = ! returnValue.values.isEmpty() | returnValue.hadEmpty;
|
---|
119 | }
|
---|
120 | return returnValue;
|
---|
121 | }
|
---|
122 |
|
---|
123 | static Usage determineBooleanUsage(Collection<OsmPrimitive> sel, String key) {
|
---|
124 |
|
---|
125 | Usage returnValue = new Usage();
|
---|
126 | returnValue.values = new TreeSet<String>();
|
---|
127 | for (OsmPrimitive s : sel) {
|
---|
128 | returnValue.values.add(OsmUtils.getNamedOsmBoolean(s.get(key)));
|
---|
129 | }
|
---|
130 | return returnValue;
|
---|
131 | }
|
---|
132 |
|
---|
133 | public static class Text extends Item {
|
---|
134 |
|
---|
135 | public String key;
|
---|
136 | public String text;
|
---|
137 | public String locale_text;
|
---|
138 | public String default_;
|
---|
139 | public String originalValue;
|
---|
140 | public boolean use_last_as_default = false;
|
---|
141 | public boolean delete_if_empty = false;
|
---|
142 |
|
---|
143 | private JComponent value;
|
---|
144 |
|
---|
145 |
|
---|
146 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
147 |
|
---|
148 | // find out if our key is already used in the selection.
|
---|
149 | Usage usage = determineTextUsage(sel, key);
|
---|
150 | AutoCompletingTextField textField = new AutoCompletingTextField();
|
---|
151 | initAutoCompletionField(textField, key);
|
---|
152 | if (usage.unused()){
|
---|
153 | if (use_last_as_default && lastValue.containsKey(key)) {
|
---|
154 | textField.setText(lastValue.get(key));
|
---|
155 | } else {
|
---|
156 | textField.setText(default_);
|
---|
157 | }
|
---|
158 | value = textField;
|
---|
159 | originalValue = null;
|
---|
160 | } else if (usage.hasUniqueValue()) {
|
---|
161 | // all objects use the same value
|
---|
162 | textField.setText(usage.getFirst());
|
---|
163 | value = textField;
|
---|
164 | originalValue = usage.getFirst();
|
---|
165 | } else {
|
---|
166 | // the objects have different values
|
---|
167 | JComboBox comboBox = new JComboBox(usage.values.toArray());
|
---|
168 | comboBox.setEditable(true);
|
---|
169 | comboBox.setEditor(textField);
|
---|
170 | comboBox.getEditor().setItem(DIFFERENT);
|
---|
171 | value=comboBox;
|
---|
172 | originalValue = DIFFERENT;
|
---|
173 | }
|
---|
174 | if(locale_text == null) {
|
---|
175 | locale_text = tr(text);
|
---|
176 | }
|
---|
177 | p.add(new JLabel(locale_text+":"), GBC.std().insets(0,0,10,0));
|
---|
178 | p.add(value, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
179 | return true;
|
---|
180 | }
|
---|
181 |
|
---|
182 | @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
|
---|
183 |
|
---|
184 | // return if unchanged
|
---|
185 | String v = (value instanceof JComboBox) ?
|
---|
186 | ((JComboBox)value).getEditor().getItem().toString() :
|
---|
187 | ((JTextField)value).getText();
|
---|
188 |
|
---|
189 | if (use_last_as_default) {
|
---|
190 | lastValue.put(key, v);
|
---|
191 | }
|
---|
192 | if (v.equals(originalValue) || (originalValue == null && v.length() == 0)) return;
|
---|
193 |
|
---|
194 | if (delete_if_empty && v.length() == 0) {
|
---|
195 | v = null;
|
---|
196 | }
|
---|
197 | cmds.add(new ChangePropertyCommand(sel, key, v));
|
---|
198 | }
|
---|
199 | @Override boolean requestFocusInWindow() {return value.requestFocusInWindow();}
|
---|
200 | }
|
---|
201 |
|
---|
202 | public static class Check extends Item {
|
---|
203 |
|
---|
204 | public String key;
|
---|
205 | public String text;
|
---|
206 | public String locale_text;
|
---|
207 | public boolean default_ = false; // only used for tagless objects
|
---|
208 | public boolean use_last_as_default = false;
|
---|
209 |
|
---|
210 | private QuadStateCheckBox check;
|
---|
211 | private QuadStateCheckBox.State initialState;
|
---|
212 | private boolean def;
|
---|
213 |
|
---|
214 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
215 |
|
---|
216 | // find out if our key is already used in the selection.
|
---|
217 | Usage usage = determineBooleanUsage(sel, key);
|
---|
218 | def = default_;
|
---|
219 |
|
---|
220 | if(locale_text == null) {
|
---|
221 | locale_text = tr(text);
|
---|
222 | }
|
---|
223 |
|
---|
224 | String oneValue = null;
|
---|
225 | for (String s : usage.values) {
|
---|
226 | oneValue = s;
|
---|
227 | }
|
---|
228 | if (usage.values.size() < 2 && (oneValue == null || OsmUtils.trueval.equals(oneValue) || OsmUtils.falseval.equals(oneValue))) {
|
---|
229 | if(def)
|
---|
230 | {
|
---|
231 | for (OsmPrimitive s : sel)
|
---|
232 | if(s.hasKeys()) {
|
---|
233 | def = false;
|
---|
234 | }
|
---|
235 | }
|
---|
236 |
|
---|
237 | // all selected objects share the same value which is either true or false or unset,
|
---|
238 | // we can display a standard check box.
|
---|
239 | initialState = OsmUtils.trueval.equals(oneValue) ?
|
---|
240 | QuadStateCheckBox.State.SELECTED :
|
---|
241 | OsmUtils.falseval.equals(oneValue) ?
|
---|
242 | QuadStateCheckBox.State.NOT_SELECTED :
|
---|
243 | def ? QuadStateCheckBox.State.SELECTED
|
---|
244 | : QuadStateCheckBox.State.UNSET;
|
---|
245 | check = new QuadStateCheckBox(locale_text, initialState,
|
---|
246 | new QuadStateCheckBox.State[] {
|
---|
247 | QuadStateCheckBox.State.SELECTED,
|
---|
248 | QuadStateCheckBox.State.NOT_SELECTED,
|
---|
249 | QuadStateCheckBox.State.UNSET });
|
---|
250 | } else {
|
---|
251 | def = false;
|
---|
252 | // the objects have different values, or one or more objects have something
|
---|
253 | // else than true/false. we display a quad-state check box
|
---|
254 | // in "partial" state.
|
---|
255 | initialState = QuadStateCheckBox.State.PARTIAL;
|
---|
256 | check = new QuadStateCheckBox(locale_text, QuadStateCheckBox.State.PARTIAL,
|
---|
257 | new QuadStateCheckBox.State[] {
|
---|
258 | QuadStateCheckBox.State.PARTIAL,
|
---|
259 | QuadStateCheckBox.State.SELECTED,
|
---|
260 | QuadStateCheckBox.State.NOT_SELECTED,
|
---|
261 | QuadStateCheckBox.State.UNSET });
|
---|
262 | }
|
---|
263 | p.add(check, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
264 | return true;
|
---|
265 | }
|
---|
266 |
|
---|
267 | @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
|
---|
268 | // if the user hasn't changed anything, don't create a command.
|
---|
269 | if (check.getState() == initialState && !def) return;
|
---|
270 |
|
---|
271 | // otherwise change things according to the selected value.
|
---|
272 | cmds.add(new ChangePropertyCommand(sel, key,
|
---|
273 | check.getState() == QuadStateCheckBox.State.SELECTED ? OsmUtils.trueval :
|
---|
274 | check.getState() == QuadStateCheckBox.State.NOT_SELECTED ? OsmUtils.falseval :
|
---|
275 | null));
|
---|
276 | }
|
---|
277 | @Override boolean requestFocusInWindow() {return check.requestFocusInWindow();}
|
---|
278 | }
|
---|
279 |
|
---|
280 | public static class Combo extends Item {
|
---|
281 |
|
---|
282 | public String key;
|
---|
283 | public String text;
|
---|
284 | public String locale_text;
|
---|
285 | public String values;
|
---|
286 | public String display_values;
|
---|
287 | public String locale_display_values;
|
---|
288 | public String default_;
|
---|
289 | public boolean delete_if_empty = false;
|
---|
290 | public boolean editable = true;
|
---|
291 | public boolean use_last_as_default = false;
|
---|
292 |
|
---|
293 | private JComboBox combo;
|
---|
294 | private LinkedHashMap<String,String> lhm;
|
---|
295 | private Usage usage;
|
---|
296 | private String originalValue;
|
---|
297 |
|
---|
298 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
299 |
|
---|
300 | // find out if our key is already used in the selection.
|
---|
301 | usage = determineTextUsage(sel, key);
|
---|
302 |
|
---|
303 | String[] value_array = values.split(",");
|
---|
304 | String[] display_array;
|
---|
305 | if(locale_display_values != null) {
|
---|
306 | display_array = locale_display_values.split(",");
|
---|
307 | } else if(display_values != null) {
|
---|
308 | display_array = display_values.split(",");
|
---|
309 | } else {
|
---|
310 | display_array = value_array;
|
---|
311 | }
|
---|
312 |
|
---|
313 | lhm = new LinkedHashMap<String,String>();
|
---|
314 | if (!usage.hasUniqueValue() && !usage.unused()){
|
---|
315 | lhm.put(DIFFERENT, DIFFERENT);
|
---|
316 | }
|
---|
317 | for (int i=0; i<value_array.length; i++) {
|
---|
318 | lhm.put(value_array[i],
|
---|
319 | (locale_display_values == null) ?
|
---|
320 | tr(display_array[i]) : display_array[i]);
|
---|
321 | }
|
---|
322 | if(!usage.unused()){
|
---|
323 | for (String s : usage.values) {
|
---|
324 | if (!lhm.containsKey(s)) {
|
---|
325 | lhm.put(s, s);
|
---|
326 | }
|
---|
327 | }
|
---|
328 | }
|
---|
329 | if (default_ != null && !lhm.containsKey(default_)) {
|
---|
330 | lhm.put(default_, default_);
|
---|
331 | }
|
---|
332 | if(!lhm.containsKey("")) {
|
---|
333 | lhm.put("", "");
|
---|
334 | }
|
---|
335 |
|
---|
336 | combo = new JComboBox(lhm.values().toArray());
|
---|
337 | combo.setEditable(editable);
|
---|
338 | AutoCompletingTextField tf = new AutoCompletingTextField();
|
---|
339 | initAutoCompletionField(tf, key);
|
---|
340 | tf.getAutoCompletionList().add(Arrays.asList(display_array), AutoCompletionItemPritority.IS_IN_STANDARD);
|
---|
341 | combo.setEditor(tf);
|
---|
342 |
|
---|
343 | if (usage.hasUniqueValue() && !usage.unused()){
|
---|
344 | originalValue=usage.getFirst();
|
---|
345 | combo.setSelectedItem(lhm.get(originalValue));
|
---|
346 | }
|
---|
347 | // use default only in case it is a totally new entry
|
---|
348 | else if(default_ != null && !usage.hadKeys()) {
|
---|
349 | combo.setSelectedItem(default_);
|
---|
350 | originalValue=DIFFERENT;
|
---|
351 | }
|
---|
352 | else if(usage.unused()){
|
---|
353 | combo.setSelectedItem("");
|
---|
354 | originalValue="";
|
---|
355 | }
|
---|
356 | else{
|
---|
357 | combo.setSelectedItem(DIFFERENT);
|
---|
358 | originalValue=DIFFERENT;
|
---|
359 | }
|
---|
360 |
|
---|
361 | if(locale_text == null) {
|
---|
362 | locale_text = tr(text);
|
---|
363 | }
|
---|
364 | p.add(new JLabel(locale_text+":"), GBC.std().insets(0,0,10,0));
|
---|
365 | p.add(combo, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
366 | return true;
|
---|
367 | }
|
---|
368 | @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
|
---|
369 | Object obj = combo.getSelectedItem();
|
---|
370 | String display = (obj == null) ? null : obj.toString();
|
---|
371 | String value = null;
|
---|
372 | if(display == null && combo.isEditable()) {
|
---|
373 | display = combo.getEditor().getItem().toString();
|
---|
374 | }
|
---|
375 |
|
---|
376 | if (display != null)
|
---|
377 | {
|
---|
378 | for (String key : lhm.keySet()) {
|
---|
379 | String k = lhm.get(key);
|
---|
380 | if (k != null && k.equals(display)) {
|
---|
381 | value=key;
|
---|
382 | }
|
---|
383 | }
|
---|
384 | if(value == null) {
|
---|
385 | value = display;
|
---|
386 | }
|
---|
387 | } else {
|
---|
388 | value = "";
|
---|
389 | }
|
---|
390 |
|
---|
391 | // no change if same as before
|
---|
392 | if (value.equals(originalValue) || (originalValue == null && value.length() == 0)) return;
|
---|
393 |
|
---|
394 | if (delete_if_empty && value.length() == 0) {
|
---|
395 | value = null;
|
---|
396 | }
|
---|
397 | cmds.add(new ChangePropertyCommand(sel, key, value));
|
---|
398 | }
|
---|
399 | @Override boolean requestFocusInWindow() {return combo.requestFocusInWindow();}
|
---|
400 | }
|
---|
401 |
|
---|
402 | public static class Label extends Item {
|
---|
403 | public String text;
|
---|
404 | public String locale_text;
|
---|
405 |
|
---|
406 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
407 | if(locale_text == null) {
|
---|
408 | locale_text = tr(text);
|
---|
409 | }
|
---|
410 | p.add(new JLabel(locale_text), GBC.eol());
|
---|
411 | return false;
|
---|
412 | }
|
---|
413 | @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {}
|
---|
414 | }
|
---|
415 |
|
---|
416 | public static class Link extends Item {
|
---|
417 | public String href;
|
---|
418 | public String text;
|
---|
419 | public String locale_text;
|
---|
420 | public String locale_href;
|
---|
421 |
|
---|
422 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
423 | if(locale_text == null) {
|
---|
424 | locale_text = text == null ? tr("More information about this feature") : tr(text);
|
---|
425 | }
|
---|
426 | String url = locale_href;
|
---|
427 | if (url == null) {
|
---|
428 | url = href;
|
---|
429 | }
|
---|
430 | if (url != null) {
|
---|
431 | p.add(new UrlLabel(url, locale_text), GBC.eol().anchor(GBC.WEST));
|
---|
432 | }
|
---|
433 | return false;
|
---|
434 | }
|
---|
435 | @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {}
|
---|
436 | }
|
---|
437 |
|
---|
438 | public static class Optional extends Item {
|
---|
439 | // TODO: Draw a box around optional stuff
|
---|
440 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
441 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
442 | p.add(new JLabel(tr("Optional Attributes:")), GBC.eol());
|
---|
443 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
444 | return false;
|
---|
445 | }
|
---|
446 | @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {}
|
---|
447 | }
|
---|
448 |
|
---|
449 | public static class Space extends Item {
|
---|
450 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
451 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
452 | return false;
|
---|
453 | }
|
---|
454 | @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {}
|
---|
455 | }
|
---|
456 |
|
---|
457 | public static class Key extends Item {
|
---|
458 | public String key;
|
---|
459 | public String value;
|
---|
460 |
|
---|
461 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) { return false; }
|
---|
462 | @Override public void addCommands(Collection<OsmPrimitive> sel, List<Command> cmds) {
|
---|
463 | cmds.add(new ChangePropertyCommand(sel, key, value != null && !value.equals("") ? value : null));
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * The types as preparsed collection.
|
---|
469 | */
|
---|
470 | public List<String> types;
|
---|
471 | public List<Item> data = new LinkedList<Item>();
|
---|
472 | private static HashMap<String,String> lastValue = new HashMap<String,String>();
|
---|
473 |
|
---|
474 | /**
|
---|
475 | * Create an empty tagging preset. This will not have any items and
|
---|
476 | * will be an empty string as text. createPanel will return null.
|
---|
477 | * Use this as default item for "do not select anything".
|
---|
478 | */
|
---|
479 | public TaggingPreset() {}
|
---|
480 |
|
---|
481 | /**
|
---|
482 | * Change the display name without changing the toolbar value.
|
---|
483 | */
|
---|
484 | public void setDisplayName() {
|
---|
485 | putValue(Action.NAME, getName());
|
---|
486 | putValue("toolbar", "tagging_" + getRawName());
|
---|
487 | putValue(SHORT_DESCRIPTION, (group != null ?
|
---|
488 | tr("Use preset ''{0}'' of group ''{1}''", getLocaleName(), group.getName()) :
|
---|
489 | tr("Use preset ''{0}''", getLocaleName())));
|
---|
490 | }
|
---|
491 |
|
---|
492 | public String getLocaleName() {
|
---|
493 | if(locale_name == null) {
|
---|
494 | locale_name = tr(name);
|
---|
495 | }
|
---|
496 | return locale_name;
|
---|
497 | }
|
---|
498 |
|
---|
499 | public String getName() {
|
---|
500 | return group != null ? group.getName() + "/" + getLocaleName() : getLocaleName();
|
---|
501 | }
|
---|
502 | public String getRawName() {
|
---|
503 | return group != null ? group.getRawName() + "/" + name : name;
|
---|
504 | }
|
---|
505 | /**
|
---|
506 | * Called from the XML parser to set the icon
|
---|
507 | *
|
---|
508 | * FIXME for Java 1.6 - use 24x24 icons for LARGE_ICON_KEY (button bar)
|
---|
509 | * and the 16x16 icons for SMALL_ICON.
|
---|
510 | */
|
---|
511 | public void setIcon(String iconName) {
|
---|
512 | Collection<String> s = Main.pref.getCollection("taggingpreset.icon.sources", null);
|
---|
513 | ImageIcon icon = ImageProvider.getIfAvailable(s, "presets", null, iconName);
|
---|
514 | if (icon == null)
|
---|
515 | {
|
---|
516 | System.out.println("Could not get presets icon " + iconName);
|
---|
517 | icon = new ImageIcon(iconName);
|
---|
518 | }
|
---|
519 | if (Math.max(icon.getIconHeight(), icon.getIconWidth()) != 16) {
|
---|
520 | icon = new ImageIcon(icon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
|
---|
521 | }
|
---|
522 | putValue(Action.SMALL_ICON, icon);
|
---|
523 | }
|
---|
524 |
|
---|
525 | /**
|
---|
526 | * Called from the XML parser to set the types this preset affects
|
---|
527 | */
|
---|
528 | private static Collection<String> allowedtypes = Arrays.asList(new String[]
|
---|
529 | {marktr("way"), marktr("node"), marktr("relation"), marktr("closedway")});
|
---|
530 | public void setType(String types) throws SAXException {
|
---|
531 | this.types = Arrays.asList(types.split(","));
|
---|
532 | for (String type : this.types) {
|
---|
533 | if(!allowedtypes.contains(type))
|
---|
534 | throw new SAXException(tr("Unknown type: {0}", type));
|
---|
535 | }
|
---|
536 | }
|
---|
537 |
|
---|
538 | public static List<TaggingPreset> readAll(Reader in) throws SAXException {
|
---|
539 | XmlObjectParser parser = new XmlObjectParser();
|
---|
540 | parser.mapOnStart("item", TaggingPreset.class);
|
---|
541 | parser.mapOnStart("separator", TaggingPresetSeparator.class);
|
---|
542 | parser.mapBoth("group", TaggingPresetMenu.class);
|
---|
543 | parser.map("text", Text.class);
|
---|
544 | parser.map("link", Link.class);
|
---|
545 | parser.mapOnStart("optional", Optional.class);
|
---|
546 | parser.map("check", Check.class);
|
---|
547 | parser.map("combo", Combo.class);
|
---|
548 | parser.map("label", Label.class);
|
---|
549 | parser.map("space", Space.class);
|
---|
550 | parser.map("key", Key.class);
|
---|
551 | LinkedList<TaggingPreset> all = new LinkedList<TaggingPreset>();
|
---|
552 | TaggingPresetMenu lastmenu = null;
|
---|
553 | parser.start(in);
|
---|
554 | while(parser.hasNext()) {
|
---|
555 | Object o = parser.next();
|
---|
556 | if (o instanceof TaggingPresetMenu) {
|
---|
557 | TaggingPresetMenu tp = (TaggingPresetMenu) o;
|
---|
558 | if(tp == lastmenu) {
|
---|
559 | lastmenu = tp.group;
|
---|
560 | } else
|
---|
561 | {
|
---|
562 | tp.setDisplayName();
|
---|
563 | tp.group = lastmenu;
|
---|
564 | lastmenu = tp;
|
---|
565 | all.add(tp);
|
---|
566 | Main.toolbar.register(tp);
|
---|
567 |
|
---|
568 | }
|
---|
569 | } else if (o instanceof TaggingPresetSeparator) {
|
---|
570 | TaggingPresetSeparator tp = (TaggingPresetSeparator) o;
|
---|
571 | tp.group = lastmenu;
|
---|
572 | all.add(tp);
|
---|
573 | } else if (o instanceof TaggingPreset) {
|
---|
574 | TaggingPreset tp = (TaggingPreset) o;
|
---|
575 | tp.group = lastmenu;
|
---|
576 | tp.setDisplayName();
|
---|
577 | all.add(tp);
|
---|
578 | Main.toolbar.register(tp);
|
---|
579 | } else {
|
---|
580 | all.getLast().data.add((Item)o);
|
---|
581 | }
|
---|
582 | }
|
---|
583 | return all;
|
---|
584 | }
|
---|
585 |
|
---|
586 | public static Collection<TaggingPreset> readFromPreferences() {
|
---|
587 | LinkedList<TaggingPreset> allPresets = new LinkedList<TaggingPreset>();
|
---|
588 | LinkedList<String> sources = new LinkedList<String>();
|
---|
589 |
|
---|
590 | if(Main.pref.getBoolean("taggingpreset.enable-defaults", true)) {
|
---|
591 | sources.add("resource://presets/presets.xml");
|
---|
592 | }
|
---|
593 | sources.addAll(Main.pref.getCollection("taggingpreset.sources", new LinkedList<String>()));
|
---|
594 |
|
---|
595 | for(String source : sources)
|
---|
596 | {
|
---|
597 | try {
|
---|
598 | MirroredInputStream s = new MirroredInputStream(source);
|
---|
599 | InputStreamReader r;
|
---|
600 | try
|
---|
601 | {
|
---|
602 | r = new InputStreamReader(s, "UTF-8");
|
---|
603 | }
|
---|
604 | catch (UnsupportedEncodingException e)
|
---|
605 | {
|
---|
606 | r = new InputStreamReader(s);
|
---|
607 | }
|
---|
608 | allPresets.addAll(TaggingPreset.readAll(new BufferedReader(r)));
|
---|
609 | } catch (IOException e) {
|
---|
610 | e.printStackTrace();
|
---|
611 | JOptionPane.showMessageDialog(
|
---|
612 | Main.parent,
|
---|
613 | tr("Could not read tagging preset source: {0}",source),
|
---|
614 | tr("Error"),
|
---|
615 | JOptionPane.ERROR_MESSAGE
|
---|
616 | );
|
---|
617 | } catch (SAXException e) {
|
---|
618 | e.printStackTrace();
|
---|
619 | JOptionPane.showMessageDialog(
|
---|
620 | Main.parent,
|
---|
621 | tr("Error parsing {0}: ", source)+e.getMessage(),
|
---|
622 | tr("Error"),
|
---|
623 | JOptionPane.ERROR_MESSAGE
|
---|
624 | );
|
---|
625 | }
|
---|
626 | }
|
---|
627 | return allPresets;
|
---|
628 | }
|
---|
629 |
|
---|
630 | private class PresetPanel extends JPanel {
|
---|
631 | boolean hasElements = false;
|
---|
632 | PresetPanel()
|
---|
633 | {
|
---|
634 | super(new GridBagLayout());
|
---|
635 | }
|
---|
636 | }
|
---|
637 |
|
---|
638 | public PresetPanel createPanel(Collection<OsmPrimitive> selected) {
|
---|
639 | if (data == null)
|
---|
640 | return null;
|
---|
641 | OsmDataLayer layer = Main.main.getEditLayer();
|
---|
642 | if (layer != null) {
|
---|
643 | AutoCompletionCache.getCacheForLayer(layer).initFromDataSet();
|
---|
644 | }
|
---|
645 | PresetPanel p = new PresetPanel();
|
---|
646 | LinkedList<Item> l = new LinkedList<Item>();
|
---|
647 | if(types != null){
|
---|
648 | JPanel pp = new JPanel();
|
---|
649 | for(String t : types){
|
---|
650 | JLabel la = new JLabel(ImageProvider.get("Mf_" + t));
|
---|
651 | la.setToolTipText(tr("Elements of type {0} are supported.", tr(t)));
|
---|
652 | pp.add(la);
|
---|
653 | }
|
---|
654 | p.add(pp, GBC.eol());
|
---|
655 | }
|
---|
656 |
|
---|
657 | for (Item i : data){
|
---|
658 | if(i instanceof Link) {
|
---|
659 | l.add(i);
|
---|
660 | } else {
|
---|
661 | if(i.addToPanel(p, selected)) {
|
---|
662 | p.hasElements = true;
|
---|
663 | }
|
---|
664 | }
|
---|
665 | }
|
---|
666 | for(Item link : l) {
|
---|
667 | link.addToPanel(p, selected);
|
---|
668 | }
|
---|
669 | return p;
|
---|
670 | }
|
---|
671 |
|
---|
672 | public void actionPerformed(ActionEvent e) {
|
---|
673 | Collection<OsmPrimitive> sel = createSelection(Main.main.getCurrentDataSet().getSelected());
|
---|
674 | PresetPanel p = createPanel(sel);
|
---|
675 | if (p == null)
|
---|
676 | return;
|
---|
677 |
|
---|
678 | int answer = 1;
|
---|
679 | if (p.getComponentCount() != 0 && (sel.size() == 0 || p.hasElements)) {
|
---|
680 | String title = trn("Change {0} object", "Change {0} objects", sel.size(), sel.size());
|
---|
681 | if(sel.size() == 0) {
|
---|
682 | if(originalSelectionEmpty) {
|
---|
683 | title = tr("Nothing selected!");
|
---|
684 | } else {
|
---|
685 | title = tr("Selection unsuitable!");
|
---|
686 | }
|
---|
687 | }
|
---|
688 |
|
---|
689 | class PresetDialog extends ExtendedDialog {
|
---|
690 | public PresetDialog(Component content, String title, boolean disableApply) {
|
---|
691 | super(Main.parent,
|
---|
692 | title,
|
---|
693 | new String[] { tr("Apply Preset"), tr("Cancel") },
|
---|
694 | true);
|
---|
695 | contentConstraints = GBC.eol().fill().insets(5,10,5,0);
|
---|
696 | setButtonIcons(new String[] {"ok.png", "cancel.png" });
|
---|
697 | setContent(content);
|
---|
698 | setupDialog();
|
---|
699 | buttons.get(0).setEnabled(!disableApply);
|
---|
700 | buttons.get(0).setToolTipText(title);
|
---|
701 | setVisible(true);
|
---|
702 | }
|
---|
703 | }
|
---|
704 |
|
---|
705 | answer = new PresetDialog(p, title, (sel.size() == 0)).getValue();
|
---|
706 | }
|
---|
707 | if (sel.size() != 0 && answer == 1) {
|
---|
708 | Command cmd = createCommand(sel);
|
---|
709 | if (cmd != null) {
|
---|
710 | Main.main.undoRedo.add(cmd);
|
---|
711 | }
|
---|
712 | }
|
---|
713 | Main.main.getCurrentDataSet().setSelected(Main.main.getCurrentDataSet().getSelected()); // force update
|
---|
714 | }
|
---|
715 |
|
---|
716 | /**
|
---|
717 | * True whenever the original selection given into createSelection was empty
|
---|
718 | */
|
---|
719 | private boolean originalSelectionEmpty = false;
|
---|
720 |
|
---|
721 | /**
|
---|
722 | * Removes all unsuitable OsmPrimitives from the given list
|
---|
723 | * @param participants List of possibile OsmPrimitives to tag
|
---|
724 | * @return Cleaned list with suitable OsmPrimitives only
|
---|
725 | */
|
---|
726 | private Collection<OsmPrimitive> createSelection(Collection<OsmPrimitive> participants) {
|
---|
727 | originalSelectionEmpty = participants.size() == 0;
|
---|
728 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
729 | for (OsmPrimitive osm : participants)
|
---|
730 | {
|
---|
731 | if (types != null)
|
---|
732 | {
|
---|
733 | if(osm instanceof Relation)
|
---|
734 | {
|
---|
735 | if(!types.contains("relation")) {
|
---|
736 | continue;
|
---|
737 | }
|
---|
738 | }
|
---|
739 | else if(osm instanceof Node)
|
---|
740 | {
|
---|
741 | if(!types.contains("node")) {
|
---|
742 | continue;
|
---|
743 | }
|
---|
744 | }
|
---|
745 | else if(osm instanceof Way)
|
---|
746 | {
|
---|
747 | if(!types.contains("way") &&
|
---|
748 | !(types.contains("closedway") && ((Way)osm).isClosed())) {
|
---|
749 | continue;
|
---|
750 | }
|
---|
751 | }
|
---|
752 | }
|
---|
753 | sel.add(osm);
|
---|
754 | }
|
---|
755 | return sel;
|
---|
756 | }
|
---|
757 |
|
---|
758 | private Command createCommand(Collection<OsmPrimitive> sel) {
|
---|
759 | List<Command> cmds = new LinkedList<Command>();
|
---|
760 | for (Item i : data) {
|
---|
761 | i.addCommands(sel, cmds);
|
---|
762 | }
|
---|
763 | if (cmds.size() == 0)
|
---|
764 | return null;
|
---|
765 | else if (cmds.size() == 1)
|
---|
766 | return cmds.get(0);
|
---|
767 | else
|
---|
768 | return new SequenceCommand(tr("Change Properties"), cmds);
|
---|
769 | }
|
---|
770 | }
|
---|