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 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){
177 if("*".equals(searchString
.trim())){
178 NavigationUtil
.warningDialog("Could not execute search", this, "Please type at least one character when using the \"*\" wildcard.");
182 ITaxonServiceConfigurator configurator
= configurationListener
.getConfigurator();
183 configurator
.setSearchString(searchString
);
184 openSearchResultsView(configurator
);
188 private String
getSearchString(){
189 String searchString
= text_search
.getText().trim();
190 if (searchString
.equals(defaultText
) || searchString
.length() == 0)
196 * Opens a new instance of the search result view to display the result to the user.
198 * @param searchResult
200 private void openSearchResultsView(ITaxonServiceConfigurator configurator
) {
201 // This variable is needed to address new instances of the view.
205 IViewPart resultsView
= TaxeditorNavigationPlugin
.getDefault()
206 .getWorkbench().getActiveWorkbenchWindow()
207 .getActivePage().showView(SearchResultView
.ID
, srv
,
208 IWorkbenchPage
.VIEW_ACTIVATE
);
209 ((SearchResultView
) resultsView
).performSearch(configurator
);
210 } catch (PartInitException e
) {
211 NavigationUtil
.error(this.getClass(), "Error opening search result.", e
);
216 * Handles drop down menu selection. Available items are defined in the enumeration SearchOption.
219 * @created Feb 2, 2010
222 class DropdownSelectionListener
extends SelectionAdapter
{
226 public DropdownSelectionListener(ToolItem dropdown
) {
227 menu
= new Menu(dropdown
.getParent().getShell());
230 public void add(SearchOption option
) {
231 MenuItem menuItem
= new MenuItem(menu
, SWT
.CHECK
);
232 menuItem
.setData(option
);
233 menuItem
.setText(option
.getLabel());
234 menuItem
.setSelection(option
.getPreference());
235 menuItem
.addSelectionListener(configurationListener
);
238 public void widgetSelected(SelectionEvent event
) {
239 if (event
.detail
== SWT
.ARROW
) {
240 ToolItem item
= (ToolItem
) event
.widget
;
241 Rectangle rect
= item
.getBounds();
242 Point pt
= item
.getParent().toDisplay(new Point(rect
.x
, rect
.y
));
243 menu
.setLocation(pt
.x
, pt
.y
+ rect
.height
);
244 menu
.setVisible(true);
252 * Handles search configuration selection.
255 * @created Feb 2, 2010
258 class ConfigurationSelectionListener
extends SelectionAdapter
{
260 private ITaxonServiceConfigurator configurator
= PreferencesUtil
.getSearchConfigurator();
266 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
267 * .swt.events.SelectionEvent)
269 public void widgetSelected(SelectionEvent e
) {
270 NavigationUtil
.info("configuration menu clicked");
271 SearchOption option
= (SearchOption
) e
.widget
.getData();
275 configurator
.setDoTaxa(configurator
.isDoTaxa() ?
false : true);
278 configurator
.setDoSynonyms(configurator
.isDoSynonyms() ?
false : true);
281 configurator
.setDoNamesWithoutTaxa(configurator
.isDoNamesWithoutTaxa() ?
false : true);
284 configurator
.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ?
false : true);
291 public ITaxonServiceConfigurator
getConfigurator() {
295 private void saveConfigurator() {
296 PreferencesUtil
.setSearchConfigurator(getConfigurator());
297 this.configurator
= PreferencesUtil
.getSearchConfigurator();
302 * Available search options.
305 * @created Feb 2, 2010
311 NAME("Names (without taxa)"),
312 COMMON_NAME("Common Names");
314 private String label
;
316 private SearchOption(String label
) {
320 public String
getLabel() {
324 public boolean getPreference() {
325 if (!PreferencesUtil
.getPreferenceStore().contains(
326 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
)) {
327 // initializes the search configurator
328 PreferencesUtil
.initializeSearchConfigurator();
333 boolean result
= PreferencesUtil
.getPreferenceStore().getBoolean(
334 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
);
337 return PreferencesUtil
.getPreferenceStore().getBoolean(
338 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_SYNONYMS
);
340 return PreferencesUtil
.getPreferenceStore().getBoolean(
341 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_NAMES
);
343 return PreferencesUtil
.getPreferenceStore().getBoolean(
344 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
);