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