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
.model
.MessagingUtils
;
39 import eu
.etaxonomy
.taxeditor
.navigation
.NavigationUtil
;
40 import eu
.etaxonomy
.taxeditor
.navigation
.internal
.TaxeditorNavigationPlugin
;
41 import eu
.etaxonomy
.taxeditor
.preference
.IPreferenceKeys
;
42 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
43 import eu
.etaxonomy
.taxeditor
.preference
.Resources
;
46 * <p>SearchBar class.</p>
53 public class SearchBar
extends WorkbenchWindowControlContribution
{
54 private Text text_search
;
55 private String secondaryId
;
57 private final String defaultText
= Messages
.SearchBar_0
;
59 final private ConfigurationSelectionListener configurationListener
= new ConfigurationSelectionListener();
63 protected Control
createControl(Composite parent
) {
64 Composite composite
= new Composite(parent
, SWT
.NONE
);
66 createLayout(composite
);
67 createSearchTextField(composite
);
68 createToolBar(composite
);
69 registerAtFocusService();
75 * Handles focus changes for the search textfield.
77 private void registerAtFocusService() {
78 IFocusService focusService
=
79 (IFocusService
) PlatformUI
.getWorkbench().getService(IFocusService
.class);
80 if (focusService
!= null) {
81 focusService
.addFocusTracker(text_search
, "navigation.textControlId");
86 * Creates the search toolbar.
89 private void createToolBar(Composite composite
) {
90 final ToolBar toolBar
= new ToolBar(composite
, SWT
.NULL
);
92 ToolItem toolItem
= new ToolItem(toolBar
, SWT
.DROP_DOWN
| SWT
.BORDER
);
93 toolItem
.setText(Messages
.SearchBar_1
);
95 DropdownSelectionListener dropdownListener
= new DropdownSelectionListener(
98 for(SearchOption searchOption
: SearchOption
.values()){
99 dropdownListener
.add(searchOption
);
102 toolItem
.addSelectionListener(dropdownListener
);
106 * Creates the search textfield.
109 private void createSearchTextField(Composite composite
) {
110 // TODO for some reason the text_search composite has a margin when
111 // either SWT.BORDER or SWT.SEARCH
112 // is applied. I am not sure how to get rid of this.
113 text_search
= new Text(composite
, SWT
.BORDER
| SWT
.SINGLE
114 | SWT
.FULL_SELECTION
);
115 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
116 text_search
.setText(defaultText
);
122 * Adds listeners to the search textfield.
124 private void addTextListeners() {
125 text_search
.addFocusListener(new FocusListener() {
127 public void focusGained(FocusEvent e
) {
128 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOCUS
));
129 if (defaultText
.equals(text_search
.getText())) {
130 text_search
.setText("");
134 public void focusLost(FocusEvent e
) {
135 if (text_search
.getText() == "") {
136 text_search
.setForeground(NavigationUtil
.getColor(Resources
.SEARCH_VIEW_FOREGROUND
));
137 text_search
.setText(defaultText
);
142 text_search
.addKeyListener(new KeyAdapter() {
145 * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
148 public void keyPressed(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){
180 if("*".equals(searchString
.trim())){
181 MessagingUtils
.warningDialog(Messages
.SearchBar_2
, this, Messages
.SearchBar_3
);
186 IFindTaxaAndNamesConfigurator configurator
= configurationListener
.getConfigurator();
187 configurator
.setTitleSearchString(searchString
);
188 openSearchResultsView(configurator
);
192 private String
getSearchString(){
193 String searchString
= text_search
.getText().trim();
194 if (searchString
.equals(defaultText
) || searchString
.length() == 0)
200 * Opens a new instance of the search result view to display the result to the user.
202 * @param searchResult
204 private void openSearchResultsView(IFindTaxaAndNamesConfigurator configurator
) {
205 boolean openResultInSeparateWindows
= PreferencesUtil
.getPreferenceStore().getBoolean((IPreferenceKeys
.SEARCH_OPEN_RESULTS_IN_SEPARATE_WINDOWS
));
206 if(openResultInSeparateWindows
){
207 //increment change secondary id so it is unique
212 IViewPart resultsView
= TaxeditorNavigationPlugin
.getDefault()
213 .getWorkbench().getActiveWorkbenchWindow()
214 .getActivePage().showView(SearchResultView
.ID
, secondaryId
,
215 IWorkbenchPage
.VIEW_ACTIVATE
);
216 ((SearchResultView
) resultsView
).performSearch(configurator
);
217 } catch (PartInitException e
) {
218 MessagingUtils
.error(this.getClass(), Messages
.SearchBar_4
, e
);
223 * Handles drop down menu selection. Available items are defined in the enumeration SearchOption.
226 * @created Feb 2, 2010
229 class DropdownSelectionListener
extends SelectionAdapter
{
231 private final Menu menu
;
233 public DropdownSelectionListener(ToolItem dropdown
) {
234 menu
= new Menu(dropdown
.getParent().getShell());
237 public void add(SearchOption option
) {
238 MenuItem menuItem
= new MenuItem(menu
, SWT
.CHECK
);
239 menuItem
.setData(option
);
240 menuItem
.setText(option
.getLabel());
241 menuItem
.setSelection(option
.getPreference());
242 menuItem
.addSelectionListener(configurationListener
);
246 public void widgetSelected(SelectionEvent event
) {
247 if (event
.detail
== SWT
.ARROW
) {
248 ToolItem item
= (ToolItem
) event
.widget
;
249 Rectangle rect
= item
.getBounds();
250 Point pt
= item
.getParent().toDisplay(new Point(rect
.x
, rect
.y
));
251 menu
.setLocation(pt
.x
, pt
.y
+ rect
.height
);
252 menu
.setVisible(true);
260 * Handles search configuration selection.
263 * @created Feb 2, 2010
266 class ConfigurationSelectionListener
extends SelectionAdapter
{
268 private IFindTaxaAndNamesConfigurator configurator
= PreferencesUtil
.getSearchConfigurator();
274 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
275 * .swt.events.SelectionEvent)
278 public void widgetSelected(SelectionEvent e
) {
279 MessagingUtils
.info(Messages
.SearchBar_5
);
280 SearchOption option
= (SearchOption
) e
.widget
.getData();
284 configurator
.setDoTaxa(configurator
.isDoTaxa() ?
false : true);
287 configurator
.setDoSynonyms(configurator
.isDoSynonyms() ?
false : true);
290 configurator
.setDoNamesWithoutTaxa(configurator
.isDoNamesWithoutTaxa() ?
false : true);
293 configurator
.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ?
false : true);
300 public IFindTaxaAndNamesConfigurator
getConfigurator() {
304 private void saveConfigurator() {
305 PreferencesUtil
.setSearchConfigurator(getConfigurator());
306 this.configurator
= PreferencesUtil
.getSearchConfigurator();
311 * Available search options.
314 * @created Feb 2, 2010
318 TAXON(Messages
.SearchBar_6
),
319 SYNONYM(Messages
.SearchBar_7
),
320 NAME(Messages
.SearchBar_8
),
321 COMMON_NAME(Messages
.SearchBar_9
);
323 private final String label
;
325 private SearchOption(String label
) {
329 public String
getLabel() {
333 public boolean getPreference() {
334 if (!PreferencesUtil
.getPreferenceStore().contains(
335 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
)) {
336 // initializes the search configurator
337 PreferencesUtil
.initializeSearchConfigurator();
342 boolean result
= PreferencesUtil
.getPreferenceStore().getBoolean(
343 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_TAXA
);
346 return PreferencesUtil
.getPreferenceStore().getBoolean(
347 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_SYNONYMS
);
349 return PreferencesUtil
.getPreferenceStore().getBoolean(
350 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_NAMES
);
352 return PreferencesUtil
.getPreferenceStore().getBoolean(
353 PreferencesUtil
.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES
);