53d9ea6aba3146b8216d25b0d01c4f5f37fa8613
[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 private TableViewer associationsViewer;
53
54 public TaxonAssociationDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
55 super(formFactory, formElement);
56 }
57
58 /** {@inheritDoc} */
59 @Override
60 protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
61
62 //TODO add context menu for deleting associations
63
64 Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listAssociatedTaxa(entity.innerDerivedUnit(), null, null, null, null);
65
66 if(!associatedTaxa.isEmpty()){
67 associationsViewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
68 associationsViewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
69 associationsViewer.setContentProvider(new ArrayContentProvider());
70 associationsViewer.setInput(associatedTaxa);
71 associationsViewer.addDoubleClickListener(this);
72 }
73 else{
74 Label label = formFactory.createLabel(getLayoutComposite(), "No associations");
75 label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
76 }
77
78 }
79
80 /** {@inheritDoc} */
81 @Override
82 public void handleEvent(Object eventSource) {
83 //empty
84 }
85
86 @Override
87 public void doubleClick(DoubleClickEvent event) {
88 if(event.getSelection() instanceof IStructuredSelection){
89 Object firstElement = ((IStructuredSelection) event.getSelection()).getFirstElement();
90 if(firstElement instanceof TaxonBase<?>){
91 TaxonBase<?> taxonBase = (TaxonBase<?>)firstElement;
92 String commandId = "eu.etaxonomy.taxeditor.editor.openTaxonEditor";
93
94
95 ArrayList parameters = new ArrayList();
96 IParameter iparam = null;
97
98 //get the command from plugin.xml
99 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
100 ICommandService cmdService = (ICommandService)window.getService(ICommandService.class);
101 Command cmd = cmdService.getCommand(commandId);
102
103 //get the parameter
104 try {
105 iparam = cmd.getParameter("eu.etaxonomy.taxeditor.editor.taxonParameter");
106 } catch (NotDefinedException e1) {
107 MessagingUtils.error(this.getClass(), "Command not defined", e1);
108 }
109 Parameterization params = new Parameterization(iparam, (taxonBase).getUuid().toString());
110 parameters.add(params);
111
112 //build the parameterized command
113 ParameterizedCommand pc = new ParameterizedCommand(cmd, (Parameterization[]) parameters.toArray(new Parameterization[parameters.size()]));
114
115 //execute the command
116 IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
117 try {
118 handlerService.executeCommand(pc, null);
119 } catch (ExecutionException e) {
120 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
121 } catch (NotDefinedException e) {
122 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
123 } catch (NotEnabledException e) {
124 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
125 } catch (NotHandledException e) {
126 MessagingUtils.error(TaxonAssociationDetailElement.class, e);
127 }
128 }
129 }
130 }
131
132 }