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.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.Component;
|
---|
9 | import java.awt.Dimension;
|
---|
10 | import java.awt.Font;
|
---|
11 | import java.awt.GridBagLayout;
|
---|
12 | import java.awt.Image;
|
---|
13 | import java.awt.Insets;
|
---|
14 | import java.awt.event.ActionEvent;
|
---|
15 | import java.io.BufferedReader;
|
---|
16 | import java.io.File;
|
---|
17 | import java.io.IOException;
|
---|
18 | import java.io.InputStream;
|
---|
19 | import java.io.InputStreamReader;
|
---|
20 | import java.io.Reader;
|
---|
21 | import java.io.UnsupportedEncodingException;
|
---|
22 | import java.util.ArrayList;
|
---|
23 | import java.util.Arrays;
|
---|
24 | import java.util.Collection;
|
---|
25 | import java.util.Collections;
|
---|
26 | import java.util.EnumSet;
|
---|
27 | import java.util.HashMap;
|
---|
28 | import java.util.HashSet;
|
---|
29 | import java.util.LinkedHashMap;
|
---|
30 | import java.util.LinkedList;
|
---|
31 | import java.util.List;
|
---|
32 | import java.util.Map;
|
---|
33 | import java.util.TreeSet;
|
---|
34 |
|
---|
35 | import javax.swing.AbstractAction;
|
---|
36 | import javax.swing.Action;
|
---|
37 | import javax.swing.ImageIcon;
|
---|
38 | import javax.swing.JComboBox;
|
---|
39 | import javax.swing.JComponent;
|
---|
40 | import javax.swing.JLabel;
|
---|
41 | import javax.swing.JList;
|
---|
42 | import javax.swing.JOptionPane;
|
---|
43 | import javax.swing.JPanel;
|
---|
44 | import javax.swing.JScrollPane;
|
---|
45 | import javax.swing.JTextField;
|
---|
46 | import javax.swing.ListCellRenderer;
|
---|
47 | import javax.swing.ListModel;
|
---|
48 | import javax.swing.SwingUtilities;
|
---|
49 |
|
---|
50 | import org.openstreetmap.josm.Main;
|
---|
51 | import org.openstreetmap.josm.actions.search.SearchCompiler;
|
---|
52 | import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
|
---|
53 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
54 | import org.openstreetmap.josm.command.Command;
|
---|
55 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
56 | import org.openstreetmap.josm.data.osm.Node;
|
---|
57 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
58 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
59 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
60 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
61 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
62 | import org.openstreetmap.josm.data.osm.Way;
|
---|
63 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
64 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
65 | import org.openstreetmap.josm.gui.MapView;
|
---|
66 | import org.openstreetmap.josm.gui.QuadStateCheckBox;
|
---|
67 | import org.openstreetmap.josm.gui.dialogs.relation.RelationEditor;
|
---|
68 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
69 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
70 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
71 | import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference.PresetPrefHelper;
|
---|
72 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
73 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPritority;
|
---|
74 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
75 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
76 | import org.openstreetmap.josm.io.MirroredInputStream;
|
---|
77 | import org.openstreetmap.josm.tools.GBC;
|
---|
78 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
79 | import org.openstreetmap.josm.tools.UrlLabel;
|
---|
80 | import org.openstreetmap.josm.tools.Utils;
|
---|
81 | import org.openstreetmap.josm.tools.XmlObjectParser;
|
---|
82 | import org.openstreetmap.josm.tools.template_engine.ParseError;
|
---|
83 | import org.openstreetmap.josm.tools.template_engine.TemplateEntry;
|
---|
84 | import org.openstreetmap.josm.tools.template_engine.TemplateParser;
|
---|
85 | import org.xml.sax.SAXException;
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * This class read encapsulate one tagging preset. A class method can
|
---|
89 | * read in all predefined presets, either shipped with JOSM or that are
|
---|
90 | * in the config directory.
|
---|
91 | *
|
---|
92 | * It is also able to construct dialogs out of preset definitions.
|
---|
93 | */
|
---|
94 | public class TaggingPreset extends AbstractAction implements MapView.LayerChangeListener {
|
---|
95 |
|
---|
96 | public enum PresetType {
|
---|
97 | NODE(/* ICON */"Mf_node"), WAY(/* ICON */"Mf_way"), RELATION(/* ICON */"Mf_relation"), CLOSEDWAY(/* ICON */"Mf_closedway");
|
---|
98 |
|
---|
99 | private final String iconName;
|
---|
100 |
|
---|
101 | PresetType(String iconName) {
|
---|
102 | this.iconName = iconName;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public String getIconName() {
|
---|
106 | return iconName;
|
---|
107 | }
|
---|
108 |
|
---|
109 | public String getName() {
|
---|
110 | return name().toLowerCase();
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static PresetType forPrimitive(OsmPrimitive p) {
|
---|
114 | return forPrimitiveType(p.getDisplayType());
|
---|
115 | }
|
---|
116 |
|
---|
117 | public static PresetType forPrimitiveType(org.openstreetmap.josm.data.osm.OsmPrimitiveType type) {
|
---|
118 | switch (type) {
|
---|
119 | case NODE:
|
---|
120 | return NODE;
|
---|
121 | case WAY:
|
---|
122 | return WAY;
|
---|
123 | case CLOSEDWAY:
|
---|
124 | return CLOSEDWAY;
|
---|
125 | case RELATION:
|
---|
126 | case MULTIPOLYGON:
|
---|
127 | return RELATION;
|
---|
128 | default:
|
---|
129 | throw new IllegalArgumentException("Unexpected primitive type: " + type);
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Enum denoting how a match (see {@link Item#matches}) is performed.
|
---|
136 | */
|
---|
137 | private enum MatchType {
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Neutral, i.e., do not consider this item for matching.
|
---|
141 | */
|
---|
142 | NONE("none"),
|
---|
143 | /**
|
---|
144 | * Positive if key matches, neutral otherwise.
|
---|
145 | */
|
---|
146 | KEY("key"),
|
---|
147 | /**
|
---|
148 | * Positive if key matches, negative otherwise.
|
---|
149 | */
|
---|
150 | KEY_REQUIRED("key!"),
|
---|
151 | /**
|
---|
152 | * Positive if key and value matches, negative otherwise.
|
---|
153 | */
|
---|
154 | KEY_VALUE("keyvalue");
|
---|
155 |
|
---|
156 | private final String value;
|
---|
157 |
|
---|
158 | private MatchType(String value) {
|
---|
159 | this.value = value;
|
---|
160 | }
|
---|
161 |
|
---|
162 | public String getValue() {
|
---|
163 | return value;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public static MatchType ofString(String type) {
|
---|
167 | for (MatchType i : EnumSet.allOf(MatchType.class)) {
|
---|
168 | if (i.getValue().equals(type)) {
|
---|
169 | return i;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | throw new IllegalArgumentException(type + " is not allowed");
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | public static final int DIALOG_ANSWER_APPLY = 1;
|
---|
177 | public static final int DIALOG_ANSWER_NEW_RELATION = 2;
|
---|
178 | public static final int DIALOG_ANSWER_CANCEL = 3;
|
---|
179 |
|
---|
180 | public TaggingPresetMenu group = null;
|
---|
181 | public String name;
|
---|
182 | public String name_context;
|
---|
183 | public String locale_name;
|
---|
184 | public final static String OPTIONAL_TOOLTIP_TEXT = "Optional tooltip text";
|
---|
185 | private static File zipIcons = null;
|
---|
186 | private static final BooleanProperty PROP_FILL_DEFAULT = new BooleanProperty("taggingpreset.fill-default-for-tagged-primitives", false);
|
---|
187 |
|
---|
188 | public static abstract class Item {
|
---|
189 |
|
---|
190 | protected void initAutoCompletionField(AutoCompletingTextField field, String key) {
|
---|
191 | OsmDataLayer layer = Main.main.getEditLayer();
|
---|
192 | if (layer == null)
|
---|
193 | return;
|
---|
194 | AutoCompletionList list = new AutoCompletionList();
|
---|
195 | Main.main.getEditLayer().data.getAutoCompletionManager().populateWithTagValues(list, key);
|
---|
196 | field.setAutoCompletionList(list);
|
---|
197 | }
|
---|
198 |
|
---|
199 | abstract boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel);
|
---|
200 |
|
---|
201 | abstract void addCommands(List<Tag> changedTags);
|
---|
202 |
|
---|
203 | boolean requestFocusInWindow() {
|
---|
204 | return false;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Tests whether the tags match this item.
|
---|
209 | * Note that for a match, at least one positive and no negative is required.
|
---|
210 | * @param tags the tags of an {@link OsmPrimitive}
|
---|
211 | * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative).
|
---|
212 | */
|
---|
213 | Boolean matches(Map<String, String> tags) {
|
---|
214 | return null;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | public static abstract class KeyedItem extends Item {
|
---|
219 |
|
---|
220 | public String key;
|
---|
221 | public String text;
|
---|
222 | public String text_context;
|
---|
223 | public String match = getDefaultMatch().getValue();
|
---|
224 |
|
---|
225 | public abstract MatchType getDefaultMatch();
|
---|
226 | public abstract Collection<String> getValues();
|
---|
227 |
|
---|
228 | @Override
|
---|
229 | Boolean matches(Map<String, String> tags) {
|
---|
230 | switch (MatchType.ofString(match)) {
|
---|
231 | case NONE:
|
---|
232 | return null;
|
---|
233 | case KEY:
|
---|
234 | return tags.containsKey(key) ? true : null;
|
---|
235 | case KEY_REQUIRED:
|
---|
236 | return tags.containsKey(key);
|
---|
237 | case KEY_VALUE:
|
---|
238 | return tags.containsKey(key) && (getValues().contains(tags.get(key)));
|
---|
239 | default:
|
---|
240 | throw new IllegalStateException();
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | }
|
---|
245 |
|
---|
246 | public static class Usage {
|
---|
247 | TreeSet<String> values;
|
---|
248 | boolean hadKeys = false;
|
---|
249 | boolean hadEmpty = false;
|
---|
250 | public boolean hasUniqueValue() {
|
---|
251 | return values.size() == 1 && !hadEmpty;
|
---|
252 | }
|
---|
253 |
|
---|
254 | public boolean unused() {
|
---|
255 | return values.isEmpty();
|
---|
256 | }
|
---|
257 | public String getFirst() {
|
---|
258 | return values.first();
|
---|
259 | }
|
---|
260 |
|
---|
261 | public boolean hadKeys() {
|
---|
262 | return hadKeys;
|
---|
263 | }
|
---|
264 | }
|
---|
265 |
|
---|
266 | public static final String DIFFERENT = tr("<different>");
|
---|
267 |
|
---|
268 | static Usage determineTextUsage(Collection<OsmPrimitive> sel, String key) {
|
---|
269 | Usage returnValue = new Usage();
|
---|
270 | returnValue.values = new TreeSet<String>();
|
---|
271 | for (OsmPrimitive s : sel) {
|
---|
272 | String v = s.get(key);
|
---|
273 | if (v != null) {
|
---|
274 | returnValue.values.add(v);
|
---|
275 | } else {
|
---|
276 | returnValue.hadEmpty = true;
|
---|
277 | }
|
---|
278 | if(s.hasKeys()) {
|
---|
279 | returnValue.hadKeys = true;
|
---|
280 | }
|
---|
281 | }
|
---|
282 | return returnValue;
|
---|
283 | }
|
---|
284 |
|
---|
285 | static Usage determineBooleanUsage(Collection<OsmPrimitive> sel, String key) {
|
---|
286 |
|
---|
287 | Usage returnValue = new Usage();
|
---|
288 | returnValue.values = new TreeSet<String>();
|
---|
289 | for (OsmPrimitive s : sel) {
|
---|
290 | String booleanValue = OsmUtils.getNamedOsmBoolean(s.get(key));
|
---|
291 | if (booleanValue != null) {
|
---|
292 | returnValue.values.add(booleanValue);
|
---|
293 | }
|
---|
294 | }
|
---|
295 | return returnValue;
|
---|
296 | }
|
---|
297 |
|
---|
298 | public static class PresetListEntry {
|
---|
299 | public String value;
|
---|
300 | public String value_context;
|
---|
301 | public String display_value;
|
---|
302 | public String short_description;
|
---|
303 | public String icon;
|
---|
304 | public String locale_display_value;
|
---|
305 | public String locale_short_description;
|
---|
306 | private final File zipIcons = TaggingPreset.zipIcons;
|
---|
307 |
|
---|
308 | public String getListDisplay() {
|
---|
309 | if (value.equals(DIFFERENT))
|
---|
310 | return "<b>"+DIFFERENT.replaceAll("<", "<").replaceAll(">", ">")+"</b>";
|
---|
311 |
|
---|
312 | if (value.equals(""))
|
---|
313 | return " ";
|
---|
314 |
|
---|
315 | final StringBuilder res = new StringBuilder("<b>");
|
---|
316 | res.append(getDisplayValue(true));
|
---|
317 | res.append("</b>");
|
---|
318 | if (getShortDescription(true) != null) {
|
---|
319 | // wrap in table to restrict the text width
|
---|
320 | res.append("<div style=\"width:300px; padding:0 0 5px 5px\">");
|
---|
321 | res.append(getShortDescription(true));
|
---|
322 | res.append("</div>");
|
---|
323 | }
|
---|
324 | return res.toString();
|
---|
325 | }
|
---|
326 |
|
---|
327 | public ImageIcon getIcon() {
|
---|
328 | return icon == null ? null : loadImageIcon(icon, zipIcons);
|
---|
329 | }
|
---|
330 |
|
---|
331 | public PresetListEntry() {
|
---|
332 | }
|
---|
333 |
|
---|
334 | public PresetListEntry(String value) {
|
---|
335 | this.value = value;
|
---|
336 | }
|
---|
337 |
|
---|
338 | public String getDisplayValue(boolean translated) {
|
---|
339 | return translated
|
---|
340 | ? Utils.firstNonNull(locale_display_value, tr(display_value), trc(value_context, value))
|
---|
341 | : Utils.firstNonNull(display_value, value);
|
---|
342 | }
|
---|
343 |
|
---|
344 | public String getShortDescription(boolean translated) {
|
---|
345 | return translated
|
---|
346 | ? Utils.firstNonNull(locale_short_description, tr(short_description))
|
---|
347 | : short_description;
|
---|
348 | }
|
---|
349 |
|
---|
350 | // toString is mainly used to initialize the Editor
|
---|
351 | @Override
|
---|
352 | public String toString() {
|
---|
353 | if (value.equals(DIFFERENT))
|
---|
354 | return DIFFERENT;
|
---|
355 | return getDisplayValue(true).replaceAll("<.*>", ""); // remove additional markup, e.g. <br>
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | public static class Text extends KeyedItem {
|
---|
360 |
|
---|
361 | public String locale_text;
|
---|
362 | public String default_;
|
---|
363 | public String originalValue;
|
---|
364 | public String use_last_as_default = "false";
|
---|
365 |
|
---|
366 | private JComponent value;
|
---|
367 |
|
---|
368 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
369 |
|
---|
370 | // find out if our key is already used in the selection.
|
---|
371 | Usage usage = determineTextUsage(sel, key);
|
---|
372 | AutoCompletingTextField textField = new AutoCompletingTextField();
|
---|
373 | initAutoCompletionField(textField, key);
|
---|
374 | if (usage.unused()){
|
---|
375 | if (!usage.hadKeys() || PROP_FILL_DEFAULT.get() || "force".equals(use_last_as_default)) {
|
---|
376 | // selected osm primitives are untagged or filling default values feature is enabled
|
---|
377 | if (!"false".equals(use_last_as_default) && lastValue.containsKey(key)) {
|
---|
378 | textField.setText(lastValue.get(key));
|
---|
379 | } else {
|
---|
380 | textField.setText(default_);
|
---|
381 | }
|
---|
382 | } else {
|
---|
383 | // selected osm primitives are tagged and filling default values feature is disabled
|
---|
384 | textField.setText("");
|
---|
385 | }
|
---|
386 | value = textField;
|
---|
387 | originalValue = null;
|
---|
388 | } else if (usage.hasUniqueValue()) {
|
---|
389 | // all objects use the same value
|
---|
390 | textField.setText(usage.getFirst());
|
---|
391 | value = textField;
|
---|
392 | originalValue = usage.getFirst();
|
---|
393 | } else {
|
---|
394 | // the objects have different values
|
---|
395 | JComboBox comboBox = new JComboBox(usage.values.toArray());
|
---|
396 | comboBox.setEditable(true);
|
---|
397 | comboBox.setEditor(textField);
|
---|
398 | comboBox.getEditor().setItem(DIFFERENT);
|
---|
399 | value=comboBox;
|
---|
400 | originalValue = DIFFERENT;
|
---|
401 | }
|
---|
402 | if(locale_text == null) {
|
---|
403 | if (text != null) {
|
---|
404 | if(text_context != null) {
|
---|
405 | locale_text = trc(text_context, fixPresetString(text));
|
---|
406 | } else {
|
---|
407 | locale_text = tr(fixPresetString(text));
|
---|
408 | }
|
---|
409 | }
|
---|
410 | }
|
---|
411 | p.add(new JLabel(locale_text+":"), GBC.std().insets(0,0,10,0));
|
---|
412 | p.add(value, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
413 | return true;
|
---|
414 | }
|
---|
415 |
|
---|
416 | @Override
|
---|
417 | public void addCommands(List<Tag> changedTags) {
|
---|
418 |
|
---|
419 | // return if unchanged
|
---|
420 | String v = (value instanceof JComboBox)
|
---|
421 | ? ((JComboBox) value).getEditor().getItem().toString()
|
---|
422 | : ((JTextField) value).getText();
|
---|
423 | v = v.trim();
|
---|
424 |
|
---|
425 | if (!"false".equals(use_last_as_default)) {
|
---|
426 | lastValue.put(key, v);
|
---|
427 | }
|
---|
428 | if (v.equals(originalValue) || (originalValue == null && v.length() == 0))
|
---|
429 | return;
|
---|
430 |
|
---|
431 | changedTags.add(new Tag(key, v));
|
---|
432 | }
|
---|
433 |
|
---|
434 | @Override
|
---|
435 | boolean requestFocusInWindow() {
|
---|
436 | return value.requestFocusInWindow();
|
---|
437 | }
|
---|
438 |
|
---|
439 | @Override
|
---|
440 | public MatchType getDefaultMatch() {
|
---|
441 | return MatchType.NONE;
|
---|
442 | }
|
---|
443 |
|
---|
444 | @Override
|
---|
445 | public Collection<String> getValues() {
|
---|
446 | if (default_ == null || default_.isEmpty()) {
|
---|
447 | return Collections.emptyList();
|
---|
448 | }
|
---|
449 | return Collections.singleton(default_);
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | public static class Check extends KeyedItem {
|
---|
454 |
|
---|
455 | public String locale_text;
|
---|
456 | public String value_on = OsmUtils.trueval;
|
---|
457 | public String value_off = OsmUtils.falseval;
|
---|
458 | public boolean default_ = false; // only used for tagless objects
|
---|
459 |
|
---|
460 | private QuadStateCheckBox check;
|
---|
461 | private QuadStateCheckBox.State initialState;
|
---|
462 | private boolean def;
|
---|
463 |
|
---|
464 | @Override public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
465 |
|
---|
466 | // find out if our key is already used in the selection.
|
---|
467 | Usage usage = determineBooleanUsage(sel, key);
|
---|
468 | def = default_;
|
---|
469 |
|
---|
470 | if(locale_text == null) {
|
---|
471 | if(text_context != null) {
|
---|
472 | locale_text = trc(text_context, fixPresetString(text));
|
---|
473 | } else {
|
---|
474 | locale_text = tr(fixPresetString(text));
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | String oneValue = null;
|
---|
479 | for (String s : usage.values) {
|
---|
480 | oneValue = s;
|
---|
481 | }
|
---|
482 | if (usage.values.size() < 2 && (oneValue == null || value_on.equals(oneValue) || value_off.equals(oneValue))) {
|
---|
483 | if (def && !PROP_FILL_DEFAULT.get()) {
|
---|
484 | // default is set and filling default values feature is disabled - check if all primitives are untagged
|
---|
485 | for (OsmPrimitive s : sel)
|
---|
486 | if(s.hasKeys()) {
|
---|
487 | def = false;
|
---|
488 | }
|
---|
489 | }
|
---|
490 |
|
---|
491 | // all selected objects share the same value which is either true or false or unset,
|
---|
492 | // we can display a standard check box.
|
---|
493 | initialState = value_on.equals(oneValue) ?
|
---|
494 | QuadStateCheckBox.State.SELECTED :
|
---|
495 | value_off.equals(oneValue) ?
|
---|
496 | QuadStateCheckBox.State.NOT_SELECTED :
|
---|
497 | def ? QuadStateCheckBox.State.SELECTED
|
---|
498 | : QuadStateCheckBox.State.UNSET;
|
---|
499 | check = new QuadStateCheckBox(locale_text, initialState,
|
---|
500 | new QuadStateCheckBox.State[] {
|
---|
501 | QuadStateCheckBox.State.SELECTED,
|
---|
502 | QuadStateCheckBox.State.NOT_SELECTED,
|
---|
503 | QuadStateCheckBox.State.UNSET });
|
---|
504 | } else {
|
---|
505 | def = false;
|
---|
506 | // the objects have different values, or one or more objects have something
|
---|
507 | // else than true/false. we display a quad-state check box
|
---|
508 | // in "partial" state.
|
---|
509 | initialState = QuadStateCheckBox.State.PARTIAL;
|
---|
510 | check = new QuadStateCheckBox(locale_text, QuadStateCheckBox.State.PARTIAL,
|
---|
511 | new QuadStateCheckBox.State[] {
|
---|
512 | QuadStateCheckBox.State.PARTIAL,
|
---|
513 | QuadStateCheckBox.State.SELECTED,
|
---|
514 | QuadStateCheckBox.State.NOT_SELECTED,
|
---|
515 | QuadStateCheckBox.State.UNSET });
|
---|
516 | }
|
---|
517 | p.add(check, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
518 | return true;
|
---|
519 | }
|
---|
520 |
|
---|
521 | @Override public void addCommands(List<Tag> changedTags) {
|
---|
522 | // if the user hasn't changed anything, don't create a command.
|
---|
523 | if (check.getState() == initialState && !def) return;
|
---|
524 |
|
---|
525 | // otherwise change things according to the selected value.
|
---|
526 | changedTags.add(new Tag(key,
|
---|
527 | check.getState() == QuadStateCheckBox.State.SELECTED ? value_on :
|
---|
528 | check.getState() == QuadStateCheckBox.State.NOT_SELECTED ? value_off :
|
---|
529 | null));
|
---|
530 | }
|
---|
531 | @Override boolean requestFocusInWindow() {return check.requestFocusInWindow();}
|
---|
532 |
|
---|
533 | @Override
|
---|
534 | public MatchType getDefaultMatch() {
|
---|
535 | return MatchType.NONE;
|
---|
536 | }
|
---|
537 |
|
---|
538 | @Override
|
---|
539 | public Collection<String> getValues() {
|
---|
540 | return Arrays.asList(value_on, value_off);
|
---|
541 | }
|
---|
542 | }
|
---|
543 |
|
---|
544 | public static abstract class ComboMultiSelect extends KeyedItem {
|
---|
545 |
|
---|
546 | public String locale_text;
|
---|
547 | public String values;
|
---|
548 | public String values_context;
|
---|
549 | public String display_values;
|
---|
550 | public String locale_display_values;
|
---|
551 | public String short_descriptions;
|
---|
552 | public String locale_short_descriptions;
|
---|
553 | public String default_;
|
---|
554 | public String delimiter = ";";
|
---|
555 | public String use_last_as_default = "false";
|
---|
556 |
|
---|
557 | protected JComponent component;
|
---|
558 | protected Map<String, PresetListEntry> lhm = new LinkedHashMap<String, PresetListEntry>();
|
---|
559 | private boolean initialized = false;
|
---|
560 | protected Usage usage;
|
---|
561 | protected Object originalValue;
|
---|
562 |
|
---|
563 | protected abstract Object getSelectedItem();
|
---|
564 | protected abstract void addToPanelAnchor(JPanel p, String def);
|
---|
565 |
|
---|
566 | protected char getDelChar() {
|
---|
567 | return delimiter.isEmpty() ? ';' : delimiter.charAt(0);
|
---|
568 | }
|
---|
569 |
|
---|
570 | @Override
|
---|
571 | public Collection<String> getValues() {
|
---|
572 | initListEntries();
|
---|
573 | return lhm.keySet();
|
---|
574 | }
|
---|
575 |
|
---|
576 | public Collection<String> getDisplayValues() {
|
---|
577 | initListEntries();
|
---|
578 | return Utils.transform(lhm.values(), new Utils.Function<PresetListEntry, String>() {
|
---|
579 |
|
---|
580 | @Override
|
---|
581 | public String apply(PresetListEntry x) {
|
---|
582 | return x.getDisplayValue(true);
|
---|
583 | }
|
---|
584 | });
|
---|
585 | }
|
---|
586 |
|
---|
587 | @Override
|
---|
588 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
589 |
|
---|
590 | initListEntries();
|
---|
591 |
|
---|
592 | // find out if our key is already used in the selection.
|
---|
593 | usage = determineTextUsage(sel, key);
|
---|
594 | if (!usage.hasUniqueValue() && !usage.unused()) {
|
---|
595 | lhm.put(DIFFERENT, new PresetListEntry(DIFFERENT));
|
---|
596 | }
|
---|
597 |
|
---|
598 | p.add(new JLabel(tr("{0}:", locale_text)), GBC.std().insets(0, 0, 10, 0));
|
---|
599 | addToPanelAnchor(p, default_);
|
---|
600 |
|
---|
601 | return true;
|
---|
602 |
|
---|
603 | }
|
---|
604 |
|
---|
605 | private void initListEntries() {
|
---|
606 | if (initialized) {
|
---|
607 | lhm.remove(DIFFERENT); // possibly added in #addToPanel
|
---|
608 | return;
|
---|
609 | } else if (lhm.isEmpty()) {
|
---|
610 | initListEntriesFromAttributes();
|
---|
611 | } else {
|
---|
612 | if (values != null) {
|
---|
613 | System.err.println(tr("Warning in tagging preset \"{0}-{1}\": "
|
---|
614 | + "Ignoring ''{2}'' attribute as ''{3}'' elements are given.",
|
---|
615 | key, text, "values", "list_entry"));
|
---|
616 | }
|
---|
617 | if (display_values != null || locale_display_values != null) {
|
---|
618 | System.err.println(tr("Warning in tagging preset \"{0}-{1}\": "
|
---|
619 | + "Ignoring ''{2}'' attribute as ''{3}'' elements are given.",
|
---|
620 | key, text, "display_values", "list_entry"));
|
---|
621 | }
|
---|
622 | if (short_descriptions != null || locale_short_descriptions != null) {
|
---|
623 | System.err.println(tr("Warning in tagging preset \"{0}-{1}\": "
|
---|
624 | + "Ignoring ''{2}'' attribute as ''{3}'' elements are given.",
|
---|
625 | key, text, "short_descriptions", "list_entry"));
|
---|
626 | }
|
---|
627 | for (PresetListEntry e : lhm.values()) {
|
---|
628 | if (e.value_context == null) {
|
---|
629 | e.value_context = values_context;
|
---|
630 | }
|
---|
631 | }
|
---|
632 | }
|
---|
633 | if (locale_text == null) {
|
---|
634 | locale_text = trc(text_context, fixPresetString(text));
|
---|
635 | }
|
---|
636 | initialized = true;
|
---|
637 | }
|
---|
638 |
|
---|
639 | private String[] initListEntriesFromAttributes() {
|
---|
640 | char delChar = getDelChar();
|
---|
641 |
|
---|
642 | String[] value_array = splitEscaped(delChar, values);
|
---|
643 |
|
---|
644 | final String displ = Utils.firstNonNull(locale_display_values, display_values);
|
---|
645 | String[] display_array = displ == null ? value_array : splitEscaped(delChar, displ);
|
---|
646 |
|
---|
647 | final String descr = Utils.firstNonNull(locale_short_descriptions, short_descriptions);
|
---|
648 | String[] short_descriptions_array = descr == null ? null : splitEscaped(delChar, descr);
|
---|
649 |
|
---|
650 | if (display_array.length != value_array.length) {
|
---|
651 | System.err.println(tr("Broken tagging preset \"{0}-{1}\" - number of items in ''display_values'' must be the same as in ''values''", key, text));
|
---|
652 | display_array = value_array;
|
---|
653 | }
|
---|
654 |
|
---|
655 | if (short_descriptions_array != null && short_descriptions_array.length != value_array.length) {
|
---|
656 | System.err.println(tr("Broken tagging preset \"{0}-{1}\" - number of items in ''short_descriptions'' must be the same as in ''values''", key, text));
|
---|
657 | short_descriptions_array = null;
|
---|
658 | }
|
---|
659 |
|
---|
660 | for (int i = 0; i < value_array.length; i++) {
|
---|
661 | final PresetListEntry e = new PresetListEntry(value_array[i]);
|
---|
662 | e.locale_display_value = locale_display_values != null
|
---|
663 | ? display_array[i]
|
---|
664 | : trc(values_context, fixPresetString(display_array[i]));
|
---|
665 | if (short_descriptions_array != null) {
|
---|
666 | e.locale_short_description = locale_short_descriptions != null
|
---|
667 | ? short_descriptions_array[i]
|
---|
668 | : tr(fixPresetString(short_descriptions_array[i]));
|
---|
669 | }
|
---|
670 | lhm.put(value_array[i], e);
|
---|
671 | display_array[i] = e.getDisplayValue(true);
|
---|
672 | }
|
---|
673 |
|
---|
674 | return display_array;
|
---|
675 | }
|
---|
676 |
|
---|
677 | protected String getDisplayIfNull(String display) {
|
---|
678 | return display;
|
---|
679 | }
|
---|
680 |
|
---|
681 | @Override
|
---|
682 | public void addCommands(List<Tag> changedTags) {
|
---|
683 | Object obj = getSelectedItem();
|
---|
684 | String display = (obj == null) ? null : obj.toString();
|
---|
685 | String value = null;
|
---|
686 | if (display == null) {
|
---|
687 | display = getDisplayIfNull(display);
|
---|
688 | }
|
---|
689 |
|
---|
690 | if (display != null) {
|
---|
691 | for (String key : lhm.keySet()) {
|
---|
692 | String k = lhm.get(key).toString();
|
---|
693 | if (k != null && k.equals(display)) {
|
---|
694 | value = key;
|
---|
695 | break;
|
---|
696 | }
|
---|
697 | }
|
---|
698 | if (value == null) {
|
---|
699 | value = display;
|
---|
700 | }
|
---|
701 | } else {
|
---|
702 | value = "";
|
---|
703 | }
|
---|
704 | value = value.trim();
|
---|
705 |
|
---|
706 | // no change if same as before
|
---|
707 | if (originalValue == null) {
|
---|
708 | if (value.length() == 0)
|
---|
709 | return;
|
---|
710 | } else if (value.equals(originalValue.toString()))
|
---|
711 | return;
|
---|
712 |
|
---|
713 | if (!"false".equals(use_last_as_default)) {
|
---|
714 | lastValue.put(key, value);
|
---|
715 | }
|
---|
716 | changedTags.add(new Tag(key, value));
|
---|
717 | }
|
---|
718 |
|
---|
719 | public void addListEntry(PresetListEntry e) {
|
---|
720 | lhm.put(e.value, e);
|
---|
721 | }
|
---|
722 |
|
---|
723 | public void addListEntries(Collection<PresetListEntry> e) {
|
---|
724 | for (PresetListEntry i : e) {
|
---|
725 | addListEntry(i);
|
---|
726 | }
|
---|
727 | }
|
---|
728 |
|
---|
729 | @Override
|
---|
730 | boolean requestFocusInWindow() {
|
---|
731 | return component.requestFocusInWindow();
|
---|
732 | }
|
---|
733 |
|
---|
734 | protected ListCellRenderer getListCellRenderer() {
|
---|
735 | return new ListCellRenderer() {
|
---|
736 |
|
---|
737 | JLabel lbl = new JLabel();
|
---|
738 | JComponent dummy = new JComponent() {
|
---|
739 | };
|
---|
740 |
|
---|
741 | public Component getListCellRendererComponent(
|
---|
742 | JList list,
|
---|
743 | Object value,
|
---|
744 | int index,
|
---|
745 | boolean isSelected,
|
---|
746 | boolean cellHasFocus) {
|
---|
747 | if (isSelected) {
|
---|
748 | lbl.setBackground(list.getSelectionBackground());
|
---|
749 | lbl.setForeground(list.getSelectionForeground());
|
---|
750 | } else {
|
---|
751 | lbl.setBackground(list.getBackground());
|
---|
752 | lbl.setForeground(list.getForeground());
|
---|
753 | }
|
---|
754 |
|
---|
755 | PresetListEntry item = (PresetListEntry) value;
|
---|
756 | lbl.setOpaque(true);
|
---|
757 | lbl.setFont(lbl.getFont().deriveFont(Font.PLAIN));
|
---|
758 | lbl.setText("<html>" + item.getListDisplay() + "</html>");
|
---|
759 | lbl.setIcon(item.getIcon());
|
---|
760 | lbl.setEnabled(list.isEnabled());
|
---|
761 | // We do not want the editor to have the maximum height of all
|
---|
762 | // entries. Return a dummy with bogus height.
|
---|
763 | if (index == -1) {
|
---|
764 | dummy.setPreferredSize(new Dimension(lbl.getPreferredSize().width, 10));
|
---|
765 | return dummy;
|
---|
766 | }
|
---|
767 | return lbl;
|
---|
768 | }
|
---|
769 | };
|
---|
770 | }
|
---|
771 |
|
---|
772 | @Override
|
---|
773 | public MatchType getDefaultMatch() {
|
---|
774 | return MatchType.NONE;
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | public static class Combo extends ComboMultiSelect {
|
---|
779 |
|
---|
780 | public boolean editable = true;
|
---|
781 | protected JComboBox combo;
|
---|
782 |
|
---|
783 | public Combo() {
|
---|
784 | delimiter = ",";
|
---|
785 | }
|
---|
786 |
|
---|
787 | @Override
|
---|
788 | protected void addToPanelAnchor(JPanel p, String def) {
|
---|
789 | if (!usage.unused()) {
|
---|
790 | for (String s : usage.values) {
|
---|
791 | if (!lhm.containsKey(s)) {
|
---|
792 | lhm.put(s, new PresetListEntry(s));
|
---|
793 | }
|
---|
794 | }
|
---|
795 | }
|
---|
796 | if (def != null && !lhm.containsKey(def)) {
|
---|
797 | lhm.put(def, new PresetListEntry(def));
|
---|
798 | }
|
---|
799 | lhm.put("", new PresetListEntry(""));
|
---|
800 |
|
---|
801 | combo = new JComboBox(lhm.values().toArray());
|
---|
802 | component = combo;
|
---|
803 | combo.setRenderer(getListCellRenderer());
|
---|
804 | combo.setEditable(editable);
|
---|
805 | combo.setMaximumRowCount(13);
|
---|
806 | AutoCompletingTextField tf = new AutoCompletingTextField();
|
---|
807 | initAutoCompletionField(tf, key);
|
---|
808 | tf.getAutoCompletionList().add(getDisplayValues(), AutoCompletionItemPritority.IS_IN_STANDARD);
|
---|
809 | combo.setEditor(tf);
|
---|
810 |
|
---|
811 | if (usage.hasUniqueValue()) {
|
---|
812 | // all items have the same value (and there were no unset items)
|
---|
813 | originalValue = lhm.get(usage.getFirst());
|
---|
814 | combo.setSelectedItem(originalValue);
|
---|
815 | } else if (def != null && usage.unused()) {
|
---|
816 | // default is set and all items were unset
|
---|
817 | if (!usage.hadKeys() || PROP_FILL_DEFAULT.get() || "force".equals(use_last_as_default)) {
|
---|
818 | // selected osm primitives are untagged or filling default feature is enabled
|
---|
819 | combo.setSelectedItem(lhm.get(def).getDisplayValue(true));
|
---|
820 | } else {
|
---|
821 | // selected osm primitives are tagged and filling default feature is disabled
|
---|
822 | combo.setSelectedItem("");
|
---|
823 | }
|
---|
824 | originalValue = lhm.get(DIFFERENT);
|
---|
825 | } else if (usage.unused()) {
|
---|
826 | // all items were unset (and so is default)
|
---|
827 | originalValue = lhm.get("");
|
---|
828 | combo.setSelectedItem(originalValue);
|
---|
829 | } else {
|
---|
830 | originalValue = lhm.get(DIFFERENT);
|
---|
831 | combo.setSelectedItem(originalValue);
|
---|
832 | }
|
---|
833 | p.add(combo, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
834 |
|
---|
835 | }
|
---|
836 |
|
---|
837 | @Override
|
---|
838 | protected Object getSelectedItem() {
|
---|
839 | return combo.getSelectedItem();
|
---|
840 |
|
---|
841 | }
|
---|
842 |
|
---|
843 | @Override
|
---|
844 | protected String getDisplayIfNull(String display) {
|
---|
845 | if (combo.isEditable())
|
---|
846 | return combo.getEditor().getItem().toString();
|
---|
847 | else
|
---|
848 | return display;
|
---|
849 |
|
---|
850 | }
|
---|
851 | }
|
---|
852 |
|
---|
853 | /**
|
---|
854 | * Class that allows list values to be assigned and retrieved as a comma-delimited
|
---|
855 | * string.
|
---|
856 | */
|
---|
857 | public static class ConcatenatingJList extends JList {
|
---|
858 | private String delimiter;
|
---|
859 | public ConcatenatingJList(String del, Object[] o) {
|
---|
860 | super(o);
|
---|
861 | delimiter = del;
|
---|
862 | }
|
---|
863 | public void setSelectedItem(Object o) {
|
---|
864 | if (o == null) {
|
---|
865 | clearSelection();
|
---|
866 | } else {
|
---|
867 | String s = o.toString();
|
---|
868 | HashSet<String> parts = new HashSet<String>(Arrays.asList(s.split(delimiter)));
|
---|
869 | ListModel lm = getModel();
|
---|
870 | int[] intParts = new int[lm.getSize()];
|
---|
871 | int j = 0;
|
---|
872 | for (int i = 0; i < lm.getSize(); i++) {
|
---|
873 | if (parts.contains((((PresetListEntry)lm.getElementAt(i)).value))) {
|
---|
874 | intParts[j++]=i;
|
---|
875 | }
|
---|
876 | }
|
---|
877 | setSelectedIndices(Arrays.copyOf(intParts, j));
|
---|
878 | // check if we have actually managed to represent the full
|
---|
879 | // value with our presets. if not, cop out; we will not offer
|
---|
880 | // a selection list that threatens to ruin the value.
|
---|
881 | setEnabled(s.equals(getSelectedItem()));
|
---|
882 | }
|
---|
883 | }
|
---|
884 | public String getSelectedItem() {
|
---|
885 | ListModel lm = getModel();
|
---|
886 | int[] si = getSelectedIndices();
|
---|
887 | StringBuilder builder = new StringBuilder();
|
---|
888 | for (int i=0; i<si.length; i++) {
|
---|
889 | if (i>0) {
|
---|
890 | builder.append(delimiter);
|
---|
891 | }
|
---|
892 | builder.append(((PresetListEntry)lm.getElementAt(si[i])).value);
|
---|
893 | }
|
---|
894 | return builder.toString();
|
---|
895 | }
|
---|
896 | }
|
---|
897 |
|
---|
898 | public static class MultiSelect extends ComboMultiSelect {
|
---|
899 |
|
---|
900 | public long rows = -1;
|
---|
901 | protected ConcatenatingJList list;
|
---|
902 |
|
---|
903 | @Override
|
---|
904 | protected void addToPanelAnchor(JPanel p, String def) {
|
---|
905 | list = new ConcatenatingJList(delimiter, lhm.values().toArray());
|
---|
906 | component = list;
|
---|
907 | ListCellRenderer renderer = getListCellRenderer();
|
---|
908 | list.setCellRenderer(renderer);
|
---|
909 |
|
---|
910 | if (usage.hasUniqueValue() && !usage.unused()) {
|
---|
911 | originalValue = usage.getFirst();
|
---|
912 | list.setSelectedItem(originalValue);
|
---|
913 | } else if (def != null && !usage.hadKeys() || PROP_FILL_DEFAULT.get() || "force".equals(use_last_as_default)) {
|
---|
914 | originalValue = DIFFERENT;
|
---|
915 | list.setSelectedItem(def);
|
---|
916 | } else if (usage.unused()) {
|
---|
917 | originalValue = null;
|
---|
918 | list.setSelectedItem(originalValue);
|
---|
919 | } else {
|
---|
920 | originalValue = DIFFERENT;
|
---|
921 | list.setSelectedItem(originalValue);
|
---|
922 | }
|
---|
923 |
|
---|
924 | JScrollPane sp = new JScrollPane(list);
|
---|
925 | // if a number of rows has been specified in the preset,
|
---|
926 | // modify preferred height of scroll pane to match that row count.
|
---|
927 | if (rows != -1) {
|
---|
928 | double height = renderer.getListCellRendererComponent(list,
|
---|
929 | new PresetListEntry("x"), 0, false, false).getPreferredSize().getHeight() * rows;
|
---|
930 | sp.setPreferredSize(new Dimension((int) sp.getPreferredSize().getWidth(), (int) height));
|
---|
931 | }
|
---|
932 | p.add(sp, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
933 |
|
---|
934 |
|
---|
935 | }
|
---|
936 |
|
---|
937 | @Override
|
---|
938 | protected Object getSelectedItem() {
|
---|
939 | return list.getSelectedItem();
|
---|
940 | }
|
---|
941 | }
|
---|
942 |
|
---|
943 | /**
|
---|
944 | * allow escaped comma in comma separated list:
|
---|
945 | * "A\, B\, C,one\, two" --> ["A, B, C", "one, two"]
|
---|
946 | * @param delimiter the delimiter, e.g. a comma. separates the entries and
|
---|
947 | * must be escaped within one entry
|
---|
948 | * @param s the string
|
---|
949 | */
|
---|
950 | private static String[] splitEscaped(char delimiter, String s) {
|
---|
951 | if (s == null) {
|
---|
952 | return new String[0];
|
---|
953 | }
|
---|
954 | List<String> result = new ArrayList<String>();
|
---|
955 | boolean backslash = false;
|
---|
956 | StringBuilder item = new StringBuilder();
|
---|
957 | for (int i=0; i<s.length(); i++) {
|
---|
958 | char ch = s.charAt(i);
|
---|
959 | if (backslash) {
|
---|
960 | item.append(ch);
|
---|
961 | backslash = false;
|
---|
962 | } else if (ch == '\\') {
|
---|
963 | backslash = true;
|
---|
964 | } else if (ch == delimiter) {
|
---|
965 | result.add(item.toString());
|
---|
966 | item.setLength(0);
|
---|
967 | } else {
|
---|
968 | item.append(ch);
|
---|
969 | }
|
---|
970 | }
|
---|
971 | if (item.length() > 0) {
|
---|
972 | result.add(item.toString());
|
---|
973 | }
|
---|
974 | return result.toArray(new String[result.size()]);
|
---|
975 | }
|
---|
976 |
|
---|
977 | public static class Label extends Item {
|
---|
978 |
|
---|
979 | public String text;
|
---|
980 | public String text_context;
|
---|
981 | public String locale_text;
|
---|
982 |
|
---|
983 | @Override
|
---|
984 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
985 | if (locale_text == null) {
|
---|
986 | if (text_context != null) {
|
---|
987 | locale_text = trc(text_context, fixPresetString(text));
|
---|
988 | } else {
|
---|
989 | locale_text = tr(fixPresetString(text));
|
---|
990 | }
|
---|
991 | }
|
---|
992 | p.add(new JLabel(locale_text), GBC.eol());
|
---|
993 | return false;
|
---|
994 | }
|
---|
995 |
|
---|
996 | @Override
|
---|
997 | public void addCommands(List<Tag> changedTags) {
|
---|
998 | }
|
---|
999 | }
|
---|
1000 |
|
---|
1001 | public static class Link extends Item {
|
---|
1002 |
|
---|
1003 | public String href;
|
---|
1004 | public String text;
|
---|
1005 | public String text_context;
|
---|
1006 | public String locale_text;
|
---|
1007 | public String locale_href;
|
---|
1008 |
|
---|
1009 | @Override
|
---|
1010 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
1011 | if (locale_text == null) {
|
---|
1012 | if (text == null) {
|
---|
1013 | locale_text = tr("More information about this feature");
|
---|
1014 | } else if (text_context != null) {
|
---|
1015 | locale_text = trc(text_context, fixPresetString(text));
|
---|
1016 | } else {
|
---|
1017 | locale_text = tr(fixPresetString(text));
|
---|
1018 | }
|
---|
1019 | }
|
---|
1020 | String url = locale_href;
|
---|
1021 | if (url == null) {
|
---|
1022 | url = href;
|
---|
1023 | }
|
---|
1024 | if (url != null) {
|
---|
1025 | p.add(new UrlLabel(url, locale_text, 2), GBC.eol().anchor(GBC.WEST));
|
---|
1026 | }
|
---|
1027 | return false;
|
---|
1028 | }
|
---|
1029 |
|
---|
1030 | @Override
|
---|
1031 | public void addCommands(List<Tag> changedTags) {
|
---|
1032 | }
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | public static class Role {
|
---|
1036 | public EnumSet<PresetType> types;
|
---|
1037 | public String key;
|
---|
1038 | public String text;
|
---|
1039 | public String text_context;
|
---|
1040 | public String locale_text;
|
---|
1041 |
|
---|
1042 | public boolean required = false;
|
---|
1043 | public long count = 0;
|
---|
1044 |
|
---|
1045 | public void setType(String types) throws SAXException {
|
---|
1046 | this.types = TaggingPreset.getType(types);
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | public void setRequisite(String str) throws SAXException {
|
---|
1050 | if("required".equals(str)) {
|
---|
1051 | required = true;
|
---|
1052 | } else if(!"optional".equals(str))
|
---|
1053 | throw new SAXException(tr("Unknown requisite: {0}", str));
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | /* return either argument, the highest possible value or the lowest
|
---|
1057 | allowed value */
|
---|
1058 | public long getValidCount(long c)
|
---|
1059 | {
|
---|
1060 | if(count > 0 && !required)
|
---|
1061 | return c != 0 ? count : 0;
|
---|
1062 | else if(count > 0)
|
---|
1063 | return count;
|
---|
1064 | else if(!required)
|
---|
1065 | return c != 0 ? c : 0;
|
---|
1066 | else
|
---|
1067 | return c != 0 ? c : 1;
|
---|
1068 | }
|
---|
1069 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
1070 | String cstring;
|
---|
1071 | if(count > 0 && !required) {
|
---|
1072 | cstring = "0,"+String.valueOf(count);
|
---|
1073 | } else if(count > 0) {
|
---|
1074 | cstring = String.valueOf(count);
|
---|
1075 | } else if(!required) {
|
---|
1076 | cstring = "0-...";
|
---|
1077 | } else {
|
---|
1078 | cstring = "1-...";
|
---|
1079 | }
|
---|
1080 | if(locale_text == null) {
|
---|
1081 | if (text != null) {
|
---|
1082 | if(text_context != null) {
|
---|
1083 | locale_text = trc(text_context, fixPresetString(text));
|
---|
1084 | } else {
|
---|
1085 | locale_text = tr(fixPresetString(text));
|
---|
1086 | }
|
---|
1087 | }
|
---|
1088 | }
|
---|
1089 | p.add(new JLabel(locale_text+":"), GBC.std().insets(0,0,10,0));
|
---|
1090 | p.add(new JLabel(key), GBC.std().insets(0,0,10,0));
|
---|
1091 | p.add(new JLabel(cstring), types == null ? GBC.eol() : GBC.std().insets(0,0,10,0));
|
---|
1092 | if(types != null){
|
---|
1093 | JPanel pp = new JPanel();
|
---|
1094 | for(PresetType t : types) {
|
---|
1095 | pp.add(new JLabel(ImageProvider.get(t.getIconName())));
|
---|
1096 | }
|
---|
1097 | p.add(pp, GBC.eol());
|
---|
1098 | }
|
---|
1099 | return true;
|
---|
1100 | }
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | public static class Roles extends Item {
|
---|
1104 |
|
---|
1105 | public List<Role> roles = new LinkedList<Role>();
|
---|
1106 |
|
---|
1107 | @Override
|
---|
1108 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
1109 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
1110 | if (roles.size() > 0) {
|
---|
1111 | JPanel proles = new JPanel(new GridBagLayout());
|
---|
1112 | proles.add(new JLabel(tr("Available roles")), GBC.std().insets(0, 0, 10, 0));
|
---|
1113 | proles.add(new JLabel(tr("role")), GBC.std().insets(0, 0, 10, 0));
|
---|
1114 | proles.add(new JLabel(tr("count")), GBC.std().insets(0, 0, 10, 0));
|
---|
1115 | proles.add(new JLabel(tr("elements")), GBC.eol());
|
---|
1116 | for (Role i : roles) {
|
---|
1117 | i.addToPanel(proles, sel);
|
---|
1118 | }
|
---|
1119 | p.add(proles, GBC.eol());
|
---|
1120 | }
|
---|
1121 | return false;
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | @Override
|
---|
1125 | public void addCommands(List<Tag> changedTags) {
|
---|
1126 | }
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | public static class Optional extends Item {
|
---|
1130 |
|
---|
1131 | // TODO: Draw a box around optional stuff
|
---|
1132 | @Override
|
---|
1133 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
1134 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
1135 | p.add(new JLabel(tr("Optional Attributes:")), GBC.eol());
|
---|
1136 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
1137 | return false;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | @Override
|
---|
1141 | public void addCommands(List<Tag> changedTags) {
|
---|
1142 | }
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | public static class Space extends Item {
|
---|
1146 |
|
---|
1147 | @Override
|
---|
1148 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
1149 | p.add(new JLabel(" "), GBC.eol()); // space
|
---|
1150 | return false;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | @Override
|
---|
1154 | public void addCommands(List<Tag> changedTags) {
|
---|
1155 | }
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 | public static class Key extends KeyedItem {
|
---|
1159 |
|
---|
1160 | public String value;
|
---|
1161 |
|
---|
1162 | @Override
|
---|
1163 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) {
|
---|
1164 | return false;
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | @Override
|
---|
1168 | public void addCommands(List<Tag> changedTags) {
|
---|
1169 | changedTags.add(new Tag(key, value));
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | @Override
|
---|
1173 | public MatchType getDefaultMatch() {
|
---|
1174 | return MatchType.KEY_VALUE;
|
---|
1175 | }
|
---|
1176 |
|
---|
1177 | @Override
|
---|
1178 | public Collection<String> getValues() {
|
---|
1179 | return Collections.singleton(value);
|
---|
1180 | }
|
---|
1181 | }
|
---|
1182 |
|
---|
1183 | /**
|
---|
1184 | * The types as preparsed collection.
|
---|
1185 | */
|
---|
1186 | public EnumSet<PresetType> types;
|
---|
1187 | public List<Item> data = new LinkedList<Item>();
|
---|
1188 | public TemplateEntry nameTemplate;
|
---|
1189 | public Match nameTemplateFilter;
|
---|
1190 | private static HashMap<String,String> lastValue = new HashMap<String,String>();
|
---|
1191 |
|
---|
1192 | /**
|
---|
1193 | * Create an empty tagging preset. This will not have any items and
|
---|
1194 | * will be an empty string as text. createPanel will return null.
|
---|
1195 | * Use this as default item for "do not select anything".
|
---|
1196 | */
|
---|
1197 | public TaggingPreset() {
|
---|
1198 | MapView.addLayerChangeListener(this);
|
---|
1199 | updateEnabledState();
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | /**
|
---|
1203 | * Change the display name without changing the toolbar value.
|
---|
1204 | */
|
---|
1205 | public void setDisplayName() {
|
---|
1206 | putValue(Action.NAME, getName());
|
---|
1207 | putValue("toolbar", "tagging_" + getRawName());
|
---|
1208 | putValue(OPTIONAL_TOOLTIP_TEXT, (group != null ?
|
---|
1209 | tr("Use preset ''{0}'' of group ''{1}''", getLocaleName(), group.getName()) :
|
---|
1210 | tr("Use preset ''{0}''", getLocaleName())));
|
---|
1211 | }
|
---|
1212 |
|
---|
1213 | public String getLocaleName() {
|
---|
1214 | if(locale_name == null) {
|
---|
1215 | if(name_context != null) {
|
---|
1216 | locale_name = trc(name_context, fixPresetString(name));
|
---|
1217 | } else {
|
---|
1218 | locale_name = tr(fixPresetString(name));
|
---|
1219 | }
|
---|
1220 | }
|
---|
1221 | return locale_name;
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | public String getName() {
|
---|
1225 | return group != null ? group.getName() + "/" + getLocaleName() : getLocaleName();
|
---|
1226 | }
|
---|
1227 | public String getRawName() {
|
---|
1228 | return group != null ? group.getRawName() + "/" + name : name;
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 | protected static ImageIcon loadImageIcon(String iconName, File zipIcons) {
|
---|
1232 | final Collection<String> s = Main.pref.getCollection("taggingpreset.icon.sources", null);
|
---|
1233 | return new ImageProvider(iconName).setDirs(s).setId("presets").setArchive(zipIcons).setOptional(true).get();
|
---|
1234 | }
|
---|
1235 |
|
---|
1236 | /*
|
---|
1237 | * Called from the XML parser to set the icon.
|
---|
1238 | * This task is performed in the background in order to speedup startup.
|
---|
1239 | *
|
---|
1240 | * FIXME for Java 1.6 - use 24x24 icons for LARGE_ICON_KEY (button bar)
|
---|
1241 | * and the 16x16 icons for SMALL_ICON.
|
---|
1242 | */
|
---|
1243 | public void setIcon(final String iconName) {
|
---|
1244 | final File zipIcons = TaggingPreset.zipIcons;
|
---|
1245 | Main.worker.submit(new Runnable() {
|
---|
1246 |
|
---|
1247 | @Override
|
---|
1248 | public void run() {
|
---|
1249 | ImageIcon icon = loadImageIcon(iconName, zipIcons);
|
---|
1250 | if (icon == null) {
|
---|
1251 | System.out.println("Could not get presets icon " + iconName);
|
---|
1252 | icon = new ImageIcon(iconName);
|
---|
1253 | }
|
---|
1254 | if (Math.max(icon.getIconHeight(), icon.getIconWidth()) != 16) {
|
---|
1255 | icon = new ImageIcon(icon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH));
|
---|
1256 | }
|
---|
1257 | putValue(Action.SMALL_ICON, icon);
|
---|
1258 | }
|
---|
1259 | });
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | // cache the parsing of types using a LRU cache (http://java-planet.blogspot.com/2005/08/how-to-set-up-simple-lru-cache-using.html)
|
---|
1263 | private static final Map<String,EnumSet<PresetType>> typeCache =
|
---|
1264 | new LinkedHashMap<String, EnumSet<PresetType>>(16, 1.1f, true);
|
---|
1265 |
|
---|
1266 | static public EnumSet<PresetType> getType(String types) throws SAXException {
|
---|
1267 | if (typeCache.containsKey(types)) {
|
---|
1268 | return typeCache.get(types);
|
---|
1269 | }
|
---|
1270 | EnumSet<PresetType> result = EnumSet.noneOf(PresetType.class);
|
---|
1271 | for (String type : Arrays.asList(types.split(","))) {
|
---|
1272 | try {
|
---|
1273 | PresetType presetType = PresetType.valueOf(type.toUpperCase());
|
---|
1274 | result.add(presetType);
|
---|
1275 | } catch (IllegalArgumentException e) {
|
---|
1276 | throw new SAXException(tr("Unknown type: {0}", type));
|
---|
1277 | }
|
---|
1278 | }
|
---|
1279 | typeCache.put(types, result);
|
---|
1280 | return result;
|
---|
1281 | }
|
---|
1282 |
|
---|
1283 | /*
|
---|
1284 | * Called from the XML parser to set the types this preset affects.
|
---|
1285 | */
|
---|
1286 | public void setType(String types) throws SAXException {
|
---|
1287 | this.types = getType(types);
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | public void setName_template(String pattern) throws SAXException {
|
---|
1291 | try {
|
---|
1292 | this.nameTemplate = new TemplateParser(pattern).parse();
|
---|
1293 | } catch (ParseError e) {
|
---|
1294 | System.err.println("Error while parsing " + pattern + ": " + e.getMessage());
|
---|
1295 | throw new SAXException(e);
|
---|
1296 | }
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | public void setName_template_filter(String filter) throws SAXException {
|
---|
1300 | try {
|
---|
1301 | this.nameTemplateFilter = SearchCompiler.compile(filter, false, false);
|
---|
1302 | } catch (org.openstreetmap.josm.actions.search.SearchCompiler.ParseError e) {
|
---|
1303 | System.err.println("Error while parsing" + filter + ": " + e.getMessage());
|
---|
1304 | throw new SAXException(e);
|
---|
1305 | }
|
---|
1306 | }
|
---|
1307 |
|
---|
1308 |
|
---|
1309 | public static List<TaggingPreset> readAll(Reader in, boolean validate) throws SAXException {
|
---|
1310 | XmlObjectParser parser = new XmlObjectParser();
|
---|
1311 | parser.mapOnStart("item", TaggingPreset.class);
|
---|
1312 | parser.mapOnStart("separator", TaggingPresetSeparator.class);
|
---|
1313 | parser.mapBoth("group", TaggingPresetMenu.class);
|
---|
1314 | parser.map("text", Text.class);
|
---|
1315 | parser.map("link", Link.class);
|
---|
1316 | parser.mapOnStart("optional", Optional.class);
|
---|
1317 | parser.mapOnStart("roles", Roles.class);
|
---|
1318 | parser.map("role", Role.class);
|
---|
1319 | parser.map("check", Check.class);
|
---|
1320 | parser.map("combo", Combo.class);
|
---|
1321 | parser.map("multiselect", MultiSelect.class);
|
---|
1322 | parser.map("label", Label.class);
|
---|
1323 | parser.map("space", Space.class);
|
---|
1324 | parser.map("key", Key.class);
|
---|
1325 | parser.map("list_entry", PresetListEntry.class);
|
---|
1326 | LinkedList<TaggingPreset> all = new LinkedList<TaggingPreset>();
|
---|
1327 | TaggingPresetMenu lastmenu = null;
|
---|
1328 | Roles lastrole = null;
|
---|
1329 | List<PresetListEntry> listEntries = new LinkedList<PresetListEntry>();
|
---|
1330 |
|
---|
1331 | if (validate) {
|
---|
1332 | parser.startWithValidation(in, "http://josm.openstreetmap.de/tagging-preset-1.0", "resource://data/tagging-preset.xsd");
|
---|
1333 | } else {
|
---|
1334 | parser.start(in);
|
---|
1335 | }
|
---|
1336 | while(parser.hasNext()) {
|
---|
1337 | Object o = parser.next();
|
---|
1338 | if (o instanceof TaggingPresetMenu) {
|
---|
1339 | TaggingPresetMenu tp = (TaggingPresetMenu) o;
|
---|
1340 | if(tp == lastmenu) {
|
---|
1341 | lastmenu = tp.group;
|
---|
1342 | } else
|
---|
1343 | {
|
---|
1344 | tp.group = lastmenu;
|
---|
1345 | tp.setDisplayName();
|
---|
1346 | lastmenu = tp;
|
---|
1347 | all.add(tp);
|
---|
1348 |
|
---|
1349 | }
|
---|
1350 | lastrole = null;
|
---|
1351 | } else if (o instanceof TaggingPresetSeparator) {
|
---|
1352 | TaggingPresetSeparator tp = (TaggingPresetSeparator) o;
|
---|
1353 | tp.group = lastmenu;
|
---|
1354 | all.add(tp);
|
---|
1355 | lastrole = null;
|
---|
1356 | } else if (o instanceof TaggingPreset) {
|
---|
1357 | TaggingPreset tp = (TaggingPreset) o;
|
---|
1358 | tp.group = lastmenu;
|
---|
1359 | tp.setDisplayName();
|
---|
1360 | all.add(tp);
|
---|
1361 | lastrole = null;
|
---|
1362 | } else {
|
---|
1363 | if (all.size() != 0) {
|
---|
1364 | if (o instanceof Roles) {
|
---|
1365 | all.getLast().data.add((Item) o);
|
---|
1366 | lastrole = (Roles) o;
|
---|
1367 | } else if (o instanceof Role) {
|
---|
1368 | if (lastrole == null) {
|
---|
1369 | throw new SAXException(tr("Preset role element without parent"));
|
---|
1370 | }
|
---|
1371 | lastrole.roles.add((Role) o);
|
---|
1372 | } else if (o instanceof PresetListEntry) {
|
---|
1373 | listEntries.add((PresetListEntry) o);
|
---|
1374 | } else {
|
---|
1375 | all.getLast().data.add((Item) o);
|
---|
1376 | if (o instanceof ComboMultiSelect) {
|
---|
1377 | ((ComboMultiSelect) o).addListEntries(listEntries);
|
---|
1378 | }
|
---|
1379 | listEntries = new LinkedList<PresetListEntry>();
|
---|
1380 | lastrole = null;
|
---|
1381 | }
|
---|
1382 | } else
|
---|
1383 | throw new SAXException(tr("Preset sub element without parent"));
|
---|
1384 | }
|
---|
1385 | }
|
---|
1386 | return all;
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | public static Collection<TaggingPreset> readAll(String source, boolean validate) throws SAXException, IOException {
|
---|
1390 | Collection<TaggingPreset> tp;
|
---|
1391 | MirroredInputStream s = new MirroredInputStream(source);
|
---|
1392 | try {
|
---|
1393 | InputStream zip = s.getZipEntry("xml","preset");
|
---|
1394 | if(zip != null) {
|
---|
1395 | zipIcons = s.getFile();
|
---|
1396 | }
|
---|
1397 | InputStreamReader r;
|
---|
1398 | try {
|
---|
1399 | r = new InputStreamReader(zip == null ? s : zip, "UTF-8");
|
---|
1400 | } catch (UnsupportedEncodingException e) {
|
---|
1401 | r = new InputStreamReader(zip == null ? s: zip);
|
---|
1402 | }
|
---|
1403 | try {
|
---|
1404 | tp = TaggingPreset.readAll(new BufferedReader(r), validate);
|
---|
1405 | } finally {
|
---|
1406 | r.close();
|
---|
1407 | }
|
---|
1408 | } finally {
|
---|
1409 | s.close();
|
---|
1410 | }
|
---|
1411 | return tp;
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | public static Collection<TaggingPreset> readAll(Collection<String> sources, boolean validate) {
|
---|
1415 | LinkedList<TaggingPreset> allPresets = new LinkedList<TaggingPreset>();
|
---|
1416 | for(String source : sources) {
|
---|
1417 | try {
|
---|
1418 | allPresets.addAll(TaggingPreset.readAll(source, validate));
|
---|
1419 | } catch (IOException e) {
|
---|
1420 | e.printStackTrace();
|
---|
1421 | JOptionPane.showMessageDialog(
|
---|
1422 | Main.parent,
|
---|
1423 | tr("Could not read tagging preset source: {0}",source),
|
---|
1424 | tr("Error"),
|
---|
1425 | JOptionPane.ERROR_MESSAGE
|
---|
1426 | );
|
---|
1427 | } catch (SAXException e) {
|
---|
1428 | System.err.println(e.getMessage());
|
---|
1429 | System.err.println(source);
|
---|
1430 | e.printStackTrace();
|
---|
1431 | JOptionPane.showMessageDialog(
|
---|
1432 | Main.parent,
|
---|
1433 | tr("Error parsing {0}: ", source)+e.getMessage(),
|
---|
1434 | tr("Error"),
|
---|
1435 | JOptionPane.ERROR_MESSAGE
|
---|
1436 | );
|
---|
1437 | }
|
---|
1438 | }
|
---|
1439 | return allPresets;
|
---|
1440 | }
|
---|
1441 |
|
---|
1442 | public static LinkedList<String> getPresetSources() {
|
---|
1443 | LinkedList<String> sources = new LinkedList<String>();
|
---|
1444 |
|
---|
1445 | for (SourceEntry e : (new PresetPrefHelper()).get()) {
|
---|
1446 | sources.add(e.url);
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | return sources;
|
---|
1450 | }
|
---|
1451 |
|
---|
1452 | public static Collection<TaggingPreset> readFromPreferences(boolean validate) {
|
---|
1453 | return readAll(getPresetSources(), validate);
|
---|
1454 | }
|
---|
1455 |
|
---|
1456 | private static class PresetPanel extends JPanel {
|
---|
1457 | boolean hasElements = false;
|
---|
1458 | PresetPanel()
|
---|
1459 | {
|
---|
1460 | super(new GridBagLayout());
|
---|
1461 | }
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 | public PresetPanel createPanel(Collection<OsmPrimitive> selected) {
|
---|
1465 | if (data == null)
|
---|
1466 | return null;
|
---|
1467 | PresetPanel p = new PresetPanel();
|
---|
1468 | LinkedList<Item> l = new LinkedList<Item>();
|
---|
1469 | if(types != null){
|
---|
1470 | JPanel pp = new JPanel();
|
---|
1471 | for(PresetType t : types){
|
---|
1472 | JLabel la = new JLabel(ImageProvider.get(t.getIconName()));
|
---|
1473 | la.setToolTipText(tr("Elements of type {0} are supported.", tr(t.getName())));
|
---|
1474 | pp.add(la);
|
---|
1475 | }
|
---|
1476 | p.add(pp, GBC.eol());
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 | JPanel items = new JPanel(new GridBagLayout());
|
---|
1480 | for (Item i : data){
|
---|
1481 | if(i instanceof Link) {
|
---|
1482 | l.add(i);
|
---|
1483 | } else {
|
---|
1484 | if(i.addToPanel(items, selected)) {
|
---|
1485 | p.hasElements = true;
|
---|
1486 | }
|
---|
1487 | }
|
---|
1488 | }
|
---|
1489 | p.add(items, GBC.eol().fill());
|
---|
1490 | if (selected.size() == 0 && !supportsRelation()) {
|
---|
1491 | GuiHelper.setEnabledRec(items, false);
|
---|
1492 | }
|
---|
1493 |
|
---|
1494 | for(Item link : l) {
|
---|
1495 | link.addToPanel(p, selected);
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | return p;
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 | public boolean isShowable()
|
---|
1502 | {
|
---|
1503 | for(Item i : data)
|
---|
1504 | {
|
---|
1505 | if(!(i instanceof Optional || i instanceof Space || i instanceof Key))
|
---|
1506 | return true;
|
---|
1507 | }
|
---|
1508 | return false;
|
---|
1509 | }
|
---|
1510 |
|
---|
1511 | public void actionPerformed(ActionEvent e) {
|
---|
1512 | if (Main.main == null) return;
|
---|
1513 | if (Main.main.getCurrentDataSet() == null) return;
|
---|
1514 |
|
---|
1515 | Collection<OsmPrimitive> sel = createSelection(Main.main.getCurrentDataSet().getSelected());
|
---|
1516 | int answer = showDialog(sel, supportsRelation());
|
---|
1517 |
|
---|
1518 | if (sel.size() != 0 && answer == DIALOG_ANSWER_APPLY) {
|
---|
1519 | Command cmd = createCommand(sel, getChangedTags());
|
---|
1520 | if (cmd != null) {
|
---|
1521 | Main.main.undoRedo.add(cmd);
|
---|
1522 | }
|
---|
1523 | } else if (answer == DIALOG_ANSWER_NEW_RELATION) {
|
---|
1524 | final Relation r = new Relation();
|
---|
1525 | final Collection<RelationMember> members = new HashSet<RelationMember>();
|
---|
1526 | for(Tag t : getChangedTags()) {
|
---|
1527 | r.put(t.getKey(), t.getValue());
|
---|
1528 | }
|
---|
1529 | for(OsmPrimitive osm : Main.main.getCurrentDataSet().getSelected()) {
|
---|
1530 | RelationMember rm = new RelationMember("", osm);
|
---|
1531 | r.addMember(rm);
|
---|
1532 | members.add(rm);
|
---|
1533 | }
|
---|
1534 | SwingUtilities.invokeLater(new Runnable() {
|
---|
1535 | @Override
|
---|
1536 | public void run() {
|
---|
1537 | RelationEditor.getEditor(Main.main.getEditLayer(), r, members).setVisible(true);
|
---|
1538 | }
|
---|
1539 | });
|
---|
1540 | }
|
---|
1541 | Main.main.getCurrentDataSet().setSelected(Main.main.getCurrentDataSet().getSelected()); // force update
|
---|
1542 |
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | public int showDialog(Collection<OsmPrimitive> sel, final boolean showNewRelation) {
|
---|
1546 | PresetPanel p = createPanel(sel);
|
---|
1547 | if (p == null)
|
---|
1548 | return DIALOG_ANSWER_CANCEL;
|
---|
1549 |
|
---|
1550 | int answer = 1;
|
---|
1551 | if (p.getComponentCount() != 0 && (sel.size() == 0 || p.hasElements)) {
|
---|
1552 | String title = trn("Change {0} object", "Change {0} objects", sel.size(), sel.size());
|
---|
1553 | if(sel.size() == 0) {
|
---|
1554 | if(originalSelectionEmpty) {
|
---|
1555 | title = tr("Nothing selected!");
|
---|
1556 | } else {
|
---|
1557 | title = tr("Selection unsuitable!");
|
---|
1558 | }
|
---|
1559 | }
|
---|
1560 |
|
---|
1561 | class PresetDialog extends ExtendedDialog {
|
---|
1562 | public PresetDialog(Component content, String title, boolean disableApply) {
|
---|
1563 | super(Main.parent,
|
---|
1564 | title,
|
---|
1565 | showNewRelation?
|
---|
1566 | new String[] { tr("Apply Preset"), tr("New relation"), tr("Cancel") }:
|
---|
1567 | new String[] { tr("Apply Preset"), tr("Cancel") },
|
---|
1568 | true);
|
---|
1569 | contentInsets = new Insets(10,5,0,5);
|
---|
1570 | if (showNewRelation) {
|
---|
1571 | setButtonIcons(new String[] {"ok.png", "dialogs/addrelation.png", "cancel.png" });
|
---|
1572 | } else {
|
---|
1573 | setButtonIcons(new String[] {"ok.png", "cancel.png" });
|
---|
1574 | }
|
---|
1575 | setContent(content);
|
---|
1576 | setDefaultButton(1);
|
---|
1577 | setupDialog();
|
---|
1578 | buttons.get(0).setEnabled(!disableApply);
|
---|
1579 | buttons.get(0).setToolTipText(title);
|
---|
1580 | // Prevent dialogs of being too narrow (fix #6261)
|
---|
1581 | Dimension d = getSize();
|
---|
1582 | if (d.width < 350) {
|
---|
1583 | d.width = 350;
|
---|
1584 | setSize(d);
|
---|
1585 | }
|
---|
1586 | showDialog();
|
---|
1587 | }
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | answer = new PresetDialog(p, title, (sel.size() == 0)).getValue();
|
---|
1591 | }
|
---|
1592 | if (!showNewRelation && answer == 2)
|
---|
1593 | return DIALOG_ANSWER_CANCEL;
|
---|
1594 | else
|
---|
1595 | return answer;
|
---|
1596 | }
|
---|
1597 |
|
---|
1598 | /**
|
---|
1599 | * True whenever the original selection given into createSelection was empty
|
---|
1600 | */
|
---|
1601 | private boolean originalSelectionEmpty = false;
|
---|
1602 |
|
---|
1603 | /**
|
---|
1604 | * Removes all unsuitable OsmPrimitives from the given list
|
---|
1605 | * @param participants List of possible OsmPrimitives to tag
|
---|
1606 | * @return Cleaned list with suitable OsmPrimitives only
|
---|
1607 | */
|
---|
1608 | public Collection<OsmPrimitive> createSelection(Collection<OsmPrimitive> participants) {
|
---|
1609 | originalSelectionEmpty = participants.size() == 0;
|
---|
1610 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>();
|
---|
1611 | for (OsmPrimitive osm : participants)
|
---|
1612 | {
|
---|
1613 | if (types != null)
|
---|
1614 | {
|
---|
1615 | if(osm instanceof Relation)
|
---|
1616 | {
|
---|
1617 | if(!types.contains(PresetType.RELATION) &&
|
---|
1618 | !(types.contains(PresetType.CLOSEDWAY) && ((Relation)osm).isMultipolygon())) {
|
---|
1619 | continue;
|
---|
1620 | }
|
---|
1621 | }
|
---|
1622 | else if(osm instanceof Node)
|
---|
1623 | {
|
---|
1624 | if(!types.contains(PresetType.NODE)) {
|
---|
1625 | continue;
|
---|
1626 | }
|
---|
1627 | }
|
---|
1628 | else if(osm instanceof Way)
|
---|
1629 | {
|
---|
1630 | if(!types.contains(PresetType.WAY) &&
|
---|
1631 | !(types.contains(PresetType.CLOSEDWAY) && ((Way)osm).isClosed())) {
|
---|
1632 | continue;
|
---|
1633 | }
|
---|
1634 | }
|
---|
1635 | }
|
---|
1636 | sel.add(osm);
|
---|
1637 | }
|
---|
1638 | return sel;
|
---|
1639 | }
|
---|
1640 |
|
---|
1641 | public List<Tag> getChangedTags() {
|
---|
1642 | List<Tag> result = new ArrayList<Tag>();
|
---|
1643 | for (Item i: data) {
|
---|
1644 | i.addCommands(result);
|
---|
1645 | }
|
---|
1646 | return result;
|
---|
1647 | }
|
---|
1648 |
|
---|
1649 | private static String fixPresetString(String s) {
|
---|
1650 | return s == null ? s : s.replaceAll("'","''");
|
---|
1651 | }
|
---|
1652 |
|
---|
1653 | public static Command createCommand(Collection<OsmPrimitive> sel, List<Tag> changedTags) {
|
---|
1654 | List<Command> cmds = new ArrayList<Command>();
|
---|
1655 | for (Tag tag: changedTags) {
|
---|
1656 | cmds.add(new ChangePropertyCommand(sel, tag.getKey(), tag.getValue()));
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | if (cmds.size() == 0)
|
---|
1660 | return null;
|
---|
1661 | else if (cmds.size() == 1)
|
---|
1662 | return cmds.get(0);
|
---|
1663 | else
|
---|
1664 | return new SequenceCommand(tr("Change Properties"), cmds);
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | private boolean supportsRelation() {
|
---|
1668 | return types == null || types.contains(PresetType.RELATION);
|
---|
1669 | }
|
---|
1670 |
|
---|
1671 | protected void updateEnabledState() {
|
---|
1672 | setEnabled(Main.main != null && Main.main.getCurrentDataSet() != null);
|
---|
1673 | }
|
---|
1674 |
|
---|
1675 | public void activeLayerChange(Layer oldLayer, Layer newLayer) {
|
---|
1676 | updateEnabledState();
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | public void layerAdded(Layer newLayer) {
|
---|
1680 | updateEnabledState();
|
---|
1681 | }
|
---|
1682 |
|
---|
1683 | public void layerRemoved(Layer oldLayer) {
|
---|
1684 | updateEnabledState();
|
---|
1685 | }
|
---|
1686 |
|
---|
1687 | @Override
|
---|
1688 | public String toString() {
|
---|
1689 | return (types == null?"":types) + " " + name;
|
---|
1690 | }
|
---|
1691 |
|
---|
1692 | public boolean typeMatches(Collection<PresetType> t) {
|
---|
1693 | return t == null || types == null || types.containsAll(t);
|
---|
1694 | }
|
---|
1695 |
|
---|
1696 | public boolean matches(Collection<PresetType> t, Map<String, String> tags, boolean onlyShowable) {
|
---|
1697 | if (onlyShowable && !isShowable()) {
|
---|
1698 | return false;
|
---|
1699 | } else if (!typeMatches(t)) {
|
---|
1700 | return false;
|
---|
1701 | }
|
---|
1702 | boolean atLeastOnePositiveMatch = false;
|
---|
1703 | for (Item item : data) {
|
---|
1704 | Boolean m = item.matches(tags);
|
---|
1705 | if (m != null && !m) {
|
---|
1706 | return false;
|
---|
1707 | } else if (m != null) {
|
---|
1708 | atLeastOnePositiveMatch = true;
|
---|
1709 | }
|
---|
1710 | }
|
---|
1711 | return atLeastOnePositiveMatch;
|
---|
1712 | }
|
---|
1713 | }
|
---|