Ticket #24516: 24516-no-automplete-for-keys.patch

File 24516-no-automplete-for-keys.patch, 4.5 KB (added by GerdP, 2 months ago)

Allow to exclude value auto-completion for certain keys specifed in a preference

  • src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java

     
    146146    /** The preference storage of recent tags */
    147147    public static final ListProperty PROPERTY_RECENT_TAGS = new ListProperty("properties.recent-tags",
    148148            Collections.emptyList());
    149     /** The preference list of tags which should not be remembered, since r9940 */
     149    /** The preference filter of tags which should not be remembered, since r9940 */
    150150    public static final StringProperty PROPERTY_TAGS_TO_IGNORE = new StringProperty("properties.recent-tags.ignore",
    151151            new SearchSetting().writeToString());
     152    /** The preference filter for tag keys for which values should not be auto-completed, since xxx */
     153    public static final StringProperty NO_AUTOCOMPLETE_KEYS = new StringProperty("properties.autocomplete.exclude-keys",
     154            new SearchSetting().writeToString());
    152155
    153156    /**
    154157     * What to do with recent tags where keys already exist
     
    182185
    183186    final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
    184187    SearchSetting tagsToIgnore;
     188    SearchSetting noAutocomplete;
    185189
    186190    /**
    187191     * Copy of recently added tags in sorted from newest to oldest order.
     
    240244        this.tagTable = tagTable;
    241245        this.tagData = propertyData;
    242246        this.valueCount = valueCount;
     247        this.noAutocomplete = SearchSetting.readFromString(NO_AUTOCOMPLETE_KEYS.get());
    243248    }
    244249
    245250    /**
     
    736741            }
    737742        }
    738743
     744        /**
     745         * Check if values should be auto-completed for the given tag key.
     746         * @param key the key
     747         * @return false if auto-completion is disabled or if the key matches the exclusion filter, true else
     748         */
     749        private boolean autocompleteValuesForKey(String key) {
     750            if (!AUTOCOMPLETE_VALUES.get())
     751                return false;
     752            if (noAutocomplete == null || Utils.isEmpty(noAutocomplete.text))
     753                return true;
     754            try {
     755                return !SearchCompiler.compile(noAutocomplete).match(new Tag(key));
     756            } catch (SearchParseError parseError) {
     757                throw new IllegalStateException(parseError);
     758            }
     759        }
     760
    739761        protected void addEventListeners() {
    740762            // OK on Enter in values
    741763            values.getEditor().addActionListener(e -> buttonAction(0, null));
     
    777799        public void focusLost(FocusEvent e) {
    778800            // update the values combobox orientation if the key changed
    779801            values.applyComponentOrientation(OrientationAction.getNamelikeOrientation(keys.getText()));
     802            // update the auto-completion setting for the given tag key
     803            values.setAutocompleteEnabled(autocompleteValuesForKey(keys.getText()));
    780804        }
    781805
    782806        protected void updateOkButtonIcon() {
     
    11621186                add(new IgnoreTagAction(tr("Ignore key ''{0}''", t.getKey()), new Tag(t.getKey(), "")));
    11631187                add(new IgnoreTagAction(tr("Ignore tag ''{0}''", t), t));
    11641188                add(new EditIgnoreTagsAction());
     1189                add(new EditExcludeAutocompleteValues(t));
    11651190            }
    11661191        }
    11671192
     
    12081233            }
    12091234        }
    12101235
     1236        class EditExcludeAutocompleteValues extends AbstractAction {
     1237            final transient Tag tag;
     1238
     1239            EditExcludeAutocompleteValues(Tag tag) {
     1240                super(tr("Edit stop-autocomplete values filter"));
     1241                this.tag = tag;
     1242                setEnabled(AUTOCOMPLETE_VALUES.get());
     1243            }
     1244
     1245            @Override
     1246            public void actionPerformed(ActionEvent e) {
     1247                SearchSetting initFilter = SearchSetting.fromString('"' + tag.getKey() + '"');
     1248                if (noAutocomplete != null && !Utils.isEmpty(noAutocomplete.text))
     1249                    initFilter = noAutocomplete;
     1250                final SearchSetting newNoAutocomplete = SearchAction.showSearchDialog(initFilter);
     1251                if (newNoAutocomplete != null) {
     1252                    noAutocomplete = newNoAutocomplete;
     1253                    NO_AUTOCOMPLETE_KEYS.put(noAutocomplete.writeToString());
     1254                }
     1255            }
     1256        }
     1257
    12111258        /**
    12121259         * Destroy the recentTagsActions.
    12131260         */