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