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