Ignore:
Timestamp:
2009-11-14T20:05:18+01:00 (15 years ago)
Author:
Gubaer
Message:

fixed #3434: Add search history to the search dialog

File:
1 edited

Legend:

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

    r2357 r2451  
    88import java.awt.event.ActionEvent;
    99import java.awt.event.KeyEvent;
     10import java.util.ArrayList;
    1011import java.util.Collection;
     12import java.util.Collections;
    1113import java.util.LinkedList;
     14import java.util.List;
    1215
    1316import javax.swing.ButtonGroup;
     
    1720import javax.swing.JPanel;
    1821import javax.swing.JRadioButton;
    19 import javax.swing.JTextField;
    2022
    2123import org.openstreetmap.josm.Main;
    2224import org.openstreetmap.josm.actions.JosmAction;
     25import org.openstreetmap.josm.data.osm.DataSet;
     26import org.openstreetmap.josm.data.osm.Filter;
    2327import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2428import org.openstreetmap.josm.gui.ExtendedDialog;
     29import org.openstreetmap.josm.gui.widgets.HistoryComboBox;
    2530import org.openstreetmap.josm.tools.GBC;
    2631import org.openstreetmap.josm.tools.Shortcut;
    27 import org.openstreetmap.josm.data.osm.Filter;
    28 import org.openstreetmap.josm.data.osm.DataSet;
    2932
    3033public class SearchAction extends JosmAction{
     
    4952        if (!isEnabled())
    5053            return;
    51         if (Main.map == null) {
    52             JOptionPane.showMessageDialog(
    53                     Main.parent,
    54                     tr("Can't search because there is no loaded data."),
    55                     tr("Warning"),
    56                     JOptionPane.WARNING_MESSAGE
    57             );
    58             return;
    59         }
    6054        SearchSetting s = lastSearch;
    6155        if (s == null) {
    62             s = new SearchSetting("", SearchMode.replace, false, false);
     56            s = new SearchSetting("", SearchMode.replace, false /* case insensitive */, false /* no regexp */);
    6357        }
    6458        SearchSetting se = showSearchDialog(s);
     
    6862    }
    6963
     64    public static List<String> getSearchExpressionHistory() {
     65        ArrayList<String> ret = new ArrayList<String>(searchHistory.size());
     66        for (SearchSetting ss: searchHistory) {
     67            ret.add(ss.text);
     68        }
     69        return ret;
     70    }
     71
    7072    public static SearchSetting showSearchDialog(SearchSetting initialValues) {
     73
     74        // -- prepare the combo box with the search expressions
     75        //
    7176        JLabel label = new JLabel( initialValues instanceof Filter ? tr("Please enter a filter string.") : tr("Please enter a search string."));
    72         final JTextField input = new JTextField(initialValues.text);
    73         input.selectAll();
    74         input.requestFocusInWindow();
     77        final HistoryComboBox hcbSearchString = new HistoryComboBox();
     78        hcbSearchString.setText(initialValues.text);
     79        hcbSearchString.getEditor().selectAll();
     80        hcbSearchString.getEditor().getEditorComponent().requestFocusInWindow();
     81        hcbSearchString.setToolTipText(tr("Enter the search expression"));
     82        // we have to reverse the history, because ComboBoxHistory will reverse it again
     83        // in addElement()
     84        //
     85        List<String> searchExpressionHistory = getSearchExpressionHistory();
     86        Collections.reverse(searchExpressionHistory);
     87        hcbSearchString.setPossibleItems(searchExpressionHistory);
     88
    7589        JRadioButton replace = new JRadioButton(tr("replace selection"), initialValues.mode == SearchMode.replace);
    7690        JRadioButton add = new JRadioButton(tr("add to selection"), initialValues.mode == SearchMode.add);
     
    88102        JPanel left = new JPanel(new GridBagLayout());
    89103        left.add(label, GBC.eop());
    90         left.add(input, GBC.eop().fill(GBC.HORIZONTAL));
     104        left.add(hcbSearchString, GBC.eop().fill(GBC.HORIZONTAL));
    91105        left.add(replace, GBC.eol());
    92         DataSet ds = Main.main.getCurrentDataSet();
    93106        left.add(add, GBC.eol());
    94107        left.add(remove, GBC.eol());
     
    147160                : (add.isSelected() ? SearchAction.SearchMode.add
    148161                        : (remove.isSelected() ? SearchAction.SearchMode.remove : SearchAction.SearchMode.in_selection));
    149         initialValues.text = input.getText();
     162        initialValues.text = hcbSearchString.getText();
    150163        initialValues.mode = mode;
    151164        initialValues.caseSensitive = caseSensitive.isSelected();
     
    164177            searchHistory.addFirst(new SearchSetting(s));
    165178        }
    166         while (searchHistory.size() > Main.pref.getInteger("search.history-size", DEFAULT_SEARCH_HISTORY_SIZE)) {
     179        int maxsize = Main.pref.getInteger("search.history-size", DEFAULT_SEARCH_HISTORY_SIZE);
     180        while (searchHistory.size() > maxsize) {
    167181            searchHistory.removeLast();
    168182        }
Note: See TracChangeset for help on using the changeset viewer.