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

File 24516-no-automplete-for-keys.2.patch, 5.5 KB (added by GerdP, 13 days ago)

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

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

     
    6969import org.openstreetmap.josm.command.SequenceCommand;
    7070import org.openstreetmap.josm.data.UndoRedoHandler;
    7171import org.openstreetmap.josm.data.osm.DataSet;
     72import org.openstreetmap.josm.data.osm.Filter;
    7273import org.openstreetmap.josm.data.osm.OsmDataManager;
    7374import org.openstreetmap.josm.data.osm.OsmPrimitive;
    7475import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     
    146147    /** The preference storage of recent tags */
    147148    public static final ListProperty PROPERTY_RECENT_TAGS = new ListProperty("properties.recent-tags",
    148149            Collections.emptyList());
    149     /** The preference list of tags which should not be remembered, since r9940 */
     150    /** The preference filter of tags which should not be remembered, since r9940 */
    150151    public static final StringProperty PROPERTY_TAGS_TO_IGNORE = new StringProperty("properties.recent-tags.ignore",
    151152            new SearchSetting().writeToString());
     153    /** The preference filter for tag keys for which values should not be auto-completed, since xxx */
     154    public static final StringProperty NO_AUTOCOMPLETE_KEYS = new StringProperty("properties.autocomplete.exclude-keys",
     155            new SearchSetting().writeToString());
    152156
    153157    /**
    154158     * What to do with recent tags where keys already exist
     
    182186
    183187    final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
    184188    SearchSetting tagsToIgnore;
     189    SearchSetting noAutocomplete;
    185190
    186191    /**
    187192     * Copy of recently added tags in sorted from newest to oldest order.
     
    240245        this.tagTable = tagTable;
    241246        this.tagData = propertyData;
    242247        this.valueCount = valueCount;
     248        this.noAutocomplete = SearchSetting.readFromString(NO_AUTOCOMPLETE_KEYS.get());
    243249    }
    244250
    245251    /**
     
    736742            }
    737743        }
    738744
     745        /**
     746         * Check if values should be auto-completed for the given tag key.
     747         * @param key the key
     748         * @return false if auto-completion is disabled or if the key matches the exclusion filter, true else
     749         */
     750        private boolean autocompleteValuesForKey(String key) {
     751            if (!AUTOCOMPLETE_VALUES.get())
     752                return false;
     753            if (noAutocomplete == null || Utils.isEmpty(noAutocomplete.text))
     754                return true;
     755            try {
     756                return !SearchCompiler.compile(noAutocomplete).match(new Tag(key));
     757            } catch (SearchParseError parseError) {
     758                throw new IllegalStateException(parseError);
     759            }
     760        }
     761
    739762        protected void addEventListeners() {
    740763            // OK on Enter in values
    741764            values.getEditor().addActionListener(e -> buttonAction(0, null));
     
    777800        public void focusLost(FocusEvent e) {
    778801            // update the values combobox orientation if the key changed
    779802            values.applyComponentOrientation(OrientationAction.getNamelikeOrientation(keys.getText()));
     803            // update the auto-completion setting for the given tag key
     804            values.setAutocompleteEnabled(autocompleteValuesForKey(keys.getText()));
    780805        }
    781806
    782807        protected void updateOkButtonIcon() {
     
    11621187                add(new IgnoreTagAction(tr("Ignore key ''{0}''", t.getKey()), new Tag(t.getKey(), "")));
    11631188                add(new IgnoreTagAction(tr("Ignore tag ''{0}''", t), t));
    11641189                add(new EditIgnoreTagsAction());
     1190                add(new EditExcludeAutocompleteValues(t));
    11651191            }
    11661192        }
    11671193
     
    11891215        class EditIgnoreTagsAction extends AbstractAction {
    11901216
    11911217            EditIgnoreTagsAction() {
    1192                 super(tr("Edit ignore list"));
     1218                super(tr("Edit ignore filter"));
    11931219            }
    11941220
    11951221            @Override
    11961222            public void actionPerformed(ActionEvent e) {
    1197                 final SearchSetting newTagsToIngore = SearchAction.showSearchDialog(tagsToIgnore);
     1223                final SearchSetting newTagsToIngore = SearchAction.showSearchDialog(new Filter(tagsToIgnore));
    11981224                if (newTagsToIngore == null) {
    11991225                    return;
    12001226                }
     
    12081234            }
    12091235        }
    12101236
     1237        class EditExcludeAutocompleteValues extends AbstractAction {
     1238            final transient Tag tag;
     1239
     1240            EditExcludeAutocompleteValues(Tag tag) {
     1241                super(tr("Edit tag filter to stop autocompletion of values"));
     1242                this.tag = tag;
     1243                setEnabled(AUTOCOMPLETE_VALUES.get());
     1244            }
     1245
     1246            @Override
     1247            public void actionPerformed(ActionEvent e) {
     1248                SearchSetting initFilter = SearchSetting.fromString('"' + tag.getKey() + '"');
     1249
     1250                if (noAutocomplete != null && !Utils.isEmpty(noAutocomplete.text))
     1251                    initFilter = noAutocomplete;
     1252                final SearchSetting newNoAutocomplete = SearchAction.showSearchDialog(new Filter(initFilter));
     1253                if (newNoAutocomplete != null) {
     1254                    noAutocomplete = newNoAutocomplete;
     1255                    NO_AUTOCOMPLETE_KEYS.put(noAutocomplete.writeToString());
     1256                }
     1257            }
     1258        }
     1259
    12111260        /**
    12121261         * Destroy the recentTagsActions.
    12131262         */