1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2013 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.taxeditor.view.derivateSearch;
|
11
|
|
12
|
import java.util.Arrays;
|
13
|
import java.util.LinkedHashMap;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.eclipse.core.commands.ExecutionException;
|
17
|
import org.eclipse.core.commands.NotEnabledException;
|
18
|
import org.eclipse.core.commands.NotHandledException;
|
19
|
import org.eclipse.core.commands.common.NotDefinedException;
|
20
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
21
|
import org.eclipse.jface.viewers.DoubleClickEvent;
|
22
|
import org.eclipse.jface.viewers.IDoubleClickListener;
|
23
|
import org.eclipse.jface.viewers.TableViewer;
|
24
|
import org.eclipse.swt.SWT;
|
25
|
import org.eclipse.swt.events.KeyAdapter;
|
26
|
import org.eclipse.swt.events.KeyEvent;
|
27
|
import org.eclipse.swt.widgets.Composite;
|
28
|
import org.eclipse.ui.handlers.IHandlerService;
|
29
|
|
30
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
|
31
|
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
|
32
|
import eu.etaxonomy.cdm.api.service.config.IdentifiableServiceConfiguratorImpl;
|
33
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
34
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
35
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
36
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
37
|
|
38
|
/**
|
39
|
* Controller class for the {@link DerivateSearchComposite}.<br>
|
40
|
* This controller contains the logic to search the data base for {@link SpecimenOrObservationBase}s
|
41
|
* and handles GUI events.
|
42
|
*
|
43
|
*/
|
44
|
public class DerivateSearchCompositeController {
|
45
|
|
46
|
private final DerivateSearchComposite derivateSearchComposite;
|
47
|
private IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase<DerivedUnitFacadeCacheStrategy>> configurator;
|
48
|
|
49
|
/**
|
50
|
* @param parent
|
51
|
*/
|
52
|
public DerivateSearchCompositeController(Composite parent) {
|
53
|
this.derivateSearchComposite = new DerivateSearchComposite(parent, SWT.NONE);
|
54
|
init();
|
55
|
}
|
56
|
|
57
|
private void init(){
|
58
|
LinkedHashMap<SpecimenOrObservationType, String> orderedTerms = AbstractUtility.orderTerms(Arrays.asList(SpecimenOrObservationType.values()));
|
59
|
String[] comboItems = new String[orderedTerms.size()];
|
60
|
int count = 0;
|
61
|
for(String termMessage:orderedTerms.values()){
|
62
|
comboItems[count] = termMessage;
|
63
|
count++;
|
64
|
}
|
65
|
|
66
|
derivateSearchComposite.getComboDerivateType().setItems(comboItems);
|
67
|
|
68
|
derivateSearchComposite.getSearchField().addKeyListener(new KeyAdapter() {
|
69
|
/*
|
70
|
* (non-Javadoc)
|
71
|
* @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
|
72
|
*/
|
73
|
@Override
|
74
|
public void keyPressed(KeyEvent e) {
|
75
|
if (e.keyCode == SWT.CR) {
|
76
|
String searchString = derivateSearchComposite.getSearchField().getText();
|
77
|
searchDerivates(searchString);
|
78
|
}
|
79
|
}
|
80
|
});
|
81
|
|
82
|
TableViewer resultViewer = derivateSearchComposite.getResultViewer();
|
83
|
resultViewer.setContentProvider(new ArrayContentProvider());
|
84
|
resultViewer.setLabelProvider(new DerivateLabelProvider());
|
85
|
resultViewer.addDoubleClickListener(new IDoubleClickListener() {
|
86
|
@Override
|
87
|
public void doubleClick(DoubleClickEvent event) {
|
88
|
String commandId = "eu.etaxonomy.taxeditor.editor.handler.openDerivateView";
|
89
|
|
90
|
IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
|
91
|
try {
|
92
|
handlerService.executeCommand(commandId, null);
|
93
|
} catch (ExecutionException e) {
|
94
|
AbstractUtility.error(DerivateSearchCompositeController.class, e);
|
95
|
} catch (NotDefinedException e) {
|
96
|
AbstractUtility.error(DerivateSearchCompositeController.class, e);
|
97
|
} catch (NotEnabledException e) {
|
98
|
AbstractUtility.error(DerivateSearchCompositeController.class, e);
|
99
|
} catch (NotHandledException e) {
|
100
|
AbstractUtility.error(DerivateSearchCompositeController.class, e);
|
101
|
}
|
102
|
|
103
|
}
|
104
|
});
|
105
|
configurator = new IdentifiableServiceConfiguratorImpl<SpecimenOrObservationBase<DerivedUnitFacadeCacheStrategy>>();
|
106
|
|
107
|
}
|
108
|
|
109
|
private void searchDerivates(String searchString){
|
110
|
configurator.setTitleSearchString(searchString);
|
111
|
List<SpecimenOrObservationBase> results = CdmStore.getSearchManager().findOccurrences(configurator, true);
|
112
|
derivateSearchComposite.getResultViewer().setInput(results);
|
113
|
}
|
114
|
|
115
|
|
116
|
/**
|
117
|
* @return the derivateSearchComposite
|
118
|
*/
|
119
|
public DerivateSearchComposite getDerivateSearchComposite() {
|
120
|
return derivateSearchComposite;
|
121
|
}
|
122
|
|
123
|
private void setEnable(boolean enabled){
|
124
|
derivateSearchComposite.setEnabled(enabled);
|
125
|
derivateSearchComposite.getResultViewer().getTable().setEnabled(enabled);
|
126
|
derivateSearchComposite.getSearchField().setEnabled(enabled);
|
127
|
}
|
128
|
|
129
|
}
|