3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.navigation
.search
;
13 import org
.eclipse
.swt
.SWT
;
14 import org
.eclipse
.swt
.events
.FocusEvent
;
15 import org
.eclipse
.swt
.events
.FocusListener
;
16 import org
.eclipse
.swt
.events
.KeyAdapter
;
17 import org
.eclipse
.swt
.events
.KeyEvent
;
18 import org
.eclipse
.swt
.events
.SelectionAdapter
;
19 import org
.eclipse
.swt
.events
.SelectionEvent
;
20 import org
.eclipse
.swt
.graphics
.Point
;
21 import org
.eclipse
.swt
.graphics
.Rectangle
;
22 import org
.eclipse
.swt
.layout
.RowLayout
;
23 import org
.eclipse
.swt
.widgets
.Composite
;
24 import org
.eclipse
.swt
.widgets
.Control
;
25 import org
.eclipse
.swt
.widgets
.Menu
;
26 import org
.eclipse
.swt
.widgets
.MenuItem
;
27 import org
.eclipse
.swt
.widgets
.Text
;
28 import org
.eclipse
.swt
.widgets
.ToolBar
;
29 import org
.eclipse
.swt
.widgets
.ToolItem
;
30 import org
.eclipse
.ui
.IViewPart
;
31 import org
.eclipse
.ui
.IWorkbenchPage
;
32 import org
.eclipse
.ui
.PartInitException
;
33 import org
.eclipse
.ui
.PlatformUI
;
34 import org
.eclipse
.ui
.menus
.WorkbenchWindowControlContribution
;
35 import org
.eclipse
.ui
.swt
.IFocusService
;
37 import eu
.etaxonomy
.cdm
.api
.service
.config
.ITaxonServiceConfigurator
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
40 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
41 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
44 * <p>SearchBar class.</p>
51 public class SearchBar
extends WorkbenchWindowControlContribution
{
52 private Text text_search
;
55 private final String defaultText
= "Use \"*\" for wildcard searching";
57 final private ConfigurationSelectionListener configurationListener
= new ConfigurationSelectionListener();
61 protected Control
createControl(Composite parent
) {
62 Composite composite
= new Composite(parent
, SWT
.NONE
);
64 createLayout(composite
);
65 createSearchTextField(composite
);
66 createToolBar(composite
);
67 registerAtFocusService();
73 * Handles focus changes for the search textfield.
75 private void registerAtFocusService() {
76 IFocusService focusService
=
77 (IFocusService
) PlatformUI
.getWorkbench().getService(IFocusService
.class);
78 if (focusService
!= null) {
79 focusService
.addFocusTracker(text_search
, "navigation.textControlId");
84 * Creates the search toolbar.
87 private void createToolBar(Composite composite
) {
88 final ToolBar toolBar
= new ToolBar(composite
, SWT
.NULL
);
90 ToolItem toolItem
= new ToolItem(toolBar
, SWT
.DROP_DOWN
| SWT
.BORDER
);
91 toolItem
.setText("Search");
93 DropdownSelectionListener dropdownListener
= new DropdownSelectionListener(
96 for(SearchOption searchOption
: SearchOption
.values()){
97 dropdownListener
.add(searchOption
);
100 toolItem
.addSelectionListener(dropdownListener
);
104 * Creates the search textfield.
107 private void createSearchTextField(Composite composite
) {
108 // TODO for some reason the text_search composite has a margin when
109 // either SWT.BORDER or SWT.SEARCH
110 // is applied. I am not sure how to get rid of this.
111 text_search
= new Text(composite
, SWT
.BORDER
| SWT
.SINGLE
112 | SWT
.FULL_SELECTION
);
113 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
114 text_search
.setText(defaultText
);
120 * Adds listeners to the search textfield.
122 private void addTextListeners() {
123 text_search
.addFocusListener(new FocusListener() {
125 public void focusGained(FocusEvent e
) {
126 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOCUS
));
127 if (defaultText
.equals(text_search
.getText())) {
128 text_search
.setText("");
132 public void focusLost(FocusEvent e
) {
133 if (text_search
.getText() == "") {
134 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
135 text_search
.setText(defaultText
);
140 text_search
.addKeyListener(new KeyAdapter() {
143 * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
146 public void keyPressed(KeyEvent e
) {
147 if (e
.keyCode
== SWT
.CR
) {
155 * Creates the search layout.
158 private void createLayout(Composite composite
) {
159 final RowLayout layout
= new RowLayout();
162 layout
.justify
= true;
163 layout
.type
= SWT
.HORIZONTAL
;
164 layout
.marginLeft
= 0;
165 layout
.marginTop
= 0;
166 layout
.marginRight
= 0;
167 layout
.marginBottom
= 0;
169 composite
.setLayout(layout
);
172 private void search(){
173 final String searchString
= getSearchString();
174 if(searchString
== null){
178 if("*".equals(searchString
.trim())){
179 NavigationUtil
.warningDialog("Could not execute search", this, "Please type at least one character when using the \"*\" wildcard.");
184 ITaxonServiceConfigurator configurator
= configurationListener
.getConfigurator();
185 configurator
.setTitleSearchString(searchString
);
186 openSearchResultsView(configurator
);
190 private String
getSearchString(){
191 String searchString
= text_search
.getText().trim();
192 if (searchString
.equals(defaultText
) || searchString
.length() == 0)
198 * Opens a new instance of the search result view to display the result to the user.
200 * @param searchResult
202 private void openSearchResultsView(ITaxonServiceConfigurator configurator
) {
203 // This variable is needed to address new instances of the view.
207 IViewPart resultsView
= TaxeditorNavigationPlugin
.getDefault()
208 .getWorkbench().getActiveWorkbenchWindow()
209 .getActivePage().showView(SearchResultView
.ID
, srv
,
210 IWorkbenchPage
.VIEW_ACTIVATE
);
211 ((SearchResultView
) resultsView
).performSearch(configurator
);
212 } catch (PartInitException e
) {
213 NavigationUtil
.error(this.getClass(), "Error opening search result.", e
);
218 * Handles drop down menu selection. Available items are defined in the enumeration SearchOption.
221 * @created Feb 2, 2010
224 class DropdownSelectionListener
extends SelectionAdapter
{
226 private final Menu menu
;
228 public DropdownSelectionListener(ToolItem dropdown
) {
229 menu
= new Menu(dropdown
.getParent().getShell());
232 public void add(SearchOption option
) {
233 MenuItem menuItem
= new MenuItem(menu
, SWT
.CHECK
);
234 menuItem
.setData(option
);
235 menuItem
.setText(option
.getLabel());
236 menuItem
.setSelection(option
.getPreference());
237 menuItem
.addSelectionListener(configurationListener
);
241 public void widgetSelected(SelectionEvent event
) {
242 if (event
.detail
== SWT
.ARROW
) {
243 ToolItem item
= (ToolItem
) event
.widget
;
244 Rectangle rect
= item
.getBounds();
245 Point pt
= item
.getParent().toDisplay(new Point(rect
.x
, rect
.y
));
246 menu
.setLocation(pt
.x
, pt
.y
+ rect
.height
);
247 menu
.setVisible(true);
255 * Handles search configuration selection.
258 * @created Feb 2, 2010
261 class ConfigurationSelectionListener
extends SelectionAdapter
{
263 private ITaxonServiceConfigurator configurator
= PreferencesUtil
.getSearchConfigurator();
269 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
270 * .swt.events.SelectionEvent)
273 public void widgetSelected(SelectionEvent e
) {
274 NavigationUtil
.info("configuration menu clicked");
275 SearchOption option
= (SearchOption
) e
.widget
.getData();
279 configurator
.setDoTaxa(configurator
.isDoTaxa() ?
false : true);
282 configurator
.setDoSynonyms(configurator
.isDoSynonyms() ?
false : true);
285 configurator
.setDoNamesWithoutTaxa(configurator
.isDoNamesWithoutTaxa() ?
false : true);
288 configurator
.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ?
false : true);
295 public ITaxonServiceConfigurator
getConfigurator() {
299 private void saveConfigurator() {
300 PreferencesUtil
.setSearchConfigurator(getConfigurator());
301 this.configurator
= PreferencesUtil
.getSearchConfigurator();
306 * Available search options.
309 * @created Feb 2, 2010
315 NAME("Names (without taxa)"),
316 COMMON_NAME("Common Names");
318 private final String label
;
320 private SearchOption(String label
) {
324 public String
getLabel() {
328 public boolean getPreference() {
329 if (!PreferencesUtil
.getPreferenceStore().contains(
330 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
)) {
331 // initializes the search configurator
332 PreferencesUtil
.initializeSearchConfigurator();
337 boolean result
= PreferencesUtil
.getPreferenceStore().getBoolean(
338 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
);
341 return PreferencesUtil
.getPreferenceStore().getBoolean(
342 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_SYNONYMS
);
344 return PreferencesUtil
.getPreferenceStore().getBoolean(
345 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_NAMES
);
347 return PreferencesUtil
.getPreferenceStore().getBoolean(
348 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
);