1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets.items;
|
---|
3 |
|
---|
4 | import javax.swing.JPanel;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
7 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPriority;
|
---|
8 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
9 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
10 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
11 | import org.openstreetmap.josm.tools.GBC;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Combobox type.
|
---|
15 | */
|
---|
16 | public class Combo extends ComboMultiSelect {
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Whether the combo box is editable, which means that the user can add other values as text.
|
---|
20 | * Default is {@code true}. If {@code false} it is readonly, which means that the user can only select an item in the list.
|
---|
21 | */
|
---|
22 | public boolean editable = true; // NOSONAR
|
---|
23 | /** The length of the combo box (number of characters allowed). */
|
---|
24 | public String length; // NOSONAR
|
---|
25 |
|
---|
26 | protected JosmComboBox<PresetListEntry> combobox;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code Combo}.
|
---|
30 | */
|
---|
31 | public Combo() {
|
---|
32 | delimiter = ",";
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | protected void addToPanelAnchor(JPanel p, String def, boolean presetInitiallyMatches) {
|
---|
37 | if (!usage.unused()) {
|
---|
38 | for (String s : usage.values) {
|
---|
39 | if (!lhm.containsKey(s)) {
|
---|
40 | lhm.put(s, new PresetListEntry(s));
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 | if (def != null && !lhm.containsKey(def)) {
|
---|
45 | lhm.put(def, new PresetListEntry(def));
|
---|
46 | }
|
---|
47 | if (!lhm.containsKey("")) {
|
---|
48 | lhm.put("", new PresetListEntry(""));
|
---|
49 | }
|
---|
50 |
|
---|
51 | combobox = new JosmComboBox<>(lhm.values().toArray(new PresetListEntry[lhm.size()]));
|
---|
52 | component = combobox;
|
---|
53 | combobox.setRenderer(getListCellRenderer());
|
---|
54 | combobox.setEditable(editable);
|
---|
55 | combobox.reinitialize(lhm.values());
|
---|
56 | AutoCompletingTextField tf = new AutoCompletingTextField();
|
---|
57 | initAutoCompletionField(tf, key);
|
---|
58 | if (Config.getPref().getBoolean("taggingpreset.display-keys-as-hint", true)) {
|
---|
59 | tf.setHint(key);
|
---|
60 | }
|
---|
61 | if (length != null && !length.isEmpty()) {
|
---|
62 | tf.setMaxChars(Integer.valueOf(length));
|
---|
63 | }
|
---|
64 | AutoCompletionList acList = tf.getAutoCompletionList();
|
---|
65 | if (acList != null) {
|
---|
66 | acList.add(getDisplayValues(), AutoCompletionItemPriority.IS_IN_STANDARD);
|
---|
67 | }
|
---|
68 | combobox.setEditor(tf);
|
---|
69 |
|
---|
70 | if (usage.hasUniqueValue()) {
|
---|
71 | // all items have the same value (and there were no unset items)
|
---|
72 | originalValue = lhm.get(usage.getFirst());
|
---|
73 | combobox.setSelectedItem(originalValue);
|
---|
74 | } else if (def != null && usage.unused()) {
|
---|
75 | // default is set and all items were unset
|
---|
76 | if (!usage.hadKeys() || PROP_FILL_DEFAULT.get() || "force".equals(use_last_as_default)) {
|
---|
77 | // selected osm primitives are untagged or filling default feature is enabled
|
---|
78 | combobox.setSelectedItem(lhm.get(def).getDisplayValue(true));
|
---|
79 | } else {
|
---|
80 | // selected osm primitives are tagged and filling default feature is disabled
|
---|
81 | combobox.setSelectedItem("");
|
---|
82 | }
|
---|
83 | originalValue = lhm.get(DIFFERENT);
|
---|
84 | } else if (usage.unused()) {
|
---|
85 | // all items were unset (and so is default)
|
---|
86 | originalValue = lhm.get("");
|
---|
87 | if (!presetInitiallyMatches && "force".equals(use_last_as_default) && LAST_VALUES.containsKey(key)) {
|
---|
88 | combobox.setSelectedItem(lhm.get(LAST_VALUES.get(key)));
|
---|
89 | } else {
|
---|
90 | combobox.setSelectedItem(originalValue);
|
---|
91 | }
|
---|
92 | } else {
|
---|
93 | originalValue = lhm.get(DIFFERENT);
|
---|
94 | combobox.setSelectedItem(originalValue);
|
---|
95 | }
|
---|
96 | p.add(combobox, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | protected Object getSelectedItem() {
|
---|
101 | return combobox.getSelectedItem();
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | protected String getDisplayIfNull() {
|
---|
106 | if (combobox.isEditable())
|
---|
107 | return combobox.getEditor().getItem().toString();
|
---|
108 | else
|
---|
109 | return null;
|
---|
110 | }
|
---|
111 | }
|
---|