25b99f0229ddaa80f91e3c18ff1d9b93ae878fe0
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / derivateSearch / DerivateSearchCompositeController.java
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.ArrayList;
13 import java.util.List;
14
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;
29
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;
43
44 /**
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.
48 *
49 */
50 public class DerivateSearchCompositeController implements Listener{
51
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;
58
59 /**
60 * @param parent
61 * @param derivateSearchView
62 */
63 public DerivateSearchCompositeController(Composite parent, DerivateSearchView derivateSearchView) {
64 this.derivateSearchComposite = new DerivateSearchComposite(parent, SWT.NONE);
65 this.derivateSearchView = derivateSearchView;
66 init();
67 }
68
69 private void init(){
70 //derivate type combo
71 enumTermComboController = new EnumTermComboController<SpecimenOrObservationType>(derivateSearchComposite.getComboDerivateType(), null, null, SpecimenOrObservationType.class, 0);
72 enumTermComboController.setSelection(SpecimenOrObservationType.Unknown);
73
74 //add listeners
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);
79
80
81 //result table
82 TableViewer resultViewer = derivateSearchComposite.getResultViewer();
83 resultViewer.setContentProvider(new ArrayContentProvider());
84 labelProvider = new DerivateLabelProvider();
85 resultViewer.setLabelProvider(labelProvider);
86 resultViewer.addDoubleClickListener(new IDoubleClickListener() {
87 @Override
88 public void doubleClick(DoubleClickEvent event) {
89 String commandId = "eu.etaxonomy.taxeditor.editor.handler.openDerivateView";
90
91 IHandlerService handlerService = (IHandlerService) AbstractUtility.getService(IHandlerService.class);
92 try {
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);
102 }
103
104 }
105 });
106 configurator = new IdentifiableServiceConfiguratorImpl<SpecimenOrObservationBase<DerivedUnitFacadeCacheStrategy>>();
107
108 }
109
110 private void searchDerivates(){
111 if(!derivateSearchView.getConversationHolder().isBound()){
112 derivateSearchView.setFocus(); //rebind the conversation
113 }
114
115
116 List<SpecimenOrObservationBase<?>> filteredByAssociatedTaxon = null;
117 //filter by taxon
118 if(selectedTaxon!=null){
119 filteredByAssociatedTaxon = CdmStore.getService(IOccurrenceService.class).listByAssociatedTaxon(null, null, selectedTaxon, null, null, null, null, null);
120 }
121
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);
130 }
131 }
132 }
133 else{
134 filteredByType = CdmStore.getSearchManager().findOccurrencesByType(configurator, type);
135 }
136
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);
143 }
144 }
145 filteredByType = specimensWithNoDetermination;
146 }
147 derivateSearchComposite.getResultViewer().setInput(filteredByType);
148 }
149
150 /* (non-Javadoc)
151 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
152 */
153 @Override
154 public void handleEvent(Event event) {
155 Widget eventSource = event.widget;
156 if(eventSource == derivateSearchComposite.getButtonSearch()
157 || (eventSource == derivateSearchComposite.getSearchField() && event.keyCode == SWT.CR)){
158 searchDerivates();
159 }
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());
164 }
165 }
166 else if(eventSource==derivateSearchComposite.getBtnClearTaxon()){
167 selectedTaxon = null;
168 derivateSearchComposite.getTextTaxonName().setText("");
169 }
170 }
171
172 /**
173 * Sets enable status of all input fields
174 * @param enabled
175 */
176 public void setEnabled(boolean enabled) {
177 if(!derivateSearchComposite.isDisposed()){
178 derivateSearchComposite.setEnabled(enabled);
179 }
180 }
181
182 /**
183 * Resets all input fields
184 */
185 public void reset(){
186 derivateSearchComposite.getResultViewer().setInput(null);
187 derivateSearchComposite.getSearchField().setText("");
188 enumTermComboController.setSelection(SpecimenOrObservationType.Unknown);
189 derivateSearchComposite.getComboDerivateType().deselectAll();
190 selectedTaxon = null;
191 }
192
193 public TableViewer getResultViewer() {
194 return derivateSearchComposite.getResultViewer();
195 }
196
197 public void setFocus() {
198 derivateSearchComposite.getResultViewer().getControl().setFocus();
199 }
200
201 /**
202 * @param conversationHolder
203 */
204 public void setConversation(ConversationHolder conversationHolder) {
205 labelProvider.setConversation(conversationHolder);
206 }
207
208 }