Disable search bar when not connected to CDM store (#3341)
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 5 Oct 2015 10:58:05 +0000 (12:58 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 5 Oct 2015 10:58:05 +0000 (12:58 +0200)
eu.etaxonomy.taxeditor.navigation/src/main/java/eu/etaxonomy/taxeditor/navigation/search/SearchBar.java

index 9b40efb837458f62e5515fced9bf07a5e30717d0..dbfa16e5e54c8b2bac826a4ec62554d78a0bab8e 100644 (file)
@@ -10,6 +10,7 @@
 
 package eu.etaxonomy.taxeditor.navigation.search;
 
 
 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;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.FocusEvent;
 import org.eclipse.swt.events.FocusListener;
@@ -27,6 +28,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.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.IViewPart;
 import org.eclipse.ui.IWorkbenchPage;
 import org.eclipse.ui.PartInitException;
@@ -35,24 +37,25 @@ import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
 import org.eclipse.ui.swt.IFocusService;
 
 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
 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.model.MessagingUtils;
-import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
 import eu.etaxonomy.taxeditor.preference.Resources;
 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
 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
  */
  * @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 Text text_search;
        private String secondaryId;
+       private ToolBar toolBar;
 
        private final String defaultText = Messages.SearchBar_0;
 
 
        private final String defaultText = Messages.SearchBar_0;
 
@@ -67,6 +70,8 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                createSearchTextField(composite);
                createToolBar(composite);
                registerAtFocusService();
                createSearchTextField(composite);
                createToolBar(composite);
                registerAtFocusService();
+               //register for context refreshes
+               CdmStore.getContextManager().addContextListener(this);
 
                return composite;
        }
 
                return composite;
        }
@@ -87,10 +92,11 @@ public class SearchBar extends WorkbenchWindowControlContribution{
         * @param composite
         */
        private void createToolBar(Composite composite) {
         * @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);
 
                ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
                toolItem.setText(Messages.SearchBar_1);
+               toolBar.setEnabled(false);
 
                DropdownSelectionListener dropdownListener = new DropdownSelectionListener(
                                toolItem);
 
                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);
                // 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.setText(defaultText);
+        text_search.setEnabled(false);
 
                addTextListeners();
        }
 
                addTextListeners();
        }
@@ -126,7 +133,7 @@ public class SearchBar extends WorkbenchWindowControlContribution{
 
                        @Override
             public void focusGained(FocusEvent e) {
 
                        @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("");
                                }
                                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() == "") {
                        @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() {
                                        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) {
                        @Override
                        public void keyPressed(KeyEvent e) {
                                if (e.keyCode == SWT.CR) {
@@ -270,13 +273,6 @@ public class SearchBar extends WorkbenchWindowControlContribution{
 
                private IFindTaxaAndNamesConfigurator configurator = PreferencesUtil.getSearchConfigurator();
 
 
                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();
                @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(
 
                public boolean getPreference() {
                        if (!PreferencesUtil.getPreferenceStore().contains(
-                                       PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_TAXA)) {
+                                       IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_TAXA)) {
                                // initializes the search configurator
                                PreferencesUtil.initializeSearchConfigurator();
                        }
                                // initializes the search configurator
                                PreferencesUtil.initializeSearchConfigurator();
                        }
@@ -342,21 +338,49 @@ public class SearchBar extends WorkbenchWindowControlContribution{
                        switch (this) {
                        case TAXON:
                                boolean result = PreferencesUtil.getPreferenceStore().getBoolean(
                        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(
                                return result;
                        case SYNONYM:
                                return PreferencesUtil.getPreferenceStore().getBoolean(
-                                               PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_SYNONYMS);
+                                               IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_SYNONYMS);
                        case NAME:
                                return PreferencesUtil.getPreferenceStore().getBoolean(
                        case NAME:
                                return PreferencesUtil.getPreferenceStore().getBoolean(
-                                               PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_NAMES);
+                                               IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_NAMES);
                        case COMMON_NAME:
                                return PreferencesUtil.getPreferenceStore().getBoolean(
                        case COMMON_NAME:
                                return PreferencesUtil.getPreferenceStore().getBoolean(
-                                                               PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES);
+                                                               IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES);
                        }
 
                        return true;
                }
 
        }
                        }
 
                        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) {
+    }
 }
 }