[9665] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.tagging.presets.items;
|
---|
| 3 |
|
---|
[16277] | 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;
|
---|
[9665] | 13 | import javax.swing.JPanel;
|
---|
| 14 |
|
---|
[12859] | 15 | import org.openstreetmap.josm.data.tagging.ac.AutoCompletionPriority;
|
---|
[16277] | 16 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[9665] | 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;
|
---|
[12846] | 20 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[16277] | 21 | import org.openstreetmap.josm.tools.ColorHelper;
|
---|
[9665] | 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). */
|
---|
[16042] | 35 | public short length; // NOSONAR
|
---|
[9665] | 36 |
|
---|
| 37 | protected JosmComboBox<PresetListEntry> combobox;
|
---|
| 38 |
|
---|
| 39 | /**
|
---|
| 40 | * Constructs a new {@code Combo}.
|
---|
| 41 | */
|
---|
| 42 | public Combo() {
|
---|
[16043] | 43 | delimiter = ',';
|
---|
[9665] | 44 | }
|
---|
| 45 |
|
---|
| 46 | @Override
|
---|
| 47 | protected void addToPanelAnchor(JPanel p, String def, boolean presetInitiallyMatches) {
|
---|
| 48 | if (!usage.unused()) {
|
---|
| 49 | for (String s : usage.values) {
|
---|
[16057] | 50 | presetListEntries.add(new PresetListEntry(s));
|
---|
[9665] | 51 | }
|
---|
| 52 | }
|
---|
[16057] | 53 | if (def != null) {
|
---|
| 54 | presetListEntries.add(new PresetListEntry(def));
|
---|
[9665] | 55 | }
|
---|
[16057] | 56 | presetListEntries.add(new PresetListEntry(""));
|
---|
[9665] | 57 |
|
---|
[16057] | 58 | combobox = new JosmComboBox<>(presetListEntries.toArray(new PresetListEntry[0]));
|
---|
[9665] | 59 | component = combobox;
|
---|
| 60 | combobox.setRenderer(getListCellRenderer());
|
---|
| 61 | combobox.setEditable(editable);
|
---|
[16057] | 62 | combobox.reinitialize(presetListEntries);
|
---|
[9665] | 63 | AutoCompletingTextField tf = new AutoCompletingTextField();
|
---|
| 64 | initAutoCompletionField(tf, key);
|
---|
[12846] | 65 | if (Config.getPref().getBoolean("taggingpreset.display-keys-as-hint", true)) {
|
---|
[9665] | 66 | tf.setHint(key);
|
---|
| 67 | }
|
---|
[16042] | 68 | if (length > 0) {
|
---|
| 69 | tf.setMaxChars((int) length);
|
---|
[9665] | 70 | }
|
---|
| 71 | AutoCompletionList acList = tf.getAutoCompletionList();
|
---|
| 72 | if (acList != null) {
|
---|
[12859] | 73 | acList.add(getDisplayValues(), AutoCompletionPriority.IS_IN_STANDARD);
|
---|
[9665] | 74 | }
|
---|
| 75 | combobox.setEditor(tf);
|
---|
| 76 |
|
---|
| 77 | if (usage.hasUniqueValue()) {
|
---|
| 78 | // all items have the same value (and there were no unset items)
|
---|
[16057] | 79 | originalValue = getListEntry(usage.getFirst());
|
---|
[9665] | 80 | combobox.setSelectedItem(originalValue);
|
---|
| 81 | } else if (def != null && usage.unused()) {
|
---|
| 82 | // default is set and all items were unset
|
---|
[16045] | 83 | if (!usage.hadKeys() || PROP_FILL_DEFAULT.get() || isForceUseLastAsDefault()) {
|
---|
[9665] | 84 | // selected osm primitives are untagged or filling default feature is enabled
|
---|
[16057] | 85 | combobox.setSelectedItem(getListEntry(def).getDisplayValue());
|
---|
[9665] | 86 | } else {
|
---|
| 87 | // selected osm primitives are tagged and filling default feature is disabled
|
---|
| 88 | combobox.setSelectedItem("");
|
---|
| 89 | }
|
---|
[16057] | 90 | originalValue = getListEntry(DIFFERENT);
|
---|
[9665] | 91 | } else if (usage.unused()) {
|
---|
| 92 | // all items were unset (and so is default)
|
---|
[16057] | 93 | originalValue = getListEntry("");
|
---|
[16045] | 94 | if (!presetInitiallyMatches && isForceUseLastAsDefault() && LAST_VALUES.containsKey(key)) {
|
---|
[16057] | 95 | combobox.setSelectedItem(getListEntry(LAST_VALUES.get(key)));
|
---|
[9665] | 96 | } else {
|
---|
| 97 | combobox.setSelectedItem(originalValue);
|
---|
| 98 | }
|
---|
| 99 | } else {
|
---|
[16057] | 100 | originalValue = getListEntry(DIFFERENT);
|
---|
[9665] | 101 | combobox.setSelectedItem(originalValue);
|
---|
| 102 | }
|
---|
[16282] | 103 | if (key != null && ("colour".equals(key) || key.startsWith("colour:") || key.endsWith(":colour"))) {
|
---|
[16277] | 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 | }
|
---|
[9665] | 113 | }
|
---|
| 114 |
|
---|
[16277] | 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 |
|
---|
[9665] | 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 | }
|
---|