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.navigation.search;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.eclipse.core.runtime.IProgressMonitor;
|
16
|
import org.eclipse.core.runtime.IStatus;
|
17
|
import org.eclipse.core.runtime.Status;
|
18
|
import org.eclipse.core.runtime.jobs.Job;
|
19
|
import org.eclipse.jface.action.GroupMarker;
|
20
|
import org.eclipse.jface.action.MenuManager;
|
21
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
22
|
import org.eclipse.jface.viewers.DoubleClickEvent;
|
23
|
import org.eclipse.jface.viewers.IDoubleClickListener;
|
24
|
import org.eclipse.jface.viewers.ISelection;
|
25
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
26
|
import org.eclipse.jface.viewers.TableViewer;
|
27
|
import org.eclipse.swt.SWT;
|
28
|
import org.eclipse.swt.layout.GridData;
|
29
|
import org.eclipse.swt.layout.GridLayout;
|
30
|
import org.eclipse.swt.widgets.Composite;
|
31
|
import org.eclipse.swt.widgets.Control;
|
32
|
import org.eclipse.swt.widgets.Display;
|
33
|
import org.eclipse.swt.widgets.Label;
|
34
|
import org.eclipse.swt.widgets.Menu;
|
35
|
import org.eclipse.swt.widgets.Text;
|
36
|
import org.eclipse.ui.IMemento;
|
37
|
import org.eclipse.ui.IWorkbenchActionConstants;
|
38
|
import org.eclipse.ui.part.ViewPart;
|
39
|
|
40
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
41
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
42
|
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
|
43
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
44
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
45
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
46
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
47
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
48
|
import eu.etaxonomy.taxeditor.model.ContextListenerAdapter;
|
49
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
50
|
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
|
51
|
import eu.etaxonomy.taxeditor.navigation.search.SearchBar.SearchOption;
|
52
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
53
|
|
54
|
/**
|
55
|
* <p>SearchResultView class.</p>
|
56
|
*
|
57
|
* @author p.ciardelli
|
58
|
* @author n.hoffmann
|
59
|
* @created 19.01.2009
|
60
|
* @version 1.0
|
61
|
*/
|
62
|
public class SearchResultView extends ViewPart implements IConversationEnabled{
|
63
|
|
64
|
private static Object[] EMPTY = new Object[0];
|
65
|
|
66
|
private class ContextListener extends ContextListenerAdapter{
|
67
|
/* (non-Javadoc)
|
68
|
* @see eu.etaxonomy.taxeditor.model.IContextListener#contextStop(org.eclipse.ui.IMemento, org.eclipse.core.runtime.IProgressMonitor)
|
69
|
*/
|
70
|
@Override
|
71
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
72
|
monitor.subTask("Getting rid of search results");
|
73
|
AbstractUtility.hideView(SearchResultView.this);
|
74
|
}
|
75
|
}
|
76
|
|
77
|
/** Constant <code>ID="eu.etaxonomy.taxeditor.navigation.searc"{trunked}</code> */
|
78
|
public static final String ID =
|
79
|
"eu.etaxonomy.taxeditor.navigation.search.searchResultView"; //$NON-NLS-1$
|
80
|
|
81
|
private TableViewer resultViewer;
|
82
|
|
83
|
private ConversationHolder conversation;
|
84
|
|
85
|
private Text searchString;
|
86
|
|
87
|
private Text configurationLabel;
|
88
|
|
89
|
private Text status;
|
90
|
|
91
|
private SearchJob searchJob;
|
92
|
|
93
|
private IContextListener contextListener;
|
94
|
|
95
|
/* (non-Javadoc)
|
96
|
* @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
|
97
|
*/
|
98
|
/** {@inheritDoc} */
|
99
|
@Override
|
100
|
public void createPartControl(Composite parent) {
|
101
|
|
102
|
conversation = CdmStore.createConversation();
|
103
|
contextListener = new ContextListener();
|
104
|
CdmStore.getContextManager().addContextListener(contextListener);
|
105
|
|
106
|
GridLayout layout = new GridLayout();
|
107
|
layout.marginWidth = 0;
|
108
|
layout.marginHeight = 0;
|
109
|
|
110
|
parent.setLayout(layout);
|
111
|
|
112
|
Composite infoComposite = createInfoComposite(parent);
|
113
|
infoComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
|
114
|
|
115
|
resultViewer = new TableViewer(parent, SWT.NONE);
|
116
|
resultViewer.setContentProvider(new ArrayContentProvider());
|
117
|
resultViewer.setLabelProvider(new SearchResultLabelProvider());
|
118
|
resultViewer.addDoubleClickListener(new IDoubleClickListener() {
|
119
|
@Override
|
120
|
public void doubleClick(DoubleClickEvent event) {
|
121
|
ISelection selection = event.getSelection();
|
122
|
if(selection instanceof IStructuredSelection){
|
123
|
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
|
124
|
if(firstElement instanceof UuidAndTitleCache){
|
125
|
NavigationUtil.openEditor((UuidAndTitleCache) firstElement);
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
});
|
130
|
|
131
|
resultViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
132
|
|
133
|
getSite().setSelectionProvider(resultViewer);
|
134
|
|
135
|
// register context menu
|
136
|
MenuManager menuMgr = new MenuManager();
|
137
|
menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
|
138
|
getSite().registerContextMenu(menuMgr, resultViewer);
|
139
|
|
140
|
Control control = resultViewer.getControl();
|
141
|
Menu menu = menuMgr.createContextMenu(control);
|
142
|
control.setMenu(menu);
|
143
|
}
|
144
|
|
145
|
private Composite createInfoComposite(Composite parent){
|
146
|
Composite composite = new Composite(parent, SWT.NULL);
|
147
|
|
148
|
composite.setLayout(new GridLayout(2, false));
|
149
|
|
150
|
Label searchStringLabel = new Label(composite, SWT.NULL);
|
151
|
searchStringLabel.setText("Search String:");
|
152
|
|
153
|
searchString = new Text(composite, SWT.NULL);
|
154
|
searchString.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
155
|
searchString.setEditable(false);
|
156
|
// searchString.setText(" ");
|
157
|
|
158
|
Label configurationDescriptionLabel = new Label(composite, SWT.NULL);
|
159
|
configurationDescriptionLabel.setText("Search for:");
|
160
|
|
161
|
configurationLabel = new Text(composite, SWT.WRAP);
|
162
|
configurationLabel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
163
|
configurationLabel.setEditable(false);
|
164
|
|
165
|
Label statusLabel = new Label(composite, SWT.NULL);
|
166
|
statusLabel.setText("Status:");
|
167
|
|
168
|
status = new Text(composite, SWT.NULL);
|
169
|
status.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
170
|
status.setEditable(false);
|
171
|
|
172
|
return composite;
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* <p>performSearch</p>
|
177
|
*
|
178
|
* @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
|
179
|
*/
|
180
|
public void performSearch(IFindTaxaAndNamesConfigurator configurator){
|
181
|
setPartName("Search: '" + configurator.getTitleSearchString() + "'");
|
182
|
|
183
|
searchString.setText(configurator.getTitleSearchString());
|
184
|
|
185
|
List<String> includedEntities = new ArrayList<String>();
|
186
|
if(configurator.isDoTaxa()) {
|
187
|
includedEntities.add(SearchOption.TAXON.getLabel());
|
188
|
}
|
189
|
if(configurator.isDoSynonyms()) {
|
190
|
includedEntities.add(SearchOption.SYNONYM.getLabel());
|
191
|
}
|
192
|
if(configurator.isDoNamesWithoutTaxa()) {
|
193
|
includedEntities.add(SearchOption.NAME.getLabel());
|
194
|
}
|
195
|
if(configurator.isDoTaxaByCommonNames()){
|
196
|
includedEntities.add(SearchOption.COMMON_NAME.getLabel());
|
197
|
}
|
198
|
configurator.setMatchMode(MatchMode.LIKE);
|
199
|
|
200
|
String includedEntitiesString = "";
|
201
|
for (int i = 0; i < includedEntities.size(); i++){
|
202
|
includedEntitiesString += includedEntities.get(i);
|
203
|
if(i < includedEntities.size() -1){
|
204
|
includedEntitiesString += ", ";
|
205
|
}
|
206
|
}
|
207
|
|
208
|
configurationLabel.setText(includedEntitiesString);
|
209
|
|
210
|
status.setText("Searching...");
|
211
|
|
212
|
searchJob = new SearchJob(Display.getCurrent(), configurator);
|
213
|
searchJob.schedule();
|
214
|
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* <p>displaySearchResult</p>
|
219
|
*
|
220
|
* @param result a {@link java.util.List} object.
|
221
|
*/
|
222
|
protected void displaySearchResult(List<UuidAndTitleCache<IdentifiableEntity>> result) {
|
223
|
if(result.size() > 0){
|
224
|
resultViewer.setInput(result);
|
225
|
status.setText(result.size() + " entities found");
|
226
|
}else{
|
227
|
resultViewer.setInput(EMPTY);
|
228
|
status.setText("Search returned no results");
|
229
|
}
|
230
|
}
|
231
|
|
232
|
/* (non-Javadoc)
|
233
|
* @see org.eclipse.ui.part.WorkbenchPart#setFocus()
|
234
|
*/
|
235
|
/** {@inheritDoc} */
|
236
|
@Override
|
237
|
public void setFocus() {
|
238
|
//logger.warn("Setting focus to search result viewer");
|
239
|
conversation.bind();
|
240
|
// pass focus to resultViewer
|
241
|
resultViewer.getControl().setFocus();
|
242
|
}
|
243
|
|
244
|
/* (non-Javadoc)
|
245
|
* @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
|
246
|
*/
|
247
|
/**
|
248
|
* <p>getConversationHolder</p>
|
249
|
*
|
250
|
* @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
|
251
|
*/
|
252
|
@Override
|
253
|
public ConversationHolder getConversationHolder() {
|
254
|
return this.conversation;
|
255
|
}
|
256
|
|
257
|
/* (non-Javadoc)
|
258
|
* @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
|
259
|
*/
|
260
|
/** {@inheritDoc} */
|
261
|
@Override
|
262
|
public void update(CdmDataChangeMap changeEvents) {
|
263
|
// TODO Auto-generated method stub
|
264
|
|
265
|
}
|
266
|
|
267
|
/* (non-Javadoc)
|
268
|
* @see org.eclipse.ui.part.WorkbenchPart#dispose()
|
269
|
*/
|
270
|
/** {@inheritDoc} */
|
271
|
@Override
|
272
|
public void dispose() {
|
273
|
super.dispose();
|
274
|
conversation.close();
|
275
|
if(searchJob != null) {
|
276
|
searchJob.cancel();
|
277
|
}
|
278
|
CdmStore.getContextManager().removeContextListener(contextListener);
|
279
|
}
|
280
|
|
281
|
/**
|
282
|
*
|
283
|
* @author n.hoffmann
|
284
|
* @created Feb 2, 2010
|
285
|
* @version 1.0
|
286
|
*/
|
287
|
class SearchJob extends Job{
|
288
|
|
289
|
private final IFindTaxaAndNamesConfigurator configurator;
|
290
|
|
291
|
private final Display display;
|
292
|
|
293
|
/**
|
294
|
* @param name
|
295
|
*/
|
296
|
public SearchJob(Display display, IFindTaxaAndNamesConfigurator configurator) {
|
297
|
super("Performing Search");
|
298
|
this.display = display;
|
299
|
this.configurator = configurator;
|
300
|
}
|
301
|
|
302
|
/* (non-Javadoc)
|
303
|
* @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
|
304
|
*/
|
305
|
@Override
|
306
|
protected IStatus run(IProgressMonitor monitor) {
|
307
|
monitor.beginTask("", 100);
|
308
|
monitor.worked(20);
|
309
|
|
310
|
final List<UuidAndTitleCache<IdentifiableEntity>> searchResult = CdmStore.getSearchManager().findTaxaAndNames(configurator);
|
311
|
monitor.worked(40);
|
312
|
|
313
|
if(! monitor.isCanceled()){
|
314
|
display.asyncExec(new Runnable() {
|
315
|
@Override
|
316
|
public void run() {
|
317
|
displaySearchResult(searchResult);
|
318
|
}
|
319
|
});
|
320
|
}else{
|
321
|
display.asyncExec(new Runnable() {
|
322
|
@Override
|
323
|
public void run() {
|
324
|
status.setText("Cancelled");
|
325
|
}
|
326
|
});
|
327
|
}
|
328
|
monitor.done();
|
329
|
return Status.OK_STATUS;
|
330
|
}
|
331
|
|
332
|
}
|
333
|
}
|