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