Project

General

Profile

Download (9.79 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2007 EDIT
4
 * European Distributed Institute of Taxonomy 
5
 * http://www.e-taxonomy.eu
6
 * 
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.
9
 */
10

    
11
package eu.etaxonomy.taxeditor.navigation.search;
12

    
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;
36

    
37
import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
38
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
39
import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
40
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
41
import eu.etaxonomy.taxeditor.preference.Resources;
42

    
43
/**
44
 * <p>SearchBar class.</p>
45
 *
46
 * @author n.hoffmann
47
 * @author e.-m.lee
48
 * @created 15.04.2009
49
 * @version 1.0
50
 */
51
public class SearchBar extends WorkbenchWindowControlContribution{
52
	private Text text_search;
53
	private String srv;
54

    
55
	private final String defaultText = "Use \"*\" for wildcard searching";
56

    
57
	final private ConfigurationSelectionListener configurationListener = new ConfigurationSelectionListener();
58

    
59
	/** {@inheritDoc} */
60
	@Override
61
	protected Control createControl(Composite parent) {
62
		Composite composite = new Composite(parent, SWT.NONE);
63

    
64
		createLayout(composite);
65
		createSearchTextField(composite);
66
		createToolBar(composite);
67
		registerAtFocusService();
68

    
69
		return composite;
70
	}
71

    
72
	/**
73
	 * Handles focus changes for the search textfield.
74
	 */
75
	private void registerAtFocusService() {
76
		IFocusService focusService = 
77
			(IFocusService) PlatformUI.getWorkbench().getService(IFocusService.class);
78
		if (focusService != null) {
79
			focusService.addFocusTracker(text_search, "navigation.textControlId");
80
		}
81
	}
82

    
83
	/**
84
	 * Creates the search toolbar.
85
	 * @param composite
86
	 */
87
	private void createToolBar(Composite composite) {
88
		final ToolBar toolBar = new ToolBar(composite, SWT.NULL);
89

    
90
		ToolItem toolItem = new ToolItem(toolBar, SWT.DROP_DOWN | SWT.BORDER);
91
		toolItem.setText("Search");
92

    
93
		DropdownSelectionListener dropdownListener = new DropdownSelectionListener(
94
				toolItem);
95

    
96
		for(SearchOption searchOption : SearchOption.values()){
97
			dropdownListener.add(searchOption);
98
		}
99

    
100
		toolItem.addSelectionListener(dropdownListener);
101
	}
102

    
103
	/**
104
	 * Creates the search textfield.
105
	 * @param composite
106
	 */
107
	private void createSearchTextField(Composite composite) {
108
		// TODO for some reason the text_search composite has a margin when
109
		// either SWT.BORDER or SWT.SEARCH
110
		// is applied. I am not sure how to get rid of this.
111
		text_search = new Text(composite, SWT.BORDER | SWT.SINGLE
112
				| SWT.FULL_SELECTION);
113
		text_search.setForeground(NavigationUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
114
		text_search.setText(defaultText);
115

    
116
		addTextListeners();
117
	}
118

    
119
	/**
120
	 * Adds listeners to the search textfield.
121
	 */
122
	private void addTextListeners() {
123
		text_search.addFocusListener(new FocusListener() {
124

    
125
			public void focusGained(FocusEvent e) {
126
				text_search.setForeground(NavigationUtil.getColor(Resources.SEARCH_VIEW_FOCUS));
127
				if (defaultText.equals(text_search.getText())) {
128
					text_search.setText("");
129
				}
130
			}
131

    
132
			public void focusLost(FocusEvent e) {
133
				if (text_search.getText() == "") {
134
					text_search.setForeground(NavigationUtil.getColor(Resources.SEARCH_VIEW_FOREGROUND));
135
					text_search.setText(defaultText);
136
				}
137
			}
138
		});
139

    
140
		text_search.addKeyListener(new KeyAdapter() {
141
			/*
142
			 * (non-Javadoc)
143
			 * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
144
			 */
145
			@Override
146
			public void keyPressed(KeyEvent e) {
147
				if (e.keyCode == SWT.CR) {
148
					search();
149
				}
150
			}
151
		});
152
	}
153

    
154
	/**
155
	 * Creates the search layout.
156
	 * @param composite
157
	 */
158
	private void createLayout(Composite composite) {
159
		final RowLayout layout = new RowLayout();
160
		layout.wrap = false;
161
		layout.pack = true;
162
		layout.justify = true;
163
		layout.type = SWT.HORIZONTAL;
164
		layout.marginLeft = 0;
165
		layout.marginTop = 0;
166
		layout.marginRight = 0;
167
		layout.marginBottom = 0;
168
		layout.spacing = 0;
169
		composite.setLayout(layout);
170
	}
171

    
172
	private void search(){
173
		final String searchString = getSearchString();
174
		if(searchString == null){
175
			return;
176
		}
177
		
178
		if("*".equals(searchString.trim())){
179
			NavigationUtil.warningDialog("Could not execute search", this, "Please type at least one character when using the \"*\" wildcard.");
180
			return;
181
		}
182
		
183
		
184
		ITaxonServiceConfigurator configurator = configurationListener.getConfigurator();
185
		configurator.setTitleSearchString(searchString);
186
		openSearchResultsView(configurator);
187
		
188
	}
189
	
190
	private String getSearchString(){
191
		String searchString = text_search.getText().trim();
192
		if (searchString.equals(defaultText) || searchString.length() == 0)
193
			return null;
194
		return searchString;
195
	}
196
	
197
	/**
198
	 * Opens a new instance of the search result view to display the result to the user.
199
	 * 
200
	 * @param searchResult
201
	 */
202
	private void openSearchResultsView(ITaxonServiceConfigurator configurator) {
203
		// This variable is needed to address new instances of the view.
204
		srv += "1";
205

    
206
		try {
207
			IViewPart resultsView = TaxeditorNavigationPlugin.getDefault()
208
					.getWorkbench().getActiveWorkbenchWindow()
209
					.getActivePage().showView(SearchResultView.ID, srv,
210
							IWorkbenchPage.VIEW_ACTIVATE);
211
			((SearchResultView) resultsView).performSearch(configurator);
212
		} catch (PartInitException e) {
213
			NavigationUtil.error(this.getClass(), "Error opening search result.", e);
214
		}
215
	}
216

    
217
	/**
218
	 * Handles drop down menu selection. Available items are defined in the enumeration SearchOption.
219
	 * 
220
	 * @author n.hoffmann
221
	 * @created Feb 2, 2010
222
	 * @version 1.0
223
	 */
224
	class DropdownSelectionListener extends SelectionAdapter {
225

    
226
		private final Menu menu;
227

    
228
		public DropdownSelectionListener(ToolItem dropdown) {
229
			menu = new Menu(dropdown.getParent().getShell());
230
		}
231

    
232
		public void add(SearchOption option) {
233
			MenuItem menuItem = new MenuItem(menu, SWT.CHECK);
234
			menuItem.setData(option);
235
			menuItem.setText(option.getLabel());
236
			menuItem.setSelection(option.getPreference());
237
			menuItem.addSelectionListener(configurationListener);
238
		}
239

    
240
		@Override
241
		public void widgetSelected(SelectionEvent event) {
242
			if (event.detail == SWT.ARROW) {
243
				ToolItem item = (ToolItem) event.widget;
244
				Rectangle rect = item.getBounds();
245
				Point pt = item.getParent().toDisplay(new Point(rect.x, rect.y));
246
				menu.setLocation(pt.x, pt.y + rect.height);
247
				menu.setVisible(true);
248
			} else {
249
				search();
250
			}
251
		}
252
	}
253

    
254
	/**
255
	 * Handles search configuration selection.
256
	 * 
257
	 * @author n.hoffmann
258
	 * @created Feb 2, 2010
259
	 * @version 1.0
260
	 */
261
	class ConfigurationSelectionListener extends SelectionAdapter {
262

    
263
		private ITaxonServiceConfigurator configurator = PreferencesUtil.getSearchConfigurator();
264
		
265
		/*
266
		 * (non-Javadoc)
267
		 * 
268
		 * @see
269
		 * org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse
270
		 * .swt.events.SelectionEvent)
271
		 */
272
		@Override
273
		public void widgetSelected(SelectionEvent e) {
274
			NavigationUtil.info("configuration menu clicked");
275
			SearchOption option = (SearchOption) e.widget.getData();
276

    
277
			switch (option){
278
			case TAXON:
279
				configurator.setDoTaxa(configurator.isDoTaxa() ? false : true);
280
				break;
281
			case SYNONYM:
282
				configurator.setDoSynonyms(configurator.isDoSynonyms() ? false : true);
283
				break;
284
			case NAME:
285
				configurator.setDoNamesWithoutTaxa(configurator.isDoNamesWithoutTaxa() ? false : true);
286
				break;
287
			case COMMON_NAME:
288
				configurator.setDoTaxaByCommonNames(getConfigurator().isDoTaxaByCommonNames() ? false : true);
289
				break;				
290
			}
291
			
292
			saveConfigurator();
293
		}
294

    
295
		public ITaxonServiceConfigurator getConfigurator() {
296
			return configurator;
297
		}
298

    
299
		private void saveConfigurator() {
300
			PreferencesUtil.setSearchConfigurator(getConfigurator());
301
			this.configurator = PreferencesUtil.getSearchConfigurator();
302
		}
303
	}
304

    
305
	/**
306
	 * Available search options.
307
	 * 
308
	 * @author n.hoffmann
309
	 * @created Feb 2, 2010
310
	 * @version 1.0
311
	 */
312
	enum SearchOption {
313
		TAXON("Taxa"), 
314
		SYNONYM("Synonyms"), 
315
		NAME("Names (without taxa)"), 
316
		COMMON_NAME("Common Names");
317

    
318
		private final String label;
319

    
320
		private SearchOption(String label) {
321
			this.label = label;
322
		}
323

    
324
		public String getLabel() {
325
			return label;
326
		}
327

    
328
		public boolean getPreference() {
329
			if (!PreferencesUtil.getPreferenceStore().contains(
330
					PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_TAXA)) {
331
				// initializes the search configurator
332
				PreferencesUtil.initializeSearchConfigurator();
333
			}
334

    
335
			switch (this) {
336
			case TAXON:
337
				boolean result = PreferencesUtil.getPreferenceStore().getBoolean(
338
								PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_TAXA);
339
				return result;
340
			case SYNONYM:
341
				return PreferencesUtil.getPreferenceStore().getBoolean(
342
						PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_SYNONYMS);
343
			case NAME:
344
				return PreferencesUtil.getPreferenceStore().getBoolean(
345
						PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_NAMES);
346
			case COMMON_NAME:
347
				return PreferencesUtil.getPreferenceStore().getBoolean(
348
								PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_COMMON_NAMES);
349
			}
350

    
351
			return true;
352
		}
353

    
354
	}
355
}
(1-1/3)