001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.presets.items;
003
004import javax.swing.JPanel;
005
006import org.openstreetmap.josm.Main;
007import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingTextField;
008import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionItemPriority;
009import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionList;
010import org.openstreetmap.josm.gui.widgets.JosmComboBox;
011import org.openstreetmap.josm.tools.GBC;
012
013/**
014 * Combobox type.
015 */
016public class Combo extends ComboMultiSelect {
017
018    public boolean editable = true;
019    protected JosmComboBox<PresetListEntry> combo;
020    public String length;
021
022    /**
023     * Constructs a new {@code Combo}.
024     */
025    public Combo() {
026        delimiter = ",";
027    }
028
029    @Override
030    protected void addToPanelAnchor(JPanel p, String def, boolean presetInitiallyMatches) {
031        if (!usage.unused()) {
032            for (String s : usage.values) {
033                if (!lhm.containsKey(s)) {
034                    lhm.put(s, new PresetListEntry(s));
035                }
036            }
037        }
038        if (def != null && !lhm.containsKey(def)) {
039            lhm.put(def, new PresetListEntry(def));
040        }
041        lhm.put("", new PresetListEntry(""));
042
043        combo = new JosmComboBox<>(lhm.values().toArray(new PresetListEntry[0]));
044        component = combo;
045        combo.setRenderer(getListCellRenderer());
046        combo.setEditable(editable);
047        combo.reinitialize(lhm.values());
048        AutoCompletingTextField tf = new AutoCompletingTextField();
049        initAutoCompletionField(tf, key);
050        if (Main.pref.getBoolean("taggingpreset.display-keys-as-hint", true)) {
051            tf.setHint(key);
052        }
053        if (length != null && !length.isEmpty()) {
054            tf.setMaxChars(Integer.valueOf(length));
055        }
056        AutoCompletionList acList = tf.getAutoCompletionList();
057        if (acList != null) {
058            acList.add(getDisplayValues(), AutoCompletionItemPriority.IS_IN_STANDARD);
059        }
060        combo.setEditor(tf);
061
062        if (usage.hasUniqueValue()) {
063            // all items have the same value (and there were no unset items)
064            originalValue = lhm.get(usage.getFirst());
065            combo.setSelectedItem(originalValue);
066        } else if (def != null && usage.unused()) {
067            // default is set and all items were unset
068            if (!usage.hadKeys() || PROP_FILL_DEFAULT.get() || "force".equals(use_last_as_default)) {
069                // selected osm primitives are untagged or filling default feature is enabled
070                combo.setSelectedItem(lhm.get(def).getDisplayValue(true));
071            } else {
072                // selected osm primitives are tagged and filling default feature is disabled
073                combo.setSelectedItem("");
074            }
075            originalValue = lhm.get(DIFFERENT);
076        } else if (usage.unused()) {
077            // all items were unset (and so is default)
078            originalValue = lhm.get("");
079            if ("force".equals(use_last_as_default) && LAST_VALUES.containsKey(key) && !presetInitiallyMatches) {
080                combo.setSelectedItem(lhm.get(LAST_VALUES.get(key)));
081            } else {
082                combo.setSelectedItem(originalValue);
083            }
084        } else {
085            originalValue = lhm.get(DIFFERENT);
086            combo.setSelectedItem(originalValue);
087        }
088        p.add(combo, GBC.eol().fill(GBC.HORIZONTAL));
089    }
090
091    @Override
092    protected Object getSelectedItem() {
093        return combo.getSelectedItem();
094
095    }
096
097    @Override
098    protected String getDisplayIfNull() {
099        if (combo.isEditable())
100            return combo.getEditor().getItem().toString();
101        else
102            return null;
103    }
104}