*/
package eu.etaxonomy.taxeditor.view.derivateSearch;
-import java.util.Arrays;
-import java.util.LinkedHashMap;
+import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.KeyAdapter;
-import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Widget;
import org.eclipse.ui.handlers.IHandlerService;
-import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy;
-import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
-import eu.etaxonomy.cdm.api.service.config.IdentifiableServiceConfiguratorImpl;
+import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
+import eu.etaxonomy.cdm.api.service.IOccurrenceService;
+import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
+import eu.etaxonomy.cdm.model.taxon.Taxon;
import eu.etaxonomy.taxeditor.model.AbstractUtility;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
import eu.etaxonomy.taxeditor.store.CdmStore;
+import eu.etaxonomy.taxeditor.store.SearchManager;
+import eu.etaxonomy.taxeditor.ui.dialog.selection.SelectionDialogFactory;
import eu.etaxonomy.taxeditor.ui.mvc.combo.EnumTermComboController;
/**
* and handles GUI events.
*
*/
-public class DerivateSearchCompositeController {
+public class DerivateSearchCompositeController implements Listener{
private final DerivateSearchComposite derivateSearchComposite;
- private IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase<DerivedUnitFacadeCacheStrategy>> configurator;
- private EnumTermComboController<SpecimenOrObservationType> enumTermComboController;
+ private EnumTermComboController<SpecimenOrObservationType> comboSpecimenTypeController;
+ private final DerivateSearchView derivateSearchView;
+ private DerivateLabelProvider labelProvider;
+ private Taxon selectedTaxon;
/**
* @param parent
+ * @param derivateSearchView
*/
- public DerivateSearchCompositeController(Composite parent) {
+ public DerivateSearchCompositeController(Composite parent, DerivateSearchView derivateSearchView) {
this.derivateSearchComposite = new DerivateSearchComposite(parent, SWT.NONE);
+ this.derivateSearchView = derivateSearchView;
init();
}
private void init(){
- LinkedHashMap<SpecimenOrObservationType, String> orderedTerms = AbstractUtility.orderTerms(Arrays.asList(SpecimenOrObservationType.values()));
- String[] comboItems = new String[orderedTerms.size()];
- int count = 0;
- for(String termMessage:orderedTerms.values()){
- comboItems[count] = termMessage;
- count++;
- }
+ //derivate type combo
+ comboSpecimenTypeController = new EnumTermComboController<SpecimenOrObservationType>(derivateSearchComposite.getComboDerivateType(), null, null, SpecimenOrObservationType.class, 0);
+ comboSpecimenTypeController.setSelection(SpecimenOrObservationType.Unknown);
-// derivateSearchComposite.getComboDerivateType().setItems(comboItems);
- enumTermComboController = new EnumTermComboController<SpecimenOrObservationType>(derivateSearchComposite.getComboDerivateType(), null, null, SpecimenOrObservationType.class, 0);
+ //add listeners
+ derivateSearchComposite.getSearchField().addListener(SWT.KeyDown, this);
+ derivateSearchComposite.getButtonSearch().addListener(SWT.Selection, this);
+ derivateSearchComposite.getBtnBrowseTaxa().addListener(SWT.Selection, this);
+ derivateSearchComposite.getBtnClearTaxon().addListener(SWT.Selection, this);
- derivateSearchComposite.getSearchField().addKeyListener(new KeyAdapter() {
- /*
- * (non-Javadoc)
- * @see org.eclipse.swt.events.KeyAdapter#keyPressed(org.eclipse.swt.events.KeyEvent)
- */
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.keyCode == SWT.CR) {
- String searchString = derivateSearchComposite.getSearchField().getText();
- searchDerivates(searchString);
- }
- }
- });
+ //result table
TableViewer resultViewer = derivateSearchComposite.getResultViewer();
resultViewer.setContentProvider(new ArrayContentProvider());
- resultViewer.setLabelProvider(new DerivateLabelProvider());
+ labelProvider = new DerivateLabelProvider();
+ resultViewer.setLabelProvider(labelProvider);
resultViewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(DoubleClickEvent event) {
try {
handlerService.executeCommand(commandId, null);
} catch (ExecutionException e) {
- AbstractUtility.error(DerivateSearchCompositeController.class, e);
+ MessagingUtils.error(DerivateSearchCompositeController.class, e);
} catch (NotDefinedException e) {
- AbstractUtility.error(DerivateSearchCompositeController.class, e);
+ MessagingUtils.error(DerivateSearchCompositeController.class, e);
} catch (NotEnabledException e) {
- AbstractUtility.error(DerivateSearchCompositeController.class, e);
+ MessagingUtils.error(DerivateSearchCompositeController.class, e);
} catch (NotHandledException e) {
- AbstractUtility.error(DerivateSearchCompositeController.class, e);
+ MessagingUtils.error(DerivateSearchCompositeController.class, e);
}
}
});
- configurator = new IdentifiableServiceConfiguratorImpl<SpecimenOrObservationBase<DerivedUnitFacadeCacheStrategy>>();
}
- private void searchDerivates(String searchString){
- configurator.setTitleSearchString(searchString);
- SpecimenOrObservationType type = enumTermComboController.getSelection();
- List<SpecimenOrObservationBase> results = CdmStore.getSearchManager().findOccurrencesByType(configurator, type);
- derivateSearchComposite.getResultViewer().setInput(results);
+ private void searchDerivates(){
+ if(!derivateSearchView.getConversationHolder().isBound()){
+ derivateSearchView.setFocus(); //rebind the conversation
+ }
+
+ String queryString = derivateSearchComposite.getSearchField().getText();
+ SpecimenOrObservationType specimenType = comboSpecimenTypeController.getSelection();
+
+ FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
+ config.setAssociatedTaxonUuid(selectedTaxon!=null?selectedTaxon.getUuid():null);
+ config.setSpecimenType(specimenType.equals(SpecimenOrObservationType.Unknown)?null:specimenType);//unknown := all types
+ config.setTitleSearchString(queryString);
+ if(selectedTaxon!=null){
+ config.setRetrieveIndirectlyAssociatedSpecimens(true);
+ config.setSpecimenType(null);
+ }
+
+ SearchManager searchManager = new SearchManager();
+ List<SpecimenOrObservationBase> occurrences = searchManager.findOccurrences(config);
+
+ //filter out specimens of wrong type (only if an associated taxon was selected)
+ if(selectedTaxon!=null && !specimenType.equals(SpecimenOrObservationType.Unknown)){
+ List<SpecimenOrObservationBase> specimensOfCorrectType = new ArrayList<SpecimenOrObservationBase>();
+ for(SpecimenOrObservationBase<?> occurrence:occurrences){
+ if(occurrence.getRecordBasis().equals(specimenType)
+ || occurrence.getRecordBasis().isKindOf(specimenType)){
+ specimensOfCorrectType.add(occurrence);
+ }
+ }
+ occurrences = specimensOfCorrectType;
+ }
+
+ //filter out assigned specimens
+ if(derivateSearchComposite.getBtnFilterUndeterminedSpecimen().getSelection()){
+ List<SpecimenOrObservationBase> specimensWithNoDetermination = new ArrayList<SpecimenOrObservationBase>();
+ for(SpecimenOrObservationBase<?> result:occurrences){
+ if(CdmStore.getService(IOccurrenceService.class).countDeterminations(result, null)==0){
+ specimensWithNoDetermination.add(result);
+ }
+ }
+ occurrences = specimensWithNoDetermination;
+ }
+ derivateSearchComposite.getResultViewer().setInput(occurrences);
}
+ @Override
+ public void handleEvent(Event event) {
+ Widget eventSource = event.widget;
+ if(eventSource == derivateSearchComposite.getButtonSearch()
+ || (eventSource == derivateSearchComposite.getSearchField() && event.keyCode == SWT.CR)){
+ searchDerivates();
+ }
+ else if(eventSource==derivateSearchComposite.getBtnBrowseTaxa()){
+ selectedTaxon = SelectionDialogFactory.getSelectionFromDialog(Taxon.class, derivateSearchView.getSite().getShell(), derivateSearchView.getConversationHolder(), null);
+ if(selectedTaxon!=null){
+ derivateSearchComposite.getTextTaxonName().setText(selectedTaxon.getTitleCache());
+ }
+ }
+ else if(eventSource==derivateSearchComposite.getBtnClearTaxon()){
+ selectedTaxon = null;
+ derivateSearchComposite.getTextTaxonName().setText("");
+ }
+ }
/**
- * @return the derivateSearchComposite
+ * Sets enable status of all input fields
+ * @param enabled
*/
- public DerivateSearchComposite getDerivateSearchComposite() {
- return derivateSearchComposite;
+ public void setEnabled(boolean enabled) {
+ if(!derivateSearchComposite.isDisposed()){
+ derivateSearchComposite.setEnabled(enabled);
+ }
}
- private void setEnable(boolean enabled){
- derivateSearchComposite.setEnabled(enabled);
- derivateSearchComposite.getResultViewer().getTable().setEnabled(enabled);
- derivateSearchComposite.getSearchField().setEnabled(enabled);
+ /**
+ * Resets all input fields
+ */
+ public void reset(){
+ if(!derivateSearchComposite.isDisposed()){
+ derivateSearchComposite.getResultViewer().setInput(null);
+ derivateSearchComposite.getSearchField().setText("");
+ derivateSearchComposite.getComboDerivateType().deselectAll();
+ comboSpecimenTypeController.setSelection(SpecimenOrObservationType.Unknown);
+ }
+ selectedTaxon = null;
+ }
+
+ public TableViewer getResultViewer() {
+ return derivateSearchComposite.getResultViewer();
+ }
+
+ public void setFocus() {
+ derivateSearchComposite.getResultViewer().getControl().setFocus();
+ }
+
+ /**
+ * @param conversationHolder
+ */
+ public void setConversation(ConversationHolder conversationHolder) {
+ labelProvider.setConversation(conversationHolder);
}
}