Changeset 9492 in josm for trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/ComboMultiSelect.java
- Timestamp:
- 2016-01-16T19:53:47+01:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/ComboMultiSelect.java
r9465 r9492 44 44 public abstract class ComboMultiSelect extends KeyedItem { 45 45 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> { 47 106 48 107 private final JLabel lbl = new JLabel(); … … 89 148 return lbl; 90 149 } 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 } 120 151 121 152 /** … … 169 200 } 170 201 202 /** 203 * Preset list entry. 204 */ 171 205 public static class PresetListEntry implements Comparable<PresetListEntry> { 172 public String value; 206 /** Entry value */ 207 public String value; // NOSONAR 173 208 /** 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 177 214 /** The location of icon file to display */ 178 public String icon; 215 public String icon; // NOSONAR 179 216 /** 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 181 218 /** The localized version of {@link #display_value}. */ 182 public String locale_display_value; 219 public String locale_display_value; // NOSONAR 183 220 /** The localized version of {@link #short_description}. */ 184 public String locale_short_description; 221 public String locale_short_description; // NOSONAR 185 222 private final File zipIcons = TaggingPresetReader.getZipIcons(); 186 223 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 190 228 191 229 /** … … 193 231 */ 194 232 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 */ 197 240 public PresetListEntry(String value) { 198 241 this.value = value; 199 242 } 200 243 244 /** 245 * Returns HTML formatted contents. 246 * @return HTML formatted contents 247 */ 201 248 public String getListDisplay() { 202 249 if (value.equals(DIFFERENT)) … … 223 270 } 224 271 272 /** 273 * Returns the value to display. 274 * @param translated whether the text must be translated 275 * @return the value to display 276 */ 225 277 public String getDisplayValue(boolean translated) { 226 278 return translated … … 229 281 } 230 282 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 */ 231 288 public String getShortDescription(boolean translated) { 232 289 return translated … … 297 354 } 298 355 356 /** 357 * Returns the values to display. 358 * @return the values to display 359 */ 299 360 public Collection<String> getDisplayValues() { 300 361 initListEntries(); … … 365 426 char delChar = getDelChar(); 366 427 367 String[] value _array = null;428 String[] valueArray = null; 368 429 369 430 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) { 372 433 try { 373 Method method = Class.forName(class _method[0]).getMethod(class_method[1]);434 Method method = Class.forName(classMethod[0]).getMethod(classMethod[1]); 374 435 // Check method is public static String[] methodName() 375 436 int mod = method.getModifiers(); 376 437 if (Modifier.isPublic(mod) && Modifier.isStatic(mod) 377 438 && method.getReturnType().equals(String[].class) && method.getParameterTypes().length == 0) { 378 value _array = (String[]) method.invoke(null);439 valueArray = (String[]) method.invoke(null); 379 440 } else { 380 441 Main.error(tr("Broken tagging preset \"{0}-{1}\" - Java method given in ''values_from'' is not \"{2}\"", key, text, … … 388 449 } 389 450 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; 395 456 if (!values_no_i18n) { 396 457 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); 398 459 } 399 460 400 461 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) { 404 465 Main.error(tr("Broken tagging preset \"{0}-{1}\" - number of items in ''display_values'' must be the same as in ''values''", 405 466 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) { 411 472 Main.error(tr("Broken tagging preset \"{0}-{1}\" - number of items in ''short_descriptions'' must be the same as in ''values''", 412 473 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]); 420 481 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) { 424 485 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])); 427 488 } 428 489 … … 481 542 } 482 543 544 /** 545 * Adds a preset list entry. 546 * @param e list entry to add 547 */ 483 548 public void addListEntry(PresetListEntry e) { 484 549 lhm.put(e.value, e); 485 550 } 486 551 552 /** 553 * Adds a collection of preset list entries. 554 * @param e list entries to add 555 */ 487 556 public void addListEntries(Collection<PresetListEntry> e) { 488 557 for (PresetListEntry i : e) {
Note:
See TracChangeset
for help on using the changeset viewer.