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 {@code SearchSetting}
050     * @param setting {@code SearchSetting} to construct information from
051     * @since 14932
052     */
053    public Filter(SearchSetting setting) {
054        super(setting);
055    }
056
057    /**
058     * Constructs a new {@code Filter} from a preference entry.
059     * @param e preference entry
060     */
061    public Filter(FilterPreferenceEntry e) {
062        this();
063        text = e.text;
064        if ("replace".equals(e.mode)) {
065            mode = SearchMode.replace;
066        } else if ("add".equals(e.mode)) {
067            mode = SearchMode.add;
068        } else if ("remove".equals(e.mode)) {
069            mode = SearchMode.remove;
070        } else if ("in_selection".equals(e.mode)) {
071            mode = SearchMode.in_selection;
072        }
073        caseSensitive = e.case_sensitive;
074        regexSearch = e.regex_search;
075        mapCSSSearch = e.mapCSS_search;
076        enable = e.enable;
077        hiding = e.hiding;
078        inverted = e.inverted;
079    }
080
081    public static class FilterPreferenceEntry {
082        @WriteExplicitly
083        @StructEntry public String version = "1";
084
085        @StructEntry public String text;
086
087        /**
088         * Mode selector which defines how a filter is combined with the previous one:<ul>
089         * <li>replace: replace selection</li>
090         * <li>add: add to selection</li>
091         * <li>remove: remove from selection</li>
092         * <li>in_selection: find in selection</li>
093         * </ul>
094         * @see SearchMode
095         */
096        @WriteExplicitly
097        @StructEntry public String mode = "add";
098
099        @StructEntry public boolean case_sensitive;
100
101        @StructEntry public boolean regex_search;
102
103        @StructEntry public boolean mapCSS_search;
104
105        /**
106         * Enabled status.
107         * @see Filter#enable
108         */
109        @WriteExplicitly
110        @StructEntry public boolean enable = true;
111
112        /**
113         * If this option is activated, the chosen objects are completely hidden.
114         * Otherwise they are disabled and shown in a shade of gray.
115         * @see Filter#hiding
116         */
117        @WriteExplicitly
118        @StructEntry public boolean hiding;
119
120        /**
121         * Normally, the specified objects are hidden and the rest is shown.
122         * If this option is activated, only the specified objects are shown and the rest is hidden.
123         * @see Filter#inverted
124         */
125        @WriteExplicitly
126        @StructEntry public boolean inverted;
127
128        @Override
129        public int hashCode() {
130            return Objects.hash(case_sensitive, enable, hiding, inverted, mapCSS_search, mode, regex_search, text, version);
131        }
132
133        @Override
134        public boolean equals(Object obj) {
135            if (this == obj)
136                return true;
137            if (obj == null || getClass() != obj.getClass())
138                return false;
139            FilterPreferenceEntry other = (FilterPreferenceEntry) obj;
140            return case_sensitive == other.case_sensitive
141                    && enable == other.enable
142                    && hiding == other.hiding
143                    && inverted == other.inverted
144                    && mapCSS_search == other.mapCSS_search
145                    && regex_search == other.regex_search
146                    && Objects.equals(mode, other.mode)
147                    && Objects.equals(text, other.text)
148                    && Objects.equals(version, other.version);
149        }
150    }
151
152    /**
153     * Returns a new preference entry for this filter.
154     * @return preference entry
155     */
156    public FilterPreferenceEntry getPreferenceEntry() {
157        FilterPreferenceEntry e = new FilterPreferenceEntry();
158        e.version = version;
159        e.text = text;
160        e.mode = mode.name();
161        e.case_sensitive = caseSensitive;
162        e.regex_search = regexSearch;
163        e.mapCSS_search = mapCSSSearch;
164        e.enable = enable;
165        e.hiding = hiding;
166        e.inverted = inverted;
167        return e;
168    }
169}