Ignore:
Timestamp:
2012-04-16T22:53:07+02:00 (12 years ago)
Author:
simon04
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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     */
Note: See TracChangeset for help on using the changeset viewer.