Project

General

Profile

Download (8.33 KB) Statistics
| Branch: | Tag: | Revision:
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.editor.name;
11

    
12
import java.util.List;
13

    
14
import org.apache.log4j.Logger;
15
import org.eclipse.jface.viewers.ArrayContentProvider;
16
import org.eclipse.jface.viewers.LabelProvider;
17
import org.eclipse.jface.viewers.TableViewer;
18
import org.eclipse.jface.viewers.ViewerComparator;
19
import org.eclipse.swt.SWT;
20
import org.eclipse.swt.events.KeyAdapter;
21
import org.eclipse.swt.events.KeyEvent;
22
import org.eclipse.swt.events.MouseAdapter;
23
import org.eclipse.swt.events.MouseEvent;
24
import org.eclipse.swt.events.SelectionAdapter;
25
import org.eclipse.swt.events.SelectionEvent;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.layout.GridLayout;
28
import org.eclipse.swt.widgets.Button;
29
import org.eclipse.swt.widgets.Composite;
30
import org.eclipse.swt.widgets.Dialog;
31
import org.eclipse.swt.widgets.Display;
32
import org.eclipse.swt.widgets.Event;
33
import org.eclipse.swt.widgets.Label;
34
import org.eclipse.swt.widgets.Listener;
35
import org.eclipse.swt.widgets.Shell;
36
import org.eclipse.swt.widgets.Table;
37
import org.eclipse.swt.widgets.Text;
38

    
39
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
40
import eu.etaxonomy.taxeditor.store.CdmStore;
41

    
42
/**
43
 * @author p.ciardelli
44
 * @created 13.11.2008
45
 * @version 1.0
46
 */
