source: josm/trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Combo.java@ 16687

Last change on this file since 16687 was 16687, checked in by simon04, 4 years ago

fix #16031 - Presets: make use_last_as_default="true" work (patch by mikko.lukas)

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