Project

General

Profile

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

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

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

    
224
		private Menu menu;
225

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

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

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

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

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

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

    
291
		public ITaxonServiceConfigurator getConfigurator() {
292
			return configurator;
293
		}
294

    
295
		private void saveConfigurator() {
296
			PreferencesUtil.setSearchConfigurator(getConfigurator());
297
			this.configurator = PreferencesUtil.getSearchConfigurator();
298
		}
299
	}
300

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

    
314
		private String label;
315

    
316
		private SearchOption(String label) {
317
			this.label = label;
318
		}
319

    
320
		public String getLabel() {
321
			return label;
322
		}
323

    
324
		public boolean getPreference() {
325
			if (!PreferencesUtil.getPreferenceStore().contains(
326
					PreferencesUtil.TAXON_SERVICE_CONFIGURATOR_TAXA)) {
327
				// initializes the search configurator
328
				PreferencesUtil.initializeSearchConfigurator();
329
			}
330

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

    
347
			return true;
348
		}
349

    
350
	}
351
}
(1-1/3)