47
public class NameSearchDialog extends Dialog {
48
	private static final Logger logger = Logger
49
			.getLogger(NameSearchDialog.class);
50
	
51
	private Table resultsTable;
52
	protected Text searchTermText;
53
		
54
	protected Object result;
55
	protected Shell shell;
56
	private Button okButton;
57
	private TableViewer resultsTableViewer;
58
	private TaxonNameBase selectedName;
59
	private int searchType;
60
	
61
	protected String dialogTitle = "Search for a name in datasource";
62
	protected String dialogMessage = "Enter a search term for a scientific name, using '*' as a wildcard.";
63
	
64
	public NameSearchDialog(Shell parent, int searchType) {
65
		super(parent, SWT.NONE);
66
		
67
		this.searchType = searchType;
68
	}
69

    
70
	public NameSearchDialog(Shell parent) {
71
		super(parent, SWT.NONE);
72
	}
73
	
74
	/**
75
	 * Open the dialog
76
	 * @return the result
77
	 */
78
	public Object open() {
79
		createContents();
80
		shell.open();
81
		shell.layout();
82
		Display display = getParent().getDisplay();
83
		while (!shell.isDisposed()) {
84
			if (!display.readAndDispatch())
85
				display.sleep();
86
		}
87
		return result;
88
	}
89
	
90
	/**
91
	 * Create contents of the dialog
92
	 */
93
	protected void createContents() {
94
		
95
		// Create shell for popup dialog		
96
		shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
97
		shell.setLayout(new GridLayout());
98
		shell.setSize(500, 375);
99
		shell.setText(dialogTitle);
100
		
101
		// Create composite for entire shell		
102
		final Composite composite = new Composite(shell, SWT.NONE);
103
		composite.setLayout(new GridLayout());
104
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
105

    
106
		// Create composite for search text, search term input, and "Search" button
107
		final Composite searchComposite = new Composite(composite, SWT.NONE);
108
		searchComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
109
		final GridLayout gridLayout = new GridLayout();
110
		gridLayout.numColumns = 2;
111
		searchComposite.setLayout(gridLayout);
112

    
113
		// Create search text
114
		final Label label = new Label(searchComposite, SWT.NONE);
115
		label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
116
		label.setText(dialogMessage);
117

    
118
		// Create input field for search term
119
		searchTermText = new Text(searchComposite, SWT.BORDER | SWT.SINGLE);
120
		final GridData gd_searchTermText = new GridData(SWT.FILL, SWT.CENTER, true, false);
121
		searchTermText.setLayoutData(gd_searchTermText);
122
		
123
		// Listen for user hitting <CR> in input field
124
		searchTermText.addKeyListener(new KeyAdapter() {
125
			public void keyReleased(KeyEvent e) {
126
				int key = e.keyCode;
127
				if (key == SWT.CR) {
128
					populateSearchResults();
129
				}
130
			}
131
		});
132

    
133
		// Create "Search" button
134
		final Button searchButton = new Button(searchComposite, SWT.NONE);
135
		searchButton.setLayoutData(new GridData());
136
		searchButton.setText("Search");
137
		searchButton.addMouseListener(new MouseAdapter() {
138
			
139
			// Populate search results resultsTable after clicking button
140
			public void mouseUp(MouseEvent e) {
141
				populateSearchResults();
142
			}
143
		});
144

    
145
		// Create composite for results table
146
		final Composite resultsComposite = new Composite(composite, SWT.NONE);
147
		resultsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
148
		resultsComposite.setLayout(new GridLayout());
149

    
150
		// Create results resultsTable
151
		resultsTableViewer = new TableViewer(resultsComposite, SWT.BORDER);
152
		resultsTable = resultsTableViewer.getTable();
153
		resultsTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
154

    
155
		// Set content provider of results resultsTable
156
		resultsTableViewer.setContentProvider(new ArrayContentProvider());
157

    
158
		// Set label provider for results resultsTable which shows ReferenceBase.getTitleCache()
159
		resultsTableViewer.setLabelProvider(new LabelProvider() {
160
			public String getText(Object element) {
161
				String text = NameSearchDialog.this.getText(element);
162
				if (text != null) {
163
					return text;
164
				}
165
				return super.getText(element);
166
			}
167
		});
168
	
169
		// Sort results alphabetically
170
		resultsTableViewer.setComparator(new ViewerComparator());
171
		
172
		// Listen for user selecting reference from results list
173
		resultsTable.addSelectionListener(new SelectionAdapter() {
174
			public void widgetSelected(SelectionEvent e) {
175
				
176
				Object data = e.item.getData();
177
				
178
				if (setSelection(data)) {
179
					okButton.setEnabled(true);					
180
				}
181
			}
182
		});
183
		
184
		// Double-clicking results entry submits selected reference
185
		resultsTable.addListener(SWT.MouseDoubleClick, new Listener() {
186
			public void handleEvent(Event event) {
187
				submitResult();
188
			}
189
		});
190
		
191
		// Create composite for "OK" and "Cancel" buttons
192
		final Composite okCancelComposite = new Composite(composite, SWT.NONE);
193
		okCancelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
194
		final GridLayout gridLayout_1 = new GridLayout();
195
		gridLayout_1.numColumns = 2;
196
		okCancelComposite.setLayout(gridLayout_1);
197

    
198
		// Create "Cancel" button
199
		final Button cancelButton = new Button(okCancelComposite, SWT.NONE);
200
		cancelButton.setText("Cancel");
201
		
202
		// Close dialog popup after clicking "Cancel" button
203
		cancelButton.addMouseListener(new MouseAdapter() {
204
			public void mouseUp(MouseEvent e) {
205
				shell.dispose();
206
			}
207
		});
208

    
209
		// Create "OK" button
210
		okButton = new Button(okCancelComposite, SWT.NONE);
211
		okButton.setEnabled(false);
212
		final GridData gd_okButton = new GridData();
213
		okButton.setLayoutData(gd_okButton);
214
		okButton.setText("OK");
215
		
216
		// Submit result when "OK" button is clicked
217
		okButton.addMouseListener(new MouseAdapter() {
218
			public void mouseUp(MouseEvent e) {
219
				submitResult();
220
			}
221
		});
222

    
223
	}
224
	
225
	/**
226
	 * @param data
227
	 * @return
228
	 */
229
	protected boolean setSelection(Object data) {
230
		if (data instanceof TaxonNameBase) {
231
			setSelectedName((TaxonNameBase) data);	
232
			return true;
233
		}
234
		return false;
235
	}
236

    
237
	/**
238
	 * @param element
239
	 */
240
	protected String getText(Object element) {
241
		if (element instanceof TaxonNameBase) {
242
			return ((TaxonNameBase) element).getTitleCache();
243
		}
244
		return null;
245
	}
246

    
247
	private void populateSearchResults() {
248
		// Get search results
249
		List resultsArray = doSearch();
250
		
251
		// Tell user if there are no results
252
		if (resultsArray.size() == 0) {
253
			resultsArray.add("Query returned no results.");
254
		}
255
		
256
		// Send results to results resultsTable
257
		resultsTableViewer.setInput(resultsArray.toArray());
258
		
259
		// Disable OK button
260
		okButton.setEnabled(false);		
261
	}
262
	
263
	protected List doSearch() {
264
		return CdmStore.searchNameString(searchTermText.getText());
265
	}
266
	
267
	/**
268
	 * Populate result and close dialog popup
269
	 */
270
	protected void submitResult() {
271
		result = getSelectedName();
272
		shell.dispose();
273
	}
274
	
275
	private void setSelectedName(TaxonNameBase selectedName) {
276
		this.selectedName = selectedName;
277
	}
278
	
279
	private TaxonNameBase getSelectedName() {
280
		return selectedName;
281
	}
282
}
(12-12/18)