Changeset 5196 in josm


Ignore:
Timestamp:
Apr 16, 2012 10:53:07 PM (13 months ago)
Author:
simon04
Message:

tagging presets: fix customized matching for combo elements (broken due to r5172), remove duplicated/inconsistent parsing/interpreting of preset values

Location:
trunk/src/org/openstreetmap/josm
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/RelationChecker.java

    r4968 r5196  
    4646 
    4747    @Override 
    48     public void initialize() throws Exception { 
     48    public void initialize() { 
    4949        initializePresets(); 
    5050    } 
  • trunk/src/org/openstreetmap/josm/data/validation/tests/TagChecker.java

    r4968 r5196  
    155155 
    156156    @Override 
    157     public void initialize() throws Exception { 
     157    public void initialize() throws IOException { 
    158158        initializeData(); 
    159159        initializePresets(); 
     
    286286     * Reads the presets data. 
    287287     * 
    288      * @throws Exception 
    289288     */ 
    290     public static void initializePresets() throws Exception { 
     289    public static void initializePresets() { 
    291290 
    292291        if (!Main.pref.getBoolean(PREF_CHECK_VALUES, true)) 
     
    308307            } 
    309308            for (TaggingPreset p : presets) { 
    310                 for(TaggingPreset.Item i : p.data) { 
    311                     if (i instanceof TaggingPreset.Combo) { 
    312                         TaggingPreset.Combo combo = (TaggingPreset.Combo) i; 
    313                         if (combo.values != null) { 
    314                             for(String value : combo.values.split(",")) { 
    315                                 presetsValueData.put(combo.key, value); 
    316                             } 
    317                         } 
    318                     } else if (i instanceof TaggingPreset.Key) { 
    319                         TaggingPreset.Key k = (TaggingPreset.Key) i; 
    320                         presetsValueData.put(k.key, k.value); 
    321                     } else if (i instanceof TaggingPreset.Text) { 
    322                         TaggingPreset.Text k = (TaggingPreset.Text) i; 
    323                         presetsValueData.putVoid(k.key); 
    324                     } else if (i instanceof TaggingPreset.Check) { 
    325                         TaggingPreset.Check k = (TaggingPreset.Check) i; 
    326                         presetsValueData.put(k.key, "yes"); 
    327                         presetsValueData.put(k.key, "no"); 
     309                for (TaggingPreset.Item i : p.data) { 
     310                    if (i instanceof TaggingPreset.KeyedItem) { 
     311                        TaggingPreset.KeyedItem ky = (TaggingPreset.KeyedItem) i; 
     312                        presetsValueData.putAll(ky.key, ky.getValues()); 
    328313                    } 
    329314                } 
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPreset.java

    r5181 r5196  
    2323import java.util.Arrays; 
    2424import java.util.Collection; 
     25import java.util.Collections; 
    2526import java.util.EnumSet; 
    2627import java.util.HashMap; 
     
    210211         * @return {@code true} if matches (positive), {@code null} if neutral, {@code false} if mismatches (negative). 
    211212         */ 
    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 
    213244    } 
    214245 
     
    325356    } 
    326357 
    327     public static class Text extends Item { 
    328  
    329         public String key; 
    330         public String text; 
     358    public static class Text extends KeyedItem { 
     359 
    331360        public String locale_text; 
    332         public String text_context; 
    333361        public String default_; 
    334362        public String originalValue; 
    335363        public String use_last_as_default = "false"; 
    336         public String match = MatchType.NONE.getValue(); 
    337364 
    338365        private JComponent value; 
     
    410437 
    411438        @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 
    431454        public String locale_text; 
    432455        public String value_on = OsmUtils.trueval; 
    433456        public String value_off = OsmUtils.falseval; 
    434457        public boolean default_ = false; // only used for tagless objects 
    435         public String match = MatchType.NONE.getValue(); 
    436458 
    437459        private QuadStateCheckBox check; 
     
    509531 
    510532        @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 
    532545        public String locale_text; 
    533546        public String values; 
     
    540553        public String delimiter = ";"; 
    541554        public String use_last_as_default = "false"; 
    542         public String match = MatchType.NONE.getValue(); 
    543555 
    544556        protected JComponent component; 
    545557        protected Map<String, PresetListEntry> lhm = new LinkedHashMap<String, PresetListEntry>(); 
     558        private boolean initialized = false; 
    546559        protected Usage usage; 
    547560        protected Object originalValue; 
    548561 
    549562        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); 
    551564 
    552565        protected char getDelChar() { 
     
    555568 
    556569        @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 
    557587        public boolean addToPanel(JPanel p, Collection<OsmPrimitive> sel) { 
     588 
     589            initListEntries(); 
    558590 
    559591            // find out if our key is already used in the selection. 
    560592            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(); 
    565610            } else { 
    566611                if (values != null) { 
     
    579624                            key, text, "short_descriptions", "list_entry")); 
    580625                } 
    581                 display_array = new String[lhm.values().size()]; 
    582                 int i = 0; 
    583626                for (PresetListEntry e : lhm.values()) { 
    584627                    if (e.value_context == null) { 
    585628                        e.value_context = values_context; 
    586629                    } 
    587                     display_array[i++] = e.getDisplayValue(true); 
    588                 } 
    589             } 
    590  
     630                } 
     631            } 
    591632            if (locale_text == null) { 
    592633                locale_text = trc(text_context, fixPresetString(text)); 
    593634            } 
    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; 
    600636        } 
    601637 
     
    621657            } 
    622658 
    623             if (!usage.hasUniqueValue() && !usage.unused()) { 
    624                 lhm.put(DIFFERENT, new PresetListEntry(DIFFERENT)); 
    625             } 
    626659            for (int i = 0; i < value_array.length; i++) { 
    627660                final PresetListEntry e = new PresetListEntry(value_array[i]); 
     
    637670                display_array[i] = e.getDisplayValue(true); 
    638671            } 
    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; 
    646672 
    647673            return display_array; 
     
    744770 
    745771        @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; 
    760774        } 
    761775    } 
     
    771785 
    772786        @Override 
    773         protected void addToPanelAnchor(JPanel p, String def, String[] display_array) { 
     787        protected void addToPanelAnchor(JPanel p, String def) { 
    774788            if (!usage.unused()) { 
    775789                for (String s : usage.values) { 
     
    791805            AutoCompletingTextField tf = new AutoCompletingTextField(); 
    792806            initAutoCompletionField(tf, key); 
    793             tf.getAutoCompletionList().add(Arrays.asList(display_array), AutoCompletionItemPritority.IS_IN_STANDARD); 
     807            tf.getAutoCompletionList().add(getDisplayValues(), AutoCompletionItemPritority.IS_IN_STANDARD); 
    794808            combo.setEditor(tf); 
    795809 
     
    861875                } 
    862876                setSelectedIndices(Arrays.copyOf(intParts, j)); 
    863                 // check if we have acutally managed to represent the full 
     877                // check if we have actually managed to represent the full 
    864878                // value with our presets. if not, cop out; we will not offer 
    865879                // a selection list that threatens to ruin the value. 
     
    887901 
    888902        @Override 
    889         protected void addToPanelAnchor(JPanel p, String def, String[] display_array) { 
     903        protected void addToPanelAnchor(JPanel p, String def) { 
    890904            list = new ConcatenatingJList(delimiter, lhm.values().toArray()); 
    891905            component = list; 
     
    979993        public void addCommands(List<Tag> changedTags) { 
    980994        } 
    981  
    982         @Override 
    983         Boolean matches(Map<String, String> tags) { 
    984             return null; 
    985         } 
    986995    } 
    987996 
     
    10171026        @Override 
    10181027        public void addCommands(List<Tag> changedTags) { 
    1019         } 
    1020  
    1021         @Override 
    1022         Boolean matches(Map<String, String> tags) { 
    1023             return null; 
    10241028        } 
    10251029    } 
     
    11171121        public void addCommands(List<Tag> changedTags) { 
    11181122        } 
    1119  
    1120         @Override 
    1121         Boolean matches(Map<String, String> tags) { 
    1122             return null; 
    1123         } 
    11241123    } 
    11251124 
     
    11381137        public void addCommands(List<Tag> changedTags) { 
    11391138        } 
    1140  
    1141         @Override 
    1142         Boolean matches(Map<String, String> tags) { 
    1143             return null; 
    1144         } 
    11451139    } 
    11461140 
     
    11561150        public void addCommands(List<Tag> changedTags) { 
    11571151        } 
    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 
    11681156        public String value; 
    1169         public String match = MatchType.KEY_VALUE.getValue(); 
    11701157 
    11711158        @Override 
     
    11801167 
    11811168        @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); 
    11951176        } 
    11961177    } 
     
    16151596    /** 
    16161597     * Removes all unsuitable OsmPrimitives from the given list 
    1617      * @param participants List of possibile OsmPrimitives to tag 
     1598     * @param participants List of possible OsmPrimitives to tag 
    16181599     * @return Cleaned list with suitable OsmPrimitives only 
    16191600     */ 
  • trunk/src/org/openstreetmap/josm/gui/tagging/TaggingPresetSearchDialog.java

    r5170 r5196  
    4545import org.openstreetmap.josm.gui.ExtendedDialog; 
    4646import org.openstreetmap.josm.gui.preferences.map.TaggingPresetPreference; 
    47 import org.openstreetmap.josm.gui.tagging.TaggingPreset.Check; 
    48 import org.openstreetmap.josm.gui.tagging.TaggingPreset.Combo; 
    4947import org.openstreetmap.josm.gui.tagging.TaggingPreset.Item; 
    5048import org.openstreetmap.josm.gui.tagging.TaggingPreset.Key; 
     
    5250import org.openstreetmap.josm.gui.tagging.TaggingPreset.Role; 
    5351import org.openstreetmap.josm.gui.tagging.TaggingPreset.Roles; 
    54 import org.openstreetmap.josm.gui.tagging.TaggingPreset.Text; 
    5552 
    5653public class TaggingPresetSearchDialog extends ExtendedDialog implements SelectionChangedListener { 
     
    122119            } 
    123120            for (Item item: preset.data) { 
    124                 if (item instanceof Check) { 
    125                     tags.add(((Check)item).key.toLowerCase()); 
    126                 } else if (item instanceof Combo) { 
     121                if (item instanceof TaggingPreset.KeyedItem) { 
     122                    tags.add(((TaggingPreset.KeyedItem) item).key); 
    127123                    // Should combo values also be added? 
    128                     tags.add(((Combo)item).key); 
    129                 } else if (item instanceof Key) { 
    130                     tags.add(((Key) item).key); 
    131                     String value = ((Key) item).value; 
    132                     if (value != null) { 
    133                         tags.add(value); 
    134                     } 
    135                 } else if (item instanceof Text) { 
    136                     tags.add(((Text) item).key); 
     124                    if (item instanceof Key && ((Key) item).value != null) { 
     125                        tags.add(((Key) item).value); 
     126                    } 
    137127                } else if (item instanceof Roles) { 
    138                     for (Role role: ((Roles) item).roles) { 
     128                    for (Role role : ((Roles) item).roles) { 
    139129                        tags.add(role.key); 
    140130                    } 
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletionManager.java

    r4869 r5196  
    149149        for (final TaggingPreset p : presets) { 
    150150            for (TaggingPreset.Item item : p.data) { 
    151                 if (item instanceof TaggingPreset.Check) { 
    152                     TaggingPreset.Check ch = (TaggingPreset.Check) item; 
    153                     if (ch.key == null) { 
     151                if (item instanceof TaggingPreset.KeyedItem) { 
     152                    TaggingPreset.KeyedItem ki = (TaggingPreset.KeyedItem) item; 
     153                    if (ki.key == null) { 
    154154                        continue; 
    155155                    } 
    156                     presetTagCache.put(ch.key, OsmUtils.falseval); 
    157                     presetTagCache.put(ch.key, OsmUtils.trueval); 
    158                 } else if (item instanceof TaggingPreset.Combo) { 
    159                     TaggingPreset.Combo co = (TaggingPreset.Combo) item; 
    160                     if (co.key == null || co.values == null) { 
    161                         continue; 
    162                     } 
    163                     for (String value : co.values.split(",")) { 
    164                         presetTagCache.put(co.key, value); 
    165                     } 
    166                 } else if (item instanceof TaggingPreset.Key) { 
    167                     TaggingPreset.Key ky = (TaggingPreset.Key) item; 
    168                     if (ky.key == null || ky.value == null) { 
    169                         continue; 
    170                     } 
    171                     presetTagCache.put(ky.key, ky.value); 
    172                 } else if (item instanceof TaggingPreset.Text) { 
    173                     TaggingPreset.Text tt = (TaggingPreset.Text) item; 
    174                     if (tt.key == null) { 
    175                         continue; 
    176                     } 
    177                     presetTagCache.putVoid(tt.key); 
    178                     if (tt.default_ != null && !tt.default_.equals("")) { 
    179                         presetTagCache.put(tt.key, tt.default_); 
    180                     } 
     156                    presetTagCache.putAll(ki.key, ki.getValues()); 
    181157                } else if (item instanceof TaggingPreset.Roles) { 
    182158                    TaggingPreset.Roles r = (TaggingPreset.Roles) item; 
     
    266242     * 
    267243     * @param list the list to populate 
    268      * @param key the tag keys 
     244     * @param keys the tag keys 
    269245     */ 
    270246    public void populateWithTagValues(AutoCompletionList list, List<String> keys) { 
Note: See TracChangeset for help on using the changeset viewer.