1
|
/**
|
2
|
* Copyright (C) 2013 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
|
package eu.etaxonomy.taxeditor.editor.view.derivate.searchFilter;
|
10
|
|
11
|
import java.util.List;
|
12
|
import java.util.UUID;
|
13
|
|
14
|
import org.eclipse.core.runtime.NullProgressMonitor;
|
15
|
import org.eclipse.swt.SWT;
|
16
|
import org.eclipse.swt.layout.GridData;
|
17
|
import org.eclipse.swt.widgets.Composite;
|
18
|
import org.eclipse.swt.widgets.Event;
|
19
|
import org.eclipse.swt.widgets.Listener;
|
20
|
import org.eclipse.swt.widgets.Widget;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
23
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
24
|
import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
|
25
|
import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator.AssignmentStatus;
|
26
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
27
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
28
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
30
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
31
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
|
32
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
33
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
34
|
import eu.etaxonomy.taxeditor.store.SearchManager;
|
35
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
|
36
|
import eu.etaxonomy.taxeditor.ui.mvc.combo.EnumTermComboController;
|
37
|
import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
|
38
|
|
39
|
/**
|
40
|
* Controller class for the {@link DerivateSearchComposite}.<br>
|
41
|
* This controller contains the logic to search the data base for {@link SpecimenOrObservationBase}s
|
42
|
* and handles GUI events.
|
43
|
*
|
44
|
*/
|
45
|
public class DerivateSearchCompositeController implements Listener{
|
46
|
|
47
|
private final DerivateSearchComposite derivateSearchComposite;
|
48
|
private EnumTermComboController<SpecimenOrObservationType> comboSpecimenTypeController;
|
49
|
private DerivateLabelProvider labelProvider;
|
50
|
private Taxon selectedTaxon;
|
51
|
private DerivateView derivativeEditor;
|
52
|
|
53
|
public DerivateSearchCompositeController(Composite parent, DerivateView derivativeEditor) {
|
54
|
this.derivateSearchComposite = new DerivateSearchComposite(parent, SWT.NONE);
|
55
|
this.derivativeEditor = derivativeEditor;
|
56
|
init();
|
57
|
}
|
58
|
|
59
|
private void init(){
|
60
|
//derivate type combo
|
61
|
comboSpecimenTypeController = new EnumTermComboController<SpecimenOrObservationType>(derivateSearchComposite.getComboDerivateType(), null, null, SpecimenOrObservationType.class, 0);
|
62
|
comboSpecimenTypeController.setSelection(SpecimenOrObservationType.Unknown);
|
63
|
|
64
|
//add listeners
|
65
|
derivateSearchComposite.getSearchField().addListener(SWT.KeyDown, this);
|
66
|
derivateSearchComposite.getButtonSearch().addListener(SWT.Selection, this);
|
67
|
derivateSearchComposite.getBtnBrowseTaxa().addListener(SWT.Selection, this);
|
68
|
derivateSearchComposite.getBtnClearTaxon().addListener(SWT.Selection, this);
|
69
|
|
70
|
}
|
71
|
|
72
|
private void searchDerivates(){
|
73
|
if(derivativeEditor.isDirty()){
|
74
|
boolean confirm = MessagingUtils.confirmDialog("Unsaved changes", "You have to save before performing this action. Save now?");
|
75
|
if(confirm){
|
76
|
derivativeEditor.save(new NullProgressMonitor());
|
77
|
}
|
78
|
else{
|
79
|
return;
|
80
|
}
|
81
|
}
|
82
|
String queryString = derivateSearchComposite.getSearchField().getText();
|
83
|
SpecimenOrObservationType specimenType = comboSpecimenTypeController.getSelection();
|
84
|
|
85
|
FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
|
86
|
config.setAssociatedTaxonUuid(selectedTaxon!=null?selectedTaxon.getUuid():null);
|
87
|
config.setSpecimenType(specimenType.equals(SpecimenOrObservationType.Unknown)?null:specimenType);//unknown := all types
|
88
|
config.setTitleSearchString(queryString);
|
89
|
if(selectedTaxon!=null){
|
90
|
config.setSpecimenType(null);
|
91
|
}
|
92
|
switch (derivateSearchComposite.getComboTaxonAssignment().getSelectionIndex()) {
|
93
|
case DerivateSearchComposite.ALL_SPECIMENS:
|
94
|
config.setAssignmentStatus(AssignmentStatus.ALL_SPECIMENS);
|
95
|
break;
|
96
|
case DerivateSearchComposite.ASSIGNED_SPECIMENS:
|
97
|
config.setAssignmentStatus(AssignmentStatus.ASSIGNED_SPECIMENS);
|
98
|
break;
|
99
|
case DerivateSearchComposite.UNASSIGNED_SPECIMENS:
|
100
|
config.setAssignmentStatus(AssignmentStatus.UNASSIGNED_SPECIMENS);
|
101
|
break;
|
102
|
default:
|
103
|
break;
|
104
|
}
|
105
|
if(derivativeEditor.getCdmEntitySession()!=null){
|
106
|
derivativeEditor.getCdmEntitySession().dispose();
|
107
|
derivativeEditor.getCdmEntitySession().bind();
|
108
|
}
|
109
|
|
110
|
SearchManager searchManager = new SearchManager();
|
111
|
List<SpecimenOrObservationBase> occurrences = searchManager.findOccurrences(config, true);
|
112
|
derivativeEditor.updateRootEntities(occurrences);
|
113
|
}
|
114
|
|
115
|
@Override
|
116
|
public void handleEvent(Event event) {
|
117
|
Widget eventSource = event.widget;
|
118
|
if(eventSource == derivateSearchComposite.getButtonSearch()
|
119
|
|| (eventSource == derivateSearchComposite.getSearchField() && event.keyCode == SWT.CR)){
|
120
|
searchDerivates();
|
121
|
}
|
122
|
else if(eventSource==derivateSearchComposite.getBtnBrowseTaxa()){
|
123
|
selectedTaxon = SelectionDialogFactory.getSelectionFromDialog(Taxon.class, this.derivateSearchComposite.getShell(), derivativeEditor.getConversationHolder(), null);
|
124
|
if(selectedTaxon!=null){
|
125
|
derivateSearchComposite.getTextTaxonName().setText(selectedTaxon.getTitleCache());
|
126
|
}
|
127
|
derivateSearchComposite.getComboTaxonAssignment().select(DerivateSearchComposite.ASSIGNED_SPECIMENS);
|
128
|
derivateSearchComposite.getComboTaxonAssignment().setEnabled(false);
|
129
|
derivateSearchComposite.getLbltaxonAssignment().setEnabled(false);
|
130
|
}
|
131
|
else if(eventSource==derivateSearchComposite.getBtnClearTaxon()){
|
132
|
selectedTaxon = null;
|
133
|
derivateSearchComposite.getTextTaxonName().setText(""); //$NON-NLS-1$
|
134
|
derivateSearchComposite.getComboTaxonAssignment().select(DerivateSearchComposite.ALL_SPECIMENS);
|
135
|
derivateSearchComposite.getComboTaxonAssignment().setEnabled(true);
|
136
|
derivateSearchComposite.getLbltaxonAssignment().setEnabled(true);
|
137
|
}
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* Sets enable status of all input fields
|
142
|
* @param enabled
|
143
|
*/
|
144
|
public void setEnabled(boolean enabled) {
|
145
|
if(!derivateSearchComposite.isDisposed()){
|
146
|
GridData layoutData = (GridData) derivateSearchComposite.getLayoutData();
|
147
|
layoutData.exclude = !enabled;
|
148
|
derivateSearchComposite.setVisible(enabled);
|
149
|
derivateSearchComposite.getParent().layout(true);
|
150
|
}
|
151
|
}
|
152
|
|
153
|
public void setLayoutData(Object layoutData){
|
154
|
derivateSearchComposite.setLayoutData(layoutData);
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* Checks whether or not the search criteria are set to their default value.<br>
|
159
|
* This does not consider the title search field.
|
160
|
* @return <code>true</code> if search criteria are set to default.
|
161
|
*/
|
162
|
public boolean isDefaultSearch(){
|
163
|
boolean isDefault = true;
|
164
|
isDefault &= comboSpecimenTypeController.getSelection()==SpecimenOrObservationType.Unknown;
|
165
|
isDefault &= derivateSearchComposite.getComboTaxonAssignment().getSelectionIndex()==DerivateSearchComposite.ALL_SPECIMENS;
|
166
|
isDefault &= selectedTaxon==null;
|
167
|
return isDefault;
|
168
|
}
|
169
|
/**
|
170
|
* Resets all input fields
|
171
|
*/
|
172
|
public void reset(){
|
173
|
if(!derivateSearchComposite.isDisposed()){
|
174
|
derivateSearchComposite.getSearchField().setText(""); //$NON-NLS-1$
|
175
|
derivateSearchComposite.getComboDerivateType().deselectAll();
|
176
|
comboSpecimenTypeController.setSelection(SpecimenOrObservationType.Unknown);
|
177
|
}
|
178
|
selectedTaxon = null;
|
179
|
}
|
180
|
|
181
|
public void setConversation(ConversationHolder conversationHolder) {
|
182
|
labelProvider.setConversation(conversationHolder);
|
183
|
}
|
184
|
|
185
|
public void setTaxonFilter(UUID taxonUuid) {
|
186
|
TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonUuid);
|
187
|
if(taxonBase!=null && taxonBase.isInstanceOf(Taxon.class)){
|
188
|
selectedTaxon = HibernateProxyHelper.deproxy(taxonBase, Taxon.class);
|
189
|
derivateSearchComposite.getTextTaxonName().setText(taxonBase.getTitleCache());
|
190
|
derivateSearchComposite.getLbltaxonAssignment().setEnabled(false);
|
191
|
derivateSearchComposite.getComboTaxonAssignment().select(DerivateSearchComposite.ASSIGNED_SPECIMENS);
|
192
|
derivateSearchComposite.getComboTaxonAssignment().setEnabled(false);
|
193
|
}
|
194
|
}
|
195
|
|
196
|
public void setFocusOnSearchField(){
|
197
|
derivateSearchComposite.getSearchField().setFocus();
|
198
|
}
|
199
|
|
200
|
}
|