merge from trunk
[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
15 import org.eclipse.core.commands.Command;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.commands.IParameter;
18 import org.eclipse.core.commands.NotEnabledException;
19 import org.eclipse.core.commands.NotHandledException;
20 import org.eclipse.core.commands.Parameterization;
21 import org.eclipse.core.commands.ParameterizedCommand;
22 import org.eclipse.core.commands.common.NotDefinedException;
23 import org.eclipse.jface.viewers.ArrayContentProvider;
24 import org.eclipse.jface.viewers.DoubleClickEvent;
25 import org.eclipse.jface.viewers.IDoubleClickListener;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.TableViewer;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.ui.IWorkbenchWindow;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.commands.ICommandService;
33 import org.eclipse.ui.handlers.IHandlerService;
34
35 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
36 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
37 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
38 import eu.etaxonomy.taxeditor.model.MessagingUtils;
39 import eu.etaxonomy.taxeditor.store.CdmStore;
40 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
41 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
42 import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
43 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
44
45 /**
46 * @author pplitzner
47 * @date Dec 1, 2014
48 *
49 */
50 public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<DerivedUnitFacade> implements IDoubleClickListener{
51
52
53 private TableViewer associationsViewer;
54 private TableViewer typeDesignationViewer;
55
56 public TaxonAssociationDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
57 super(formFactory, formElement);
58 }
59
60 /** {@inheritDoc} */
61 @Override
62 protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
63
64 //TODO add context menu for deleting associations
65
66 Label associationsLabel = formFactory.createLabel(getLayoutComposite(), "Individuals Associations");
67 associationsLabel.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
68
69 associationsViewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
70 associationsViewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
71 associationsViewer.setContentProvider(new ArrayContentProvider());
72 Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listAssociatedTaxa(entity.innerDerivedUnit(), null, null, null, null);
73 associationsViewer.setInput(associatedTaxa);
74 associationsViewer.addDoubleClickListener(this);
75
76 Label typeLabel = formFactory.createLabel(getLayoutComposite(), "Type Designations");
77 typeLabel.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
78
79 typeDesignationViewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
80 typeDesignationViewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
81 typeDesignationViewer.setContentProvider(new ArrayContentProvider());
82 Collection<TaxonBase<?>> typedTaxa = CdmStore.getService(IOccurrenceService.class).listTypedTaxa(entity.innerDerivedUnit(), null, null, null, null);
83 typeDesignationViewer.setInput(typedTaxa);
84 typeDesignationViewer.addDoubleClickListener(this);
85 }
86
87 /** {@inheritDoc} */
88 @Override
89 public void handleEvent(Object eventSource) {
90 //empty
91 }
92
93 /* (non-Javadoc)
94 * @see org.eclipse.jface.viewers.IDoubleClickListener#doubleClick(org.eclipse.jface.viewers.DoubleClickEvent)
95 */
96 @Override
97 public void doubleClick(DoubleClickEvent event) {
98 if(associationsViewer.getSelection() instanceof IStructuredSelection){
99 Object firstElement = ((IStructuredSelection) associationsViewer.getSelection()).getFirstElement();
100 if(firstElement instanceof TaxonBase<?>){
101 TaxonBase<?> taxonBase = (TaxonBase<?>)firstElement;
102 String commandId = "eu.etaxonomy.taxeditor.editor.openTaxonEditor";
103
104
105 ArrayList parameters = new ArrayList();
106 IParameter iparam = null;
107
108 //get the command from plugin.xml
109 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
110 ICommandService cmdService = (ICommandService)window.getService(ICommandService.class);
111 Command cmd = cmdService.getCommand(commandId);
112
113 //get the parameter
114 try {
115 iparam = cmd.getParameter("eu.etaxonomy.taxeditor.editor.taxonParameter");
116 } catch (NotDefinedException e1) {
117 MessagingUtils.error(this.getClass(), "Command not defined", e1);
118 }
119 Parameterization params = new Parameterization(iparam, (taxonBase).getUuid().toString());
120 parameters.add(params);
121
122 //build the parameterized command
123 ParameterizedCommand pc = new ParameterizedCommand(cmd, (Parameterization[]) parameters.toArray(new Parameterization[parameters.size()]));
124
125 //execute the command
126 IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
127 try {
128 handlerService.executeCommand(pc, null);
129 } catch (ExecutionException e) {
130 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
131 } catch (NotDefinedException e) {
132 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
133 } catch (NotEnabledException e) {
134 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
135 } catch (NotHandledException e) {
136 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
137 }
138 }
139 }
140 }
141
142 }