ref #4611 Restructure string externalization for taxeditor.navigation
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / search / SearchBar.java
index bd04a0febe04f5be864afbb76ce684595bb4d0fe..9d4c93f61939cd00424478085050685fd494c4de 100644 (file)
@@ -1,4 +1,3 @@
-// $Id$
 /**
  * Copyright (C) 2007 EDIT
  * European Distributed Institute of Taxonomy
@@ -10,6 +9,7 @@
 
 package eu.etaxonomy.taxeditor.navigation.search;
 
+import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.FocusEvent;
 import org.eclipse.swt.events.FocusListener;
@@ -27,6 +27,7 @@ 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;
@@ -35,24 +36,26 @@ 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.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.IContextListener;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
 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 extends WorkbenchWindowControlContribution implements IContextListener{
        private Text text_search;
        private String secondaryId;
+       private ToolBar toolBar;
 
        private final String defaultText = Messages.SearchBar_0;
 
@@ -67,6 +70,8 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                createSearchTextField(composite);
                createToolBar(composite);
                registerAtFocusService();
+               //register for context refreshes
+               CdmStore.getContextManager().addContextListener(this);
 
                return composite;
        }
@@ -87,10 +92,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);
@@ -112,8 +118,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();
        }
@@ -126,7 +133,7 @@ public class SearchBar extends WorkbenchWindowControlContribution{
 
                        @Override
             public void focusGained(FocusEvent e) {
-                               text_search.setForeground(NavigationUtil.getColor(Resources.SEARCH_VIEW_FOCUS));
+                               text_search.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOCUS));
                                if (defaultText.equals(text_search.getText())) {
                                        text_search.setText("");
                                }
@@ -135,17 +142,13 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                        @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) {
@@ -179,7 +182,7 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                        return;
                }
 
-               if("*".equals(searchString.trim())){
+               if(!searchString.trim().matches(".*\\p{L}+.*")){
                        MessagingUtils.warningDialog(Messages.SearchBar_2, this, Messages.SearchBar_3);
                        return;
                }
@@ -270,13 +273,6 @@ public class SearchBar extends WorkbenchWindowControlContribution{
 
                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) {
                        SearchOption option = (SearchOption) e.widget.getData();
@@ -334,7 +330,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();
                        }
@@ -342,21 +338,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) {
+    }
 }