890942c61e989f509cabce7e616a7077c3d34224
[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 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<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listAssociatedTaxa(entity.innerDerivedUnit(), null, null, null, null);
79 associationsViewer.setInput(associatedTaxa);
80 associationsViewer.addDoubleClickListener(this);
81
82 Label typeLabel = formFactory.createLabel(getLayoutComposite(), "Type Designations");
83 typeLabel.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
84
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));
97 }
98 }
99 }
100 }
101 typeDesignationViewer.setInput(typedTaxa);
102 typeDesignationViewer.addDoubleClickListener(this);
103 }
104
105 /** {@inheritDoc} */
106 @Override
107 public void handleEvent(Object eventSource) {
108 //empty
109 }
110
111 /* (non-Javadoc)
112 * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
113 */
114 @Override
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";
121
122
123 ArrayList parameters = new ArrayList();
124 IParameter iparam = null;
125
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);
130
131 //get the parameter
132 try {
133 iparam = cmd.getParameter("eu.etaxonomy.taxeditor.editor.taxonParameter");
134 } catch (NotDefinedException e1) {
135 MessagingUtils.error(this.getClass(), "Command not defined", e1);
136 }
137 Parameterization params = new Parameterization(iparam, (taxonBase).getUuid().toString());
138 parameters.add(params);
139
140 //build the parameterized command
141 ParameterizedCommand pc = new ParameterizedCommand(cmd, (Parameterization[]) parameters.toArray(new Parameterization[parameters.size()]));
142
143 //execute the command
144 IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
145 try {
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);
155 }
156 }
157 }
158 }
159
160 }