Ignore:
Timestamp:
2016-03-06T17:12:42+01:00 (8 years ago)
Author:
simon04
Message:

see #12554 - Allow to ignore keys/tags from recent tags

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java

    r9939 r9940  
    3737import java.util.List;
    3838import java.util.Map;
     39import java.util.Objects;
    3940import java.util.TreeMap;
    4041
     
    5758import javax.swing.KeyStroke;
    5859import javax.swing.ListCellRenderer;
     60import javax.swing.SwingUtilities;
    5961import javax.swing.table.DefaultTableModel;
    6062import javax.swing.text.JTextComponent;
     
    6264import org.openstreetmap.josm.Main;
    6365import org.openstreetmap.josm.actions.JosmAction;
     66import org.openstreetmap.josm.actions.search.SearchAction;
     67import org.openstreetmap.josm.actions.search.SearchCompiler;
    6468import org.openstreetmap.josm.command.ChangePropertyCommand;
    6569import org.openstreetmap.josm.command.Command;
     
    7175import org.openstreetmap.josm.data.preferences.EnumProperty;
    7276import org.openstreetmap.josm.data.preferences.IntegerProperty;
     77import org.openstreetmap.josm.data.preferences.StringProperty;
    7378import org.openstreetmap.josm.gui.ExtendedDialog;
    7479import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
     
    125130            DEFAULT_LRU_TAGS_NUMBER);
    126131    /** The preference storage of recent tags */
    127     public static final CollectionProperty COLLECTION_PROPERTY = new CollectionProperty("properties.recent-tags",
     132    public static final CollectionProperty PROPERTY_RECENT_TAGS = new CollectionProperty("properties.recent-tags",
    128133            Collections.<String>emptyList());
     134    public static final StringProperty PROPERTY_TAGS_TO_IGNORE = new StringProperty("properties.recent-tags.ignore",
     135            new SearchAction.SearchSetting().writeToString());
    129136
    130137    /**
     
    159166
    160167    final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
     168    SearchAction.SearchSetting tagsToIgnore;
    161169
    162170    // Copy of recently added tags, used to cache initial status
     
    286294     */
    287295    public void loadTagsIfNeeded() {
     296        loadTagsToIgnore();
    288297        if (PROPERTY_REMEMBER_TAGS.get() && recentTags.isEmpty()) {
    289             recentTags.loadFromPreference(COLLECTION_PROPERTY);
    290         }
     298            recentTags.loadFromPreference(PROPERTY_RECENT_TAGS);
     299        }
     300    }
     301
     302    void loadTagsToIgnore() {
     303        final SearchAction.SearchSetting searchSetting = Utils.firstNonNull(
     304                SearchAction.SearchSetting.readFromString(PROPERTY_TAGS_TO_IGNORE.get()), new SearchAction.SearchSetting());
     305        if (!Objects.equals(tagsToIgnore, searchSetting)) {
     306            try {
     307                tagsToIgnore = searchSetting;
     308                recentTags.setTagsToIgnore(tagsToIgnore);
     309            } catch (SearchCompiler.ParseError parseError) {
     310                warnAboutParseError(parseError);
     311                tagsToIgnore = new SearchAction.SearchSetting();
     312                recentTags.setTagsToIgnore(new SearchCompiler.Never());
     313            }
     314        }
     315    }
     316
     317    private void warnAboutParseError(SearchCompiler.ParseError parseError) {
     318        Main.warn(parseError);
     319        JOptionPane.showMessageDialog(
     320                Main.parent,
     321                parseError.getMessage(),
     322                tr("Error"),
     323                JOptionPane.ERROR_MESSAGE
     324        );
    291325    }
    292326
     
    296330    public void saveTagsIfNeeded() {
    297331        if (PROPERTY_REMEMBER_TAGS.get() && !recentTags.isEmpty()) {
    298             recentTags.saveToPreference(COLLECTION_PROPERTY);
     332            recentTags.saveToPreference(PROPERTY_RECENT_TAGS);
    299333        }
    300334    }
     
    924958                        public void mouseClicked(MouseEvent e) {
    925959                            action.actionPerformed(null);
    926                             if (e.isShiftDown()) {
     960                            if (SwingUtilities.isRightMouseButton(e)) {
     961                                new TagPopupMenu(t).show(e.getComponent(), e.getX(), e.getY());
     962                            } else if (e.isShiftDown()) {
    927963                                // add tags on Shift-Click
    928964                                performTagAdding();
     
    952988        }
    953989
     990        class TagPopupMenu extends JPopupMenu {
     991
     992            TagPopupMenu(Tag t) {
     993                add(new IgnoreTagAction(tr("Ignore key ''{0}''", t.getKey()), new Tag(t.getKey(), "")));
     994                add(new IgnoreTagAction(tr("Ignore tag ''{0}''", t), t));
     995                add(new EditIgnoreTagsAction());
     996            }
     997        }
     998
     999        class IgnoreTagAction extends AbstractAction {
     1000            final Tag tag;
     1001
     1002            IgnoreTagAction(String name, Tag tag) {
     1003                super(name);
     1004                this.tag = tag;
     1005            }
     1006
     1007            @Override
     1008            public void actionPerformed(ActionEvent e) {
     1009                try {
     1010                    recentTags.ignoreTag(tag, tagsToIgnore);
     1011                    PROPERTY_TAGS_TO_IGNORE.put(tagsToIgnore.writeToString());
     1012                } catch (SearchCompiler.ParseError parseError) {
     1013                    throw new IllegalStateException(parseError);
     1014                }
     1015            }
     1016        }
     1017
     1018        class EditIgnoreTagsAction extends AbstractAction {
     1019
     1020            EditIgnoreTagsAction() {
     1021                super(tr("Edit ignore list"));
     1022            }
     1023
     1024            @Override
     1025            public void actionPerformed(ActionEvent e) {
     1026                final SearchAction.SearchSetting newTagsToIngore = SearchAction.showSearchDialog(tagsToIgnore);
     1027                if (newTagsToIngore == null) {
     1028                    return;
     1029                }
     1030                try {
     1031                    tagsToIgnore = newTagsToIngore;
     1032                    recentTags.setTagsToIgnore(tagsToIgnore);
     1033                    PROPERTY_TAGS_TO_IGNORE.put(tagsToIgnore.writeToString());
     1034                } catch (SearchCompiler.ParseError parseError) {
     1035                    warnAboutParseError(parseError);
     1036                }
     1037            }
     1038        }
     1039
    9541040        public void destroyActions() {
    9551041            for (JosmAction action : recentTagsActions) {
Note: See TracChangeset for help on using the changeset viewer.