1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets.items;
|
---|
3 |
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Arrays;
|
---|
6 | import java.util.Collection;
|
---|
7 | import java.util.List;
|
---|
8 |
|
---|
9 | import javax.swing.JPanel;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
13 | import org.openstreetmap.josm.data.osm.Tag;
|
---|
14 | import org.openstreetmap.josm.gui.widgets.QuadStateCheckBox;
|
---|
15 | import org.openstreetmap.josm.tools.GBC;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Checkbox type.
|
---|
19 | */
|
---|
20 | public class Check extends KeyedItem {
|
---|
21 |
|
---|
22 | /** The localized version of {@link #text}. */
|
---|
23 | public String locale_text; // NOSONAR
|
---|
24 | /** the value to set when checked (default is "yes") */
|
---|
25 | public String value_on = OsmUtils.TRUE_VALUE; // NOSONAR
|
---|
26 | /** the value to set when unchecked (default is "no") */
|
---|
27 | public String value_off = OsmUtils.FALSE_VALUE; // NOSONAR
|
---|
28 | /** whether the off value is disabled in the dialog, i.e., only unset or yes are provided */
|
---|
29 | public boolean disable_off; // NOSONAR
|
---|
30 | /** "on" or "off" or unset (default is unset) */
|
---|
31 | public String default_; // only used for tagless objects // NOSONAR
|
---|
32 |
|
---|
33 | private QuadStateCheckBox check;
|
---|
34 | private QuadStateCheckBox.State initialState;
|
---|
35 | private Boolean def;
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel, boolean presetInitiallyMatches) {
|
---|
39 |
|
---|
40 | // find out if our key is already used in the selection.
|
---|
41 | final Usage usage = determineBooleanUsage(sel, key);
|
---|
42 | final String oneValue = usage.values.isEmpty() ? null : usage.values.last();
|
---|
43 | def = "on".equals(default_) ? Boolean.TRUE : "off".equals(default_) ? Boolean.FALSE : null;
|
---|
44 |
|
---|
45 | if (locale_text == null) {
|
---|
46 | locale_text = getLocaleText(text, text_context, null);
|
---|
47 | }
|
---|
48 |
|
---|
49 | if (usage.values.size() < 2 && (oneValue == null || value_on.equals(oneValue) || value_off.equals(oneValue))) {
|
---|
50 | if (def != null && !PROP_FILL_DEFAULT.get()) {
|
---|
51 | // default is set and filling default values feature is disabled - check if all primitives are untagged
|
---|
52 | for (OsmPrimitive s : sel) {
|
---|
53 | if (s.hasKeys()) {
|
---|
54 | def = null;
|
---|
55 | }
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | // all selected objects share the same value which is either true or false or unset,
|
---|
60 | // we can display a standard check box.
|
---|
61 | initialState = value_on.equals(oneValue) || Boolean.TRUE.equals(def)
|
---|
62 | ? QuadStateCheckBox.State.SELECTED
|
---|
63 | : value_off.equals(oneValue) || Boolean.FALSE.equals(def)
|
---|
64 | ? QuadStateCheckBox.State.NOT_SELECTED
|
---|
65 | : QuadStateCheckBox.State.UNSET;
|
---|
66 |
|
---|
67 | } else {
|
---|
68 | def = null;
|
---|
69 | // the objects have different values, or one or more objects have something
|
---|
70 | // else than true/false. we display a quad-state check box
|
---|
71 | // in "partial" state.
|
---|
72 | initialState = QuadStateCheckBox.State.PARTIAL;
|
---|
73 | }
|
---|
74 |
|
---|
75 | final List<QuadStateCheckBox.State> allowedStates = new ArrayList<>(4);
|
---|
76 | if (QuadStateCheckBox.State.PARTIAL == initialState)
|
---|
77 | allowedStates.add(QuadStateCheckBox.State.PARTIAL);
|
---|
78 | allowedStates.add(QuadStateCheckBox.State.SELECTED);
|
---|
79 | if (!disable_off || value_off.equals(oneValue))
|
---|
80 | allowedStates.add(QuadStateCheckBox.State.NOT_SELECTED);
|
---|
81 | allowedStates.add(QuadStateCheckBox.State.UNSET);
|
---|
82 | check = new QuadStateCheckBox(locale_text, initialState,
|
---|
83 | allowedStates.toArray(new QuadStateCheckBox.State[0]));
|
---|
84 | check.setPropertyText(key);
|
---|
85 | check.setState(check.getState()); // to update the tooltip text
|
---|
86 |
|
---|
87 | p.add(check, GBC.eol()); // Do not fill, see #15104
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 |
|
---|
91 | @Override
|
---|
92 | public void addCommands(List<Tag> changedTags) {
|
---|
93 | // if the user hasn't changed anything, don't create a command.
|
---|
94 | if (def == null && check.getState() == initialState) return;
|
---|
95 |
|
---|
96 | // otherwise change things according to the selected value.
|
---|
97 | changedTags.add(new Tag(key,
|
---|
98 | check.getState() == QuadStateCheckBox.State.SELECTED ? value_on :
|
---|
99 | check.getState() == QuadStateCheckBox.State.NOT_SELECTED ? value_off :
|
---|
100 | null));
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public MatchType getDefaultMatch() {
|
---|
105 | return MatchType.NONE;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public Collection<String> getValues() {
|
---|
110 | return disable_off ? Arrays.asList(value_on) : Arrays.asList(value_on, value_off);
|
---|
111 | }
|
---|
112 |
|
---|
113 | @Override
|
---|
114 | public String toString() {
|
---|
115 | return "Check ["
|
---|
116 | + (locale_text != null ? "locale_text=" + locale_text + ", " : "")
|
---|
117 | + (value_on != null ? "value_on=" + value_on + ", " : "")
|
---|
118 | + (value_off != null ? "value_off=" + value_off + ", " : "")
|
---|
119 | + "default_=" + default_ + ", "
|
---|
120 | + (check != null ? "check=" + check + ", " : "")
|
---|
121 | + (initialState != null ? "initialState=" + initialState
|
---|
122 | + ", " : "") + "def=" + def + ']';
|
---|
123 | }
|
---|
124 | }
|
---|