editor now updatable via updateSite
[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.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.reference.INomenclaturalReference;
40 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
41 import eu.etaxonomy.taxeditor.store.CdmStore;
42
43 /**
44 * @author p.ciardelli
45 * @created 13.11.2008
46 * @version 1.0
47 */
48 public class ReferenceSearchDialog extends Dialog {
49
50 private Table resultsTable;
51 private Text searchTermText;
52
53 protected Object result;
54 private Shell shell;
55 private Button okButton;
56 private TableViewer resultsTableViewer;
57 private ReferenceBase<?> selectedReference;
58 private int searchType;
59
60 public ReferenceSearchDialog(Shell parent, int searchType) {
61 super(parent, SWT.NONE);
62
63 this.searchType = searchType;
64 }
65
66 public ReferenceSearchDialog(Shell parent) {
67 super(parent, SWT.NONE);
68 }
69
70 /**
71 * Open the dialog
72 * @return the result
73 */
74 public Object open() {
75 createContents();
76 shell.open();
77 shell.layout();
78 Display display = getParent().getDisplay();
79 while (!shell.isDisposed()) {
80 if (!display.readAndDispatch())
81 display.sleep();
82 }
83 return result;
84 }
85
86 /**
87 * Create contents of the dialog
88 */
89 protected void createContents() {
90
91 // Create shell for popup dialog
92 shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
93 shell.setLayout(new GridLayout());
94 shell.setSize(500, 375);
95 shell.setText("Search for a reference in datasource");
96
97 // Create composite for entire shell
98 final Composite composite = new Composite(shell, SWT.NONE);
99 composite.setLayout(new GridLayout());
100 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
101
102 // Create composite for search text, search term input, and "Search" button
103 final Composite searchComposite = new Composite(composite, SWT.NONE);
104 searchComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
105 final GridLayout gridLayout = new GridLayout();
106 gridLayout.numColumns = 2;
107 searchComposite.setLayout(gridLayout);
108
109 // Create search text
110 final Label label = new Label(searchComposite, SWT.NONE);
111 label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
112 label.setText("Enter a search term for a full reference, using '*' as a wildcard.");
113
114 // Create input field for search term
115 searchTermText = new Text(searchComposite, SWT.BORDER | SWT.SINGLE);
116 final GridData gd_searchTermText = new GridData(SWT.FILL, SWT.CENTER, true, false);
117 searchTermText.setLayoutData(gd_searchTermText);
118
119 // Listen for user hitting <CR> in input field
120 searchTermText.addKeyListener(new KeyAdapter() {
121 public void keyReleased(KeyEvent e) {
122 int key = e.keyCode;
123 if (key == SWT.CR) {
124 populateSearchResults();
125 }
126 }
127 });
128
129 // Create "Search" button
130 final Button searchButton = new Button(searchComposite, SWT.NONE);
131 searchButton.setLayoutData(new GridData());
132 searchButton.setText("Search");
133 searchButton.addMouseListener(new MouseAdapter() {
134
135 // Populate search results resultsTable after clicking button
136 public void mouseUp(MouseEvent e) {
137 populateSearchResults();
138 }
139 });
140
141 // Create composite for results table
142 final Composite resultsComposite = new Composite(composite, SWT.NONE);
143 resultsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
144 resultsComposite.setLayout(new GridLayout());
145
146 // Create results resultsTable
147 resultsTableViewer = new TableViewer(resultsComposite, SWT.BORDER);
148 resultsTable = resultsTableViewer.getTable();
149 resultsTable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
150
151 // Set content provider of results resultsTable
152 resultsTableViewer.setContentProvider(new ArrayContentProvider());
153
154 // Set label provider for results resultsTable which shows ReferenceBase.getTitleCache()
155 resultsTableViewer.setLabelProvider(new LabelProvider() {
156 public String getText(Object element) {
157 if (element instanceof ReferenceBase) {
158 return ((ReferenceBase<?>) element).getTitleCache();
159 }
160 return super.getText(element);
161 }
162 });
163
164 // Sort results alphabetically
165 resultsTableViewer.setComparator(new ViewerComparator());
166
167 // Listen for user selecting reference from results list
168 resultsTable.addSelectionListener(new SelectionAdapter() {
169 public void widgetSelected(SelectionEvent e) {
170
171 Object data = e.item.getData();
172
173 // Make sure selection is a ReferenceBase
174 if (data instanceof ReferenceBase) {
175
176 setSelectedReference((ReferenceBase<?>) data);
177
178 // Enable "OK" button
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 // FIXME typification very loose
226 private void populateSearchResults() {
227 // Get search results
228 List resultsArray = CdmStore.getReferencesByTitle(searchTermText.getText());
229
230 // Delete non-nomenclatural references as needed
231 if (searchType == IReferenceSearch.NOMREF) {
232 List nomenclaturalResultsArray = new ArrayList();
233 for (Object result : resultsArray) {
234 if (result instanceof INomenclaturalReference) {
235 nomenclaturalResultsArray.add(result);
236 }
237 }
238 resultsArray = nomenclaturalResultsArray;
239 }
240
241 // Tell user if there are no results
242 if (resultsArray.size() == 0) {
243 resultsArray.add("Query returned no results.");
244 }
245
246 // Send results to results resultsTable
247 resultsTableViewer.setInput(resultsArray.toArray());
248
249 // Disable OK button
250 okButton.setEnabled(false);
251 }
252
253 /**
254 * Populate result and close dialog popup
255 */
256 private void submitResult() {
257 result = getSelectedReference();
258 shell.dispose();
259 }
260
261 private void setSelectedReference(ReferenceBase<?> selectedReference) {
262 this.selectedReference = selectedReference;
263 }
264
265 private ReferenceBase<?> getSelectedReference() {
266 return selectedReference;
267 }
268 }