Changeset 5196 in josm for trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
- Timestamp:
- 2012-04-16T22:53:07+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r5181 r5196 23 23 import java.util.Arrays; 24 24 import java.util.Collection; 25 import java.util.Collections; 25 26 import java.util.EnumSet; 26 27 import java.util.HashMap; … … 210 211 * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative). 211 212 */ 212 abstract Boolean matches(Map<String, String> tags); 213 Boolean matches(Map<String, String> tags) { 214 return null; 215 } 216 } 217 218 public static abstract class KeyedItem extends Item { 219 220 public String key; 221 public String text; 222 public String text_context; 223 public String match = getDefaultMatch().getValue(); 224 225 public abstract MatchType getDefaultMatch(); 226 public abstract Collection<String> getValues(); 227 228 @Override 229 Boolean matches(Map<String, String> tags) { 230 switch (MatchType.ofString(match)) { 231 case NONE: 232 return null; 233 case KEY: 234 return tags.containsKey(key) ? true : null; 235 case KEY_REQUIRED: 236 return tags.containsKey(key); 237 case KEY_VALUE: 238 return tags.containsKey(key) && (getValues().contains(tags.get(key))); 239 default: 240 throw new IllegalStateException(); 241 } 242 } 243 213 244 } 214 245 … … 325 356 } 326 357 327 public static class Text extends Item { 328 329 public String key; 330 public String text; 358 public static class Text extends KeyedItem { 359 331 360 public String locale_text; 332 public String text_context;333 361 public String default_; 334 362 public String originalValue; 335 363 public String use_last_as_default = "false"; 336 public String match = MatchType.NONE.getValue();337 364 338 365 private JComponent value; … … 410 437 411 438 @Override 412 Boolean matches(Map<String, String> tags) { 413 switch (MatchType.ofString(match)) { 414 case NONE: 415 return null; 416 case KEY: 417 return tags.containsKey(key) ? true : null; 418 case KEY_REQUIRED: 419 return tags.containsKey(key); 420 default: 421 throw new IllegalArgumentException("key_value matching not supported for <text>: " + text); 422 } 423 } 424 } 425 426 public static class Check extends Item { 427 428 public String key; 429 public String text; 430 public String text_context; 439 public MatchType getDefaultMatch() { 440 return MatchType.NONE; 441 } 442 443 @Override 444 public Collection<String> getValues() { 445 if (default_ == null || default_.isEmpty()) { 446 return Collections.emptyList(); 447 } 448 return Collections.singleton(default_); 449 } 450 } 451 452 public static class Check extends KeyedItem { 453 431 454 public String locale_text; 432 455 public String value_on = OsmUtils.trueval; 433 456 public String value_off = OsmUtils.falseval; 434 457 public boolean default_ = false; // only used for tagless objects 435 public String match = MatchType.NONE.getValue();436 458 437 459 private QuadStateCheckBox check; … … 509 531 510 532 @Override 511 Boolean matches(Map<String, String> tags) { 512 switch (MatchType.ofString(match)) { 513 case NONE: 514 return null; 515 case KEY: 516 return tags.containsKey(key) ? true : null; 517 case KEY_REQUIRED: 518 return tags.containsKey(key); 519 case KEY_VALUE: 520 return value_off.equals(tags.get(key)) || value_on.equals(tags.get(key)); 521 default: 522 throw new IllegalStateException(); 523 } 524 } 525 } 526 527 public static abstract class ComboMultiSelect extends Item { 528 529 public String key; 530 public String text; 531 public String text_context; 533 public MatchType getDefaultMatch() { 534 return MatchType.NONE; 535 } 536 537 @Override 538 public Collection<String> getValues() { 539 return Arrays.asList(value_on, value_off); 540 } 541 } 542 543 public static abstract class ComboMultiSelect extends KeyedItem { 544 532 545 public String locale_text; 533 546 public String values; … … 540 553 public String delimiter = ";"; 541 554 public String use_last_as_default = "false"; 542 public String match = MatchType.NONE.getValue();543 555 544 556 protected JComponent component; 545 557 protected Map<String, PresetListEntry> lhm = new LinkedHashMap<String, PresetListEntry>(); 558 private boolean initialized = false; 546 559 protected Usage usage; 547 560 protected Object originalValue; 548 561 549 562 protected abstract Object getSelectedItem(); 550 protected abstract void addToPanelAnchor(JPanel p, String def , String[] display_array);563 protected abstract void addToPanelAnchor(JPanel p, String def); 551 564 552 565 protected char getDelChar() { … … 555 568 556 569 @Override 570 public Collection<String> getValues() { 571 initListEntries(); 572 return lhm.keySet(); 573 } 574 575 public Collection<String> getDisplayValues() { 576 initListEntries(); 577 return Utils.transform(lhm.values(), new Utils.Function<PresetListEntry, String>() { 578 579 @Override 580 public String apply(PresetListEntry x) { 581 return x.getDisplayValue(true); 582 } 583 }); 584 } 585 586 @Override 557 587 public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) { 588 589 initListEntries(); 558 590 559 591 // find out if our key is already used in the selection. 560 592 usage = determineTextUsage(sel, key); 561 562 String[] display_array; 563 if (lhm.isEmpty()) { 564 display_array = initListEntriesFromAttributes(); 593 if (!usage.hasUniqueValue() && !usage.unused()) { 594 lhm.put(DIFFERENT, new PresetListEntry(DIFFERENT)); 595 } 596 597 p.add(new JLabel(tr("{0}:", locale_text)), GBC.std().insets(0, 0, 10, 0)); 598 addToPanelAnchor(p, default_); 599 600 return true; 601 602 } 603 604 private void initListEntries() { 605 if (initialized) { 606 lhm.remove(DIFFERENT); // possibly added in #addToPanel 607 return; 608 } else if (lhm.isEmpty()) { 609 initListEntriesFromAttributes(); 565 610 } else { 566 611 if (values != null) { … … 579 624 key, text, "short_descriptions", "list_entry")); 580 625 } 581 display_array = new String[lhm.values().size()];582 int i = 0;583 626 for (PresetListEntry e : lhm.values()) { 584 627 if (e.value_context == null) { 585 628 e.value_context = values_context; 586 629 } 587 display_array[i++] = e.getDisplayValue(true); 588 } 589 } 590 630 } 631 } 591 632 if (locale_text == null) { 592 633 locale_text = trc(text_context, fixPresetString(text)); 593 634 } 594 p.add(new JLabel(tr("{0}:", locale_text)), GBC.std().insets(0, 0, 10, 0)); 595 596 addToPanelAnchor(p, default_, display_array); 597 598 return true; 599 635 initialized = true; 600 636 } 601 637 … … 621 657 } 622 658 623 if (!usage.hasUniqueValue() && !usage.unused()) {624 lhm.put(DIFFERENT, new PresetListEntry(DIFFERENT));625 }626 659 for (int i = 0; i < value_array.length; i++) { 627 660 final PresetListEntry e = new PresetListEntry(value_array[i]); … … 637 670 display_array[i] = e.getDisplayValue(true); 638 671 } 639 640 // as addToPanel may be called several times, set String to null to avoid "Ignoring * attribute as * elements are given"641 values = null;642 display_values = null;643 locale_display_values = null;644 short_descriptions = null;645 locale_short_descriptions = null;646 672 647 673 return display_array; … … 744 770 745 771 @Override 746 Boolean matches(Map<String, String> tags) { 747 switch (MatchType.ofString(match)) { 748 case NONE: 749 return null; 750 case KEY: 751 return tags.containsKey(key) ? true : null; 752 case KEY_REQUIRED: 753 return tags.containsKey(key); 754 case KEY_VALUE: 755 return tags.containsKey(key) 756 && Arrays.asList(splitEscaped(getDelChar(), values)).contains(tags.get(key)); 757 default: 758 throw new IllegalStateException(); 759 } 772 public MatchType getDefaultMatch() { 773 return MatchType.NONE; 760 774 } 761 775 } … … 771 785 772 786 @Override 773 protected void addToPanelAnchor(JPanel p, String def , String[] display_array) {787 protected void addToPanelAnchor(JPanel p, String def) { 774 788 if (!usage.unused()) { 775 789 for (String s : usage.values) { … … 791 805 AutoCompletingTextField tf = new AutoCompletingTextField(); 792 806 initAutoCompletionField(tf, key); 793 tf.getAutoCompletionList().add( Arrays.asList(display_array), AutoCompletionItemPritority.IS_IN_STANDARD);807 tf.getAutoCompletionList().add(getDisplayValues(), AutoCompletionItemPritority.IS_IN_STANDARD); 794 808 combo.setEditor(tf); 795 809 … … 861 875 } 862 876 setSelectedIndices(Arrays.copyOf(intParts, j)); 863 // check if we have ac utally managed to represent the full877 // check if we have actually managed to represent the full 864 878 // value with our presets. if not, cop out; we will not offer 865 879 // a selection list that threatens to ruin the value. … … 887 901 888 902 @Override 889 protected void addToPanelAnchor(JPanel p, String def , String[] display_array) {903 protected void addToPanelAnchor(JPanel p, String def) { 890 904 list = new ConcatenatingJList(delimiter, lhm.values().toArray()); 891 905 component = list; … … 979 993 public void addCommands(List<Tag> changedTags) { 980 994 } 981 982 @Override983 Boolean matches(Map<String, String> tags) {984 return null;985 }986 995 } 987 996 … … 1017 1026 @Override 1018 1027 public void addCommands(List<Tag> changedTags) { 1019 }1020 1021 @Override1022 Boolean matches(Map<String, String> tags) {1023 return null;1024 1028 } 1025 1029 } … … 1117 1121 public void addCommands(List<Tag> changedTags) { 1118 1122 } 1119 1120 @Override1121 Boolean matches(Map<String, String> tags) {1122 return null;1123 }1124 1123 } 1125 1124 … … 1138 1137 public void addCommands(List<Tag> changedTags) { 1139 1138 } 1140 1141 @Override1142 Boolean matches(Map<String, String> tags) {1143 return null;1144 }1145 1139 } 1146 1140 … … 1156 1150 public void addCommands(List<Tag> changedTags) { 1157 1151 } 1158 1159 @Override 1160 Boolean matches(Map<String, String> tags) { 1161 return null; 1162 } 1163 } 1164 1165 public static class Key extends Item { 1166 1167 public String key; 1152 } 1153 1154 public static class Key extends KeyedItem { 1155 1168 1156 public String value; 1169 public String match = MatchType.KEY_VALUE.getValue();1170 1157 1171 1158 @Override … … 1180 1167 1181 1168 @Override 1182 Boolean matches(Map<String, String> tags) { 1183 switch (MatchType.ofString(match)) { 1184 case NONE: 1185 return null; 1186 case KEY: 1187 return tags.containsKey(key) ? true : null; 1188 case KEY_REQUIRED: 1189 return tags.containsKey(key); 1190 case KEY_VALUE: 1191 return value.equals(tags.get(key)); 1192 default: 1193 throw new IllegalStateException(); 1194 } 1169 public MatchType getDefaultMatch() { 1170 return MatchType.KEY_VALUE; 1171 } 1172 1173 @Override 1174 public Collection<String> getValues() { 1175 return Collections.singleton(value); 1195 1176 } 1196 1177 } … … 1615 1596 /** 1616 1597 * Removes all unsuitable OsmPrimitives from the given list 1617 * @param participants List of possib ile OsmPrimitives to tag1598 * @param participants List of possible OsmPrimitives to tag 1618 1599 * @return Cleaned list with suitable OsmPrimitives only 1619 1600 */
Note:
See TracChangeset
for help on using the changeset viewer.