225f7a98ff8adf3e787a0c48ee308232e3b20f5f
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / reference / ReferenceSearchDialog.java
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.propertysheet.reference;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.jface.viewers.ArrayContentProvider;
17 import org.eclipse.jface.viewers.LabelProvider;
18 import org.eclipse.jface.viewers.TableViewer;
19 import org.eclipse.jface.viewers.ViewerComparator;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.KeyAdapter;
22 import org.eclipse.swt.events.KeyEvent;
23 import org.eclipse.swt.events.MouseAdapter;
24 import org.eclipse.swt.events.MouseEvent;
25 import org.eclipse.swt.events.SelectionAdapter;
26 import org.eclipse.swt.events.SelectionEvent;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Dialog;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Event;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Listener;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Table;
38 import org.eclipse.swt.widgets.Text;
39
40 import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
41 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
42 import eu.etaxonomy.taxeditor.store.CdmStore;
43
44 /**
45 * @author p.ciardelli
46 * @created 13.11.2008
47 * @version 1.0
48 */
49 public class ReferenceSearchDialog extends Dialog {
50 private static final Logger logger = Logger
51 .getLogger(ReferenceSearchDialog.class);
52
53 private Table resultsTable;
54 private Text searchTermText;
55
56 protected Object result;
57 private Shell shell;
58 private Button okButton;
59 private TableViewer resultsTableViewer;
60 private ReferenceBase selectedReference;
61 private int searchType;
62
63 public ReferenceSearchDialog(Shell parent, int searchType) {
64 super(parent, SWT.NONE);
65
66 this.searchType = searchType;
67 }
68
69 public ReferenceSearchDialog(Shell parent) {
70 super(parent, SWT.NONE);
71 }
72
73 /**
74 * Open the dialog
75 * @return the result
76 */
77 public Object open() {
78 createContents();
79 shell.open();
80 shell.layout();
81 Display display = getParent().getDisplay();
82 while (!shell.isDisposed()) {
83 if (!display.readAndDispatch())
84 display.sleep();
85 }
86 return result;
87 }
88
89 /**
90 * Create contents of the dialog
91 */
92 protected void createContents() {
93
94 // Create shell for popup dialog
95 shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
96 shell.setLayout(new GridLayout());
97 shell.setSize(500, 375);
98 shell.setText("Search for a reference in datasource");
99
100 // Create composite for entire shell
101 final Composite composite = new Composite(shell, SWT.NONE);
102 composite.setLayout(new GridLayout());
103 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
104
105 // Create composite for search text, search term input, and "Search" button
106 final Composite searchComposite = new Composite(composite, SWT.NONE);
107 searchComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
108 final GridLayout gridLayout = new GridLayout();
109 gridLayout.numColumns = 2;
110 searchComposite.setLayout(gridLayout);
111
112 // Create search text
113 final Label label = new Label(searchComposite, SWT.NONE);
114 label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
115 label.setText("Enter a search term for a full reference, using '*' as a wildcard.");
116
117 // Create input field for search term
118 searchTermText = new Text(searchComposite, SWT.BORDER | SWT.SINGLE);
119 final GridData gd_searchTermText = new GridData(SWT.FILL, SWT.CENTER, true, false);
120 searchTermText.setLayoutData(gd_searchTermText);
121
122 // Listen for user hitting <CR> in input field
123 searchTermText.addKeyListener(new KeyAdapter() {
124 public void keyReleased(KeyEvent e) {
125 int key = e.keyCode;
126 if (key == SWT.CR) {
127 populateSearchResults();
128 }
129 }
130 });
131
132 // Create "Search" button
133 final Button searchButton = new Button(searchComposite, SWT.NONE);
134 searchButton.setLayoutData(new GridData());
135 searchButton.setText("Search");
136 searchButton.addMouseListener(new MouseAdapter() {
137
138 // Populate search results resultsTable after clicking button
139 public void mouseUp(MouseEvent e) {
140 populateSearchResults();
141 }
142 });
143
144 // Create composite for results table
145 final Composite resultsComposite = new Composite(composite, SWT.NONE);
146 resultsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
147 resultsComposite.setLayout(new GridLayout());
148
149 // Create results resultsTable
150 resultsTableViewer = new TableViewer(resultsComposite, SWT.BORDER);
151 resultsTable = resultsTableViewer.getTable();
152 resultsTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
153
154 // Set content provider of results resultsTable
155 resultsTableViewer.setContentProvider(new ArrayContentProvider());
156
157 // Set label provider for results resultsTable which shows ReferenceBase.getTitleCache()
158 resultsTableViewer.setLabelProvider(new LabelProvider() {
159 public String getText(Object element) {
160 if (element instanceof ReferenceBase) {
161 return ((ReferenceBase) element).getTitleCache();
162 }
163 return super.getText(element);
164 }
165 });
166
167 // Sort results alphabetically
168 resultsTableViewer.setComparator(new ViewerComparator());
169
170 // Listen for user selecting reference from results list
171 resultsTable.addSelectionListener(new SelectionAdapter() {
172 public void widgetSelected(SelectionEvent e) {
173
174 Object data = e.item.getData();
175
176 // Make sure selection is a ReferenceBase
177 if (data instanceof ReferenceBase) {
178
179 setSelectedReference((ReferenceBase) data);
180
181 // Enable "OK" button
182 okButton.setEnabled(true);
183 }
184 }
185 });
186
187 // Double-clicking results entry submits selected reference
188 resultsTable.addListener(SWT.MouseDoubleClick, new Listener() {
189 public void handleEvent(Event event) {
190 submitResult();
191 }
192 });
193
194 // Create composite for "OK" and "Cancel" buttons
195 final Composite okCancelComposite = new Composite(composite, SWT.NONE);
196 okCancelComposite.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
197 final GridLayout gridLayout_1 = new GridLayout();
198 gridLayout_1.numColumns = 2;
199 okCancelComposite.setLayout(gridLayout_1);
200
201 // Create "Cancel" button
202 final Button cancelButton = new Button(okCancelComposite, SWT.NONE);
203 cancelButton.setText("Cancel");
204
205 // Close dialog popup after clicking "Cancel" button
206 cancelButton.addMouseListener(new MouseAdapter() {
207 public void mouseUp(MouseEvent e) {
208 shell.dispose();
209 }
210 });
211
212 // Create "OK" button
213 okButton = new Button(okCancelComposite, SWT.NONE);
214 okButton.setEnabled(false);
215 final GridData gd_okButton = new GridData();
216 okButton.setLayoutData(gd_okButton);
217 okButton.setText("OK");
218
219 // Submit result when "OK" button is clicked
220 okButton.addMouseListener(new MouseAdapter() {
221 public void mouseUp(MouseEvent e) {
222 submitResult();
223 }
224 });
225
226 }
227
228 private void populateSearchResults() {
229 // Get search results
230 List resultsArray = CdmStore.getReferencesByTitle(searchTermText.getText());
231
232 // Delete non-nomenclatural references as needed
233 if (searchType == IReferenceSearch.NOMREF) {
234 List nomenclaturalResultsArray = new ArrayList();
235 for (Object result : resultsArray) {
236 if (result instanceof INomenclaturalReference) {
237 nomenclaturalResultsArray.add(result);
238 }
239 }
240 resultsArray = nomenclaturalResultsArray;
241 }
242
243 // Tell user if there are no results
244 if (resultsArray.size() == 0) {
245 resultsArray.add("Query returned no results.");
246 }
247
248 // Send results to results resultsTable
249 resultsTableViewer.setInput(resultsArray.toArray());
250
251 // Disable OK button
252 okButton.setEnabled(false);
253 }
254
255 /**
256 * Populate result and close dialog popup
257 */
258 private void submitResult() {
259 result = getSelectedReference();
260 shell.dispose();
261 }
262
263 private void setSelectedReference(ReferenceBase selectedReference) {
264 this.selectedReference = selectedReference;
265 }
266
267 private ReferenceBase getSelectedReference() {
268 return selectedReference;
269 }
270 }