Ignore:
Timestamp:
2017-08-26T00:55:22+02:00 (7 years ago)
Author:
Don-vip
Message:

see #15182 - extract SearchMode and SearchSetting from actions.search.SearchAction to data.osm.search

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java

    r12656 r12659  
    44import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    55import static org.openstreetmap.josm.tools.I18n.tr;
    6 import static org.openstreetmap.josm.tools.I18n.trc;
    76import static org.openstreetmap.josm.tools.I18n.trn;
    87
     
    2524import java.util.List;
    2625import java.util.Map;
    27 import java.util.Objects;
    2826import java.util.Set;
    2927import java.util.function.Predicate;
     
    5048import org.openstreetmap.josm.data.osm.OsmPrimitive;
    5149import org.openstreetmap.josm.data.osm.search.SearchParseError;
     50import org.openstreetmap.josm.data.osm.search.SearchSetting;
    5251import org.openstreetmap.josm.data.osm.search.SearchCompiler;
     52import org.openstreetmap.josm.data.osm.search.SearchMode;
    5353import org.openstreetmap.josm.gui.ExtendedDialog;
    5454import org.openstreetmap.josm.gui.MainApplication;
     
    8686
    8787    private static final String SEARCH_EXPRESSION = "searchExpression";
    88 
    89     /**
    90      * Search mode.
    91      */
    92     public enum SearchMode {
    93         /** replace selection */
    94         replace('R'),
    95         /** add to selection */
    96         add('A'),
    97         /** remove from selection */
    98         remove('D'),
    99         /** find in selection */
    100         in_selection('S');
    101 
    102         private final char code;
    103 
    104         SearchMode(char code) {
    105             this.code = code;
    106         }
    107 
    108         /**
    109          * Returns the unique character code of this mode.
    110          * @return the unique character code of this mode
    111          */
    112         public char getCode() {
    113             return code;
    114         }
    115 
    116         /**
    117          * Returns the search mode matching the given character code.
    118          * @param code character code
    119          * @return search mode matching the given character code
    120          */
    121         public static SearchMode fromCode(char code) {
    122             for (SearchMode mode: values()) {
    123                 if (mode.getCode() == code)
    124                     return mode;
    125             }
    126             return null;
    127         }
    128     }
    12988
    13089    private static final LinkedList<SearchSetting> searchHistory = new LinkedList<>();
     
    446405
    447406        if (inSelection.isSelected()) {
    448             initialValues.mode = SearchAction.SearchMode.in_selection;
     407            initialValues.mode = SearchMode.in_selection;
    449408        } else if (replace.isSelected()) {
    450             initialValues.mode = SearchAction.SearchMode.replace;
     409            initialValues.mode = SearchMode.replace;
    451410        } else if (add.isSelected()) {
    452             initialValues.mode = SearchAction.SearchMode.add;
     411            initialValues.mode = SearchMode.add;
    453412        } else {
    454             initialValues.mode = SearchAction.SearchMode.remove;
     413            initialValues.mode = SearchMode.remove;
    455414        }
    456415
     
    850809
    851810    /**
    852      * This class defines a set of parameters that is used to
    853      * perform search within the search dialog.
    854      */
    855     public static class SearchSetting {
    856         public String text;
    857         public SearchMode mode;
    858         public boolean caseSensitive;
    859         public boolean regexSearch;
    860         public boolean mapCSSSearch;
    861         public boolean allElements;
    862 
    863         /**
    864          * Constructs a new {@code SearchSetting}.
    865          */
    866         public SearchSetting() {
    867             text = "";
    868             mode = SearchMode.replace;
    869         }
    870 
    871         /**
    872          * Constructs a new {@code SearchSetting} from an existing one.
    873          * @param original original search settings
    874          */
    875         public SearchSetting(SearchSetting original) {
    876             text = original.text;
    877             mode = original.mode;
    878             caseSensitive = original.caseSensitive;
    879             regexSearch = original.regexSearch;
    880             mapCSSSearch = original.mapCSSSearch;
    881             allElements = original.allElements;
    882         }
    883 
    884         @Override
    885         public String toString() {
    886             String cs = caseSensitive ?
    887                     /*case sensitive*/  trc("search", "CS") :
    888                         /*case insensitive*/  trc("search", "CI");
    889             String rx = regexSearch ? ", " +
    890                             /*regex search*/ trc("search", "RX") : "";
    891             String css = mapCSSSearch ? ", " +
    892                             /*MapCSS search*/ trc("search", "CSS") : "";
    893             String all = allElements ? ", " +
    894                             /*all elements*/ trc("search", "A") : "";
    895             return '"' + text + "\" (" + cs + rx + css + all + ", " + mode + ')';
    896         }
    897 
    898         @Override
    899         public boolean equals(Object other) {
    900             if (this == other) return true;
    901             if (other == null || getClass() != other.getClass()) return false;
    902             SearchSetting that = (SearchSetting) other;
    903             return caseSensitive == that.caseSensitive &&
    904                     regexSearch == that.regexSearch &&
    905                     mapCSSSearch == that.mapCSSSearch &&
    906                     allElements == that.allElements &&
    907                     mode == that.mode &&
    908                     Objects.equals(text, that.text);
    909         }
    910 
    911         @Override
    912         public int hashCode() {
    913             return Objects.hash(text, mode, caseSensitive, regexSearch, mapCSSSearch, allElements);
    914         }
    915 
    916         /**
    917          * <p>Transforms a string following a certain format, namely "[R | A | D | S][C?,R?,A?,M?] [a-zA-Z]"
    918          * where the first part defines the mode of the search, see {@link SearchMode}, the second defines
    919          * a set of attributes within the {@code SearchSetting} class and the second is the search query.
    920          * <p>
    921          * Attributes are as follows:
    922          * <ul>
    923          *     <li>C - if search is case sensitive
    924          *     <li>R - if the regex syntax is used
    925          *     <li>A - if all objects are considered
    926          *     <li>M - if the mapCSS syntax is used
    927          * </ul>
    928          * <p>For example, "RC type:node" is a valid string representation of an object that replaces the
    929          * current selection, is case sensitive and searches for all objects of type node.
    930          * @param s A string representation of a {@code SearchSetting} object
    931          *          from which the object must be built.
    932          * @return A {@code SearchSetting} defined by the input string.
    933          */
    934         public static SearchSetting readFromString(String s) {
    935             if (s.isEmpty())
    936                 return null;
    937 
    938             SearchSetting result = new SearchSetting();
    939 
    940             int index = 1;
    941 
    942             result.mode = SearchMode.fromCode(s.charAt(0));
    943             if (result.mode == null) {
    944                 result.mode = SearchMode.replace;
    945                 index = 0;
    946             }
    947 
    948             while (index < s.length()) {
    949                 if (s.charAt(index) == 'C') {
    950                     result.caseSensitive = true;
    951                 } else if (s.charAt(index) == 'R') {
    952                     result.regexSearch = true;
    953                 } else if (s.charAt(index) == 'A') {
    954                     result.allElements = true;
    955                 } else if (s.charAt(index) == 'M') {
    956                     result.mapCSSSearch = true;
    957                 } else if (s.charAt(index) == ' ') {
    958                     break;
    959                 } else {
    960                     Logging.warn("Unknown char in SearchSettings: " + s);
    961                     break;
    962                 }
    963                 index++;
    964             }
    965 
    966             if (index < s.length() && s.charAt(index) == ' ') {
    967                 index++;
    968             }
    969 
    970             result.text = s.substring(index);
    971 
    972             return result;
    973         }
    974 
    975         /**
    976          * Builds a string representation of the {@code SearchSetting} object,
    977          * see {@link #readFromString(String)} for more details.
    978          * @return A string representation of the {@code SearchSetting} object.
    979          */
    980         public String writeToString() {
    981             if (text == null || text.isEmpty())
    982                 return "";
    983 
    984             StringBuilder result = new StringBuilder();
    985             result.append(mode.getCode());
    986             if (caseSensitive) {
    987                 result.append('C');
    988             }
    989             if (regexSearch) {
    990                 result.append('R');
    991             }
    992             if (mapCSSSearch) {
    993                 result.append('M');
    994             }
    995             if (allElements) {
    996                 result.append('A');
    997             }
    998             result.append(' ')
    999                   .append(text);
    1000             return result.toString();
    1001         }
    1002     }
    1003 
    1004     /**
    1005811     * {@link ActionParameter} implementation with {@link SearchSetting} as value type.
    1006812     * @since 12547 (moved from {@link ActionParameter})
Note: See TracChangeset for help on using the changeset viewer.