Project

General

Profile

Download (2.9 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.editor.handler;
2

    
3
import java.util.HashSet;
4
import java.util.Iterator;
5
import java.util.Set;
6
import java.util.UUID;
7

    
8
import org.eclipse.core.commands.AbstractHandler;
9
import org.eclipse.core.commands.ExecutionEvent;
10
import org.eclipse.core.commands.ExecutionException;
11
import org.eclipse.jface.viewers.ISelection;
12
import org.eclipse.jface.viewers.IStructuredSelection;
13
import org.eclipse.ui.PartInitException;
14
import org.eclipse.ui.handlers.HandlerUtil;
15

    
16
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
17
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
18
import eu.etaxonomy.taxeditor.editor.EditorUtil;
19
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
20
import eu.etaxonomy.taxeditor.model.MessagingUtils;
21

    
22
public class OpenDerivateViewHandler extends AbstractHandler {
23

    
24
    @Override
25
    public Object execute(ExecutionEvent event) throws ExecutionException {
26
        Set<UUID> selectedElementUUIDs = new HashSet<UUID>();
27
        Object parameter = null;
28
        //check if parameter is set
29
        if(event.getParameter("eu.etaxonomy.taxeditor.specimenUuidParameter")!=null){
30
            parameter = event.getObjectParameterForExecution("eu.etaxonomy.taxeditor.specimenUuidParameter");
31
        }
32
        if(parameter instanceof UUID){
33
            selectedElementUUIDs.add((UUID) parameter);
34
        }
35
        else{
36
            //if not, try with current selection
37
            ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
38
            if(currentSelection instanceof IStructuredSelection){
39
                Iterator<?> selectionIterator = ((IStructuredSelection) currentSelection).iterator();
40
                while(selectionIterator.hasNext()){
41
                    Object object = selectionIterator.next();
42
                    if(object instanceof SpecimenOrObservationBase<?>){
43
                        selectedElementUUIDs.add(((SpecimenOrObservationBase<?>) object).getUuid());
44
                    }
45
                    else if(object instanceof IndividualsAssociation){
46
                        SpecimenOrObservationBase specimen = ((IndividualsAssociation) object).getAssociatedSpecimenOrObservation();
47
                        if(specimen!=null){
48
                            selectedElementUUIDs.add(specimen.getUuid());
49
                        }
50
                    }
51
                }
52
            }
53
        }
54
        DerivateViewEditorInput input = new DerivateViewEditorInput(selectedElementUUIDs);
55
        try {
56
            EditorUtil.open(input);
57
        } catch (PartInitException e) {
58
            MessagingUtils.error(OpenDerivateViewHandler.class, "Could not open Derivative Editor", e);
59
        } catch (NullPointerException npe){
60
            MessagingUtils.messageDialog("Failed to open Editor", OpenDerivateViewHandler.class, "Could not open Derivative Editor. The derivate hierarchy is corrupted!", npe);
61
        }
62
        return null;
63
    }
64

    
65
}
(3-3/9)