- adapted to method signature change in cdmlib
[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.HashSet;
15 import java.util.Set;
16
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;
36
37 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
38 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
39 import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
40 import eu.etaxonomy.cdm.model.description.TaxonDescription;
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;
50
51 /**
52 * @author pplitzner
53 * @date Dec 1, 2014
54 *
55 */
56 public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<DerivedUnitFacade> implements IDoubleClickListener{
57
58
59 private TableViewer associationsViewer;
60 private TableViewer typeDesignationViewer;
61
62 public TaxonAssociationDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
63 super(formFactory, formElement);
64 }
65
66 /** {@inheritDoc} */
67 @Override
68 protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
69
70 //TODO add context menu for deleting associations
71
72 Label associationsLabel = formFactory.createLabel(getLayoutComposite(), "Individuals Associations");
73 associationsLabel.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
74
75 associationsViewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
76 associationsViewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
77 associationsViewer.setContentProvider(new ArrayContentProvider());
78 Collection<IndividualsAssociation> individualsAssociations = CdmStore.getService(IOccurrenceService.class).listIndividualsAssociations(entity.innerDerivedUnit(), null, null, null, null);
79 //TODO implement service method for this which is just used in the label provider
80 Collection<TaxonBase<?>> associatedTaxa = new HashSet<TaxonBase<?>>();
81 for (IndividualsAssociation individualsAssociation : individualsAssociations) {
82 if(individualsAssociation.getInDescription() instanceof TaxonDescription){
83 TaxonDescription taxonDescription = (TaxonDescription)individualsAssociation.getInDescription();
84 associatedTaxa.add(taxonDescription.getTaxon());
85 }
86 }
87 associationsViewer.setInput(associatedTaxa);
88 associationsViewer.addDoubleClickListener(this);
89
90 Label typeLabel = formFactory.createLabel(getLayoutComposite(), "Type Designations");
91 typeLabel.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
92
93 typeDesignationViewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
94 typeDesignationViewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
95 typeDesignationViewer.setContentProvider(new ArrayContentProvider());
96 Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
97 //TODO implement service method for this which is just used in the label provider
98 Collection<TaxonBase<?>> typedTaxa = new HashSet<TaxonBase<?>>();
99 for (SpecimenTypeDesignation specimenTypeDesignation : typeDesignations) {
100 for (TaxonNameBase taxonNameBase : specimenTypeDesignation.getTypifiedNames()) {
101 Set taxa = taxonNameBase.getTaxa();
102 for (Object taxon : taxa) {
103 if(taxon instanceof TaxonBase<?>){
104 typedTaxa.add((TaxonBase<?>) taxon);
105 }
106 }
107 }
108 }
109 typeDesignationViewer.setInput(typedTaxa);
110 typeDesignationViewer.addDoubleClickListener(this);
111 }
112
113 /** {@inheritDoc} */
114 @Override
115 public void handleEvent(Object eventSource) {
116 //empty
117 }
118
119 /* (non-Javadoc)
120 * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
121 */
122 @Override
123 public void doubleClick(DoubleClickEvent event) {
124 if(associationsViewer.getSelection() instanceof IStructuredSelection){
125 Object firstElement = ((IStructuredSelection) associationsViewer.getSelection()).getFirstElement();
126 if(firstElement instanceof TaxonBase<?>){
127 TaxonBase<?> taxonBase = (TaxonBase<?>)firstElement;
128 String commandId = "eu.etaxonomy.taxeditor.editor.openTaxonEditor";
129
130
131 ArrayList parameters = new ArrayList();
132 IParameter iparam = null;
133
134 //get the command from plugin.xml
135 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
136 ICommandService cmdService = (ICommandService)window.getService(ICommandService.class);
137 Command cmd = cmdService.getCommand(commandId);
138
139 //get the parameter
140 try {
141 iparam = cmd.getParameter("eu.etaxonomy.taxeditor.editor.taxonParameter");
142 } catch (NotDefinedException e1) {
143 MessagingUtils.error(this.getClass(), "Command not defined", e1);
144 }
145 Parameterization params = new Parameterization(iparam, (taxonBase).getUuid().toString());
146 parameters.add(params);
147
148 //build the parameterized command
149 ParameterizedCommand pc = new ParameterizedCommand(cmd, (Parameterization[]) parameters.toArray(new Parameterization[parameters.size()]));
150
151 //execute the command
152 IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
153 try {
154 handlerService.executeCommand(pc, null);
155 } catch (ExecutionException e) {
156 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
157 } catch (NotDefinedException e) {
158 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
159 } catch (NotEnabledException e) {
160 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
161 } catch (NotHandledException e) {
162 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
163 }
164 }
165 }
166 }
167
168 }