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