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
;
14 import java
.util
.HashSet
;
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
.TableViewer
;
30 import org
.eclipse
.swt
.SWT
;
31 import org
.eclipse
.swt
.widgets
.Label
;
32 import org
.eclipse
.ui
.IWorkbenchWindow
;
33 import org
.eclipse
.ui
.PlatformUI
;
34 import org
.eclipse
.ui
.commands
.ICommandService
;
35 import org
.eclipse
.ui
.handlers
.IHandlerService
;
37 import eu
.etaxonomy
.cdm
.api
.facade
.DerivedUnitFacade
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
39 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
40 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
41 import eu
.etaxonomy
.cdm
.model
.name
.SpecimenTypeDesignation
;
42 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
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
{
59 private TableViewer associationsViewer
;
60 private TableViewer typeDesignationViewer
;
62 public TaxonAssociationDetailElement(CdmFormFactory formFactory
, ICdmFormElement formElement
) {
63 super(formFactory
, formElement
);
68 protected void createControls(ICdmFormElement formElement
, DerivedUnitFacade entity
, int style
) {
70 //TODO add context menu for deleting associations
72 Label associationsLabel
= formFactory
.createLabel(getLayoutComposite(), "Individuals Associations");
73 associationsLabel
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
75 associationsViewer
= new TableViewer(getLayoutComposite(), SWT
.FULL_SELECTION
);
76 associationsViewer
.getTable().setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
77 associationsViewer
.setContentProvider(new ArrayContentProvider());
78 Collection
<TaxonBase
<?
>> associatedTaxa
= CdmStore
.getService(IOccurrenceService
.class).listAssociatedTaxa(entity
.innerDerivedUnit(), null, null, null, null);
79 associationsViewer
.setInput(associatedTaxa
);
80 associationsViewer
.addDoubleClickListener(this);
82 Label typeLabel
= formFactory
.createLabel(getLayoutComposite(), "Type Designations");
83 typeLabel
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
85 typeDesignationViewer
= new TableViewer(getLayoutComposite(), SWT
.FULL_SELECTION
);
86 typeDesignationViewer
.getTable().setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
87 typeDesignationViewer
.setContentProvider(new ArrayContentProvider());
88 Collection
<SpecimenTypeDesignation
> typeDesignations
= CdmStore
.getService(IOccurrenceService
.class).listTypeDesignations(entity
.innerDerivedUnit(), null, null, null, null);
89 //TODO implement service method for this which is just used in the label provider
90 Collection
<TaxonBase
<?
>> typedTaxa
= new HashSet
<TaxonBase
<?
>>();
91 for (SpecimenTypeDesignation specimenTypeDesignation
: typeDesignations
) {
92 for (TaxonNameBase taxonNameBase
: specimenTypeDesignation
.getTypifiedNames()) {
93 Set taxa
= taxonNameBase
.getTaxa();
94 for (Object taxon
: taxa
) {
95 if(taxon
instanceof CdmBase
&& ((CdmBase
)taxon
).isInstanceOf(TaxonBase
.class)){
96 typedTaxa
.add(HibernateProxyHelper
.deproxy(taxon
, TaxonBase
.class));
101 typeDesignationViewer
.setInput(typedTaxa
);
102 typeDesignationViewer
.addDoubleClickListener(this);
107 public void handleEvent(Object eventSource
) {
112 * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
115 public void doubleClick(DoubleClickEvent event
) {
116 if(event
.getSelection() instanceof IStructuredSelection
){
117 Object firstElement
= ((IStructuredSelection
) event
.getSelection()).getFirstElement();
118 if(firstElement
instanceof TaxonBase
<?
>){
119 TaxonBase
<?
> taxonBase
= (TaxonBase
<?
>)firstElement
;
120 String commandId
= "eu.etaxonomy.taxeditor.editor.openTaxonEditor";
123 ArrayList parameters
= new ArrayList();
124 IParameter iparam
= null;
126 //get the command from plugin.xml
127 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
128 ICommandService cmdService
= (ICommandService
)window
.getService(ICommandService
.class);
129 Command cmd
= cmdService
.getCommand(commandId
);
133 iparam
= cmd
.getParameter("eu.etaxonomy.taxeditor.editor.taxonParameter");
134 } catch (NotDefinedException e1
) {
135 MessagingUtils
.error(this.getClass(), "Command not defined", e1
);
137 Parameterization params
= new Parameterization(iparam
, (taxonBase
).getUuid().toString());
138 parameters
.add(params
);
140 //build the parameterized command
141 ParameterizedCommand pc
= new ParameterizedCommand(cmd
, (Parameterization
[]) parameters
.toArray(new Parameterization
[parameters
.size()]));
143 //execute the command
144 IHandlerService handlerService
= (IHandlerService
)window
.getService(IHandlerService
.class);
146 handlerService
.executeCommand(pc
, null);
147 } catch (ExecutionException e
) {
148 MessagingUtils
.error(TaxonAssociationDetailElement
.class, e
);
149 } catch (NotDefinedException e
) {
150 MessagingUtils
.error(TaxonAssociationDetailElement
.class, e
);
151 } catch (NotEnabledException e
) {
152 MessagingUtils
.error(TaxonAssociationDetailElement
.class, e
);
153 } catch (NotHandledException e
) {
154 MessagingUtils
.error(TaxonAssociationDetailElement
.class, e
);