Project

General

Profile

Download (9.2 KB) Statistics
| Branch: | Tag: | Revision:
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.Set;
15
import java.util.UUID;
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.LabelProvider;
30
import org.eclipse.jface.viewers.TableViewer;
31
import org.eclipse.swt.SWT;
32
import org.eclipse.swt.widgets.Label;
33
import org.eclipse.ui.IWorkbenchWindow;
34
import org.eclipse.ui.PlatformUI;
35
import org.eclipse.ui.commands.ICommandService;
36
import org.eclipse.ui.handlers.IHandlerService;
37

    
38
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
39
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
40
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
41
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
42
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
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
    public TaxonAssociationDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
59
        super(formFactory, formElement);
60
    }
61

    
62
    /** {@inheritDoc} */
63
    @Override
64
    protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
65

    
66
        //TODO add context menu for deleting associations
67

    
68
        Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listIndividualsAssociationTaxa(entity.innerDerivedUnit(), null, null, null, null);
69
        Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
70
        Collection<DeterminationEvent> determinationEvents = CdmStore.getService(IOccurrenceService.class).listDeterminationEvents(entity.innerDerivedUnit(), null, null, null, null);
71

    
72
        if(associatedTaxa.isEmpty() && typeDesignations.isEmpty() && determinationEvents.isEmpty()){
73
            Label label = formFactory.createLabel(getLayoutComposite(), "No associations");
74
            label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
75
            return;
76
        }
77
        if(!associatedTaxa.isEmpty()){
78
            TableViewer viewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
79
            viewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
80
            viewer.setContentProvider(new ArrayContentProvider());
81
            viewer.setLabelProvider(new LabelProvider(){
82
                @Override
83
                public String getText(Object element) {
84
                    return "Associated with "+element.toString();
85
                }
86
            });
87
            viewer.setInput(associatedTaxa);
88
            viewer.addDoubleClickListener(this);
89
        }
90
        if(!typeDesignations.isEmpty()){
91
            TableViewer viewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
92
            viewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
93
            viewer.setContentProvider(new ArrayContentProvider());
94
            viewer.setLabelProvider(new LabelProvider(){
95
                @Override
96
                public String getText(Object element) {
97
                    SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)element;
98
                    String label = typeDesignation.getTypeStatus().getLabel()+" of ";
99
                        Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
100
                        for (TaxonNameBase taxonNameBase : typifiedNames) {
101
                            label += taxonNameBase+", ";
102
                        }
103
                        if(label.endsWith(", ")){
104
                            label = label.substring(0, label.length()-2);
105
                        }
106
                    return label;
107
                }
108
            });
109
            viewer.setInput(typeDesignations);
110
            viewer.addDoubleClickListener(this);
111
        }
112
        if(!determinationEvents.isEmpty()){
113
            TableViewer viewer = new TableViewer(getLayoutComposite(), SWT.FULL_SELECTION);
114
            viewer.getTable().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
115
            viewer.setContentProvider(new ArrayContentProvider());
116
            viewer.setLabelProvider(new LabelProvider(){
117
                @Override
118
                public String getText(Object element) {
119
                    DeterminationEvent determinationEvent = (DeterminationEvent)element;
120
                    if(determinationEvent.getTaxon()!=null){
121
                        return "Determined as taxon "+determinationEvent.getTaxon();
122
                    }
123
                    if(determinationEvent.getTaxonName()!=null){
124
                        return "Determined as name "+determinationEvent.getTaxonName();
125
                    }
126
                    return element.toString();
127
                }
128
            });
129
            viewer.setInput(determinationEvents);
130
            viewer.addDoubleClickListener(this);
131
        }
132
    }
133

    
134
    /** {@inheritDoc} */
135
    @Override
136
    public void handleEvent(Object eventSource) {
137
        //empty
138
    }
139

    
140
    @Override
141
    public void doubleClick(DoubleClickEvent event) {
142
        if(event.getSelection() instanceof IStructuredSelection){
143
            Object firstElement = ((IStructuredSelection) event.getSelection()).getFirstElement();
144
            UUID taxonToOpenUuid = null;
145
            if(firstElement instanceof TaxonBase<?>){
146
                taxonToOpenUuid = ((TaxonBase<?>)firstElement).getUuid();
147
            }
148
            else if(firstElement instanceof SpecimenTypeDesignation){
149
                //TODO how to open an editor for all typed names?
150
            }
151
            else if(firstElement instanceof DeterminationEvent){
152
                if(((DeterminationEvent) firstElement).getTaxon()!=null){
153
                    taxonToOpenUuid = ((DeterminationEvent) firstElement).getTaxon().getUuid();
154
                }
155
                else if(((DeterminationEvent) firstElement).getTaxonName()!=null){
156
                    //TODO how to open editor for taxon name
157
                }
158
            }
159
            if(taxonToOpenUuid!=null){
160
                String commandId = "eu.etaxonomy.taxeditor.editor.openTaxonEditor";
161

    
162

    
163
                ArrayList parameters = new ArrayList();
164
                IParameter iparam = null;
165

    
166
                //get the command from plugin.xml
167
                IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
168
                ICommandService cmdService = (ICommandService)window.getService(ICommandService.class);
169
                Command cmd = cmdService.getCommand(commandId);
170

    
171
                //get the parameter
172
                try {
173
                    iparam = cmd.getParameter("eu.etaxonomy.taxeditor.editor.taxonParameter");
174
                } catch (NotDefinedException e1) {
175
                    MessagingUtils.error(this.getClass(), "Command not defined", e1);
176
                }
177
                Parameterization params = new Parameterization(iparam, taxonToOpenUuid.toString());
178
                parameters.add(params);
179

    
180
                //build the parameterized command
181
                ParameterizedCommand pc = new ParameterizedCommand(cmd, (Parameterization[]) parameters.toArray(new Parameterization[parameters.size()]));
182

    
183
                //execute the command
184
                IHandlerService handlerService = (IHandlerService)window.getService(IHandlerService.class);
185
                try {
186
                    handlerService.executeCommand(pc, null);
187
                } catch (ExecutionException e) {
188
                    MessagingUtils.error(TaxonAssociationDetailElement.class, e);
189
                } catch (NotDefinedException e) {
190
                    MessagingUtils.error(TaxonAssociationDetailElement.class, e);
191
                } catch (NotEnabledException e) {
192
                    MessagingUtils.error(TaxonAssociationDetailElement.class, e);
193
                } catch (NotHandledException e) {
194
                    MessagingUtils.error(TaxonAssociationDetailElement.class, e);
195
                }
196
            }
197
        }
198
    }
199
}
(3-3/4)