Allow using the search bar only if at least one letter is entered
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / search / SearchBar.java
index 347a5998770085ca81c1324af11cd7508e6e94a3..9b40efb837458f62e5515fced9bf07a5e30717d0 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
  * Copyright (C) 2007 EDIT
- * European Distributed Institute of Taxonomy 
+ * European Distributed Institute of Taxonomy
  * http://www.e-taxonomy.eu
- * 
+ *
  * The contents of this file are subject to the Mozilla Public License Version 1.1
  * See LICENSE.TXT at the top of this package for the full license terms.
  */
@@ -35,6 +35,7 @@ import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
 import org.eclipse.ui.swt.IFocusService;
 
 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
@@ -53,7 +54,7 @@ public class SearchBar extends WorkbenchWindowControlContribution{
        private Text text_search;
        private String secondaryId;
 
-       private final String defaultText = "Use \"*\" for wildcard searching";
+       private final String defaultText = Messages.SearchBar_0;
 
        final private ConfigurationSelectionListener configurationListener = new ConfigurationSelectionListener();
 
@@ -74,7 +75,7 @@ public class SearchBar extends WorkbenchWindowControlContribution{
         * Handles focus changes for the search textfield.
         */
        private void registerAtFocusService() {
-               IFocusService focusService = 
+               IFocusService focusService =
                        (IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);
                if (focusService != null) {
                        focusService.addFocusTracker(text_search, "navigation.textControlId");
@@ -89,7 +90,7 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                final ToolBar toolBar = new ToolBar(composite, SWT.NULL);
 
                ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
-               toolItem.setText("Search");
+               toolItem.setText(Messages.SearchBar_1);
 
                DropdownSelectionListener dropdownListener = new DropdownSelectionListener(
                                toolItem);
@@ -123,14 +124,16 @@ public class SearchBar extends WorkbenchWindowControlContribution{
        private void addTextListeners() {
                text_search.addFocusListener(new FocusListener() {
 
-                       public void focusGained(FocusEvent e) {
+                       @Override
+            public void focusGained(FocusEvent e) {
                                text_search.setForeground(NavigationUtil.getColor(Resources.SEARCH_VIEW_FOCUS));
                                if (defaultText.equals(text_search.getText())) {
                                        text_search.setText("");
                                }
                        }
 
-                       public void focusLost(FocusEvent e) {
+                       @Override
+            public void focusLost(FocusEvent e) {
                                if (text_search.getText() == "") {
                                        text_search.setForeground(NavigationUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
                                        text_search.setText(defaultText);
@@ -175,35 +178,36 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                if(searchString == null){
                        return;
                }
-               
-               if("*".equals(searchString.trim())){
-                       NavigationUtil.warningDialog("Could not execute search", this, "Please type at least one character when using the \"*\" wildcard.");
+
+               if(!searchString.trim().matches(".*\\p{L}+.*")){
+                       MessagingUtils.warningDialog(Messages.SearchBar_2, this, Messages.SearchBar_3);
                        return;
                }
-               
-               
+
+
                IFindTaxaAndNamesConfigurator configurator = configurationListener.getConfigurator();
                configurator.setTitleSearchString(searchString);
                openSearchResultsView(configurator);
-               
+
        }
-       
+
        private String getSearchString(){
                String searchString = text_search.getText().trim();
-               if (searchString.equals(defaultText) || searchString.length() == 0)
-                       return null;
+               if (searchString.equals(defaultText) || searchString.length() == 0) {
+            return null;
+        }
                return searchString;
        }
-       
+
        /**
         * Opens a new instance of the search result view to display the result to the user.
-        * 
+        *
         * @param searchResult
         */
        private void openSearchResultsView(IFindTaxaAndNamesConfigurator configurator) {
                boolean openResultInSeparateWindows = PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.SEARCH_OPEN_RESULTS_IN_SEPARATE_WINDOWS));
                if(openResultInSeparateWindows){
-                       //increment change secondary id so it is unique 
+                       //increment change secondary id so it is unique
                        secondaryId += "1";
                }
 
@@ -214,13 +218,13 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                                                        IWorkbenchPage.VIEW_ACTIVATE);
                        ((SearchResultView) resultsView).performSearch(configurator);
                } catch (PartInitException e) {
-                       NavigationUtil.error(this.getClass(), "Error opening search result.", e);
+                       MessagingUtils.error(this.getClass(), Messages.SearchBar_4, e);
                }
        }
 
        /**
         * Handles drop down menu selection. Available items are defined in the enumeration SearchOption.
-        * 
+        *
         * @author n.hoffmann
         * @created Feb 2, 2010
         * @version 1.0
@@ -257,7 +261,7 @@ public class SearchBar extends WorkbenchWindowControlContribution{
 
        /**
         * Handles search configuration selection.
-        * 
+        *
         * @author n.hoffmann
         * @created Feb 2, 2010
         * @version 1.0
@@ -265,17 +269,16 @@ public class SearchBar extends WorkbenchWindowControlContribution{
        class ConfigurationSelectionListener extends SelectionAdapter {
 
                private IFindTaxaAndNamesConfigurator configurator = PreferencesUtil.getSearchConfigurator();
-               
+
                /*
                 * (non-Javadoc)
-                * 
+                *
                 * @see
                 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
                 * .swt.events.SelectionEvent)
                 */
                @Override
                public void widgetSelected(SelectionEvent e) {
-                       NavigationUtil.info("configuration menu clicked");
                        SearchOption option = (SearchOption) e.widget.getData();
 
                        switch (option){
@@ -290,9 +293,9 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                                break;
                        case COMMON_NAME:
                                configurator.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ? false : true);
-                               break;                          
+                               break;
                        }
-                       
+
                        saveConfigurator();
                }
 
@@ -308,16 +311,16 @@ public class SearchBar extends WorkbenchWindowControlContribution{
 
        /**
         * Available search options.
-        * 
+        *
         * @author n.hoffmann
         * @created Feb 2, 2010
         * @version 1.0
         */
        enum SearchOption {
-               TAXON("Taxa"), 
-               SYNONYM("Synonyms"), 
-               NAME("Names (without taxa)"), 
-               COMMON_NAME("Common Names");
+               TAXON(Messages.SearchBar_6),
+               SYNONYM(Messages.SearchBar_7),
+               NAME(Messages.SearchBar_8),
+               COMMON_NAME(Messages.SearchBar_9);
 
                private final String label;