Ignore:
Timestamp:
2020-06-21T09:54:52+02:00 (4 years ago)
Author:
simon04
Message:

Extract PresetListEntry class

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/ComboMultiSelect.java

    r16689 r16690  
    2222import java.util.stream.IntStream;
    2323
    24 import javax.swing.ImageIcon;
    2524import javax.swing.JComponent;
    2625import javax.swing.JLabel;
     
    3130import org.openstreetmap.josm.data.osm.OsmPrimitive;
    3231import org.openstreetmap.josm.data.osm.Tag;
    33 import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
    3432import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetSelector;
    3533import org.openstreetmap.josm.spi.preferences.Config;
    36 import org.openstreetmap.josm.tools.AlphanumComparator;
    3734import org.openstreetmap.josm.tools.GBC;
    3835import org.openstreetmap.josm.tools.Logging;
     
    152149
    153150            return lbl;
    154         }
    155     }
    156 
    157     /**
    158      * Preset list entry.
    159      */
    160     public static class PresetListEntry implements Comparable<PresetListEntry> {
    161         /** Entry value */
    162         public String value; // NOSONAR
    163         /** The context used for translating {@link #value} */
    164         public String value_context; // NOSONAR
    165         /** Value displayed to the user */
    166         public String display_value; // NOSONAR
    167         /** Text to be displayed below {@code display_value}. */
    168         public String short_description; // NOSONAR
    169         /** The location of icon file to display */
    170         public String icon; // NOSONAR
    171         /** The size of displayed icon. If not set, default is size from icon file */
    172         public short icon_size; // NOSONAR
    173         /** The localized version of {@link #display_value}. */
    174         public String locale_display_value; // NOSONAR
    175         /** The localized version of {@link #short_description}. */
    176         public String locale_short_description; // NOSONAR
    177 
    178         /** Cached width (currently only for Combo) to speed up preset dialog initialization */
    179         public short preferredWidth = -1; // NOSONAR
    180         /** Cached height (currently only for Combo) to speed up preset dialog initialization */
    181         public short preferredHeight = -1; // NOSONAR
    182 
    183         /**
    184          * Constructs a new {@code PresetListEntry}, uninitialized.
    185          */
    186         public PresetListEntry() {
    187             // Public default constructor is needed
    188         }
    189 
    190         /**
    191          * Constructs a new {@code PresetListEntry}, initialized with a value.
    192          * @param value value
    193          */
    194         public PresetListEntry(String value) {
    195             this.value = value;
    196         }
    197 
    198         /**
    199          * Returns HTML formatted contents.
    200          * @return HTML formatted contents
    201          */
    202         public String getListDisplay() {
    203             if (value.equals(DIFFERENT))
    204                 return "<b>" + Utils.escapeReservedCharactersHTML(DIFFERENT) + "</b>";
    205 
    206             String displayValue = Utils.escapeReservedCharactersHTML(getDisplayValue());
    207             String shortDescription = getShortDescription(true);
    208 
    209             if (displayValue.isEmpty() && (shortDescription == null || shortDescription.isEmpty()))
    210                 return "&nbsp;";
    211 
    212             final StringBuilder res = new StringBuilder("<b>").append(displayValue).append("</b>");
    213             if (shortDescription != null) {
    214                 // wrap in table to restrict the text width
    215                 res.append("<div style=\"width:300px; padding:0 0 5px 5px\">")
    216                    .append(shortDescription)
    217                    .append("</div>");
    218             }
    219             return res.toString();
    220         }
    221 
    222         /**
    223          * Returns the entry icon, if any.
    224          * @return the entry icon, or {@code null}
    225          */
    226         public ImageIcon getIcon() {
    227             return icon == null ? null : loadImageIcon(icon, TaggingPresetReader.getZipIcons(), (int) icon_size);
    228         }
    229 
    230         /**
    231          * Returns the value to display.
    232          * @return the value to display
    233          */
    234         public String getDisplayValue() {
    235             return Utils.firstNonNull(locale_display_value, tr(display_value), trc(value_context, value));
    236         }
    237 
    238         /**
    239          * Returns the short description to display.
    240          * @param translated whether the text must be translated
    241          * @return the short description to display
    242          */
    243         public String getShortDescription(boolean translated) {
    244             return translated
    245                     ? Utils.firstNonNull(locale_short_description, tr(short_description))
    246                             : short_description;
    247         }
    248 
    249         // toString is mainly used to initialize the Editor
    250         @Override
    251         public String toString() {
    252             if (DIFFERENT.equals(value))
    253                 return DIFFERENT;
    254             String displayValue = getDisplayValue();
    255             return displayValue != null ? displayValue.replaceAll("<.*>", "") : ""; // remove additional markup, e.g. <br>
    256         }
    257 
    258         @Override
    259         public boolean equals(Object o) {
    260             if (this == o) return true;
    261             if (o == null || getClass() != o.getClass()) return false;
    262             PresetListEntry that = (PresetListEntry) o;
    263             return Objects.equals(value, that.value);
    264         }
    265 
    266         @Override
    267         public int hashCode() {
    268             return Objects.hash(value);
    269         }
    270 
    271         @Override
    272         public int compareTo(PresetListEntry o) {
    273             return AlphanumComparator.getInstance().compare(this.getDisplayValue(), o.getDisplayValue());
    274151        }
    275152    }
Note: See TracChangeset for help on using the changeset viewer.