Ignore:
Timestamp:
2012-01-21T11:08:47+01:00 (12 years ago)
Author:
simon04
Message:

see #7230 - alternative search dialog for beta testing (enabled by setting dialog.search.new to true)

File:
1 edited

Legend:

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

    r4679 r4832  
    88
    99import java.awt.Dimension;
     10import java.awt.FlowLayout;
    1011import java.awt.Font;
    1112import java.awt.GridBagLayout;
    1213import java.awt.event.ActionEvent;
    1314import java.awt.event.KeyEvent;
     15import java.awt.event.MouseAdapter;
     16import java.awt.event.MouseEvent;
    1417import java.util.ArrayList;
     18import java.util.Arrays;
    1519import java.util.Collection;
    1620import java.util.Collections;
     
    2630import javax.swing.JPanel;
    2731import javax.swing.JRadioButton;
     32import javax.swing.JTextField;
     33import javax.swing.text.BadLocationException;
    2834
    2935import org.openstreetmap.josm.Main;
     
    4248import org.openstreetmap.josm.tools.Property;
    4349import org.openstreetmap.josm.tools.Shortcut;
     50import org.openstreetmap.josm.tools.Utils;
    4451
    4552public class SearchAction extends JosmAction implements ParameterizedAction {
     
    151158        public String toString() {
    152159            return s.toString();
     160        }
     161    }
     162
     163    private static class SearchKeywordRow extends JPanel {
     164
     165        private final HistoryComboBox hcb;
     166
     167        public SearchKeywordRow(HistoryComboBox hcb) {
     168            super(new FlowLayout(FlowLayout.LEFT));
     169            this.hcb = hcb;
     170        }
     171
     172        public SearchKeywordRow addTitle(String title) {
     173            add(new JLabel(tr("{0}: ", title)));
     174            return this;
     175        }
     176
     177        public SearchKeywordRow addKeyword(String displayText, final String insertText, String description, String... examples) {
     178            JLabel label = new JLabel("<html><style>td{border:1px solid gray;}</style><table><tr><td>" + displayText + "</td></tr></table></html>");
     179            add(label);
     180            if (description != null || examples.length > 0) {
     181                label.setToolTipText("<html>"
     182                        + description
     183                        + (examples.length > 0 ? ("<ul><li>" + Utils.join("</li><li>", Arrays.asList(examples)) + "</li></ul>") : "")
     184                        + "</html>");
     185            }
     186            if (insertText != null) {
     187                label.addMouseListener(new MouseAdapter() {
     188
     189                    @Override
     190                    public void mouseClicked(MouseEvent e) {
     191                        try {
     192                            JTextField tf = (JTextField) hcb.getEditor().getEditorComponent();
     193                            tf.getDocument().insertString(tf.getCaretPosition(), " " + insertText, null);
     194                        } catch (BadLocationException ex) {
     195                            throw new RuntimeException(ex.getMessage(), ex);
     196                        }
     197                    }
     198                });
     199            }
     200            return this;
    153201        }
    154202    }
     
    188236        allElements.setToolTipText(tr("Also include incomplete and deleted objects in search."));
    189237        final JCheckBox regexSearch   = new JCheckBox(tr("regular expression"), initialValues.regexSearch);
    190         final JCheckBox addOnToolbar  = new JCheckBox(tr("Add toolbar button"), false);
     238        final JCheckBox addOnToolbar  = new JCheckBox(tr("add toolbar button"), false);
    191239
    192240        JPanel top = new JPanel(new GridBagLayout());
     
    206254        }
    207255
    208         JPanel right = new JPanel();
     256        final JPanel right;
     257        if (Main.pref.getBoolean("dialog.search.new", false)) {
     258            right = new JPanel(new GridBagLayout());
     259            buildHintsNew(right, hcbSearchString);
     260        } else {
     261            right = new JPanel();
     262            buildHints(right);
     263        }
     264
     265        final JPanel p = new JPanel(new GridBagLayout());
     266        p.add(top, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 5, 0));
     267        p.add(left, GBC.std().anchor(GBC.NORTH).insets(5, 10, 10, 0));
     268        p.add(right, GBC.eol());
     269        ExtendedDialog dialog = new ExtendedDialog(
     270                Main.parent,
     271                initialValues instanceof Filter ? tr("Filter") : tr("Search"),
     272                        new String[] {
     273                    initialValues instanceof Filter ? tr("Submit filter") : tr("Start Search"),
     274                            tr("Cancel")}
     275        ) {
     276            @Override
     277            protected void buttonAction(int buttonIndex, ActionEvent evt) {
     278                if (buttonIndex == 0) {
     279                    try {
     280                        SearchCompiler.compile(hcbSearchString.getText(), caseSensitive.isSelected(), regexSearch.isSelected());
     281                        super.buttonAction(buttonIndex, evt);
     282                    } catch (ParseError e) {
     283                        JOptionPane.showMessageDialog(
     284                                Main.parent,
     285                                tr("Search expression is not valid: \n\n {0}", e.getMessage()),
     286                                tr("Invalid search expression"),
     287                                JOptionPane.ERROR_MESSAGE);
     288                    }
     289                } else {
     290                    super.buttonAction(buttonIndex, evt);
     291                }
     292            }
     293        };
     294        dialog.setButtonIcons(new String[] {"dialogs/search.png", "cancel.png"});
     295        dialog.configureContextsensitiveHelp("/Action/Search", true /* show help button */);
     296        dialog.setContent(p);
     297        dialog.showDialog();
     298        int result = dialog.getValue();
     299
     300        if(result != 1) return null;
     301
     302        // User pressed OK - let's perform the search
     303        SearchMode mode = replace.isSelected() ? SearchAction.SearchMode.replace
     304                : (add.isSelected() ? SearchAction.SearchMode.add
     305                        : (remove.isSelected() ? SearchAction.SearchMode.remove : SearchAction.SearchMode.in_selection));
     306        initialValues.text = hcbSearchString.getText();
     307        initialValues.mode = mode;
     308        initialValues.caseSensitive = caseSensitive.isSelected();
     309        initialValues.allElements = allElements.isSelected();
     310        initialValues.regexSearch = regexSearch.isSelected();
     311       
     312        if (addOnToolbar.isSelected()) {
     313            ToolbarPreferences.ActionDefinition aDef =
     314                    new ToolbarPreferences.ActionDefinition(Main.main.menu.search);
     315            aDef.getParameters().put("searchExpression", initialValues);
     316            // parametrized action definition is now composed
     317            ActionParser actionParser = new ToolbarPreferences.ActionParser(null);
     318            String res = actionParser.saveAction(aDef);
     319           
     320            Collection<String> t = new LinkedList<String>(ToolbarPreferences.getToolString());
     321            // add custom search button to toolbar preferences
     322            if (!t.contains(res)) t.add(res);
     323            Main.pref.putCollection("toolbar", t);
     324            Main.toolbar.refreshToolbarControl();
     325        }
     326        return initialValues;
     327    }
     328
     329    private static void buildHints(JPanel right) {
    209330        DescriptionTextBuilder descriptionText = new DescriptionTextBuilder();
    210331        descriptionText.append("<html><style>li.header{font-size:110%; list-style-type:none; margin-top:5px;}</style><ul>");
     
    251372        description.setFont(description.getFont().deriveFont(Font.PLAIN));
    252373        right.add(description);
    253 
    254         final JPanel p = new JPanel(new GridBagLayout());
    255         p.add(top, GBC.eol().fill(GBC.HORIZONTAL).insets(5, 5, 5, 0));
    256         p.add(left, GBC.std().anchor(GBC.NORTH).insets(5, 10, 10, 0));
    257         p.add(right, GBC.eol());
    258         ExtendedDialog dialog = new ExtendedDialog(
    259                 Main.parent,
    260                 initialValues instanceof Filter ? tr("Filter") : tr("Search"),
    261                         new String[] {
    262                     initialValues instanceof Filter ? tr("Submit filter") : tr("Start Search"),
    263                             tr("Cancel")}
    264         ) {
    265             @Override
    266             protected void buttonAction(int buttonIndex, ActionEvent evt) {
    267                 if (buttonIndex == 0) {
    268                     try {
    269                         SearchCompiler.compile(hcbSearchString.getText(), caseSensitive.isSelected(), regexSearch.isSelected());
    270                         super.buttonAction(buttonIndex, evt);
    271                     } catch (ParseError e) {
    272                         JOptionPane.showMessageDialog(
    273                                 Main.parent,
    274                                 tr("Search expression is not valid: \n\n {0}", e.getMessage()),
    275                                 tr("Invalid search expression"),
    276                                 JOptionPane.ERROR_MESSAGE);
    277                     }
    278                 } else {
    279                     super.buttonAction(buttonIndex, evt);
    280                 }
    281             }
    282         };
    283         dialog.setButtonIcons(new String[] {"dialogs/search.png", "cancel.png"});
    284         dialog.configureContextsensitiveHelp("/Action/Search", true /* show help button */);
    285         dialog.setContent(p);
    286         dialog.showDialog();
    287         int result = dialog.getValue();
    288 
    289         if(result != 1) return null;
    290 
    291         // User pressed OK - let's perform the search
    292         SearchMode mode = replace.isSelected() ? SearchAction.SearchMode.replace
    293                 : (add.isSelected() ? SearchAction.SearchMode.add
    294                         : (remove.isSelected() ? SearchAction.SearchMode.remove : SearchAction.SearchMode.in_selection));
    295         initialValues.text = hcbSearchString.getText();
    296         initialValues.mode = mode;
    297         initialValues.caseSensitive = caseSensitive.isSelected();
    298         initialValues.allElements = allElements.isSelected();
    299         initialValues.regexSearch = regexSearch.isSelected();
    300        
    301         if (addOnToolbar.isSelected()) {
    302             ToolbarPreferences.ActionDefinition aDef =
    303                     new ToolbarPreferences.ActionDefinition(Main.main.menu.search);
    304             aDef.getParameters().put("searchExpression", initialValues);
    305             // parametrized action definition is now composed
    306             ActionParser actionParser = new ToolbarPreferences.ActionParser(null);
    307             String res = actionParser.saveAction(aDef);
    308            
    309             Collection<String> t = new LinkedList<String>(ToolbarPreferences.getToolString());
    310             // add custom search button to toolbar preferences
    311             if (!t.contains(res)) t.add(res);
    312             Main.pref.putCollection("toolbar", t);
    313             Main.toolbar.refreshToolbarControl();
    314         }
    315         return initialValues;
     374    }
     375
     376    private static void buildHintsNew(JPanel right, HistoryComboBox hcbSearchString) {
     377        right.add(new SearchKeywordRow(hcbSearchString)
     378                .addTitle(tr("basic examples"))
     379                .addKeyword("Baker Street", "Baker Street", tr("''Baker'' and ''Street'' in any key"))
     380                .addKeyword("\"Baker Street\"", "\"Baker Street\"", tr("''Baker Street'' in any key"))
     381                , GBC.eol());
     382        right.add(new SearchKeywordRow(hcbSearchString)
     383                .addTitle(tr("basics"))
     384                .addKeyword("<i>key</i>:<i>valuefragment</i>", null, tr("''valuefragment'' anywhere in ''key''"), "name:str matches name=Bakerstreet")
     385                .addKeyword("-<i>key</i>:<i>valuefragment</i>", null, tr("''valuefragment'' nowhere in ''key''"))
     386                .addKeyword("<i>key</i>=<i>value</i>", null, tr("''key'' with exactly ''value''"))
     387                .addKeyword("<i>key</i>=*", null, tr("''key'' with any value"))
     388                .addKeyword("*=<i>value</i>", null, tr("''value'' in any key"))
     389                .addKeyword("<i>key</i>=", null, tr("matches if ''key'' exists"))
     390                , GBC.eol());
     391        right.add(new SearchKeywordRow(hcbSearchString)
     392                .addTitle(tr("combinators"))
     393                .addKeyword("<i>expr</i> <i>expr</i>", null, tr("logical and (both expressions have to be satisfied)"))
     394                .addKeyword("<i>expr</i> | <i>expr</i>", "| ", tr("logical or (at least one expression has to be satisfied)"))
     395                .addKeyword("<i>expr</i> OR <i>expr</i>", "OR ", tr("logical or (at least one expression has to be satisfied)"))
     396                .addKeyword("-<i>expr</i>", null, tr("logical not"))
     397                .addKeyword("(<i>expr</i>)", null, tr("use parenthesis to group expressions"))
     398                .addKeyword("\"key\"=\"value\"", null, tr("to quote operators.<br>Within quoted strings the <b>\"</b> and <b>\\</b> characters need to be escaped by a preceding <b>\\</b> (e.g. <b>\\\"</b> and <b>\\\\</b>)."), "\"addr:street\"")
     399                , GBC.eol());
     400
     401        if (Main.pref.getBoolean("expert", false)) {
     402            right.add(new SearchKeywordRow(hcbSearchString)
     403                .addTitle(tr("objects"))
     404                .addKeyword("type:node", "type:node ", tr("all ways"))
     405                .addKeyword("type:way", "type:way ", tr("all ways"))
     406                .addKeyword("type:relation", "type:relation ", tr("all relations"))
     407                .addKeyword("closed", "closed ", tr("all closed ways"))
     408                , GBC.eol());
     409            right.add(new SearchKeywordRow(hcbSearchString)
     410                .addTitle(tr("metadata"))
     411                .addKeyword("user:", "user:", tr("objects changed by user", "user:anonymous"))
     412                .addKeyword("id:", "id:", tr("objects with given ID"), "id:0 (new objects)")
     413                .addKeyword("version:", "version:", tr("objects with given version"), "version:0 (objects without an assigned version)")
     414                .addKeyword("changeset:", "changeset:", tr("objects with given changeset ID"), "changeset:0 (objects without an assigned changeset)")
     415                .addKeyword("timestamp:", "timestamp:", tr("objects with last modification timestamp within range"), "timestamp:2012/", "timestamp:2008/2011-02-04T12")
     416                , GBC.eol());
     417            right.add(new SearchKeywordRow(hcbSearchString)
     418                .addTitle(tr("properties"))
     419                .addKeyword("nodes:<i>20-</i>", "nodes:", tr("objects with at least 20 nodes"))
     420                .addKeyword("tags:<i>5-10</i>", "tags:", tr("objects having 5 to 10 tags"))
     421                .addKeyword("role:", "role:", tr("objects with given role in a relation"))
     422                .addKeyword("areasize:<i>-100</i>", "areasize:", tr("closed ways with an area of 100 m\u00b2"))
     423                , GBC.eol());
     424            right.add(new SearchKeywordRow(hcbSearchString)
     425                .addTitle(tr("state"))
     426                .addKeyword("modified", "modified ", tr("all modified objects"))
     427                .addKeyword("new", "new ", tr("all new objects"))
     428                .addKeyword("selected", "selected ", tr("all selected objects"))
     429                .addKeyword("incomplete", "incomplete ", tr("all incomplete objects"))
     430                , GBC.eol());
     431            right.add(new SearchKeywordRow(hcbSearchString)
     432                .addTitle(tr("relations"))
     433                .addKeyword("child <i>expr</i>", "child ", tr("all children of objects matching the expression"), "child building")
     434                .addKeyword("parent <i>expr</i>", "parent ", tr("all parents of objects matching the expression"), "parent bus_stop")
     435                , GBC.eol());
     436            right.add(new SearchKeywordRow(hcbSearchString)
     437                .addTitle(tr("view"))
     438                .addKeyword("inview", "inview ", tr("objects in current view"))
     439                .addKeyword("allinview", "allinview ", tr("objects (and all its way nodes / relation members) in current view"))
     440                .addKeyword("indownloadedarea", "indownloadedarea ", tr("objects in downloaded area"))
     441                .addKeyword("allindownloadedarea", "allindownloadedarea ", tr("objects (and all its way nodes / relation members) in downloaded area"))
     442                , GBC.eol());
     443        }
    316444    }
    317445
Note: See TracChangeset for help on using the changeset viewer.