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 javax.annotation.PostConstruct;
|
13
|
import javax.inject.Inject;
|
14
|
|
15
|
import org.eclipse.core.runtime.IProgressMonitor;
|
16
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
17
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
18
|
import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
|
19
|
import org.eclipse.swt.SWT;
|
20
|
import org.eclipse.swt.events.FocusEvent;
|
21
|
import org.eclipse.swt.events.FocusListener;
|
22
|
import org.eclipse.swt.events.KeyAdapter;
|
23
|
import org.eclipse.swt.events.KeyEvent;
|
24
|
import org.eclipse.swt.events.SelectionAdapter;
|
25
|
import org.eclipse.swt.events.SelectionEvent;
|
26
|
import org.eclipse.swt.graphics.Point;
|
27
|
import org.eclipse.swt.graphics.Rectangle;
|
28
|
import org.eclipse.swt.layout.RowLayout;
|
29
|
import org.eclipse.swt.widgets.Composite;
|
30
|
import org.eclipse.swt.widgets.Control;
|
31
|
import org.eclipse.swt.widgets.Menu;
|
32
|
import org.eclipse.swt.widgets.MenuItem;
|
33
|
import org.eclipse.swt.widgets.Text;
|
34
|
import org.eclipse.swt.widgets.ToolBar;
|
35
|
import org.eclipse.swt.widgets.ToolItem;
|
36
|
import org.eclipse.ui.IMemento;
|
37
|
import org.eclipse.ui.PlatformUI;
|
38
|
import org.eclipse.ui.swt.IFocusService;
|
39
|
|
40
|
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
|
41
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
42
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
43
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
44
|
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
|
45
|
import eu.etaxonomy.taxeditor.navigation.search.e4.SearchResultViewE4;
|
46
|
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
|
47
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
48
|
import eu.etaxonomy.taxeditor.preference.Resources;
|
49
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
50
|
|
51
|
/**
|
52
|
* @author n.hoffmann
|
53
|
* @author e.-m.lee
|
54
|
* @created 15.04.2009
|
55
|
* @version 1.0
|
56
|
*/
|
57
|
public class SearchBar implements IContextListener{
|
58
|
private Text text_search;
|
59
|
private ToolBar toolBar;
|
60
|
|
61
|
private final String defaultText = Messages.SearchBar_0;
|
62
|
|
63
|
@Inject
|
64
|
private EPartService partService;
|
65
|
|
66
|
final private ConfigurationSelectionListener configurationListener = new ConfigurationSelectionListener();
|
67
|
|
68
|
/** {@inheritDoc} */
|
69
|
@PostConstruct
|
70
|
protected Control createControl(Composite parent) {
|
71
|
Composite composite = new Composite(parent, SWT.NONE);
|
72
|
|
73
|
createLayout(composite);
|
74
|
createSearchTextField(composite);
|
75
|
createToolBar(composite);
|
76
|
registerAtFocusService();
|
77
|
//register for context refreshes
|
78
|
CdmStore.getContextManager().addContextListener(this);
|
79
|
|
80
|
return composite;
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Handles focus changes for the search textfield.
|
85
|
*/
|
86
|
private void registerAtFocusService() {
|
87
|
IFocusService focusService =
|
88
|
PlatformUI.getWorkbench().getService(IFocusService.class);
|
89
|
if (focusService != null) {
|
90
|
focusService.addFocusTracker(text_search, "navigation.textControlId");
|
91
|
}
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Creates the search toolbar.
|
96
|
* @param composite
|
97
|
*/
|
98
|
private void createToolBar(Composite composite) {
|
99
|
toolBar = new ToolBar(composite, SWT.NULL);
|
100
|
|
101
|
ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
|
102
|
toolItem.setText(Messages.SearchBar_1);
|
103
|
toolBar.setEnabled(false);
|
104
|
|
105
|
DropdownSelectionListener dropdownListener = new DropdownSelectionListener(
|
106
|
toolItem);
|
107
|
|
108
|
for(SearchOption searchOption : SearchOption.values()){
|
109
|
dropdownListener.add(searchOption);
|
110
|
}
|
111
|
|
112
|
toolItem.addSelectionListener(dropdownListener);
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* Creates the search textfield.
|
117
|
* @param composite
|
118
|
*/
|
119
|
private void createSearchTextField(Composite composite) {
|
120
|
// TODO for some reason the text_search composite has a margin when
|
121
|
// either SWT.BORDER or SWT.SEARCH
|
122
|
// is applied. I am not sure how to get rid of this.
|
123
|
text_search = new Text(composite, SWT.BORDER | SWT.SINGLE
|
124
|
| SWT.FULL_SELECTION);
|
125
|
text_search.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND));
|
126
|
text_search.setText(defaultText);
|
127
|
text_search.setEnabled(false);
|
128
|
|
129
|
addTextListeners();
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Adds listeners to the search textfield.
|
134
|
*/
|
135
|
private void addTextListeners() {
|
136
|
text_search.addFocusListener(new FocusListener() {
|
137
|
|
138
|
@Override
|
139
|
public void focusGained(FocusEvent e) {
|
140
|
text_search.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOCUS));
|
141
|
if (defaultText.equals(text_search.getText())) {
|
142
|
text_search.setText("");
|
143
|
}
|
144
|
}
|
145
|
|
146
|
@Override
|
147
|
public void focusLost(FocusEvent e) {
|
148
|
if (text_search.getText() == "") {
|
149
|
text_search.setForeground(AbstractUtility.getColor(Resources.SEARCH_VIEW_FOREGROUND));
|
150
|
text_search.setText(defaultText);
|
151
|
}
|
152
|
}
|
153
|
});
|
154
|
|
155
|
text_search.addKeyListener(new KeyAdapter() {
|
156
|
@Override
|
157
|
public void keyPressed(KeyEvent e) {
|
158
|
if (e.keyCode == SWT.CR) {
|
159
|
search();
|
160
|
}
|
161
|
}
|
162
|
});
|
163
|
}
|
164
|
|
165
|
/**
|
166
|
* Creates the search layout.
|
167
|
* @param composite
|
168
|
*/
|
169
|
private void createLayout(Composite composite) {
|
170
|
final RowLayout layout = new RowLayout();
|
171
|
layout.wrap = false;
|
172
|
layout.pack = true;
|
173
|
layout.justify = true;
|
174
|
layout.type = SWT.HORIZONTAL;
|
175
|
layout.marginLeft = 0;
|
176
|
layout.marginTop = 0;
|
177
|
layout.marginRight = 0;
|
178
|
layout.marginBottom = 0;
|
179
|
layout.spacing = 0;
|
180
|
composite.setLayout(layout);
|
181
|
}
|
182
|
|
183
|
private void search(){
|
184
|
final String searchString = getSearchString();
|
185
|
if(searchString == null){
|
186
|
return;
|
187
|
}
|
188
|
|
189
|
if(!searchString.trim().matches(".*\\p{L}+.*")){
|
190
|
MessagingUtils.warningDialog(Messages.SearchBar_2, this, Messages.SearchBar_3);
|
191
|
return;
|
192
|
}
|
193
|
|
194
|
|
195
|
IFindTaxaAndNamesConfigurator configurator = configurationListener.getConfigurator();
|
196
|
configurator.setTitleSearchString(searchString);
|
197
|
openSearchResultsView(configurator);
|
198
|
|
199
|
}
|
200
|
|
201
|
private String getSearchString(){
|
202
|
String searchString = text_search.getText().trim();
|
203
|
if (searchString.equals(defaultText) || searchString.length() == 0) {
|
204
|
return null;
|
205
|
}
|
206
|
return searchString;
|
207
|
}
|
208
|
|
209
|
/**
|
210
|
* Opens a new instance of the search result view to display the result to the user.
|
211
|
*
|
212
|
* @param searchResult
|
213
|
*/
|
214
|
private void openSearchResultsView(IFindTaxaAndNamesConfigurator configurator) {
|
215
|
boolean openResultInSeparateWindows = PreferencesUtil.getPreferenceStore().getBoolean((IPreferenceKeys.SEARCH_OPEN_RESULTS_IN_SEPARATE_WINDOWS));
|
216
|
|
217
|
MPart part = partService.createPart("eu.etaxonomy.taxeditor.navigation.search.e4.SearchResultViewE4");
|
218
|
part = partService.showPart(part, PartState.ACTIVATE);
|
219
|
SearchResultViewE4 resultView = (SearchResultViewE4)part.getObject();
|
220
|
resultView.performSearch(configurator);
|
221
|
}
|
222
|
|
223
|
/**
|
224
|
* Handles drop down menu selection. Available items are defined in the enumeration SearchOption.
|
225
|
*
|
226
|
* @author n.hoffmann
|
227
|
* @created Feb 2, 2010
|
228
|
* @version 1.0
|
229
|
*/
|
230
|
class DropdownSelectionListener extends SelectionAdapter {
|
231
|
|
232
|
private final Menu menu;
|
233
|
|
234
|
public DropdownSelectionListener(ToolItem dropdown) {
|
235
|
menu = new Menu(dropdown.getParent().getShell());
|
236
|
}
|
237
|
|
238
|
public void add(SearchOption option) {
|
239
|
MenuItem menuItem = new MenuItem(menu, SWT.CHECK);
|
240
|
menuItem.setData(option);
|
241
|
menuItem.setText(option.getLabel());
|
242
|
menuItem.setSelection(option.getPreference());
|
243
|
menuItem.addSelectionListener(configurationListener);
|
244
|
}
|
245
|
|
246
|
@Override
|
247
|
public void widgetSelected(SelectionEvent event) {
|
248
|
if (event.detail == SWT.ARROW) {
|
249
|
ToolItem item = (ToolItem) event.widget;
|
250
|
Rectangle rect = item.getBounds();
|
251
|
Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
|
252
|
menu.setLocation(pt.x, pt.y + rect.height);
|
253
|
menu.setVisible(true);
|
254
|
} else {
|
255
|
search();
|
256
|
}
|
257
|
}
|
258
|
}
|
259
|
|
260
|
/**
|
261
|
* Handles search configuration selection.
|
262
|
*
|
263
|
* @author n.hoffmann
|
264
|
* @created Feb 2, 2010
|
265
|
* @version 1.0
|
266
|
*/
|
267
|
class ConfigurationSelectionListener extends SelectionAdapter {
|
268
|
|
269
|
private IFindTaxaAndNamesConfigurator configurator = PreferencesUtil.getSearchConfigurator();
|
270
|
|
271
|
@Override
|
272
|
public void widgetSelected(SelectionEvent e) {
|
273
|
SearchOption option = (SearchOption) e.widget.getData();
|
274
|
|
275
|
switch (option){
|
276
|
case TAXON:
|
277
|
configurator.setDoTaxa(configurator.isDoTaxa() ? false : true);
|
278
|
break;
|
279
|
case SYNONYM:
|
280
|
configurator.setDoSynonyms(configurator.isDoSynonyms() ? false : true);
|
281
|
break;
|
282
|
case NAME:
|
283
|
configurator.setDoNamesWithoutTaxa(configurator.isDoNamesWithoutTaxa() ? false : true);
|
284
|
break;
|
285
|
case COMMON_NAME:
|
286
|
configurator.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ? false : true);
|
287
|
break;
|
288
|
}
|
289
|
|
290
|
saveConfigurator();
|
291
|
}
|
292
|
|
293
|
public IFindTaxaAndNamesConfigurator getConfigurator() {
|
294
|
return configurator;
|
295
|
}
|
296
|
|
297
|
private void saveConfigurator() {
|
298
|
PreferencesUtil.setSearchConfigurator(getConfigurator());
|
299
|
this.configurator = PreferencesUtil.getSearchConfigurator();
|
300
|
}
|
301
|
}
|
302
|
|
303
|
@Override
|
304
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
305
|
}
|
306
|
|
307
|
@Override
|
308
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
309
|
if(!text_search.isDisposed()){
|
310
|
text_search.setEnabled(false);
|
311
|
}
|
312
|
if(!toolBar.isDisposed()){
|
313
|
toolBar.setEnabled(false);
|
314
|
}
|
315
|
}
|
316
|
|
317
|
@Override
|
318
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
319
|
if(!text_search.isDisposed()){
|
320
|
text_search.setEnabled(true);
|
321
|
}
|
322
|
if(!toolBar.isDisposed()){
|
323
|
toolBar.setEnabled(true);
|
324
|
}
|
325
|
}
|
326
|
|
327
|
@Override
|
328
|
public void contextRefresh(IProgressMonitor monitor) {
|
329
|
}
|
330
|
|
331
|
@Override
|
332
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
333
|
}
|
334
|
}
|