import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
-import org.eclipse.swt.events.KeyAdapter;
-import org.eclipse.swt.events.KeyEvent;
-import org.eclipse.swt.events.MouseAdapter;
-import org.eclipse.swt.events.MouseEvent;
-import org.eclipse.swt.layout.GridData;
-import org.eclipse.swt.layout.GridLayout;
-import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
+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.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
-import eu.etaxonomy.taxeditor.model.ImageResources;
+import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
import eu.etaxonomy.taxeditor.model.Resources;
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
/**
* @author n.hoffmann
public class SearchBar extends WorkbenchWindowControlContribution{
private static final Logger logger = Logger.getLogger(SearchBar.class);
private Text text_search;
- private Button button_search;
private String srv;
private String defaultText = "Use \"*\" for wildcard searching";
- private Button button_configure;
+
+ final private ConfigurationSelectionListener configurationListener = new ConfigurationSelectionListener();
@Override
protected Control createControl(Composite parent) {
//parent.setLayout(new FillLayout());
Composite composite = new Composite(parent, SWT.NONE);
- final GridLayout gridLayout = new GridLayout();
- gridLayout.numColumns = 3;
- gridLayout.marginWidth = 0;
- gridLayout.marginHeight = 0;
- gridLayout.verticalSpacing = 0;
- gridLayout.horizontalSpacing = 0;
- composite.setLayout(gridLayout);
+
+ final RowLayout layout = new RowLayout();
+ layout.wrap = false;
+ layout.pack = true;
+ layout.justify = true;
+ layout.type = SWT.HORIZONTAL;
+ layout.marginLeft = 0;
+ layout.marginTop = 0;
+ layout.marginRight = 0;
+ layout.marginBottom = 0;
+ layout.spacing = 0;
+ composite.setLayout(layout);
+
// FIXME we have this here for debugging purposes, remove color once the layout of the search bar is correct
composite.setBackground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
- text_search = new Text(composite, SWT.SEARCH);
- GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
-
- text_search.setLayoutData(gridData);
+ // TODO for some reason the text_search composite has a margin when either SWT.BORDER or SWT.SEARCH
+ // 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(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
text_search.setText(defaultText);
}
}
});
- text_search.addKeyListener(new KeyAdapter() {
- public void keyReleased(KeyEvent e) {
- int key = e.keyCode;
- if (key == SWT.CR) {
- openSearchResultsView(text_search);
- } else {
- setSearchButtonEnabled();
- }
- }
- });
+
- button_configure = new Button(composite, SWT.DROP_DOWN);
- button_configure.setImage(ImageResources.getImage(ImageResources.MOVE_ICON));
+ final ToolBar toolBar = new ToolBar(composite, SWT.NULL);
- button_search = new Button(composite, SWT.PUSH);
- button_search.setText("Search");
- button_search.setEnabled(false);
- button_search.addMouseListener(new MouseAdapter() {
-
- // Populate search results resultsTable after clicking button
- public void mouseUp(MouseEvent e) {
- openSearchResultsView(text_search);
- }
- });
- return composite;
- }
+ ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN);
+ toolItem.setText("Search");
+
+ DropdownSelectionListener dropdownListener = new DropdownSelectionListener(toolItem);
+
+ dropdownListener.add(SearchOption.TAXON);
+ dropdownListener.add(SearchOption.SYNONYM);
+ dropdownListener.add(SearchOption.NAME);
+ dropdownListener.add(SearchOption.COMMON_NAME);
+
+ toolItem.addSelectionListener(dropdownListener);
- protected void setSearchButtonEnabled() {
- if (text_search.getText().length() == 0) {
- button_search.setEnabled(false);
- } else {
- button_search.setEnabled(true);
- }
+
+ return composite;
}
-
- private void openSearchResultsView(Text searchText) {
- if(searchText.getText().length() > 0){
+
+ private void openSearchResultsView() {
+ if(text_search.getText().trim().length() > 0){
+
+ ITaxonServiceConfigurator configurator = configurationListener.getConfigurator();
+ configurator.setSearchString(text_search.getText().trim());
+
srv += "1";
logger.info("Opening search results window " + srv);
try {
IViewPart resultsView = TaxeditorNavigationPlugin.getDefault().getWorkbench()
.getActiveWorkbenchWindow().getActivePage().showView(SearchResultView.ID,
srv, IWorkbenchPage.VIEW_ACTIVATE);
- ((SearchResultView) resultsView).performSearch(searchText.getText());
+ ((SearchResultView) resultsView).performSearch(configurator);
} catch (PartInitException e) {
logger.error("Error opening search result.", e);
}
}
}
+
+ class DropdownSelectionListener extends SelectionAdapter {
+
+ private Menu menu;
+
+ public DropdownSelectionListener(ToolItem dropdown) {
+ menu = new Menu(dropdown.getParent().getShell());
+ }
+
+ public void add(SearchOption option) {
+ MenuItem menuItem = new MenuItem(menu, SWT.CHECK);
+ menuItem.setData(option);
+ menuItem.setText(option.getLabel());
+ menuItem.setSelection(option.getPreference());
+ menuItem.addSelectionListener(configurationListener);
+ }
+
+ public void widgetSelected(SelectionEvent event) {
+ if (event.detail == SWT.ARROW) {
+ ToolItem item = (ToolItem) event.widget;
+ Rectangle rect = item.getBounds();
+ Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
+ menu.setLocation(pt.x, pt.y + rect.height);
+ menu.setVisible(true);
+ } else {
+ openSearchResultsView();
+ }
+ }
+ }
+
+ class ConfigurationSelectionListener extends SelectionAdapter{
+
+ ITaxonServiceConfigurator configurator;
+
+ /* (non-Javadoc)
+ * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
+ */
+ public void widgetSelected(SelectionEvent e) {
+ logger.warn("configuration menu clicked");
+ SearchOption option = (SearchOption) e.widget.getData();
+
+ if(option == SearchOption.TAXON){
+ getConfigurator().setDoTaxa(configurator.isDoTaxa() ? false : true);
+ }
+
+ if(option == SearchOption.SYNONYM){
+ getConfigurator().setDoSynonyms(configurator.isDoSynonyms() ? false : true);
+ }
+
+ if(option == SearchOption.NAME){
+ getConfigurator().setDoNamesWithoutTaxa(configurator.isDoNamesWithoutTaxa() ? false : true);
+ }
+
+ if(option == SearchOption.COMMON_NAME){
+ getConfigurator().setDoTaxaByCommonNames(configurator.isDoTaxaByCommonNames() ? false : true);
+ }
+ saveConfigurator();
+ }
+
+ public ITaxonServiceConfigurator getConfigurator(){
+ if(configurator == null){
+ configurator = PreferencesUtil.getSearchConfigurator();
+ }
+ return configurator;
+ }
+
+ private void saveConfigurator(){
+ PreferencesUtil.setSearchConfigurator(configurator);
+ this.configurator = PreferencesUtil.getSearchConfigurator();
+ }
+ }
+
+ enum SearchOption {
+ TAXON("Taxa"),
+ SYNONYM("Synonyms"),
+ NAME("Names (without taxa)"),
+ COMMON_NAME("Common Names");
+
+ private String label;
+
+ private SearchOption(String label){
+ this.label = label;
+ }
+
+ public String getLabel(){
+ return label;
+ }
+
+ public boolean getPreference(){
+ switch(this){
+ case TAXON:
+ return PreferencesUtil.getPreferenceStore().getBoolean(PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_TAXA);
+ case SYNONYM:
+ return PreferencesUtil.getPreferenceStore().getBoolean(PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_SYNONYMS);
+ case NAME:
+ return PreferencesUtil.getPreferenceStore().getBoolean(PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_NAMES);
+ case COMMON_NAME:
+ return PreferencesUtil.getPreferenceStore().getBoolean(PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES);
+ }
+
+ return true;
+ }
+
+
+
+ }
}