Changeset 12659 in josm for trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java
- Timestamp:
- 2017-08-26T00:55:22+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/search/SearchAction.java
r12656 r12659 4 4 import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 5 5 import static org.openstreetmap.josm.tools.I18n.tr; 6 import static org.openstreetmap.josm.tools.I18n.trc;7 6 import static org.openstreetmap.josm.tools.I18n.trn; 8 7 … … 25 24 import java.util.List; 26 25 import java.util.Map; 27 import java.util.Objects;28 26 import java.util.Set; 29 27 import java.util.function.Predicate; … … 50 48 import org.openstreetmap.josm.data.osm.OsmPrimitive; 51 49 import org.openstreetmap.josm.data.osm.search.SearchParseError; 50 import org.openstreetmap.josm.data.osm.search.SearchSetting; 52 51 import org.openstreetmap.josm.data.osm.search.SearchCompiler; 52 import org.openstreetmap.josm.data.osm.search.SearchMode; 53 53 import org.openstreetmap.josm.gui.ExtendedDialog; 54 54 import org.openstreetmap.josm.gui.MainApplication; … … 86 86 87 87 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 mode111 */112 public char getCode() {113 return code;114 }115 116 /**117 * Returns the search mode matching the given character code.118 * @param code character code119 * @return search mode matching the given character code120 */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 }129 88 130 89 private static final LinkedList<SearchSetting> searchHistory = new LinkedList<>(); … … 446 405 447 406 if (inSelection.isSelected()) { 448 initialValues.mode = Search Action.SearchMode.in_selection;407 initialValues.mode = SearchMode.in_selection; 449 408 } else if (replace.isSelected()) { 450 initialValues.mode = Search Action.SearchMode.replace;409 initialValues.mode = SearchMode.replace; 451 410 } else if (add.isSelected()) { 452 initialValues.mode = Search Action.SearchMode.add;411 initialValues.mode = SearchMode.add; 453 412 } else { 454 initialValues.mode = Search Action.SearchMode.remove;413 initialValues.mode = SearchMode.remove; 455 414 } 456 415 … … 850 809 851 810 /** 852 * This class defines a set of parameters that is used to853 * 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 settings874 */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 @Override885 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 @Override899 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 @Override912 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 defines919 * 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 sensitive924 * <li>R - if the regex syntax is used925 * <li>A - if all objects are considered926 * <li>M - if the mapCSS syntax is used927 * </ul>928 * <p>For example, "RC type:node" is a valid string representation of an object that replaces the929 * current selection, is case sensitive and searches for all objects of type node.930 * @param s A string representation of a {@code SearchSetting} object931 * 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 /**1005 811 * {@link ActionParameter} implementation with {@link SearchSetting} as value type. 1006 812 * @since 12547 (moved from {@link ActionParameter})
Note:
See TracChangeset
for help on using the changeset viewer.