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