34da746194fc0e6ea975b6606d2b577b1747c203
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / association / TaxonAssociationDetailElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2014 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.ui.section.occurrence.association;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Set;
15
16 import org.eclipse.jface.action.MenuManager;
17 import org.eclipse.jface.viewers.ArrayContentProvider;
18 import org.eclipse.jface.viewers.DoubleClickEvent;
19 import org.eclipse.jface.viewers.IDoubleClickListener;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.LabelProvider;
22 import org.eclipse.jface.viewers.ListViewer;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Label;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.ui.handlers.HandlerUtil;
28
29 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
30 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
33 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
34 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
35 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36 import eu.etaxonomy.taxeditor.model.MessagingUtils;
37 import eu.etaxonomy.taxeditor.store.CdmStore;
38 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
39 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
40 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
41 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
42 import eu.etaxonomy.taxeditor.view.CdmViewerChooser;
43
44 /**
45 * @author pplitzner
46 * @date Dec 1, 2014
47 *
48 */
49 public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<DerivedUnitFacade> implements IDoubleClickListener{
50
51 public TaxonAssociationDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
52 super(formFactory, formElement);
53 }
54
55 /** {@inheritDoc} */
56 @Override
57 protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
58
59 //TODO add context menu for deleting associations
60
61 Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listIndividualsAssociationTaxa(entity.innerDerivedUnit(), null, null, null, null);
62 Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
63 Collection<DeterminationEvent> determinationEvents = CdmStore.getService(IOccurrenceService.class).listDeterminationEvents(entity.innerDerivedUnit(), null, null, null, null);
64
65 Collection<CdmBase> associations = new ArrayList<>();
66 associations.addAll(associatedTaxa);
67 associations.addAll(typeDesignations);
68 associations.addAll(determinationEvents);
69
70 if(associatedTaxa.isEmpty() && typeDesignations.isEmpty() && determinationEvents.isEmpty()){
71 Label label = formFactory.createLabel(getLayoutComposite(), "No associations");
72 label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
73 return;
74 }
75 if(!associations.isEmpty()){
76 ListViewer viewer = new ListViewer(getLayoutComposite(), SWT.SINGLE);
77 viewer.getList().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
78 viewer.setContentProvider(new ArrayContentProvider());
79 viewer.setLabelProvider(new LabelProvider(){
80 @Override
81 public String getText(Object element) {
82 if(element instanceof TaxonBase){
83 return "Associated with "+element.toString();
84 }
85 else if(element instanceof DeterminationEvent){
86 DeterminationEvent determinationEvent = (DeterminationEvent)element;
87 if(determinationEvent.getTaxon()!=null){
88 return "Determined as taxon "+determinationEvent.getTaxon();
89 }
90 if(determinationEvent.getTaxonName()!=null){
91 return "Determined as name "+determinationEvent.getTaxonName();
92 }
93 }
94 else if(element instanceof SpecimenTypeDesignation){
95 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)element;
96 String label = typeDesignation.getTypeStatus()!=null?typeDesignation.getTypeStatus().getLabel()+" of ":"Type of ";
97 Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
98 for (TaxonNameBase taxonNameBase : typifiedNames) {
99 label += taxonNameBase+", ";
100 }
101 if(label.endsWith(", ")){
102 label = label.substring(0, label.length()-2);
103 }
104 return label;
105 }
106 return "";
107 }
108 });
109 viewer.setInput(associations);
110 viewer.addDoubleClickListener(this);
111 }
112 }
113
114 /** {@inheritDoc} */
115 @Override
116 public void handleEvent(Object eventSource) {
117 //empty
118 }
119
120 @Override
121 public void doubleClick(DoubleClickEvent event) {
122 if(event.getSelection() instanceof IStructuredSelection){
123 Object firstElement = ((IStructuredSelection) event.getSelection()).getFirstElement();
124 if(firstElement instanceof SpecimenTypeDesignation){
125 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)firstElement;
126 Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
127 if(typifiedNames.size()==1){
128 firstElement = typifiedNames.iterator().next();
129 }
130 if(typifiedNames.size()==0 || typifiedNames.size()>=2){
131 MessagingUtils.informationDialog("Could not open name", "Type designation has either 0 or more than 1 names associated");
132 }
133 }
134 else if(firstElement instanceof DeterminationEvent){
135 if(((DeterminationEvent) firstElement).getTaxon()!=null){
136 firstElement = ((DeterminationEvent) firstElement).getTaxon();
137 }
138 else if(((DeterminationEvent) firstElement).getTaxonName()!=null){
139 firstElement = ((DeterminationEvent) firstElement).getTaxonName();
140 }
141 }
142 new CdmViewerChooser(event.getViewer().getControl().getShell()).chooseViewer(firstElement);
143 }
144 }
145 }