001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.tagging.presets.items;
003
004import java.util.List;
005
006import org.openstreetmap.josm.data.osm.Tag;
007import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
008import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
009
010import javax.swing.ImageIcon;
011import javax.swing.JLabel;
012import javax.swing.SwingConstants;
013
014/**
015 * A tagging preset item displaying a localizable text.
016 * @since 6190
017 */
018public abstract class TextItem extends TaggingPresetItem {
019
020    /** The text to display */
021    public String text; // NOSONAR
022
023    /** The context used for translating {@link #text} */
024    public String text_context; // NOSONAR
025
026    /** The localized version of {@link #text} */
027    public String locale_text; // NOSONAR
028
029    /** The location of icon file to display */
030    public String icon; // NOSONAR
031    /** The size of displayed icon. If not set, default is 16px */
032    public short icon_size = 16; // NOSONAR
033
034    protected final void initializeLocaleText(String defaultText) {
035        if (locale_text == null) {
036            locale_text = getLocaleText(text, text_context, defaultText);
037        }
038    }
039
040    @Override
041    public void addCommands(List<Tag> changedTags) {
042        // Do nothing
043    }
044
045    protected String fieldsToString() {
046        return (text != null ? "text=" + text + ", " : "")
047                + (text_context != null ? "text_context=" + text_context + ", " : "")
048                + (locale_text != null ? "locale_text=" + locale_text : "");
049    }
050
051    /**
052     * Defines the label icon from this entry's icon
053     * @param label the component
054     * @since 17605
055     */
056    protected void addIcon(JLabel label) {
057        label.setIcon(getIcon());
058        label.setHorizontalAlignment(SwingConstants.LEADING);
059    }
060
061    /**
062     * Returns the entry icon, if any.
063     * @return the entry icon, or {@code null}
064     * @since 17605
065     */
066    public ImageIcon getIcon() {
067        return icon == null ? null : loadImageIcon(icon, TaggingPresetReader.getZipIcons(), (int) icon_size);
068    }
069
070    @Override
071    public String toString() {
072        return getClass().getSimpleName() + " [" + fieldsToString() + ']';
073    }
074}