3 * Copyright (C) 2013 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.view
.derivateSearch
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
15 import org
.eclipse
.core
.commands
.ExecutionException
;
16 import org
.eclipse
.core
.commands
.NotEnabledException
;
17 import org
.eclipse
.core
.commands
.NotHandledException
;
18 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
19 import org
.eclipse
.jface
.viewers
.ArrayContentProvider
;
20 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
21 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
22 import org
.eclipse
.jface
.viewers
.TableViewer
;
23 import org
.eclipse
.swt
.SWT
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.swt
.widgets
.Event
;
26 import org
.eclipse
.swt
.widgets
.Listener
;
27 import org
.eclipse
.swt
.widgets
.Widget
;
28 import org
.eclipse
.ui
.handlers
.IHandlerService
;
30 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
31 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacadeCacheStrategy
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.config
.IIdentifiableEntityServiceConfigurator
;
34 import eu
.etaxonomy
.cdm
.api
.service
.config
.IdentifiableServiceConfiguratorImpl
;
35 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
36 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationType
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
39 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
40 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
41 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.SelectionDialogFactory
;
42 import eu
.etaxonomy
.taxeditor
.ui
.mvc
.combo
.EnumTermComboController
;
45 * Controller class for the {@link DerivateSearchComposite}.<br>
46 * This controller contains the logic to search the data base for {@link SpecimenOrObservationBase}s
47 * and handles GUI events.
50 public class DerivateSearchCompositeController
implements Listener
{
52 private final DerivateSearchComposite derivateSearchComposite
;
53 private IIdentifiableEntityServiceConfigurator
<SpecimenOrObservationBase
<DerivedUnitFacadeCacheStrategy
>> configurator
;
54 private EnumTermComboController
<SpecimenOrObservationType
> enumTermComboController
;
55 private final DerivateSearchView derivateSearchView
;
56 private DerivateLabelProvider labelProvider
;
57 private Taxon selectedTaxon
;
61 * @param derivateSearchView
63 public DerivateSearchCompositeController(Composite parent
, DerivateSearchView derivateSearchView
) {
64 this.derivateSearchComposite
= new DerivateSearchComposite(parent
, SWT
.NONE
);
65 this.derivateSearchView
= derivateSearchView
;
71 enumTermComboController
= new EnumTermComboController
<SpecimenOrObservationType
>(derivateSearchComposite
.getComboDerivateType(), null, null, SpecimenOrObservationType
.class, 0);
72 enumTermComboController
.setSelection(SpecimenOrObservationType
.Unknown
);
75 derivateSearchComposite
.getSearchField().addListener(SWT
.KeyDown
, this);
76 derivateSearchComposite
.getButtonSearch().addListener(SWT
.Selection
, this);
77 derivateSearchComposite
.getBtnBrowseTaxa().addListener(SWT
.Selection
, this);
78 derivateSearchComposite
.getBtnClearTaxon().addListener(SWT
.Selection
, this);
82 TableViewer resultViewer
= derivateSearchComposite
.getResultViewer();
83 resultViewer
.setContentProvider(new ArrayContentProvider());
84 labelProvider
= new DerivateLabelProvider();
85 resultViewer
.setLabelProvider(labelProvider
);
86 resultViewer
.addDoubleClickListener(new IDoubleClickListener() {
88 public void doubleClick(DoubleClickEvent event
) {
89 String commandId
= "eu.etaxonomy.taxeditor.editor.handler.openDerivateView";
91 IHandlerService handlerService
= (IHandlerService
) AbstractUtility
.getService(IHandlerService
.class);
93 handlerService
.executeCommand(commandId
, null);
94 } catch (ExecutionException e
) {
95 MessagingUtils
.error(DerivateSearchCompositeController
.class, e
);
96 } catch (NotDefinedException e
) {
97 MessagingUtils
.error(DerivateSearchCompositeController
.class, e
);
98 } catch (NotEnabledException e
) {
99 MessagingUtils
.error(DerivateSearchCompositeController
.class, e
);
100 } catch (NotHandledException e
) {
101 MessagingUtils
.error(DerivateSearchCompositeController
.class, e
);
106 configurator
= new IdentifiableServiceConfiguratorImpl
<SpecimenOrObservationBase
<DerivedUnitFacadeCacheStrategy
>>();
110 private void searchDerivates(){
111 if(!derivateSearchView
.getConversationHolder().isBound()){
112 derivateSearchView
.setFocus(); //rebind the conversation
116 List
<SpecimenOrObservationBase
<?
>> filteredByAssociatedTaxon
= null;
118 if(selectedTaxon
!=null){
119 filteredByAssociatedTaxon
= CdmStore
.getService(IOccurrenceService
.class).listByAssociatedTaxon(null, null, selectedTaxon
, null, null, null, null, null);
122 //filter by specimen type
123 configurator
.setTitleSearchString(derivateSearchComposite
.getSearchField().getText());
124 SpecimenOrObservationType type
= enumTermComboController
.getSelection();
125 List
<SpecimenOrObservationBase
> filteredByType
= new ArrayList
<SpecimenOrObservationBase
>();
126 if(filteredByAssociatedTaxon
!=null){
127 for (SpecimenOrObservationBase specimenOrObservationBase
: filteredByAssociatedTaxon
) {
128 if(type
.equals(SpecimenOrObservationType
.Unknown
) || specimenOrObservationBase
.getRecordBasis().equals(type
)){
129 filteredByType
.add(specimenOrObservationBase
);
134 filteredByType
= CdmStore
.getSearchManager().findOccurrencesByType(configurator
, type
);
137 //filter out assigned specimens
138 if(derivateSearchComposite
.getBtnFilterUndeterminedSpecimen().getSelection()){
139 List
<SpecimenOrObservationBase
> specimensWithNoDetermination
= new ArrayList
<SpecimenOrObservationBase
>();
140 for(SpecimenOrObservationBase
<?
> result
:filteredByType
){
141 if(CdmStore
.getService(IOccurrenceService
.class).countDeterminations(result
, null)==0){
142 specimensWithNoDetermination
.add(result
);
145 filteredByType
= specimensWithNoDetermination
;
147 derivateSearchComposite
.getResultViewer().setInput(filteredByType
);
151 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
154 public void handleEvent(Event event
) {
155 Widget eventSource
= event
.widget
;
156 if(eventSource
== derivateSearchComposite
.getButtonSearch()
157 || (eventSource
== derivateSearchComposite
.getSearchField() && event
.keyCode
== SWT
.CR
)){
160 else if(eventSource
==derivateSearchComposite
.getBtnBrowseTaxa()){
161 selectedTaxon
= SelectionDialogFactory
.getSelectionFromDialog(Taxon
.class, derivateSearchView
.getSite().getShell(), derivateSearchView
.getConversationHolder(), null);
162 if(selectedTaxon
!=null){
163 derivateSearchComposite
.getTextTaxonName().setText(selectedTaxon
.getTitleCache());
166 else if(eventSource
==derivateSearchComposite
.getBtnClearTaxon()){
167 selectedTaxon
= null;
168 derivateSearchComposite
.getTextTaxonName().setText("");
173 * Sets enable status of all input fields
176 public void setEnabled(boolean enabled
) {
177 if(!derivateSearchComposite
.isDisposed()){
178 derivateSearchComposite
.setEnabled(enabled
);
183 * Resets all input fields
186 derivateSearchComposite
.getResultViewer().setInput(null);
187 derivateSearchComposite
.getSearchField().setText("");
188 enumTermComboController
.setSelection(SpecimenOrObservationType
.Unknown
);
189 derivateSearchComposite
.getComboDerivateType().deselectAll();
190 selectedTaxon
= null;
193 public TableViewer
getResultViewer() {
194 return derivateSearchComposite
.getResultViewer();
197 public void setFocus() {
198 derivateSearchComposite
.getResultViewer().getControl().setFocus();
202 * @param conversationHolder
204 public void setConversation(ConversationHolder conversationHolder
) {
205 labelProvider
.setConversation(conversationHolder
);