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
.IFindTaxaAndNamesConfigurator
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
40 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
41 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
42 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
45 * <p>SearchBar class.</p>
52 public class SearchBar
extends WorkbenchWindowControlContribution
{
53 private Text text_search
;
54 private String secondaryId
;
56 private final String defaultText
= Messages
.SearchBar_0
;
58 final private ConfigurationSelectionListener configurationListener
= new ConfigurationSelectionListener();
62 protected Control
createControl(Composite parent
) {
63 Composite composite
= new Composite(parent
, SWT
.NONE
);
65 createLayout(composite
);
66 createSearchTextField(composite
);
67 createToolBar(composite
);
68 registerAtFocusService();
74 * Handles focus changes for the search textfield.
76 private void registerAtFocusService() {
77 IFocusService focusService
=
78 (IFocusService
) PlatformUI
.getWorkbench().getService(IFocusService
.class);
79 if (focusService
!= null) {
80 focusService
.addFocusTracker(text_search
, "navigation.textControlId");
85 * Creates the search toolbar.
88 private void createToolBar(Composite composite
) {
89 final ToolBar toolBar
= new ToolBar(composite
, SWT
.NULL
);
91 ToolItem toolItem
= new ToolItem(toolBar
, SWT
.DROP_DOWN
| SWT
.BORDER
);
92 toolItem
.setText(Messages
.SearchBar_1
);
94 DropdownSelectionListener dropdownListener
= new DropdownSelectionListener(
97 for(SearchOption searchOption
: SearchOption
.values()){
98 dropdownListener
.add(searchOption
);
101 toolItem
.addSelectionListener(dropdownListener
);
105 * Creates the search textfield.
108 private void createSearchTextField(Composite composite
) {
109 // TODO for some reason the text_search composite has a margin when
110 // either SWT.BORDER or SWT.SEARCH
111 // is applied. I am not sure how to get rid of this.
112 text_search
= new Text(composite
, SWT
.BORDER
| SWT
.SINGLE
113 | SWT
.FULL_SELECTION
);
114 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
115 text_search
.setText(defaultText
);
121 * Adds listeners to the search textfield.
123 private void addTextListeners() {
124 text_search
.addFocusListener(new FocusListener() {
126 public void focusGained(FocusEvent e
) {
127 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOCUS
));
128 if (defaultText
.equals(text_search
.getText())) {
129 text_search
.setText("");
133 public void focusLost(FocusEvent e
) {
134 if (text_search
.getText() == "") {
135 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
136 text_search
.setText(defaultText
);
141 text_search
.addKeyListener(new KeyAdapter() {
144 * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
147 public void keyPressed(KeyEvent e
) {
148 if (e
.keyCode
== SWT
.CR
) {
156 * Creates the search layout.
159 private void createLayout(Composite composite
) {
160 final RowLayout layout
= new RowLayout();
163 layout
.justify
= true;
164 layout
.type
= SWT
.HORIZONTAL
;
165 layout
.marginLeft
= 0;
166 layout
.marginTop
= 0;
167 layout
.marginRight
= 0;
168 layout
.marginBottom
= 0;
170 composite
.setLayout(layout
);
173 private void search(){
174 final String searchString
= getSearchString();
175 if(searchString
== null){
179 if("*".equals(searchString
.trim())){
180 NavigationUtil
.warningDialog(Messages
.SearchBar_2
, this, Messages
.SearchBar_3
);
185 IFindTaxaAndNamesConfigurator configurator
= configurationListener
.getConfigurator();
186 configurator
.setTitleSearchString(searchString
);
187 openSearchResultsView(configurator
);
191 private String
getSearchString(){
192 String searchString
= text_search
.getText().trim();
193 if (searchString
.equals(defaultText
) || searchString
.length() == 0)
199 * Opens a new instance of the search result view to display the result to the user.
201 * @param searchResult
203 private void openSearchResultsView(IFindTaxaAndNamesConfigurator configurator
) {
204 boolean openResultInSeparateWindows
= PreferencesUtil
.getPreferenceStore().getBoolean((IPreferenceKeys
.SEARCH_OPEN_RESULTS_IN_SEPARATE_WINDOWS
));
205 if(openResultInSeparateWindows
){
206 //increment change secondary id so it is unique
211 IViewPart resultsView
= TaxeditorNavigationPlugin
.getDefault()
212 .getWorkbench().getActiveWorkbenchWindow()
213 .getActivePage().showView(SearchResultView
.ID
, secondaryId
,
214 IWorkbenchPage
.VIEW_ACTIVATE
);
215 ((SearchResultView
) resultsView
).performSearch(configurator
);
216 } catch (PartInitException e
) {
217 NavigationUtil
.error(this.getClass(), Messages
.SearchBar_4
, e
);
222 * Handles drop down menu selection. Available items are defined in the enumeration SearchOption.
225 * @created Feb 2, 2010
228 class DropdownSelectionListener
extends SelectionAdapter
{
230 private final Menu menu
;
232 public DropdownSelectionListener(ToolItem dropdown
) {
233 menu
= new Menu(dropdown
.getParent().getShell());
236 public void add(SearchOption option
) {
237 MenuItem menuItem
= new MenuItem(menu
, SWT
.CHECK
);
238 menuItem
.setData(option
);
239 menuItem
.setText(option
.getLabel());
240 menuItem
.setSelection(option
.getPreference());
241 menuItem
.addSelectionListener(configurationListener
);
245 public void widgetSelected(SelectionEvent event
) {
246 if (event
.detail
== SWT
.ARROW
) {
247 ToolItem item
= (ToolItem
) event
.widget
;
248 Rectangle rect
= item
.getBounds();
249 Point pt
= item
.getParent().toDisplay(new Point(rect
.x
, rect
.y
));
250 menu
.setLocation(pt
.x
, pt
.y
+ rect
.height
);
251 menu
.setVisible(true);
259 * Handles search configuration selection.
262 * @created Feb 2, 2010
265 class ConfigurationSelectionListener
extends SelectionAdapter
{
267 private IFindTaxaAndNamesConfigurator configurator
= PreferencesUtil
.getSearchConfigurator();
273 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
274 * .swt.events.SelectionEvent)
277 public void widgetSelected(SelectionEvent e
) {
278 NavigationUtil
.info(Messages
.SearchBar_5
);
279 SearchOption option
= (SearchOption
) e
.widget
.getData();
283 configurator
.setDoTaxa(configurator
.isDoTaxa() ?
false : true);
286 configurator
.setDoSynonyms(configurator
.isDoSynonyms() ?
false : true);
289 configurator
.setDoNamesWithoutTaxa(configurator
.isDoNamesWithoutTaxa() ?
false : true);
292 configurator
.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ?
false : true);
299 public IFindTaxaAndNamesConfigurator
getConfigurator() {
303 private void saveConfigurator() {
304 PreferencesUtil
.setSearchConfigurator(getConfigurator());
305 this.configurator
= PreferencesUtil
.getSearchConfigurator();
310 * Available search options.
313 * @created Feb 2, 2010
317 TAXON(Messages
.SearchBar_6
),
318 SYNONYM(Messages
.SearchBar_7
),
319 NAME(Messages
.SearchBar_8
),
320 COMMON_NAME(Messages
.SearchBar_9
);
322 private final String label
;
324 private SearchOption(String label
) {
328 public String
getLabel() {
332 public boolean getPreference() {
333 if (!PreferencesUtil
.getPreferenceStore().contains(
334 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
)) {
335 // initializes the search configurator
336 PreferencesUtil
.initializeSearchConfigurator();
341 boolean result
= PreferencesUtil
.getPreferenceStore().getBoolean(
342 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
);
345 return PreferencesUtil
.getPreferenceStore().getBoolean(
346 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_SYNONYMS
);
348 return PreferencesUtil
.getPreferenceStore().getBoolean(
349 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_NAMES
);
351 return PreferencesUtil
.getPreferenceStore().getBoolean(
352 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
);