1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.navigation.search;
|
11
|
|
12
|
import org.eclipse.core.runtime.IProgressMonitor;
|
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.IMemento;
|
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;
|
37
|
|
38
|
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
|
39
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
40
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
41
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
42
|
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
|
43
|
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
|
44
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
45
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
46
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
47
|
|
48
|
/**
|
49
|
* @author n.hoffmann
|
50
|
* @author e.-m.lee
|
51
|
* @created 15.04.2009
|
52
|
* @version 1.0
|
53
|
*/
|
54
|
public class SearchBar extends WorkbenchWindowControlContribution implements IContextListener{
|
55
|
private Text text_search;
|
56
|
private String secondaryId;
|
57
|
private ToolBar toolBar;
|
58
|
|
59
|
private final String defaultText = Messages.SearchBar_0;
|
60
|
|
61
|
final private ConfigurationSelectionListener configurationListener = new ConfigurationSelectionListener();
|
62
|
|
63
|
/** {@inheritDoc} */
|
64
|
@Override
|
65
|
protected Control createControl(Composite parent) {
|
66
|
Composite composite = new Composite(parent, SWT.NONE);
|
67
|
|
68
|
createLayout(composite);
|
69
|
createSearchTextField(composite);
|
70
|
createToolBar(composite);
|
71
|
registerAtFocusService();
|
72
|
//register for context refreshes
|
73
|
CdmStore.getContextManager().addContextListener(this);
|
74
|
|
75
|
return composite;
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* Handles focus changes for the search textfield.
|
80
|
*/
|
81
|
private void registerAtFocusService() {
|
82
|
IFocusService focusService =
|
83
|
(IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);
|
84
|
if (focusService != null) {
|
85
|
focusService.addFocusTracker(text_search, "navigation.textControlId");
|
86
|
}
|
87
|
}
|
88
|
|
89
|
/**
|
90
|
* Creates the search toolbar.
|
91
|
* @param composite
|
92
|
*/
|
93
|
private void createToolBar(Composite composite) {
|
94
|
toolBar = new ToolBar(composite, SWT.NULL);
|
95
|
|
96
|
ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
|
97
|
toolItem.setText(Messages.SearchBar_1);
|
98
|
toolBar.setEnabled(false);
|
99
|
|
100
|
DropdownSelectionListener dropdownListener = new DropdownSelectionListener(
|
101
|
toolItem);
|
102
|
|
103
|
for(SearchOption searchOption : SearchOption.values()){
|
104
|
dropdownListener.add(searchOption);
|
105
|
}
|
106
|
|
107
|
toolItem.addSelectionListener(dropdownListener);
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* Creates the search textfield.
|
112
|
* @param composite
|
113
|
*/
|
114
|
private void createSearchTextField(Composite composite) {
|
115
|
// TODO for some reason the text_search composite has a margin when
|
116
|
// either SWT.BORDER or SWT.SEARCH
|
117
|
// is applied. I am not sure how to get rid of this.
|
118
|
text_search = new Text(composite, SWT.BORDER | SWT.SINGLE
|
119
|
| SWT.FULL_SELECTION);
|
120
|
text_search.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND));
|
121
|
text_search.setText(defaultText);
|
122
|
text_search.setEnabled(false);
|
123
|
|
124
|
addTextListeners();
|
125
|
}
|
126
|
|
127
|
/**
|
128
|
* Adds listeners to the search textfield.
|
129
|
*/
|
130
|
private void addTextListeners() {
|
131
|
text_search.addFocusListener(new FocusListener() {
|
132
|
|
133
|
@Override
|
134
|
public void focusGained(FocusEvent e) {
|
135
|
text_search.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOCUS));
|
136
|
if (defaultText.equals(text_search.getText())) {
|
137
|
text_search.setText("");
|
138
|
}
|
139
|
}
|
140
|
|
141
|
@Override
|
142
|
public void focusLost(FocusEvent e) {
|
143
|
if (text_search.getText() == "") {
|
144
|
text_search.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND));
|
145
|
text_search.setText(defaultText);
|
146
|
}
|
147
|
}
|
148
|
});
|
149
|
|
150
|
text_search.addKeyListener(new KeyAdapter() {
|
151
|
@Override
|
152
|
public void keyPressed(KeyEvent e) {
|
153
|
if (e.keyCode == SWT.CR) {
|
154
|
search();
|
155
|
}
|
156
|
}
|
157
|
});
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* Creates the search layout.
|
162
|
* @param composite
|
163
|
*/
|
164
|
private void createLayout(Composite composite) {
|
165
|
final RowLayout layout = new RowLayout();
|
166
|
layout.wrap = false;
|
167
|
layout.pack = true;
|
168
|
layout.justify = true;
|
169
|
layout.type = SWT.HORIZONTAL;
|
170
|
layout.marginLeft = 0;
|
171
|
layout.marginTop = 0;
|
172
|
layout.marginRight = 0;
|
173
|
layout.marginBottom = 0;
|
174
|
layout.spacing = 0;
|
175
|
composite.setLayout(layout);
|
176
|
}
|
177
|
|
178
|
private void search(){
|
179
|
final String searchString = getSearchString();
|
180
|
if(searchString == null){
|
181
|
return;
|
182
|
}
|
183
|
|
184
|
if(!searchString.trim().matches(".*\\p{L}+.*")){
|
185
|
MessagingUtils.warningDialog(Messages.SearchBar_2, this, Messages.SearchBar_3);
|
186
|
return;
|
187
|
}
|
188
|
|
189
|
|
190
|
IFindTaxaAndNamesConfigurator configurator = configurationListener.getConfigurator();
|
191
|
configurator.setTitleSearchString(searchString);
|
192
|
openSearchResultsView(configurator);
|
193
|
|
194
|
}
|
195
|
|
196
|
private String getSearchString(){
|
197
|
String searchString = text_search.getText().trim();
|
198
|
if (searchString.equals(defaultText) || searchString.length() == 0) {
|
199
|
return null;
|
200
|
}
|
201
|
return searchString;
|
202
|
}
|
203
|
|
204
|
/**
|
205
|
* Opens a new instance of the search result view to display the result to the user.
|
206
|
*
|
207
|
* @param searchResult
|
208
|
*/
|
209
|
private void openSearchResultsView(IFindTaxaAndNamesConfigurator configurator) {
|
210
|
boolean openResultInSeparateWindows = PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.SEARCH_OPEN_RESULTS_IN_SEPARATE_WINDOWS));
|
211
|
if(openResultInSeparateWindows){
|
212
|
//increment change secondary id so it is unique
|
213
|
secondaryId += "1";
|
214
|
}
|
215
|
|
216
|
try {
|
217
|
IViewPart resultsView = TaxeditorNavigationPlugin.getDefault()
|
218
|
.getWorkbench().getActiveWorkbenchWindow()
|
219
|
.getActivePage().showView(SearchResultView.ID, secondaryId,
|
220
|
IWorkbenchPage.VIEW_ACTIVATE);
|
221
|
((SearchResultView) resultsView).performSearch(configurator);
|
222
|
} catch (PartInitException e) {
|
223
|
MessagingUtils.error(this.getClass(), Messages.SearchBar_4, e);
|
224
|
}
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* Handles drop down menu selection. Available items are defined in the enumeration SearchOption.
|
229
|
*
|
230
|
* @author n.hoffmann
|
231
|
* @created Feb 2, 2010
|
232
|
* @version 1.0
|
233
|
*/
|
234
|
class DropdownSelectionListener extends SelectionAdapter {
|
235
|
|
236
|
private final Menu menu;
|
237
|
|
238
|
public DropdownSelectionListener(ToolItem dropdown) {
|
239
|
menu = new Menu(dropdown.getParent().getShell());
|
240
|
}
|
241
|
|
242
|
public void add(SearchOption option) {
|
243
|
MenuItem menuItem = new MenuItem(menu, SWT.CHECK);
|
244
|
menuItem.setData(option);
|
245
|
menuItem.setText(option.getLabel());
|
246
|
menuItem.setSelection(option.getPreference());
|
247
|
menuItem.addSelectionListener(configurationListener);
|
248
|
}
|
249
|
|
250
|
@Override
|
251
|
public void widgetSelected(SelectionEvent event) {
|
252
|
if (event.detail == SWT.ARROW) {
|
253
|
ToolItem item = (ToolItem) event.widget;
|
254
|
Rectangle rect = item.getBounds();
|
255
|
Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
|
256
|
menu.setLocation(pt.x, pt.y + rect.height);
|
257
|
menu.setVisible(true);
|
258
|
} else {
|
259
|
search();
|
260
|
}
|
261
|
}
|
262
|
}
|
263
|
|
264
|
/**
|
265
|
* Handles search configuration selection.
|
266
|
*
|
267
|
* @author n.hoffmann
|
268
|
* @created Feb 2, 2010
|
269
|
* @version 1.0
|
270
|
*/
|
271
|
class ConfigurationSelectionListener extends SelectionAdapter {
|
272
|
|
273
|
private IFindTaxaAndNamesConfigurator configurator = PreferencesUtil.getSearchConfigurator();
|
274
|
|
275
|
@Override
|
276
|
public void widgetSelected(SelectionEvent e) {
|
277
|
SearchOption option = (SearchOption) e.widget.getData();
|
278
|
|
279
|
switch (option){
|
280
|
case TAXON:
|
281
|
configurator.setDoTaxa(configurator.isDoTaxa() ? false : true);
|
282
|
break;
|
283
|
case SYNONYM:
|
284
|
configurator.setDoSynonyms(configurator.isDoSynonyms() ? false : true);
|
285
|
break;
|
286
|
case NAME:
|
287
|
configurator.setDoNamesWithoutTaxa(configurator.isDoNamesWithoutTaxa() ? false : true);
|
288
|
break;
|
289
|
case COMMON_NAME:
|
290
|
configurator.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ? false : true);
|
291
|
break;
|
292
|
}
|
293
|
|
294
|
saveConfigurator();
|
295
|
}
|
296
|
|
297
|
public IFindTaxaAndNamesConfigurator getConfigurator() {
|
298
|
return configurator;
|
299
|
}
|
300
|
|
301
|
private void saveConfigurator() {
|
302
|
PreferencesUtil.setSearchConfigurator(getConfigurator());
|
303
|
this.configurator = PreferencesUtil.getSearchConfigurator();
|
304
|
}
|
305
|
}
|
306
|
|
307
|
/**
|
308
|
* Available search options.
|
309
|
*
|
310
|
* @author n.hoffmann
|
311
|
* @created Feb 2, 2010
|
312
|
* @version 1.0
|
313
|
*/
|
314
|
enum SearchOption {
|
315
|
TAXON(Messages.SearchBar_6),
|
316
|
SYNONYM(Messages.SearchBar_7),
|
317
|
NAME(Messages.SearchBar_8),
|
318
|
COMMON_NAME(Messages.SearchBar_9);
|
319
|
|
320
|
private final String label;
|
321
|
|
322
|
private SearchOption(String label) {
|
323
|
this.label = label;
|
324
|
}
|
325
|
|
326
|
public String getLabel() {
|
327
|
return label;
|
328
|
}
|
329
|
|
330
|
public boolean getPreference() {
|
331
|
if (!PreferencesUtil.getPreferenceStore().contains(
|
332
|
IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_TAXA)) {
|
333
|
// initializes the search configurator
|
334
|
PreferencesUtil.initializeSearchConfigurator();
|
335
|
}
|
336
|
|
337
|
switch (this) {
|
338
|
case TAXON:
|
339
|
boolean result = PreferencesUtil.getPreferenceStore().getBoolean(
|
340
|
IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_TAXA);
|
341
|
return result;
|
342
|
case SYNONYM:
|
343
|
return PreferencesUtil.getPreferenceStore().getBoolean(
|
344
|
IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_SYNONYMS);
|
345
|
case NAME:
|
346
|
return PreferencesUtil.getPreferenceStore().getBoolean(
|
347
|
IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_NAMES);
|
348
|
case COMMON_NAME:
|
349
|
return PreferencesUtil.getPreferenceStore().getBoolean(
|
350
|
IPreferenceKeys.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES);
|
351
|
}
|
352
|
|
353
|
return true;
|
354
|
}
|
355
|
|
356
|
}
|
357
|
|
358
|
@Override
|
359
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
360
|
}
|
361
|
|
362
|
@Override
|
363
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
364
|
if(!text_search.isDisposed()){
|
365
|
text_search.setEnabled(false);
|
366
|
}
|
367
|
if(!toolBar.isDisposed()){
|
368
|
toolBar.setEnabled(false);
|
369
|
}
|
370
|
}
|
371
|
|
372
|
@Override
|
373
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
374
|
text_search.setEnabled(true);
|
375
|
toolBar.setEnabled(true);
|
376
|
}
|
377
|
|
378
|
@Override
|
379
|
public void contextRefresh(IProgressMonitor monitor) {
|
380
|
}
|
381
|
|
382
|
@Override
|
383
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
384
|
}
|
385
|
}
|