a7d9f73c40b7607dd2ae8f8900d9c9b50aff4fcd
[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.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 Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listAssociatedTaxa(entity.innerDerivedUnit(), null, null, null, null);
73 if(associatedTaxa.isEmpty()){
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 associationsViewer.setInput(associatedTaxa);
81 associationsViewer.addDoubleClickListener(this);
82 }
83
84 Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
85 //TODO implement service method for this which is just used in the label provider
86 Collection<TaxonBase<?>> typedTaxa = new HashSet<TaxonBase<?>>();
87 for (SpecimenTypeDesignation specimenTypeDesignation : typeDesignations) {
88 for (TaxonNameBase taxonNameBase : specimenTypeDesignation.getTypifiedNames()) {
89 Set taxa = taxonNameBase.getTaxa();
90 for (Object taxon : taxa) {
91 if(taxon instanceof CdmBase && ((CdmBase)taxon).isInstanceOf(TaxonBase.class)){
92 typedTaxa.add(HibernateProxyHelper.deproxy(taxon, TaxonBase.class));
93 }
94 }
95 }
96 }
97 if(!typedTaxa.isEmpty()){
98 Label typeLabel = formFactory.createLabel(getLayoutComposite(), "Type Designations");
99 typeLabel.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
100
101 typeDesignationViewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
102 typeDesignationViewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
103 typeDesignationViewer.setContentProvider(new ArrayContentProvider());
104 typeDesignationViewer.setInput(typedTaxa);
105 typeDesignationViewer.addDoubleClickListener(this);
106 }
107 }
108
109 /** {@inheritDoc} */
110 @Override
111 public void handleEvent(Object eventSource) {
112 //empty
113 }
114
115 /* (non-Javadoc)
116 * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
117 */
118 @Override
119 public void doubleClick(DoubleClickEvent event) {
120 if(event.getSelection() instanceof IStructuredSelection){
121 Object firstElement = ((IStructuredSelection) event.getSelection()).getFirstElement();
122 if(firstElement instanceof TaxonBase<?>){
123 TaxonBase<?> taxonBase = (TaxonBase<?>)firstElement;
124 String commandId = "eu.etaxonomy.taxeditor.editor.openTaxonEditor";
125
126
127 ArrayList parameters = new ArrayList();
128 IParameter iparam = null;
129
130 //get the command from plugin.xml
131 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
132 ICommandService cmdService = (ICommandService)window.getService(ICommandService.class);
133 Command cmd = cmdService.getCommand(commandId);
134
135 //get the parameter
136 try {
137 iparam = cmd.getParameter("eu.etaxonomy.taxeditor.editor.taxonParameter");
138 } catch (NotDefinedException e1) {
139 MessagingUtils.error(this.getClass(), "Command not defined", e1);
140 }
141 Parameterization params = new Parameterization(iparam, (taxonBase).getUuid().toString());
142 parameters.add(params);
143
144 //build the parameterized command
145 ParameterizedCommand pc = new ParameterizedCommand(cmd, (Parameterization[]) parameters.toArray(new Parameterization[parameters.size()]));
146
147 //execute the command
148 IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
149 try {
150 handlerService.executeCommand(pc, null);
151 } catch (ExecutionException e) {
152 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
153 } catch (NotDefinedException e) {
154 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
155 } catch (NotEnabledException e) {
156 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
157 } catch (NotHandledException e) {
158 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
159 }
160 }
161 }
162 }
163
164 }