3 * Copyright (C) 2014 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
.ui
.section
.occurrence
.association
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
15 import java
.util
.UUID
;
17 import org
.eclipse
.core
.commands
.Command
;
18 import org
.eclipse
.core
.commands
.ExecutionException
;
19 import org
.eclipse
.core
.commands
.IParameter
;
20 import org
.eclipse
.core
.commands
.NotEnabledException
;
21 import org
.eclipse
.core
.commands
.NotHandledException
;
22 import org
.eclipse
.core
.commands
.Parameterization
;
23 import org
.eclipse
.core
.commands
.ParameterizedCommand
;
24 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
25 import org
.eclipse
.jface
.viewers
.ArrayContentProvider
;
26 import org
.eclipse
.jface
.viewers
.DoubleClickEvent
;
27 import org
.eclipse
.jface
.viewers
.IDoubleClickListener
;
28 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.LabelProvider
;
30 import org
.eclipse
.jface
.viewers
.TableViewer
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.widgets
.Label
;
33 import org
.eclipse
.ui
.IWorkbenchWindow
;
34 import org
.eclipse
.ui
.PlatformUI
;
35 import org
.eclipse
.ui
.commands
.ICommandService
;
36 import org
.eclipse
.ui
.handlers
.IHandlerService
;
38 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
39 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
40 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
41 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
42 import eu
.etaxonomy
.cdm
.model
.occurrence
.DeterminationEvent
;
43 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
44 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
45 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
46 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
47 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
48 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
49 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractCdmDetailElement
;
56 public class TaxonAssociationDetailElement
extends AbstractCdmDetailElement
<DerivedUnitFacade
> implements IDoubleClickListener
{
58 public TaxonAssociationDetailElement(CdmFormFactory formFactory
, ICdmFormElement formElement
) {
59 super(formFactory
, formElement
);
64 protected void createControls(ICdmFormElement formElement
, DerivedUnitFacade entity
, int style
) {
66 //TODO add context menu for deleting associations
68 Collection
<TaxonBase
<?
>> associatedTaxa
= CdmStore
.getService(IOccurrenceService
.class).listIndividualsAssociationTaxa(entity
.innerDerivedUnit(), null, null, null, null);
69 Collection
<SpecimenTypeDesignation
> typeDesignations
= CdmStore
.getService(IOccurrenceService
.class).listTypeDesignations(entity
.innerDerivedUnit(), null, null, null, null);
70 Collection
<DeterminationEvent
> determinationEvents
= CdmStore
.getService(IOccurrenceService
.class).listDeterminationEvents(entity
.innerDerivedUnit(), null, null, null, null);
72 if(associatedTaxa
.isEmpty() && typeDesignations
.isEmpty() && determinationEvents
.isEmpty()){
73 Label label
= formFactory
.createLabel(getLayoutComposite(), "No associations");
74 label
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
77 if(!associatedTaxa
.isEmpty()){
78 TableViewer viewer
= new TableViewer(getLayoutComposite(), SWT
.FULL_SELECTION
);
79 viewer
.getTable().setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
80 viewer
.setContentProvider(new ArrayContentProvider());
81 viewer
.setLabelProvider(new LabelProvider(){
83 public String
getText(Object element
) {
84 return "Associated with "+element
.toString();
87 viewer
.setInput(associatedTaxa
);
88 viewer
.addDoubleClickListener(this);
90 if(!typeDesignations
.isEmpty()){
91 TableViewer viewer
= new TableViewer(getLayoutComposite(), SWT
.FULL_SELECTION
);
92 viewer
.getTable().setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
93 viewer
.setContentProvider(new ArrayContentProvider());
94 viewer
.setLabelProvider(new LabelProvider(){
96 public String
getText(Object element
) {
97 SpecimenTypeDesignation typeDesignation
= (SpecimenTypeDesignation
)element
;
98 String label
= typeDesignation
.getTypeStatus().getLabel()+" of ";
99 Set
<TaxonNameBase
> typifiedNames
= typeDesignation
.getTypifiedNames();
100 for (TaxonNameBase taxonNameBase
: typifiedNames
) {
101 label
+= taxonNameBase
+", ";
103 if(label
.endsWith(", ")){
104 label
= label
.substring(0, label
.length()-2);
109 viewer
.setInput(typeDesignations
);
110 viewer
.addDoubleClickListener(this);
112 if(!determinationEvents
.isEmpty()){
113 TableViewer viewer
= new TableViewer(getLayoutComposite(), SWT
.FULL_SELECTION
);
114 viewer
.getTable().setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
115 viewer
.setContentProvider(new ArrayContentProvider());
116 viewer
.setLabelProvider(new LabelProvider(){
118 public String
getText(Object element
) {
119 DeterminationEvent determinationEvent
= (DeterminationEvent
)element
;
120 if(determinationEvent
.getTaxon()!=null){
121 return "Determined as taxon "+determinationEvent
.getTaxon();
123 if(determinationEvent
.getTaxonName()!=null){
124 return "Determined as name "+determinationEvent
.getTaxonName();
126 return element
.toString();
129 viewer
.setInput(determinationEvents
);
130 viewer
.addDoubleClickListener(this);
136 public void handleEvent(Object eventSource
) {
141 public void doubleClick(DoubleClickEvent event
) {
142 if(event
.getSelection() instanceof IStructuredSelection
){
143 Object firstElement
= ((IStructuredSelection
) event
.getSelection()).getFirstElement();
144 UUID taxonToOpenUuid
= null;
145 if(firstElement
instanceof TaxonBase
<?
>){
146 taxonToOpenUuid
= ((TaxonBase
<?
>)firstElement
).getUuid();
148 else if(firstElement
instanceof SpecimenTypeDesignation
){
149 //TODO how to open an editor for all typed names?
151 else if(firstElement
instanceof DeterminationEvent
){
152 if(((DeterminationEvent
) firstElement
).getTaxon()!=null){
153 taxonToOpenUuid
= ((DeterminationEvent
) firstElement
).getTaxon().getUuid();
155 else if(((DeterminationEvent
) firstElement
).getTaxonName()!=null){
156 //TODO how to open editor for taxon name
159 if(taxonToOpenUuid
!=null){
160 String commandId
= "eu.etaxonomy.taxeditor.editor.openTaxonEditor";
163 ArrayList parameters
= new ArrayList();
164 IParameter iparam
= null;
166 //get the command from plugin.xml
167 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
168 ICommandService cmdService
= (ICommandService
)window
.getService(ICommandService
.class);
169 Command cmd
= cmdService
.getCommand(commandId
);
173 iparam
= cmd
.getParameter("eu.etaxonomy.taxeditor.editor.taxonParameter");
174 } catch (NotDefinedException e1
) {
175 MessagingUtils
.error(this.getClass(), "Command not defined", e1
);
177 Parameterization params
= new Parameterization(iparam
, taxonToOpenUuid
.toString());
178 parameters
.add(params
);
180 //build the parameterized command
181 ParameterizedCommand pc
= new ParameterizedCommand(cmd
, (Parameterization
[]) parameters
.toArray(new Parameterization
[parameters
.size()]));
183 //execute the command
184 IHandlerService handlerService
= (IHandlerService
)window
.getService(IHandlerService
.class);
186 handlerService
.executeCommand(pc
, null);
187 } catch (ExecutionException e
) {
188 MessagingUtils
.error(TaxonAssociationDetailElement
.class, e
);
189 } catch (NotDefinedException e
) {
190 MessagingUtils
.error(TaxonAssociationDetailElement
.class, e
);
191 } catch (NotEnabledException e
) {
192 MessagingUtils
.error(TaxonAssociationDetailElement
.class, e
);
193 } catch (NotHandledException e
) {
194 MessagingUtils
.error(TaxonAssociationDetailElement
.class, e
);