2 * Copyright (C) 2013 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.searchFilter
;
11 import java
.util
.List
;
12 import java
.util
.UUID
;
14 import org
.eclipse
.swt
.SWT
;
15 import org
.eclipse
.swt
.layout
.GridData
;
16 import org
.eclipse
.swt
.widgets
.Composite
;
17 import org
.eclipse
.swt
.widgets
.Event
;
18 import org
.eclipse
.swt
.widgets
.Listener
;
19 import org
.eclipse
.swt
.widgets
.Widget
;
21 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
22 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.config
.FindOccurrencesConfigurator
;
24 import eu
.etaxonomy
.cdm
.api
.service
.config
.FindOccurrencesConfigurator
.AssignmentStatus
;
25 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
26 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
27 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
30 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
31 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
32 import eu
.etaxonomy
.taxeditor
.store
.SearchManager
;
33 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
34 import eu
.etaxonomy
.taxeditor
.ui
.mvc
.combo
.EnumTermComboController
;
35 import eu
.etaxonomy
.taxeditor
.view
.derivateSearch
.DerivateLabelProvider
;
38 * Controller class for the {@link DerivateSearchComposite}.<br>
39 * This controller contains the logic to search the data base for {@link SpecimenOrObservationBase}s
40 * and handles GUI events.
43 public class DerivateSearchCompositeController
implements Listener
{
45 private final DerivateSearchComposite derivateSearchComposite
;
46 private EnumTermComboController
<SpecimenOrObservationType
> comboSpecimenTypeController
;
47 private DerivateLabelProvider labelProvider
;
48 private Taxon selectedTaxon
;
49 private DerivateView derivativeEditor
;
51 public DerivateSearchCompositeController(Composite parent
, DerivateView derivativeEditor
) {
52 this.derivateSearchComposite
= new DerivateSearchComposite(parent
, SWT
.NONE
);
53 this.derivativeEditor
= derivativeEditor
;
59 comboSpecimenTypeController
= new EnumTermComboController
<SpecimenOrObservationType
>(derivateSearchComposite
.getComboDerivateType(), null, null, SpecimenOrObservationType
.class, 0);
60 comboSpecimenTypeController
.setSelection(SpecimenOrObservationType
.Unknown
);
63 derivateSearchComposite
.getSearchField().addListener(SWT
.KeyDown
, this);
64 derivateSearchComposite
.getButtonSearch().addListener(SWT
.Selection
, this);
65 derivateSearchComposite
.getBtnBrowseTaxa().addListener(SWT
.Selection
, this);
66 derivateSearchComposite
.getBtnClearTaxon().addListener(SWT
.Selection
, this);
70 private void searchDerivates(){
71 String queryString
= derivateSearchComposite
.getSearchField().getText();
72 SpecimenOrObservationType specimenType
= comboSpecimenTypeController
.getSelection();
74 FindOccurrencesConfigurator config
= new FindOccurrencesConfigurator();
75 config
.setAssociatedTaxonUuid(selectedTaxon
!=null?selectedTaxon
.getUuid():null);
76 config
.setSpecimenType(specimenType
.equals(SpecimenOrObservationType
.Unknown
)?
null:specimenType
);//unknown := all types
77 config
.setTitleSearchString(queryString
);
78 if(selectedTaxon
!=null){
79 config
.setSpecimenType(null);
81 switch (derivateSearchComposite
.getComboTaxonAssignment().getSelectionIndex()) {
82 case DerivateSearchComposite
.ALL_SPECIMENS
:
83 config
.setAssignmentStatus(AssignmentStatus
.ALL_SPECIMENS
);
85 case DerivateSearchComposite
.ASSIGNED_SPECIMENS
:
86 config
.setAssignmentStatus(AssignmentStatus
.ASSIGNED_SPECIMENS
);
88 case DerivateSearchComposite
.UNASSIGNED_SPECIMENS
:
89 config
.setAssignmentStatus(AssignmentStatus
.UNASSIGNED_SPECIMENS
);
95 SearchManager searchManager
= new SearchManager();
96 List
<SpecimenOrObservationBase
> occurrences
= searchManager
.findOccurrences(config
, true);
97 derivativeEditor
.updateRootEntities(occurrences
);
101 public void handleEvent(Event event
) {
102 Widget eventSource
= event
.widget
;
103 if(eventSource
== derivateSearchComposite
.getButtonSearch()
104 || (eventSource
== derivateSearchComposite
.getSearchField() && event
.keyCode
== SWT
.CR
)){
107 else if(eventSource
==derivateSearchComposite
.getBtnBrowseTaxa()){
108 selectedTaxon
= SelectionDialogFactory
.getSelectionFromDialog(Taxon
.class, this.derivateSearchComposite
.getShell(), derivativeEditor
.getConversationHolder(), null);
109 if(selectedTaxon
!=null){
110 derivateSearchComposite
.getTextTaxonName().setText(selectedTaxon
.getTitleCache());
112 derivateSearchComposite
.getComboTaxonAssignment().select(DerivateSearchComposite
.ASSIGNED_SPECIMENS
);
113 derivateSearchComposite
.getComboTaxonAssignment().setEnabled(false);
114 derivateSearchComposite
.getLbltaxonAssignment().setEnabled(false);
116 else if(eventSource
==derivateSearchComposite
.getBtnClearTaxon()){
117 selectedTaxon
= null;
118 derivateSearchComposite
.getTextTaxonName().setText(""); //$NON-NLS-1$
119 derivateSearchComposite
.getComboTaxonAssignment().select(DerivateSearchComposite
.ALL_SPECIMENS
);
120 derivateSearchComposite
.getComboTaxonAssignment().setEnabled(true);
121 derivateSearchComposite
.getLbltaxonAssignment().setEnabled(true);
126 * Sets enable status of all input fields
129 public void setEnabled(boolean enabled
) {
130 if(!derivateSearchComposite
.isDisposed()){
131 GridData layoutData
= (GridData
) derivateSearchComposite
.getLayoutData();
132 layoutData
.exclude
= !enabled
;
133 derivateSearchComposite
.setVisible(enabled
);
134 derivateSearchComposite
.getParent().layout(true);
138 public void setLayoutData(Object layoutData
){
139 derivateSearchComposite
.setLayoutData(layoutData
);
143 * Checks whether or not the search criteria are set to their default value.<br>
144 * This does not consider the title search field.
145 * @return <code>true</code> if search criteria are set to default.
147 public boolean isDefaultSearch(){
148 boolean isDefault
= true;
149 isDefault
&= comboSpecimenTypeController
.getSelection()==SpecimenOrObservationType
.Unknown
;
150 isDefault
&= derivateSearchComposite
.getComboTaxonAssignment().getSelectionIndex()==DerivateSearchComposite
.ALL_SPECIMENS
;
151 isDefault
&= selectedTaxon
==null;
155 * Resets all input fields
158 if(!derivateSearchComposite
.isDisposed()){
159 derivateSearchComposite
.getSearchField().setText(""); //$NON-NLS-1$
160 derivateSearchComposite
.getComboDerivateType().deselectAll();
161 comboSpecimenTypeController
.setSelection(SpecimenOrObservationType
.Unknown
);
163 selectedTaxon
= null;
166 public void setConversation(ConversationHolder conversationHolder
) {
167 labelProvider
.setConversation(conversationHolder
);
170 public void setTaxonFilter(UUID taxonUuid
) {
171 TaxonBase taxonBase
= CdmStore
.getService(ITaxonService
.class).load(taxonUuid
);
172 if(taxonBase
!=null && taxonBase
.isInstanceOf(Taxon
.class)){
173 selectedTaxon
= HibernateProxyHelper
.deproxy(taxonBase
, Taxon
.class);
174 derivateSearchComposite
.getTextTaxonName().setText(taxonBase
.getTitleCache());
175 derivateSearchComposite
.getLbltaxonAssignment().setEnabled(false);
176 derivateSearchComposite
.getComboTaxonAssignment().select(DerivateSearchComposite
.ASSIGNED_SPECIMENS
);
177 derivateSearchComposite
.getComboTaxonAssignment().setEnabled(false);
181 public void setFocusOnSearchField(){
182 derivateSearchComposite
.getSearchField().setFocus();