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