Ignore:
Timestamp:
2010-04-11T12:08:22+02:00 (14 years ago)
Author:
jttt
Message:

Allow to set parameters for actions on toolbar. Used by SearchAction to set searching expression (see #4546)

Location:
trunk/src/org/openstreetmap/josm/actions
Files:
3 added
1 edited

Legend:

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

    r3102 r3175  
    1616import java.util.LinkedList;
    1717import java.util.List;
     18import java.util.Map;
    1819
    1920import javax.swing.ButtonGroup;
     
    2526
    2627import org.openstreetmap.josm.Main;
     28import org.openstreetmap.josm.actions.ActionParameter;
    2729import org.openstreetmap.josm.actions.JosmAction;
     30import org.openstreetmap.josm.actions.ParameterizedAction;
     31import org.openstreetmap.josm.actions.ActionParameter.SearchSettingsActionParameter;
    2832import org.openstreetmap.josm.data.osm.DataSet;
    2933import org.openstreetmap.josm.data.osm.Filter;
     
    3438import org.openstreetmap.josm.tools.Shortcut;
    3539
    36 public class SearchAction extends JosmAction{
     40public class SearchAction extends JosmAction implements ParameterizedAction {
    3741
    3842    public static final int DEFAULT_SEARCH_HISTORY_SIZE = 10;
    3943
     44    private static final String SEARCH_EXPRESSION = "searchExpression";
     45
    4046    public static enum SearchMode {
    41         replace, add, remove, in_selection
     47        replace('R'), add('A'), remove('D'), in_selection('S');
     48
     49        private final char code;
     50
     51        SearchMode(char code) {
     52            this.code = code;
     53        }
     54
     55        public char getCode() {
     56            return code;
     57        }
     58
     59        public static SearchMode fromCode(char code) {
     60            for (SearchMode mode: values()) {
     61                if (mode.getCode() == code)
     62                    return mode;
     63            }
     64            return null;
     65        }
    4266    }
    4367
     
    5680            return;
    5781        search();
     82    }
     83
     84    public void actionPerformed(ActionEvent e, Map<String, Object> parameters) {
     85        if (parameters.get(SEARCH_EXPRESSION) == null) {
     86            actionPerformed(e);
     87        } else {
     88            searchWithoutHistory((SearchSetting) parameters.get(SEARCH_EXPRESSION));
     89        }
    5890    }
    5991
     
    349381            return text.hashCode();
    350382        }
     383
     384        public static SearchSetting readFromString(String s) {
     385            if (s.length() == 0)
     386                return null;
     387
     388            SearchSetting result = new SearchSetting();
     389
     390            int index = 1;
     391
     392            result.mode = SearchMode.fromCode(s.charAt(0));
     393            if (result.mode == null) {
     394                result.mode = SearchMode.replace;
     395                index = 0;
     396            }
     397
     398            while (index < s.length()) {
     399                if (s.charAt(index) == 'C') {
     400                    result.caseSensitive = true;
     401                } else if (s.charAt(index) == 'R') {
     402                    result.regexSearch = true;
     403                } else if (s.charAt(index) == ' ') {
     404                    break;
     405                } else {
     406                    System.out.println("Uknown char in SearchSettings: " + s);
     407                    break;
     408                }
     409                index++;
     410            }
     411
     412            if (index < s.length() && s.charAt(index) == ' ') {
     413                index++;
     414            }
     415
     416            result.text = s.substring(index);
     417
     418            return result;
     419        }
     420
     421        public String writeToString() {
     422            if (text == null || text.length() == 0)
     423                return "";
     424
     425            StringBuilder result = new StringBuilder();
     426            result.append(mode.getCode());
     427            if (caseSensitive) {
     428                result.append('C');
     429            }
     430            if (regexSearch) {
     431                result.append('R');
     432            }
     433            result.append(' ');
     434            result.append(text);
     435            return result.toString();
     436        }
    351437    }
    352438
     
    359445        setEnabled(getEditLayer() != null);
    360446    }
     447
     448    public List<ActionParameter<?>> getActionParameters() {
     449        return Collections.<ActionParameter<?>>singletonList(new SearchSettingsActionParameter(SEARCH_EXPRESSION));
     450    }
    361451}
Note: See TracChangeset for help on using the changeset viewer.