ref #6566 Migrate SearchBar
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / search / SearchBar.java
index dccf5decd285aa4531b398c4ac0bcba618c9be0f..ee748ada0aee51fd4ea5cb0890f71031e81d15b6 100644 (file)
@@ -1,15 +1,17 @@
-// $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.
  */
 
 package eu.etaxonomy.taxeditor.navigation.search;
 
+import javax.annotation.PostConstruct;
+
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.FocusEvent;
 import org.eclipse.swt.events.FocusListener;
@@ -27,38 +29,41 @@ import org.eclipse.swt.widgets.MenuItem;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.swt.widgets.ToolBar;
 import org.eclipse.swt.widgets.ToolItem;
+import org.eclipse.ui.IMemento;
 import org.eclipse.ui.IViewPart;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
 import org.eclipse.ui.swt.IFocusService;
 
 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
-import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
+import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.IContextListener;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
+import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.preference.Resources;
+import eu.etaxonomy.taxeditor.store.CdmStore;
 
 /**
- * <p>SearchBar class.</p>
- *
  * @author n.hoffmann
  * @author e.-m.lee
  * @created 15.04.2009
  * @version 1.0
  */
-public class SearchBar extends WorkbenchWindowControlContribution{
+public class SearchBar implements IContextListener{
        private Text text_search;
        private String secondaryId;
+       private ToolBar toolBar;
 
        private final String defaultText = Messages.SearchBar_0;
 
        final private ConfigurationSelectionListener configurationListener = new ConfigurationSelectionListener();
 
        /** {@inheritDoc} */
-       @Override
+       @PostConstruct
        protected Control createControl(Composite parent) {
                Composite composite = new Composite(parent, SWT.NONE);
 
@@ -66,6 +71,8 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                createSearchTextField(composite);
                createToolBar(composite);
                registerAtFocusService();
+               //register for context refreshes
+               CdmStore.getContextManager().addContextListener(this);
 
                return composite;
        }
@@ -74,8 +81,8 @@ public class SearchBar extends WorkbenchWindowControlContribution{
         * Handles focus changes for the search textfield.
         */
        private void registerAtFocusService() {
-               IFocusService focusService = 
-                       (IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);
+               IFocusService focusService =
+                       PlatformUI.getWorkbench().getService(IFocusService.class);
                if (focusService != null) {
                        focusService.addFocusTracker(text_search, "navigation.textControlId");
                }
@@ -86,10 +93,11 @@ public class SearchBar extends WorkbenchWindowControlContribution{
         * @param composite
         */
        private void createToolBar(Composite composite) {
-               final ToolBar toolBar = new ToolBar(composite, SWT.NULL);
+               toolBar = new ToolBar(composite, SWT.NULL);
 
                ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
                toolItem.setText(Messages.SearchBar_1);
+               toolBar.setEnabled(false);
 
                DropdownSelectionListener dropdownListener = new DropdownSelectionListener(
                                toolItem);
@@ -111,8 +119,9 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                // is applied. I am not sure how to get rid of this.
                text_search = new Text(composite, SWT.BORDER | SWT.SINGLE
                                | SWT.FULL_SELECTION);
-               text_search.setForeground(NavigationUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
+               text_search.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND));
                text_search.setText(defaultText);
+        text_search.setEnabled(false);
 
                addTextListeners();
        }
@@ -123,26 +132,24 @@ public class SearchBar extends WorkbenchWindowControlContribution{
        private void addTextListeners() {
                text_search.addFocusListener(new FocusListener() {
 
-                       public void focusGained(FocusEvent e) {
-                               text_search.setForeground(NavigationUtil.getColor(Resources.SEARCH_VIEW_FOCUS));
+                       @Override
+            public void focusGained(FocusEvent e) {
+                               text_search.setForeground(AbstractUtility.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.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND));
                                        text_search.setText(defaultText);
                                }
                        }
                });
 
                text_search.addKeyListener(new KeyAdapter() {
-                       /*
-                        * (non-Javadoc)
-                        * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
-                        */
                        @Override
                        public void keyPressed(KeyEvent e) {
                                if (e.keyCode == SWT.CR) {
@@ -175,35 +182,36 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                if(searchString == null){
                        return;
                }
-               
-               if("*".equals(searchString.trim())){
-                       NavigationUtil.warningDialog(Messages.SearchBar_2, this, Messages.SearchBar_3);
+
+               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 +222,13 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                                                        IWorkbenchPage.VIEW_ACTIVATE);
                        ((SearchResultView) resultsView).performSearch(configurator);
                } catch (PartInitException e) {
-                       NavigationUtil.error(this.getClass(), Messages.SearchBar_4, 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 +265,7 @@ public class SearchBar extends WorkbenchWindowControlContribution{
 
        /**
         * Handles search configuration selection.
-        * 
+        *
         * @author n.hoffmann
         * @created Feb 2, 2010
         * @version 1.0
@@ -265,17 +273,9 @@ 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(Messages.SearchBar_5);
                        SearchOption option = (SearchOption) e.widget.getData();
 
                        switch (option){
@@ -290,9 +290,9 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                                break;
                        case COMMON_NAME:
                                configurator.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ? false : true);
-                               break;                          
+                               break;
                        }
-                       
+
                        saveConfigurator();
                }
 
@@ -308,15 +308,15 @@ public class SearchBar extends WorkbenchWindowControlContribution{
 
        /**
         * Available search options.
-        * 
+        *
         * @author n.hoffmann
         * @created Feb 2, 2010
         * @version 1.0
         */
        enum SearchOption {
-               TAXON(Messages.SearchBar_6), 
-               SYNONYM(Messages.SearchBar_7), 
-               NAME(Messages.SearchBar_8), 
+               TAXON(Messages.SearchBar_6),
+               SYNONYM(Messages.SearchBar_7),
+               NAME(Messages.SearchBar_8),
                COMMON_NAME(Messages.SearchBar_9);
 
                private final String label;
@@ -331,7 +331,7 @@ public class SearchBar extends WorkbenchWindowControlContribution{
 
                public boolean getPreference() {
                        if (!PreferencesUtil.getPreferenceStore().contains(
-                                       PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_TAXA)) {
+                                       IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_TAXA)) {
                                // initializes the search configurator
                                PreferencesUtil.initializeSearchConfigurator();
                        }
@@ -339,21 +339,49 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                        switch (this) {
                        case TAXON:
                                boolean result = PreferencesUtil.getPreferenceStore().getBoolean(
-                                                               PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_TAXA);
+                                                               IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_TAXA);
                                return result;
                        case SYNONYM:
                                return PreferencesUtil.getPreferenceStore().getBoolean(
-                                               PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_SYNONYMS);
+                                               IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_SYNONYMS);
                        case NAME:
                                return PreferencesUtil.getPreferenceStore().getBoolean(
-                                               PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_NAMES);
+                                               IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_NAMES);
                        case COMMON_NAME:
                                return PreferencesUtil.getPreferenceStore().getBoolean(
-                                                               PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES);
+                                                               IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES);
                        }
 
                        return true;
                }
 
        }
+
+    @Override
+    public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
+    }
+
+    @Override
+    public void contextStop(IMemento memento, IProgressMonitor monitor) {
+        if(!text_search.isDisposed()){
+            text_search.setEnabled(false);
+        }
+        if(!toolBar.isDisposed()){
+            toolBar.setEnabled(false);
+        }
+    }
+
+    @Override
+    public void contextStart(IMemento memento, IProgressMonitor monitor) {
+        text_search.setEnabled(true);
+        toolBar.setEnabled(true);
+    }
+
+    @Override
+    public void contextRefresh(IProgressMonitor monitor) {
+    }
+
+    @Override
+    public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
+    }
 }