1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.tagging.presets.items;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 |
|
---|
10 | import javax.swing.AbstractAction;
|
---|
11 | import javax.swing.JButton;
|
---|
12 | import javax.swing.JColorChooser;
|
---|
13 | import javax.swing.JPanel;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.tagging.ac.AutoCompletionPriority;
|
---|
16 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
17 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
|
---|
18 | import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
|
---|
19 | import org.openstreetmap.josm.gui.widgets.JosmComboBox;
|
---|
20 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
21 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
22 | import org.openstreetmap.josm.tools.GBC;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Combobox type.
|
---|
26 | */
|
---|
27 | public class Combo extends ComboMultiSelect {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Whether the combo box is editable, which means that the user can add other values as text.
|
---|
31 | * Default is {@code true}. If {@code false} it is readonly, which means that the user can only select an item in the list.
|
---|
32 | */
|
---|
33 | public boolean editable = true; // NOSONAR
|
---|
34 | /** The length of the combo box (number of characters allowed). */
|
---|
35 | public short length; // NOSONAR
|
---|
36 |
|
---|
37 | protected JosmComboBox<PresetListEntry> combobox;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Constructs a new {@code Combo}.
|
---|
41 | */
|
---|
42 | public Combo() {
|
---|
43 | delimiter = ',';
|
---|
44 | }
|
---|
45 |
|
---|
46 | @Override
|
---|
47 | protected void addToPanelAnchor(JPanel p, String def, boolean presetInitiallyMatches) {
|
---|
48 | if (!usage.unused()) {
|
---|
49 | for (String s : usage.values) {
|
---|
50 | presetListEntries.add(new PresetListEntry(s));
|
---|
51 | }
|
---|
52 | }
|
---|
53 | if (def != null) {
|
---|
54 | presetListEntries.add(new PresetListEntry(def));
|
---|
55 | }
|
---|
56 | presetListEntries.add(new PresetListEntry(""));
|
---|
57 |
|
---|
58 | combobox = new JosmComboBox<>(presetListEntries.toArray(new PresetListEntry[0]));
|
---|
59 | component = combobox;
|
---|
60 | combobox.setRenderer(getListCellRenderer());
|
---|
61 | combobox.setEditable(editable);
|
---|
62 | combobox.reinitialize(presetListEntries);
|
---|
63 | AutoCompletingTextField tf = new AutoCompletingTextField();
|
---|
64 | initAutoCompletionField(tf, key);
|
---|
65 | if (Config.getPref().getBoolean("taggingpreset.display-keys-as-hint", true)) {
|
---|
66 | tf.setHint(key);
|
---|
67 | }
|
---|
68 | if (length > 0) {
|
---|
69 | tf.setMaxChars((int) length);
|
---|
70 | }
|
---|
71 | AutoCompletionList acList = tf.getAutoCompletionList();
|
---|
72 | if (acList != null) {
|
---|
73 | acList.add(getDisplayValues(), AutoCompletionPriority.IS_IN_STANDARD);
|
---|
74 | }
|
---|
75 | combobox.setEditor(tf);
|
---|
76 |
|
---|
77 | if (usage.hasUniqueValue()) {
|
---|
78 | // all items have the same value (and there were no unset items)
|
---|
79 | originalValue = getListEntry(usage.getFirst());
|
---|
80 | combobox.setSelectedItem(originalValue);
|
---|
81 | } else if (def != null && usage.unused()) {
|
---|
82 | // default is set and all items were unset
|
---|
83 | if (!usage.hadKeys() || PROP_FILL_DEFAULT.get() || isForceUseLastAsDefault()) {
|
---|
84 | // selected osm primitives are untagged or filling default feature is enabled
|
---|
85 | combobox.setSelectedItem(getListEntry(def).getDisplayValue());
|
---|
86 | } else {
|
---|
87 | // selected osm primitives are tagged and filling default feature is disabled
|
---|
88 | combobox.setSelectedItem("");
|
---|
89 | }
|
---|
90 | originalValue = getListEntry(DIFFERENT);
|
---|
91 | } else if (usage.unused()) {
|
---|
92 | // all items were unset (and so is default)
|
---|
93 | originalValue = getListEntry("");
|
---|
94 | if (!presetInitiallyMatches && isForceUseLastAsDefault() && LAST_VALUES.containsKey(key)) {
|
---|
95 | combobox.setSelectedItem(getListEntry(LAST_VALUES.get(key)));
|
---|
96 | } else {
|
---|
97 | combobox.setSelectedItem(originalValue);
|
---|
98 | }
|
---|
99 | } else {
|
---|
100 | originalValue = getListEntry(DIFFERENT);
|
---|
101 | combobox.setSelectedItem(originalValue);
|
---|
102 | }
|
---|
103 | if (key != null && ("colour".equals(key) || key.startsWith("colour:") || key.endsWith(":colour"))) {
|
---|
104 | p.add(combobox, GBC.std().fill(GBC.HORIZONTAL));
|
---|
105 | JButton button = new JButton(new ChooseColorAction());
|
---|
106 | p.add(button, GBC.eol().fill(GBC.VERTICAL));
|
---|
107 | ActionListener updateColor = ignore -> button.setBackground(getColor());
|
---|
108 | updateColor.actionPerformed(null);
|
---|
109 | combobox.addActionListener(updateColor);
|
---|
110 | } else {
|
---|
111 | p.add(combobox, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | class ChooseColorAction extends AbstractAction {
|
---|
116 | ChooseColorAction() {
|
---|
117 | putValue(SHORT_DESCRIPTION, tr("Select color"));
|
---|
118 | }
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void actionPerformed(ActionEvent e) {
|
---|
122 | Color color = getColor();
|
---|
123 | color = JColorChooser.showDialog(MainApplication.getMainPanel(), tr("Select color"), color);
|
---|
124 | setColor(color);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected void setColor(Color color) {
|
---|
129 | if (color != null) {
|
---|
130 | combobox.setSelectedItem(ColorHelper.color2html(color));
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | protected Color getColor() {
|
---|
135 | return ColorHelper.html2color(String.valueOf(getSelectedItem()));
|
---|
136 | }
|
---|
137 |
|
---|
138 | @Override
|
---|
139 | protected Object getSelectedItem() {
|
---|
140 | return combobox.getSelectedItem();
|
---|
141 | }
|
---|
142 |
|
---|
143 | @Override
|
---|
144 | protected String getDisplayIfNull() {
|
---|
145 | if (combobox.isEditable())
|
---|
146 | return combobox.getEditor().getItem().toString();
|
---|
147 | else
|
---|
148 | return null;
|
---|
149 | }
|
---|
150 | }
|
---|