Changeset 5155 in josm
- Timestamp:
- 2012-04-01T19:48:01+02:00 (13 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/data/defaultpresets.xml
r5150 r5155 25 25 key: key to set 26 26 value: value to set 27 match: none/key/key!/keyvalue (default is "ketvalue", see below for more information) 27 28 28 29 text: text box 29 30 key: key to set 30 31 text: fixed label to display 31 default: default string to display 32 delete_if_empty: true/false33 use_last_as_default: true/false/force32 default: default string to display (defaults to "") 33 use_last_as_default: true/false/force (default is "false") 34 match: none/key/key!/keyvalue (default is "none", see below for more information) 34 35 35 36 combo: combo box, with multiple choices and possible to enter free form text … … 46 47 ... 47 48 <short_description>last description</short_description> 48 default: default string to display 49 delete_if_empty: true/false50 use_last_as_default: true/false/force49 default: default string to display (defaults to "") 50 use_last_as_default: true/false/force (default is "false") 51 match: none/key/key!/keyvalue (default is none, see below for more information) 51 52 52 53 multiselect: list of values from which zero or more can be selected … … 66 67 ... 67 68 <short_description>last description</short_description> 68 default: default string to display 69 delete_if_empty: true/false70 use_last_as_default: true/false/force69 default: default string to display (defaults to "") 70 use_last_as_default: true/false/force (default is "false") 71 match: none/key/key!/keyvalue (default is "none", see below for more information) 71 72 72 73 check: checkbox … … 74 75 text: fixed label to display 75 76 default: ticked on/off 76 delete_if_empty: true/false77 77 value_on: the value to set when checked (default is "yes") 78 value_off: the value to set when unchecked (default is 'no') 78 value_off: the value to set when unchecked (default is "no") 79 match: none/key/key!/keyvalue (default is "none", see below for more information) 79 80 80 81 role: type to specify possible roles in relations … … 103 104 no specific translation has been given in XML file. When no "display_values" 104 105 are supplied, then "values" will be treated as "display_values" and translated instead. 106 107 The match attribute allows to change the matching process, i.e., determining whether 108 the tags of an OSM object fit into this preset. 109 - none: neutral, i.e., do not consider this item for matching 110 - key: positive if key matches, neutral otherwise 111 - key!: positive if key matches, negative otherwise 112 - keyvalue: positive if key and value matches, negative otherwise 113 Note that for a match, at least one positive and no negative is required. 105 114 --> 106 115 <group name="Highways" icon="presets/way_secondary.png"> -
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PresetListPanel.java
r4968 r5155 11 11 import java.util.Collection; 12 12 import java.util.Collections; 13 import java.util.Hashtable;14 13 import java.util.List; 15 14 import java.util.Map; … … 24 23 import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference; 25 24 import org.openstreetmap.josm.gui.tagging.TaggingPreset; 26 import org.openstreetmap.josm.gui.tagging.TaggingPreset.Check;27 import org.openstreetmap.josm.gui.tagging.TaggingPreset.Combo;28 25 import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType; 29 import org.openstreetmap.josm.gui.tagging.TaggingPreset.Text;30 26 import org.openstreetmap.josm.tools.GBC; 31 27 … … 82 78 } 83 79 84 public void updatePresets( int nodes, int ways, int relations, int closedways, Map<String, Map<String, Integer>> valueCount, PresetHandler presetHandler){80 public void updatePresets(final Collection<PresetType> types, final Map<String, String> tags, PresetHandler presetHandler) { 85 81 86 82 removeAll(); 87 int total = nodes+ways+relations+closedways; 88 if(total == 0) { 83 if (types.isEmpty()) { 89 84 setVisible(false); 90 85 return; 91 86 } 92 87 93 for(TaggingPreset t : TaggingPresetPreference.taggingPresets) { 94 if( 95 ( t.types == null 96 || (relations > 0 && t.types.contains(PresetType.RELATION)) 97 || (nodes > 0 && t.types.contains(PresetType.NODE)) 98 || (ways+closedways > 0 && t.types.contains(PresetType.WAY)) 99 || (closedways > 0 && t.types.contains(PresetType.CLOSEDWAY)) 100 ) 101 && t.isShowable()) 102 { 103 int found = 0; 104 for(TaggingPreset.Item i : t.data) { 105 if(i instanceof TaggingPreset.Key) { 106 String val = ((TaggingPreset.Key)i).value; 107 String key = ((TaggingPreset.Key)i).key; 108 // we subtract 100 if not found and add 1 if found 109 found -= 100; 110 if(key == null || !valueCount.containsKey(key)) { 111 continue; 112 } 88 for (TaggingPreset t : TaggingPresetPreference.taggingPresets) { 89 if (!t.matches(types, tags)) { 90 continue; 91 } 113 92 114 Map<String, Integer> v = valueCount.get(key); 115 if(v.size() == 1 && val != null && v.containsKey(val) && v.get(val) == total) { 116 found += 101; 117 } 118 } else { 119 String key = null; 120 if ((i instanceof Text) && ((Text)i).required) { 121 key = ((Text)i).key; 122 } else if ((i instanceof Combo) && ((Combo)i).required) { 123 key = ((Combo)i).key; 124 } else if ((i instanceof Check) && ((Check)i).required) { 125 key = ((Check)i).key; 126 } 127 if (key != null) { 128 if (valueCount.get(key) != null) { 129 found += 1; 130 } else { 131 found -= 100; 132 } 133 } 134 } 135 } 136 137 if(found <= 0) { 138 continue; 139 } 140 141 JLabel lbl = new JLabel(t.getName() + " …"); 142 lbl.setIcon((Icon) t.getValue(Action.SMALL_ICON)); 143 lbl.addMouseListener(new PresetLabelML(lbl, t, presetHandler)); 144 add(lbl, GBC.eol().fill(GBC.HORIZONTAL)); 145 } 93 JLabel lbl = new JLabel(t.getName() + " …"); 94 lbl.setIcon((Icon) t.getValue(Action.SMALL_ICON)); 95 lbl.addMouseListener(new PresetLabelML(lbl, t, presetHandler)); 96 add(lbl, GBC.eol().fill(GBC.HORIZONTAL)); 146 97 } 147 98 148 if (getComponentCount() > 0) {99 if (getComponentCount() > 0) { 149 100 setVisible(true); 150 101 // This ensures the presets are exactly as high as needed. … … 157 108 } 158 109 } 159 160 110 } -
trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java
r5109 r5155 31 31 import java.util.Collections; 32 32 import java.util.Comparator; 33 import java.util.EnumSet; 33 34 import java.util.HashMap; 34 35 import java.util.HashSet; … … 104 105 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 105 106 import org.openstreetmap.josm.gui.tagging.TaggingPreset; 107 import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType; 106 108 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletingComboBox; 107 109 import org.openstreetmap.josm.gui.tagging.ac.AutoCompletionListItem; … … 261 263 index, isSelected, cellHasFocus); 262 264 if (c instanceof JLabel) { 263 String str = null; 264 str=((AutoCompletionListItem) value).getValue(); 265 if (valueCount.containsKey(objKey)){ 266 Map<String, Integer> m=valueCount.get(objKey); 265 String str = ((AutoCompletionListItem) value).getValue(); 266 if (valueCount.containsKey(objKey)) { 267 Map<String, Integer> m = valueCount.get(objKey); 267 268 if (m.containsKey(str)) { 268 str +="("+m.get(str)+")";269 c.setFont(c.getFont().deriveFont(Font.ITALIC +Font.BOLD));269 str = tr("{0} ({1})", str, m.get(str)); 270 c.setFont(c.getFont().deriveFont(Font.ITALIC + Font.BOLD)); 270 271 } 271 272 } 272 ((JLabel) c).setText(str);273 ((JLabel) c).setText(str); 273 274 } 274 275 return c; … … 947 948 // re-load property data 948 949 propertyData.setRowCount(0); 949 int nodes = 0; 950 int ways = 0; 951 int relations = 0; 952 int closedways = 0; 953 954 Map<String, Integer> keyCount = new HashMap<String, Integer>(); 950 951 final Map<String, Integer> keyCount = new HashMap<String, Integer>(); 952 final Map<String, String> tags = new HashMap<String, String>(); 955 953 valueCount.clear(); 954 EnumSet<PresetType> types = EnumSet.noneOf(TaggingPreset.PresetType.class); 956 955 for (OsmPrimitive osm : newSelection) { 957 if(osm instanceof Node) { 958 ++nodes; 959 } else if(osm instanceof Relation) { 960 ++relations; 961 } else if(((Way)osm).isClosed()) { 962 ++closedways; 963 } else { 964 ++ways; 965 } 966 for (String key: osm.keySet()) { 956 types.add(PresetType.forPrimitive(osm)); 957 for (String key : osm.keySet()) { 967 958 String value = osm.get(key); 968 959 keyCount.put(key, keyCount.containsKey(key) ? keyCount.get(key) + 1 : 1); 969 960 if (valueCount.containsKey(key)) { 970 961 Map<String, Integer> v = valueCount.get(key); 971 v.put(value, v.containsKey(value) ? v.get(value) + 1 : 1);962 v.put(value, v.containsKey(value) ? v.get(value) + 1 : 1); 972 963 } else { 973 TreeMap<String, Integer> v = new TreeMap<String, Integer>();964 TreeMap<String, Integer> v = new TreeMap<String, Integer>(); 974 965 v.put(value, 1); 975 966 valueCount.put(key, v); … … 978 969 } 979 970 for (Entry<String, Map<String, Integer>> e : valueCount.entrySet()) { 980 int count =0;981 for (Entry<String, Integer> e1 : e.getValue().entrySet()) {982 count +=e1.getValue();971 int count = 0; 972 for (Entry<String, Integer> e1 : e.getValue().entrySet()) { 973 count += e1.getValue(); 983 974 } 984 975 if (count < newSelection.size()) { 985 e.getValue().put("", newSelection.size() -count);976 e.getValue().put("", newSelection.size() - count); 986 977 } 987 978 propertyData.addRow(new Object[]{e.getKey(), e.getValue()}); 979 tags.put(e.getKey(), e.getValue().size() == 1 980 ? e.getValue().keySet().iterator().next() : tr("<different>")); 988 981 } 989 982 … … 1026 1019 } 1027 1020 1028 presets.updatePresets( nodes, ways, relations, closedways, valueCount, presetHandler);1021 presets.updatePresets(types, tags, presetHandler); 1029 1022 1030 1023 membershipTable.getTableHeader().setVisible(membershipData.getRowCount() > 0); -
trunk/src/org/openstreetmap/josm/gui/tagging/TagEditorPanel.java
r4191 r5155 6 6 import java.awt.GridBagLayout; 7 7 import java.awt.Insets; 8 import java.util.HashMap; 9 import java.util.Map; 10 import java.util.Map.Entry; 8 import java.util.EnumSet; 11 9 12 10 import javax.swing.BoxLayout; … … 181 179 182 180 private void updatePresets() { 183 Map<String, Map<String, Integer>> valuesCount = new HashMap<String, Map<String,Integer>>(); 184 for (Entry<String, String> entry: model.getTags().entrySet()) { 185 Map<String, Integer> values = new HashMap<String, Integer>(); 186 values.put(entry.getValue(), 1); 187 valuesCount.put(entry.getKey(), values); 188 } 189 presetListPanel.updatePresets(0, 0, 1, 0, valuesCount, presetHandler); 181 presetListPanel.updatePresets( 182 EnumSet.of(TaggingPreset.PresetType.RELATION), 183 model.getTags(), presetHandler); 190 184 validate(); 191 185 } -
trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java
r5147 r5155 108 108 return name().toLowerCase(); 109 109 } 110 111 public static PresetType forPrimitive(OsmPrimitive p) { 112 return forPrimitiveType(p.getDisplayType()); 113 } 114 115 public static PresetType forPrimitiveType(org.openstreetmap.josm.data.osm.OsmPrimitiveType type) { 116 switch (type) { 117 case NODE: 118 return NODE; 119 case WAY: 120 return WAY; 121 case CLOSEDWAY: 122 return CLOSEDWAY; 123 case RELATION: 124 return RELATION; 125 default: 126 throw new IllegalArgumentException(); 127 } 128 } 129 } 130 131 /** 132 * Enum denoting how a match (see {@link Item#matches}) is performed. 133 */ 134 private enum MatchType { 135 136 /** 137 * Neutral, i.e., do not consider this item for matching. 138 */ 139 NONE("none"), 140 /** 141 * Positive if key matches, neutral otherwise. 142 */ 143 KEY("key"), 144 /** 145 * Positive if key matches, negative otherwise. 146 */ 147 KEY_REQUIRED("key!"), 148 /** 149 * Positive if key and value matches, negative otherwise. 150 */ 151 KEY_VALUE("keyvalue"); 152 153 private final String value; 154 155 private MatchType(String value) { 156 this.value = value; 157 } 158 159 public String getValue() { 160 return value; 161 } 162 163 public static MatchType ofString(String type) { 164 for (MatchType i : EnumSet.allOf(MatchType.class)) { 165 if (i.getValue().equals(type)) { 166 return i; 167 } 168 } 169 throw new IllegalArgumentException(type + " is not allowed"); 170 } 110 171 } 111 172 … … 140 201 return false; 141 202 } 203 204 /** 205 * Tests whether the tags match this item. 206 * Note that for a match, at least one positive and no negative is required. 207 * @param tags the tags of an {@link OsmPrimitive} 208 * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative). 209 */ 210 abstract Boolean matches(Map<String, String> tags); 142 211 } 143 212 … … 248 317 public String originalValue; 249 318 public String use_last_as_default = "false"; 250 public boolean delete_if_empty = false; 251 public boolean required = false; 319 public String match = MatchType.NONE.getValue(); 252 320 253 321 private JComponent value; … … 316 384 return; 317 385 318 if (delete_if_empty && v.length() == 0) {319 v = null;320 }321 386 changedTags.add(new Tag(key, v)); 322 387 } … … 325 390 boolean requestFocusInWindow() { 326 391 return value.requestFocusInWindow(); 392 } 393 394 @Override 395 Boolean matches(Map<String, String> tags) { 396 switch (MatchType.ofString(match)) { 397 case NONE: 398 return null; 399 case KEY: 400 return tags.containsKey(key) ? true : null; 401 case KEY_REQUIRED: 402 return tags.containsKey(key); 403 default: 404 throw new IllegalArgumentException("key_value matching not supported for <text>: " + text); 405 } 327 406 } 328 407 } … … 337 416 public String value_off = OsmUtils.falseval; 338 417 public boolean default_ = false; // only used for tagless objects 339 public boolean required = false;418 public String match = MatchType.NONE.getValue(); 340 419 341 420 private QuadStateCheckBox check; … … 411 490 } 412 491 @Override boolean requestFocusInWindow() {return check.requestFocusInWindow();} 492 493 @Override 494 Boolean matches(Map<String, String> tags) { 495 switch (MatchType.ofString(match)) { 496 case NONE: 497 return null; 498 case KEY: 499 return tags.containsKey(key) ? true : null; 500 case KEY_REQUIRED: 501 return tags.containsKey(key); 502 case KEY_VALUE: 503 return value_off.equals(tags.get(key)) || value_on.equals(tags.get(key)); 504 default: 505 throw new IllegalStateException(); 506 } 507 } 413 508 } 414 509 … … 427 522 public String default_; 428 523 public String delimiter = ";"; 429 public boolean delete_if_empty = false;430 524 public String use_last_as_default = "false"; 431 public boolean required = false;525 public String match = MatchType.NONE.getValue(); 432 526 433 527 protected List<String> short_description_list; … … 440 534 protected abstract void addToPanelAnchor(JPanel p, String def, String[] display_array); 441 535 536 protected char getDelChar() { 537 return delimiter.isEmpty() ? ';' : delimiter.charAt(0); 538 } 539 442 540 @Override 443 541 public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) { … … 447 545 String def = default_; 448 546 449 char delChar = ';'; 450 if (!delimiter.isEmpty()) { 451 delChar = delimiter.charAt(0); 452 } 547 char delChar = getDelChar(); 453 548 454 549 String[] value_array = splitEscaped(delChar, values); … … 552 647 return; 553 648 554 if (delete_if_empty && value.length() == 0) {555 value = null;556 }557 649 if (!"false".equals(use_last_as_default)) { 558 650 lastValue.put(key, value); … … 607 699 } 608 700 }; 701 } 702 703 @Override 704 Boolean matches(Map<String, String> tags) { 705 switch (MatchType.ofString(match)) { 706 case NONE: 707 return null; 708 case KEY: 709 return tags.containsKey(key) ? true : null; 710 case KEY_REQUIRED: 711 return tags.containsKey(key); 712 case KEY_VALUE: 713 return tags.containsKey(key) 714 && Arrays.asList(splitEscaped(getDelChar(), values)).contains(tags.get(key)); 715 default: 716 throw new IllegalStateException(); 717 } 609 718 } 610 719 } … … 828 937 public void addCommands(List<Tag> changedTags) { 829 938 } 939 940 @Override 941 Boolean matches(Map<String, String> tags) { 942 return null; 943 } 830 944 } 831 945 … … 862 976 public void addCommands(List<Tag> changedTags) { 863 977 } 978 979 @Override 980 Boolean matches(Map<String, String> tags) { 981 return null; 982 } 864 983 } 865 984 … … 871 990 public String locale_text; 872 991 873 public boolean required =false;992 public boolean required = false; 874 993 public long count = 0; 875 994 … … 956 1075 public void addCommands(List<Tag> changedTags) { 957 1076 } 1077 1078 @Override 1079 Boolean matches(Map<String, String> tags) { 1080 return null; 1081 } 958 1082 } 959 1083 … … 972 1096 public void addCommands(List<Tag> changedTags) { 973 1097 } 1098 1099 @Override 1100 Boolean matches(Map<String, String> tags) { 1101 return null; 1102 } 974 1103 } 975 1104 … … 985 1114 public void addCommands(List<Tag> changedTags) { 986 1115 } 1116 1117 @Override 1118 Boolean matches(Map<String, String> tags) { 1119 return null; 1120 } 987 1121 } 988 1122 … … 991 1125 public String key; 992 1126 public String value; 1127 public String match = MatchType.KEY_VALUE.getValue(); 993 1128 994 1129 @Override … … 1000 1135 public void addCommands(List<Tag> changedTags) { 1001 1136 changedTags.add(new Tag(key, value)); 1137 } 1138 1139 @Override 1140 Boolean matches(Map<String, String> tags) { 1141 switch (MatchType.ofString(match)) { 1142 case NONE: 1143 return null; 1144 case KEY: 1145 return tags.containsKey(key) ? true : null; 1146 case KEY_REQUIRED: 1147 return tags.containsKey(key); 1148 case KEY_VALUE: 1149 return value.equals(tags.get(key)); 1150 default: 1151 throw new IllegalStateException(); 1152 } 1002 1153 } 1003 1154 } … … 1501 1652 return (types == null?"":types) + " " + name; 1502 1653 } 1654 1655 public boolean matches(Collection<PresetType> t, Map<String, String> tags) { 1656 if (!isShowable()) { 1657 return false; 1658 } else if (t != null && types != null && !types.containsAll(t)) { 1659 return false; 1660 } 1661 boolean atLeastOnePositiveMatch = false; 1662 for (Item item : data) { 1663 Boolean m = item.matches(tags); 1664 if (m != null && !m) { 1665 return false; 1666 } else if (m != null) { 1667 atLeastOnePositiveMatch = true; 1668 } 1669 } 1670 return atLeastOnePositiveMatch; 1671 } 1503 1672 } -
trunk/src/org/openstreetmap/josm/tools/TaggingPresetNameTemplateList.java
r4968 r5155 2 2 package org.openstreetmap.josm.tools; 3 3 4 import java.util.ArrayList; 4 import java.util.EnumSet; 5 import java.util.LinkedList; 5 6 import java.util.List; 6 7 7 8 import org.openstreetmap.josm.data.osm.OsmPrimitive; 8 import org.openstreetmap.josm.data.osm.Way;9 9 import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference; 10 10 import org.openstreetmap.josm.gui.tagging.TaggingPreset; 11 import org.openstreetmap.josm.gui.tagging.TaggingPreset.Check;12 import org.openstreetmap.josm.gui.tagging.TaggingPreset.Combo;13 11 import org.openstreetmap.josm.gui.tagging.TaggingPreset.PresetType; 14 import org.openstreetmap.josm.gui.tagging.TaggingPreset.Text;15 12 16 13 /** … … 27 24 return instance; 28 25 } 29 30 private final List<TaggingPreset> presetsWithPattern = new ArrayList<TaggingPreset>(); 26 private final List<TaggingPreset> presetsWithPattern = new LinkedList<TaggingPreset>(); 31 27 32 28 private TaggingPresetNameTemplateList() { 33 29 if (TaggingPresetPreference.taggingPresets != null) { 34 for (TaggingPreset tp : TaggingPresetPreference.taggingPresets) {30 for (TaggingPreset tp : TaggingPresetPreference.taggingPresets) { 35 31 if (tp.nameTemplate != null) { 36 32 presetsWithPattern.add(tp); … … 42 38 public TaggingPreset findPresetTemplate(OsmPrimitive primitive) { 43 39 44 PresetType presetType; 45 switch (primitive.getType()) { 46 case NODE: 47 presetType = PresetType.NODE; 48 break; 49 case WAY: 50 if (((Way) primitive).isClosed()) { 51 presetType = PresetType.CLOSEDWAY; 52 } else { 53 presetType = PresetType.WAY; 54 } 55 break; 56 case RELATION: 57 presetType = PresetType.RELATION; 58 break; 59 default: 60 throw new AssertionError(); 61 } 62 63 for(TaggingPreset t : presetsWithPattern) { 64 65 66 if ( t.types == null 67 || t.types.contains(presetType) 68 || (presetType == PresetType.CLOSEDWAY && t.types.contains(PresetType.WAY))) { 69 int found = 0; 70 71 if (t.nameTemplateFilter != null) { 72 if (t.nameTemplateFilter.match(primitive)) 73 return t; 74 else { 75 continue; 76 } 77 } 78 79 for(TaggingPreset.Item i : t.data) { 80 if(i instanceof TaggingPreset.Key) { 81 String val = ((TaggingPreset.Key)i).value; 82 String key = ((TaggingPreset.Key)i).key; 83 // we subtract 100 if not found and add 1 if found 84 if (val != null && val.equals(primitive.get(key))) { 85 found+=1; 86 } else { 87 found-=100; 88 } 89 } else { 90 String key = null; 91 if ((i instanceof Text) && ((Text)i).required) { 92 key = ((Text)i).key; 93 } else if ((i instanceof Combo) && ((Combo)i).required) { 94 key = ((Combo)i).key; 95 } else if ((i instanceof Check) && ((Check)i).required) { 96 key = ((Check)i).key; 97 } 98 if (key != null) { 99 if (primitive.get(key) != null) { 100 found += 1; 101 } else { 102 found -= 100; 103 } 104 } 105 } 106 } 107 108 if(found > 0) 109 return t; // First matching preset wins 40 for (TaggingPreset t : presetsWithPattern) { 41 if (t.matches(EnumSet.of(PresetType.forPrimitive(primitive)), primitive.getKeys())) { 42 return t; 110 43 } 111 44 } 112 113 45 return null; 114 115 46 } 116 117 118 47 }
Note:
See TracChangeset
for help on using the changeset viewer.