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