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
.apache
.log4j
.Logger
;
14 import org
.eclipse
.swt
.SWT
;
15 import org
.eclipse
.swt
.events
.FocusEvent
;
16 import org
.eclipse
.swt
.events
.FocusListener
;
17 import org
.eclipse
.swt
.events
.KeyAdapter
;
18 import org
.eclipse
.swt
.events
.KeyEvent
;
19 import org
.eclipse
.swt
.events
.SelectionAdapter
;
20 import org
.eclipse
.swt
.events
.SelectionEvent
;
21 import org
.eclipse
.swt
.graphics
.Point
;
22 import org
.eclipse
.swt
.graphics
.Rectangle
;
23 import org
.eclipse
.swt
.layout
.RowLayout
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Control
;
26 import org
.eclipse
.swt
.widgets
.Menu
;
27 import org
.eclipse
.swt
.widgets
.MenuItem
;
28 import org
.eclipse
.swt
.widgets
.Text
;
29 import org
.eclipse
.swt
.widgets
.ToolBar
;
30 import org
.eclipse
.swt
.widgets
.ToolItem
;
31 import org
.eclipse
.ui
.IViewPart
;
32 import org
.eclipse
.ui
.IWorkbenchPage
;
33 import org
.eclipse
.ui
.PartInitException
;
34 import org
.eclipse
.ui
.PlatformUI
;
35 import org
.eclipse
.ui
.menus
.WorkbenchWindowControlContribution
;
36 import org
.eclipse
.ui
.swt
.IFocusService
;
38 import eu
.etaxonomy
.cdm
.api
.service
.config
.ITaxonServiceConfigurator
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
40 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
41 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
42 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
50 public class SearchBar
extends WorkbenchWindowControlContribution
{
51 private static final Logger logger
= Logger
.getLogger(SearchBar
.class);
52 private Text text_search
;
55 private String defaultText
= "Use \"*\" for wildcard searching";
57 final private ConfigurationSelectionListener configurationListener
= new ConfigurationSelectionListener();
60 protected Control
createControl(Composite parent
) {
61 Composite composite
= new Composite(parent
, SWT
.NONE
);
63 createLayout(composite
);
64 createSearchTextField(composite
);
65 createToolBar(composite
);
66 registerAtFocusService();
72 * Handles focus changes for the search textfield.
74 private void registerAtFocusService() {
75 IFocusService focusService
=
76 (IFocusService
) PlatformUI
.getWorkbench().getService(IFocusService
.class);
77 if (focusService
!= null) {
78 focusService
.addFocusTracker(text_search
, "navigation.textControlId");
83 * Creates the search toolbar.
86 private void createToolBar(Composite composite
) {
87 final ToolBar toolBar
= new ToolBar(composite
, SWT
.NULL
);
89 ToolItem toolItem
= new ToolItem(toolBar
, SWT
.DROP_DOWN
| SWT
.BORDER
);
90 toolItem
.setText("Search");
92 DropdownSelectionListener dropdownListener
= new DropdownSelectionListener(
95 for(SearchOption searchOption
: SearchOption
.values()){
96 dropdownListener
.add(searchOption
);
99 toolItem
.addSelectionListener(dropdownListener
);
103 * Creates the search textfield.
106 private void createSearchTextField(Composite composite
) {
107 // TODO for some reason the text_search composite has a margin when
108 // either SWT.BORDER or SWT.SEARCH
109 // is applied. I am not sure how to get rid of this.
110 text_search
= new Text(composite
, SWT
.BORDER
| SWT
.SINGLE
111 | SWT
.FULL_SELECTION
);
112 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
113 text_search
.setText(defaultText
);
119 * Adds listeners to the search textfield.
121 private void addTextListeners() {
122 text_search
.addFocusListener(new FocusListener() {
124 public void focusGained(FocusEvent e
) {
125 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOCUS
));
126 if (defaultText
.equals(text_search
.getText())) {
127 text_search
.setText("");
131 public void focusLost(FocusEvent e
) {
132 if (text_search
.getText() == "") {
133 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
134 text_search
.setText(defaultText
);
139 text_search
.addKeyListener(new KeyAdapter() {
144 * org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt
148 public void keyReleased(KeyEvent e
) {
149 if (e
.keyCode
== SWT
.CR
) {
157 * Creates the search layout.
160 private void createLayout(Composite composite
) {
161 final RowLayout layout
= new RowLayout();
164 layout
.justify
= true;
165 layout
.type
= SWT
.HORIZONTAL
;
166 layout
.marginLeft
= 0;
167 layout
.marginTop
= 0;
168 layout
.marginRight
= 0;
169 layout
.marginBottom
= 0;
171 composite
.setLayout(layout
);
174 private void search(){
175 final String searchString
= getSearchString();
176 if(searchString
!= null){
177 ITaxonServiceConfigurator configurator
= configurationListener
.getConfigurator();
178 configurator
.setSearchString(searchString
);
179 openSearchResultsView(configurator
);
184 private String
getSearchString(){
185 String searchString
= text_search
.getText().trim();
186 if (searchString
.equals(defaultText
) || searchString
.length() == 0)
192 * Opens a new instance of the search result view to display the result to the user.
194 * @param searchResult
196 private void openSearchResultsView(ITaxonServiceConfigurator configurator
) {
197 // This variable is needed to address new instances of the view.
199 logger
.info("Opening search results window " + srv
);
201 IViewPart resultsView
= TaxeditorNavigationPlugin
.getDefault()
202 .getWorkbench().getActiveWorkbenchWindow()
203 .getActivePage().showView(SearchResultView
.ID
, srv
,
204 IWorkbenchPage
.VIEW_ACTIVATE
);
205 ((SearchResultView
) resultsView
).performSearch(configurator
);
206 } catch (PartInitException e
) {
207 logger
.error("Error opening search result.", e
);
208 throw new RuntimeException(e
);
213 * Handles drop down menu selection. Available items are defined in the enumeration SearchOption.
216 * @created Feb 2, 2010
219 class DropdownSelectionListener
extends SelectionAdapter
{
223 public DropdownSelectionListener(ToolItem dropdown
) {
224 menu
= new Menu(dropdown
.getParent().getShell());
227 public void add(SearchOption option
) {
228 MenuItem menuItem
= new MenuItem(menu
, SWT
.CHECK
);
229 menuItem
.setData(option
);
230 menuItem
.setText(option
.getLabel());
231 menuItem
.setSelection(option
.getPreference());
232 menuItem
.addSelectionListener(configurationListener
);
235 public void widgetSelected(SelectionEvent event
) {
236 if (event
.detail
== SWT
.ARROW
) {
237 ToolItem item
= (ToolItem
) event
.widget
;
238 Rectangle rect
= item
.getBounds();
239 Point pt
= item
.getParent().toDisplay(new Point(rect
.x
, rect
.y
));
240 menu
.setLocation(pt
.x
, pt
.y
+ rect
.height
);
241 menu
.setVisible(true);
249 * Handles search configuration selection.
252 * @created Feb 2, 2010
255 class ConfigurationSelectionListener
extends SelectionAdapter
{
257 private ITaxonServiceConfigurator configurator
= PreferencesUtil
.getSearchConfigurator();
263 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
264 * .swt.events.SelectionEvent)
266 public void widgetSelected(SelectionEvent e
) {
267 logger
.warn("configuration menu clicked");
268 SearchOption option
= (SearchOption
) e
.widget
.getData();
272 configurator
.setDoTaxa(configurator
.isDoTaxa() ?
false : true);
275 configurator
.setDoSynonyms(configurator
.isDoSynonyms() ?
false : true);
278 configurator
.setDoNamesWithoutTaxa(configurator
.isDoNamesWithoutTaxa() ?
false : true);
281 configurator
.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ?
false : true);
288 public ITaxonServiceConfigurator
getConfigurator() {
292 private void saveConfigurator() {
293 PreferencesUtil
.setSearchConfigurator(getConfigurator());
294 this.configurator
= PreferencesUtil
.getSearchConfigurator();
299 * Available search options.
302 * @created Feb 2, 2010
306 TAXON("Taxa"), SYNONYM("Synonyms"), NAME("Names (without taxa)"),
307 COMMON_NAME("Common Names");
309 private String label
;
311 private SearchOption(String label
) {
315 public String
getLabel() {
319 public boolean getPreference() {
320 if (!PreferencesUtil
.getPreferenceStore().contains(
321 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
)) {
322 // initializes the search configurator
323 PreferencesUtil
.initializeSearchConfigurator();
328 boolean result
= PreferencesUtil
.getPreferenceStore().getBoolean(
329 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
);
332 return PreferencesUtil
.getPreferenceStore().getBoolean(
333 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_SYNONYMS
);
335 return PreferencesUtil
.getPreferenceStore().getBoolean(
336 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_NAMES
);
338 return PreferencesUtil
.getPreferenceStore().getBoolean(
339 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
);