Changeset 12659 in josm for trunk


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

Location:
trunk
Files:
2 added
15 edited
1 moved

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})
  • trunk/src/org/openstreetmap/josm/data/osm/Filter.java

    r11973 r12659  
    44import java.util.Objects;
    55
    6 import org.openstreetmap.josm.actions.search.SearchAction.SearchMode;
    7 import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
    86import org.openstreetmap.josm.data.Preferences.pref;
    97import org.openstreetmap.josm.data.Preferences.writeExplicitly;
     8import org.openstreetmap.josm.data.osm.search.SearchMode;
     9import org.openstreetmap.josm.data.osm.search.SearchSetting;
    1010
    1111/**
  • trunk/src/org/openstreetmap/josm/data/osm/FilterMatcher.java

    r12656 r12659  
    66import java.util.List;
    77
    8 import org.openstreetmap.josm.actions.search.SearchAction.SearchMode;
    98import org.openstreetmap.josm.data.osm.search.SearchParseError;
    109import org.openstreetmap.josm.data.osm.search.SearchCompiler;
     10import org.openstreetmap.josm.data.osm.search.SearchMode;
    1111import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
    1212import org.openstreetmap.josm.data.osm.search.SearchCompiler.Not;
  • trunk/src/org/openstreetmap/josm/data/osm/search/SearchCompiler.java

    r12656 r12659  
    2424
    2525import org.openstreetmap.josm.Main;
    26 import org.openstreetmap.josm.actions.search.SearchAction;
    2726import org.openstreetmap.josm.data.Bounds;
    2827import org.openstreetmap.josm.data.coor.LatLon;
     
    6261 *   fact expression
    6362 *   fact
    64  * 
     63 *
    6564 * fact =
    6665 *  ( expression )
     
    7170 *  term
    7271 *  </pre>
    73  * 
     72 *
    7473 * @author Imi
    7574 * @since 12656 (moved from actions.search package)
     
    16471646     * @see #compile(String)
    16481647     */
    1649     public static Match compile(SearchAction.SearchSetting setting) throws SearchParseError {
     1648    public static Match compile(SearchSetting setting) throws SearchParseError {
    16501649        if (setting.mapCSSSearch) {
    16511650            return compileMapCSS(setting.text);
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r12647 r12659  
    7373import org.openstreetmap.josm.data.osm.DataSet;
    7474import org.openstreetmap.josm.data.osm.OsmPrimitive;
     75import org.openstreetmap.josm.data.osm.search.SearchMode;
    7576import org.openstreetmap.josm.data.validation.OsmValidator;
    7677import org.openstreetmap.josm.gui.ProgramArguments.Option;
     
    10241025            tasks.add(MainApplication.worker.submit(() -> {
    10251026                for (String s : selectionArguments) {
    1026                     SearchAction.search(s, SearchAction.SearchMode.add);
     1027                    SearchAction.search(s, SearchMode.add);
    10271028                }
    10281029            }));
  • trunk/src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r12636 r12659  
    4444import org.openstreetmap.josm.actions.relation.EditRelationAction;
    4545import org.openstreetmap.josm.actions.relation.SelectInRelationListAction;
    46 import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
    4746import org.openstreetmap.josm.data.SelectionChangedListener;
    4847import org.openstreetmap.josm.data.coor.LatLon;
     
    5857import org.openstreetmap.josm.data.osm.event.DatasetEventManager;
    5958import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
     59import org.openstreetmap.josm.data.osm.search.SearchSetting;
    6060import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
    6161import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/PropertiesDialog.java

    r12656 r12659  
    6060import org.openstreetmap.josm.actions.relation.SelectMembersAction;
    6161import org.openstreetmap.josm.actions.relation.SelectRelationAction;
    62 import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
    6362import org.openstreetmap.josm.command.ChangeCommand;
    6463import org.openstreetmap.josm.command.ChangePropertyCommand;
     
    7877import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
    7978import org.openstreetmap.josm.data.osm.search.SearchCompiler;
     79import org.openstreetmap.josm.data.osm.search.SearchSetting;
    8080import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
    8181import org.openstreetmap.josm.data.preferences.StringProperty;
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/RecentTagCollection.java

    r12656 r12659  
    88import java.util.Map;
    99
    10 import org.openstreetmap.josm.actions.search.SearchAction;
    1110import org.openstreetmap.josm.data.osm.Tag;
    1211import org.openstreetmap.josm.data.osm.search.SearchParseError;
     12import org.openstreetmap.josm.data.osm.search.SearchSetting;
    1313import org.openstreetmap.josm.data.osm.search.SearchCompiler;
    1414import org.openstreetmap.josm.data.preferences.CollectionProperty;
     
    8282    }
    8383
    84     public void setTagsToIgnore(SearchAction.SearchSetting tagsToIgnore) throws SearchParseError {
     84    public void setTagsToIgnore(SearchSetting tagsToIgnore) throws SearchParseError {
    8585        setTagsToIgnore(tagsToIgnore.text.isEmpty() ? SearchCompiler.Never.INSTANCE : SearchCompiler.compile(tagsToIgnore));
    8686    }
    8787
    88     public SearchAction.SearchSetting ignoreTag(Tag tagToIgnore, SearchAction.SearchSetting settingToUpdate) throws SearchParseError {
     88    public SearchSetting ignoreTag(Tag tagToIgnore, SearchSetting settingToUpdate) throws SearchParseError {
    8989        final String forTag = SearchCompiler.buildSearchStringForTag(tagToIgnore.getKey(), tagToIgnore.getValue());
    9090        settingToUpdate.text = settingToUpdate.text.isEmpty()
  • trunk/src/org/openstreetmap/josm/gui/dialogs/properties/TagEditHelper.java

    r12656 r12659  
    7070import org.openstreetmap.josm.data.osm.Tag;
    7171import org.openstreetmap.josm.data.osm.search.SearchParseError;
     72import org.openstreetmap.josm.data.osm.search.SearchSetting;
    7273import org.openstreetmap.josm.data.osm.search.SearchCompiler;
    7374import org.openstreetmap.josm.data.preferences.BooleanProperty;
     
    130131            Collections.<String>emptyList());
    131132    public static final StringProperty PROPERTY_TAGS_TO_IGNORE = new StringProperty("properties.recent-tags.ignore",
    132             new SearchAction.SearchSetting().writeToString());
     133            new SearchSetting().writeToString());
    133134
    134135    /**
     
    163164
    164165    final RecentTagCollection recentTags = new RecentTagCollection(MAX_LRU_TAGS_NUMBER);
    165     SearchAction.SearchSetting tagsToIgnore;
     166    SearchSetting tagsToIgnore;
    166167
    167168    /**
     
    330331
    331332    void loadTagsToIgnore() {
    332         final SearchAction.SearchSetting searchSetting = Utils.firstNonNull(
    333                 SearchAction.SearchSetting.readFromString(PROPERTY_TAGS_TO_IGNORE.get()), new SearchAction.SearchSetting());
     333        final SearchSetting searchSetting = Utils.firstNonNull(
     334                SearchSetting.readFromString(PROPERTY_TAGS_TO_IGNORE.get()), new SearchSetting());
    334335        if (!Objects.equals(tagsToIgnore, searchSetting)) {
    335336            try {
     
    338339            } catch (SearchParseError parseError) {
    339340                warnAboutParseError(parseError);
    340                 tagsToIgnore = new SearchAction.SearchSetting();
     341                tagsToIgnore = new SearchSetting();
    341342                recentTags.setTagsToIgnore(SearchCompiler.Never.INSTANCE);
    342343            }
     
    10241025            @Override
    10251026            public void actionPerformed(ActionEvent e) {
    1026                 final SearchAction.SearchSetting newTagsToIngore = SearchAction.showSearchDialog(tagsToIgnore);
     1027                final SearchSetting newTagsToIngore = SearchAction.showSearchDialog(tagsToIgnore);
    10271028                if (newTagsToIngore == null) {
    10281029                    return;
  • trunk/src/org/openstreetmap/josm/gui/tagging/presets/items/Roles.java

    r12656 r12659  
    1313import javax.swing.JPanel;
    1414
    15 import org.openstreetmap.josm.actions.search.SearchAction;
    1615import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1716import org.openstreetmap.josm.data.osm.Tag;
    1817import org.openstreetmap.josm.data.osm.search.SearchParseError;
     18import org.openstreetmap.josm.data.osm.search.SearchSetting;
    1919import org.openstreetmap.josm.data.osm.search.SearchCompiler;
    2020import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetItem;
     
    7878        public void setMember_expression(String memberExpression) throws SAXException {
    7979            try {
    80                 final SearchAction.SearchSetting searchSetting = new SearchAction.SearchSetting();
     80                final SearchSetting searchSetting = new SearchSetting();
    8181                searchSetting.text = memberExpression;
    8282                searchSetting.caseSensitive = true;
  • trunk/test/unit/org/openstreetmap/josm/actions/CreateMultipolygonActionTest.java

    r12656 r12659  
    1212import org.junit.Test;
    1313import org.openstreetmap.josm.TestUtils;
    14 import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
    1514import org.openstreetmap.josm.command.SequenceCommand;
    1615import org.openstreetmap.josm.data.osm.DataSet;
     
    1918import org.openstreetmap.josm.data.osm.Way;
    2019import org.openstreetmap.josm.data.osm.search.SearchParseError;
     20import org.openstreetmap.josm.data.osm.search.SearchSetting;
    2121import org.openstreetmap.josm.data.osm.search.SearchCompiler;
    2222import org.openstreetmap.josm.io.OsmReader;
  • trunk/test/unit/org/openstreetmap/josm/actions/JoinAreasActionTest.java

    r12643 r12659  
    2525import org.openstreetmap.josm.data.osm.RelationMember;
    2626import org.openstreetmap.josm.data.osm.Way;
     27import org.openstreetmap.josm.data.osm.search.SearchMode;
    2728import org.openstreetmap.josm.gui.MainApplication;
    2829import org.openstreetmap.josm.gui.layer.Layer;
     
    8485            for (String ref : new String[]{"A", "B", "C", "D", "E"}) {
    8586                System.out.print("Joining ways " + ref);
    86                 Collection<OsmPrimitive> found = SearchAction.searchAndReturn("type:way ref="+ref, SearchAction.SearchMode.replace);
     87                Collection<OsmPrimitive> found = SearchAction.searchAndReturn("type:way ref="+ref, SearchMode.replace);
    8788                assertEquals(2, found.size());
    8889
    8990                MainApplication.getMenu().joinAreas.join(Utils.filteredCollection(found, Way.class));
    9091
    91                 Collection<OsmPrimitive> found2 = SearchAction.searchAndReturn("type:way ref="+ref, SearchAction.SearchMode.replace);
     92                Collection<OsmPrimitive> found2 = SearchAction.searchAndReturn("type:way ref="+ref, SearchMode.replace);
    9293                assertEquals(1, found2.size());
    9394                System.out.println(" ==> OK");
  • trunk/test/unit/org/openstreetmap/josm/data/osm/FilterTest.java

    r12656 r12659  
    1616import org.junit.Rule;
    1717import org.junit.Test;
    18 import org.openstreetmap.josm.actions.search.SearchAction.SearchMode;
    1918import org.openstreetmap.josm.data.coor.LatLon;
    2019import org.openstreetmap.josm.data.osm.Filter.FilterPreferenceEntry;
     20import org.openstreetmap.josm.data.osm.search.SearchMode;
    2121import org.openstreetmap.josm.data.osm.search.SearchParseError;
    2222import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
  • trunk/test/unit/org/openstreetmap/josm/data/osm/search/SearchCompilerTest.java

    r12656 r12659  
    1919import org.junit.Test;
    2020import org.openstreetmap.josm.TestUtils;
    21 import org.openstreetmap.josm.actions.search.SearchAction.SearchSetting;
    2221import org.openstreetmap.josm.data.coor.LatLon;
    2322import org.openstreetmap.josm.data.osm.DataSet;
  • trunk/test/unit/org/openstreetmap/josm/data/osm/search/SearchModeTest.java

    r12656 r12659  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.actions.search;
     2package org.openstreetmap.josm.data.osm.search;
    33
    44import org.junit.Rule;
    55import org.junit.Test;
    66import org.openstreetmap.josm.TestUtils;
    7 import org.openstreetmap.josm.actions.search.SearchAction.SearchMode;
    8 import org.openstreetmap.josm.data.osm.search.SearchCompiler;
    97import org.openstreetmap.josm.testutils.JOSMTestRules;
    108
     
    1210
    1311/**
    14  * Unit tests for class {@link SearchCompiler}.
     12 * Unit tests for class {@link SearchMode}.
    1513 */
    16 public class SearchActionTest {
     14public class SearchModeTest {
    1715
    1816    /**
  • trunk/test/unit/org/openstreetmap/josm/gui/dialogs/properties/RecentTagCollectionTest.java

    r12656 r12659  
    1111import org.junit.Rule;
    1212import org.junit.Test;
    13 import org.openstreetmap.josm.actions.search.SearchAction;
    1413import org.openstreetmap.josm.data.osm.Tag;
    1514import org.openstreetmap.josm.data.osm.search.SearchParseError;
     15import org.openstreetmap.josm.data.osm.search.SearchSetting;
    1616import org.openstreetmap.josm.data.preferences.CollectionProperty;
    1717import org.openstreetmap.josm.testutils.JOSMTestRules;
     
    6363        recentTags.add(bar);
    6464        recentTags.add(baz);
    65         final SearchAction.SearchSetting searchSetting = new SearchAction.SearchSetting();
     65        final SearchSetting searchSetting = new SearchSetting();
    6666        recentTags.ignoreTag(baz, searchSetting);
    6767        recentTags.ignoreTag(new Tag("something", "else"), searchSetting);
Note: See TracChangeset for help on using the changeset viewer.