001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.osm;
003
004import org.openstreetmap.josm.actions.search.SearchAction.SearchMode;
005import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
006import org.openstreetmap.josm.data.Preferences.pref;
007import org.openstreetmap.josm.data.Preferences.writeExplicitly;
008
009/**
010 * Data class representing one entry in the filter dialog.
011 *
012 * @author Petr_DlouhĂ˝
013 * @since 2125
014 */
015public class Filter extends SearchSetting {
016    private static final String version = "1";
017
018    /**
019     * Enabled status.
020     * @see FilterPreferenceEntry#enable
021     */
022    public boolean enable = true;
023
024    /**
025     * If this option is activated, the chosen objects are completely hidden.
026     * Otherwise they are disabled and shown in a shade of gray.
027     * @see FilterPreferenceEntry#hiding
028     */
029    public boolean hiding;
030
031    /**
032     * Normally, the specified objects are hidden and the rest is shown.
033     * If this option is activated, only the specified objects are shown and the rest is hidden.
034     * @see FilterPreferenceEntry#inverted
035     */
036    public boolean inverted;
037
038    /**
039     * Constructs a new {@code Filter}.
040     */
041    public Filter() {
042        super();
043        mode = SearchMode.add;
044    }
045
046    /**
047     * Constructs a new {@code Filter} from a preference entry.
048     * @param e preference entry
049     */
050    public Filter(FilterPreferenceEntry e) {
051        this();
052        text = e.text;
053        if ("replace".equals(e.mode)) {
054            mode = SearchMode.replace;
055        } else if ("add".equals(e.mode)) {
056            mode = SearchMode.add;
057        } else if ("remove".equals(e.mode)) {
058            mode = SearchMode.remove;
059        } else  if ("in_selection".equals(e.mode)) {
060            mode = SearchMode.in_selection;
061        }
062        caseSensitive = e.case_sensitive;
063        regexSearch = e.regex_search;
064        mapCSSSearch = e.mapCSS_search;
065        enable = e.enable;
066        hiding = e.hiding;
067        inverted = e.inverted;
068    }
069
070    public static class FilterPreferenceEntry {
071        @writeExplicitly
072        @pref public String version = "1";
073
074        @pref public String text;
075
076        /**
077         * Mode selector which defines how a filter is combined with the previous one:<ul>
078         * <li>replace: replace selection</li>
079         * <li>add: add to selection</li>
080         * <li>remove: remove from selection</li>
081         * <li>in_selection: find in selection</li>
082         * </ul>
083         * @see SearchMode
084         */
085        @writeExplicitly
086        @pref public String mode = "add";
087
088        @pref public boolean case_sensitive;
089
090        @pref public boolean regex_search;
091
092        @pref public boolean mapCSS_search;
093
094        /**
095         * Enabled status.
096         * @see Filter#enable
097         */
098        @writeExplicitly
099        @pref public boolean enable = true;
100
101        /**
102         * If this option is activated, the chosen objects are completely hidden.
103         * Otherwise they are disabled and shown in a shade of gray.
104         * @see Filter#hiding
105         */
106        @writeExplicitly
107        @pref public boolean hiding;
108
109        /**
110         * Normally, the specified objects are hidden and the rest is shown.
111         * If this option is activated, only the specified objects are shown and the rest is hidden.
112         * @see Filter#inverted
113         */
114        @writeExplicitly
115        @pref public boolean inverted;
116    }
117
118    /**
119     * Returns a new preference entry for this filter.
120     * @return preference entry
121     */
122    public FilterPreferenceEntry getPreferenceEntry() {
123        FilterPreferenceEntry e = new FilterPreferenceEntry();
124        e.version = version;
125        e.text = text;
126        e.mode = mode.toString();
127        e.case_sensitive = caseSensitive;
128        e.regex_search = regexSearch;
129        e.mapCSS_search = mapCSSSearch;
130        e.enable = enable;
131        e.hiding = hiding;
132        e.inverted = inverted;
133        return e;
134    }
135}