Ignore:
Timestamp:
2016-01-16T19:53:47+01:00 (8 years ago)
Author:
Don-vip
Message:

presets items: fix sonar issues, add javadoc

File:
1 edited

Legend:

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

    r9465 r9492  
    4444public abstract class ComboMultiSelect extends KeyedItem {
    4545
    46     private static final ListCellRenderer<PresetListEntry> RENDERER = new ListCellRenderer<PresetListEntry>() {
     46    private static final Renderer RENDERER = new Renderer();
     47
     48    /** The localized version of {@link #text}. */
     49    public String locale_text; // NOSONAR
     50    /**
     51     * A list of entries.
     52     * The list has to be separated by commas (for the {@link Combo} box) or by the specified delimiter (for the {@link MultiSelect}).
     53     * If a value contains the delimiter, the delimiter may be escaped with a backslash.
     54     * If a value contains a backslash, it must also be escaped with a backslash. */
     55    public String values; // NOSONAR
     56    /**
     57     * To use instead of {@link #values} if the list of values has to be obtained with a Java method of this form:
     58     * <p>{@code public static String[] getValues();}<p>
     59     * The value must be: {@code full.package.name.ClassName#methodName}.
     60     */
     61    public String values_from; // NOSONAR
     62    /** The context used for translating {@link #values} */
     63    public String values_context; // NOSONAR
     64    /** Disabled internationalisation for value to avoid mistakes, see #11696 */
     65    public boolean values_no_i18n; // NOSONAR
     66    /** Whether to sort the values, defaults to true. */
     67    public boolean values_sort = true; // NOSONAR
     68    /**
     69     * A list of entries that is displayed to the user.
     70     * Must be the same number and order of entries as {@link #values} and editable must be false or not specified.
     71     * For the delimiter character and escaping, see the remarks at {@link #values}.
     72     */
     73    public String display_values; // NOSONAR
     74    /** The localized version of {@link #display_values}. */
     75    public String locale_display_values; // NOSONAR
     76    /**
     77     * A delimiter-separated list of texts to be displayed below each {@code display_value}.
     78     * (Only if it is not possible to describe the entry in 2-3 words.)
     79     * Instead of comma separated list instead using {@link #values}, {@link #display_values} and {@link #short_descriptions},
     80     * the following form is also supported:<p>
     81     * {@code <list_entry value="" display_value="" short_description="" icon="" icon_size="" />}
     82     */
     83    public String short_descriptions; // NOSONAR
     84    /** The localized version of {@link #short_descriptions}. */
     85    public String locale_short_descriptions; // NOSONAR
     86    /** The default value for the item. If not specified, the current value of the key is chosen as default (if applicable).*/
     87    public String default_; // NOSONAR
     88    /**
     89     * The character that separates values.
     90     * In case of {@link Combo} the default is comma.
     91     * In case of {@link MultiSelect} the default is semicolon and this will also be used to separate selected values in the tag.
     92     */
     93    public String delimiter = ";"; // NOSONAR
     94    /** whether the last value is used as default. Using "force" enforces this behaviour also for already tagged objects. Default is "false".*/
     95    public String use_last_as_default = "false"; // NOSONAR
     96    /** whether to use values for search via {@link TaggingPresetSelector} */
     97    public String values_searchable = "false"; // NOSONAR
     98
     99    protected JComponent component;
     100    protected final Map<String, PresetListEntry> lhm = new LinkedHashMap<>();
     101    private boolean initialized;
     102    protected Usage usage;
     103    protected Object originalValue;
     104
     105    private static final class Renderer implements ListCellRenderer<PresetListEntry> {
    47106
    48107        private final JLabel lbl = new JLabel();
     
    89148            return lbl;
    90149        }
    91     };
    92 
    93     /** The localized version of {@link #text}. */
    94     public String locale_text;
    95     public String values;
    96     public String values_from;
    97     /** The context used for translating {@link #values} */
    98     public String values_context;
    99     /** Disabled internationalisation for value to avoid mistakes, see #11696 */
    100     public boolean values_no_i18n;
    101     /** Whether to sort the values, defaults to true. */
    102     public boolean values_sort = true;
    103     public String display_values;
    104     /** The localized version of {@link #display_values}. */
    105     public String locale_display_values;
    106     public String short_descriptions;
    107     /** The localized version of {@link #short_descriptions}. */
    108     public String locale_short_descriptions;
    109     public String default_;
    110     public String delimiter = ";";
    111     public String use_last_as_default = "false";
    112     /** whether to use values for search via {@link TaggingPresetSelector} */
    113     public String values_searchable = "false";
    114 
    115     protected JComponent component;
    116     protected final Map<String, PresetListEntry> lhm = new LinkedHashMap<>();
    117     private boolean initialized;
    118     protected Usage usage;
    119     protected Object originalValue;
     150    }
    120151
    121152    /**
     
    169200    }
    170201
     202    /**
     203     * Preset list entry.
     204     */
    171205    public static class PresetListEntry implements Comparable<PresetListEntry> {
    172         public String value;
     206        /** Entry value */
     207        public String value; // NOSONAR
    173208        /** The context used for translating {@link #value} */
    174         public String value_context;
    175         public String display_value;
    176         public String short_description;
     209        public String value_context; // NOSONAR
     210        /** Value displayed to the user */
     211        public String display_value; // NOSONAR
     212        /** Text to be displayed below {@code display_value}. */
     213        public String short_description; // NOSONAR
    177214        /** The location of icon file to display */
    178         public String icon;
     215        public String icon; // NOSONAR
    179216        /** The size of displayed icon. If not set, default is size from icon file */
    180         public String icon_size;
     217        public String icon_size; // NOSONAR
    181218        /** The localized version of {@link #display_value}. */
    182         public String locale_display_value;
     219        public String locale_display_value; // NOSONAR
    183220        /** The localized version of {@link #short_description}. */
    184         public String locale_short_description;
     221        public String locale_short_description; // NOSONAR
    185222        private final File zipIcons = TaggingPresetReader.getZipIcons();
    186223
    187         // Cached size (currently only for Combo) to speed up preset dialog initialization
    188         public int prefferedWidth = -1;
    189         public int prefferedHeight = -1;
     224        /** Cached width (currently only for Combo) to speed up preset dialog initialization */
     225        public int prefferedWidth = -1; // NOSONAR
     226        /** Cached height (currently only for Combo) to speed up preset dialog initialization */
     227        public int prefferedHeight = -1; // NOSONAR
    190228
    191229        /**
     
    193231         */
    194232        public PresetListEntry() {
    195         }
    196 
     233            // Public default constructor is needed
     234        }
     235
     236        /**
     237         * Constructs a new {@code PresetListEntry}, initialized with a value.
     238         * @param value value
     239         */
    197240        public PresetListEntry(String value) {
    198241            this.value = value;
    199242        }
    200243
     244        /**
     245         * Returns HTML formatted contents.
     246         * @return HTML formatted contents
     247         */
    201248        public String getListDisplay() {
    202249            if (value.equals(DIFFERENT))
     
    223270        }
    224271
     272        /**
     273         * Returns the value to display.
     274         * @param translated whether the text must be translated
     275         * @return the value to display
     276         */
    225277        public String getDisplayValue(boolean translated) {
    226278            return translated
     
    229281        }
    230282
     283        /**
     284         * Returns the short description to display.
     285         * @param translated whether the text must be translated
     286         * @return the short description to display
     287         */
    231288        public String getShortDescription(boolean translated) {
    232289            return translated
     
    297354    }
    298355
     356    /**
     357     * Returns the values to display.
     358     * @return the values to display
     359     */
    299360    public Collection<String> getDisplayValues() {
    300361        initListEntries();
     
    365426        char delChar = getDelChar();
    366427
    367         String[] value_array = null;
     428        String[] valueArray = null;
    368429
    369430        if (values_from != null) {
    370             String[] class_method = values_from.split("#");
    371             if (class_method != null && class_method.length == 2) {
     431            String[] classMethod = values_from.split("#");
     432            if (classMethod != null && classMethod.length == 2) {
    372433                try {
    373                     Method method = Class.forName(class_method[0]).getMethod(class_method[1]);
     434                    Method method = Class.forName(classMethod[0]).getMethod(classMethod[1]);
    374435                    // Check method is public static String[] methodName()
    375436                    int mod = method.getModifiers();
    376437                    if (Modifier.isPublic(mod) && Modifier.isStatic(mod)
    377438                            && method.getReturnType().equals(String[].class) && method.getParameterTypes().length == 0) {
    378                         value_array = (String[]) method.invoke(null);
     439                        valueArray = (String[]) method.invoke(null);
    379440                    } else {
    380441                        Main.error(tr("Broken tagging preset \"{0}-{1}\" - Java method given in ''values_from'' is not \"{2}\"", key, text,
     
    388449        }
    389450
    390         if (value_array == null) {
    391             value_array = splitEscaped(delChar, values);
    392         }
    393 
    394         String[] display_array = value_array;
     451        if (valueArray == null) {
     452            valueArray = splitEscaped(delChar, values);
     453        }
     454
     455        String[] displayArray = valueArray;
    395456        if (!values_no_i18n) {
    396457            final String displ = Utils.firstNonNull(locale_display_values, display_values);
    397             display_array = displ == null ? value_array : splitEscaped(delChar, displ);
     458            displayArray = displ == null ? valueArray : splitEscaped(delChar, displ);
    398459        }
    399460
    400461        final String descr = Utils.firstNonNull(locale_short_descriptions, short_descriptions);
    401         String[] short_descriptions_array = descr == null ? null : splitEscaped(delChar, descr);
    402 
    403         if (display_array.length != value_array.length) {
     462        String[] shortDescriptionsArray = descr == null ? null : splitEscaped(delChar, descr);
     463
     464        if (displayArray.length != valueArray.length) {
    404465            Main.error(tr("Broken tagging preset \"{0}-{1}\" - number of items in ''display_values'' must be the same as in ''values''",
    405466                            key, text));
    406             Main.error(tr("Detailed information: {0} <> {1}", Arrays.toString(display_array), Arrays.toString(value_array)));
    407             display_array = value_array;
    408         }
    409 
    410         if (short_descriptions_array != null && short_descriptions_array.length != value_array.length) {
     467            Main.error(tr("Detailed information: {0} <> {1}", Arrays.toString(displayArray), Arrays.toString(valueArray)));
     468            displayArray = valueArray;
     469        }
     470
     471        if (shortDescriptionsArray != null && shortDescriptionsArray.length != valueArray.length) {
    411472            Main.error(tr("Broken tagging preset \"{0}-{1}\" - number of items in ''short_descriptions'' must be the same as in ''values''",
    412473                            key, text));
    413             Main.error(tr("Detailed information: {0} <> {1}", Arrays.toString(short_descriptions_array), Arrays.toString(value_array)));
    414             short_descriptions_array = null;
    415         }
    416 
    417         final List<PresetListEntry> entries = new ArrayList<>(value_array.length);
    418         for (int i = 0; i < value_array.length; i++) {
    419             final PresetListEntry e = new PresetListEntry(value_array[i]);
     474            Main.error(tr("Detailed information: {0} <> {1}", Arrays.toString(shortDescriptionsArray), Arrays.toString(valueArray)));
     475            shortDescriptionsArray = null;
     476        }
     477
     478        final List<PresetListEntry> entries = new ArrayList<>(valueArray.length);
     479        for (int i = 0; i < valueArray.length; i++) {
     480            final PresetListEntry e = new PresetListEntry(valueArray[i]);
    420481            e.locale_display_value = locale_display_values != null || values_no_i18n
    421                     ? display_array[i]
    422                     : trc(values_context, fixPresetString(display_array[i]));
    423             if (short_descriptions_array != null) {
     482                    ? displayArray[i]
     483                    : trc(values_context, fixPresetString(displayArray[i]));
     484            if (shortDescriptionsArray != null) {
    424485                e.locale_short_description = locale_short_descriptions != null
    425                         ? short_descriptions_array[i]
    426                         : tr(fixPresetString(short_descriptions_array[i]));
     486                        ? shortDescriptionsArray[i]
     487                        : tr(fixPresetString(shortDescriptionsArray[i]));
    427488            }
    428489
     
    481542    }
    482543
     544    /**
     545     * Adds a preset list entry.
     546     * @param e list entry to add
     547     */
    483548    public void addListEntry(PresetListEntry e) {
    484549        lhm.put(e.value, e);
    485550    }
    486551
     552    /**
     553     * Adds a collection of preset list entries.
     554     * @param e list entries to add
     555     */
    487556    public void addListEntries(Collection<PresetListEntry> e) {
    488557        for (PresetListEntry i : e) {
Note: See TracChangeset for help on using the changeset viewer.