merge
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / handler / OpenDerivateViewHandler.java
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;
28 //check if parameter is set
29 try {
30 parameter = event.getObjectParameterForExecution("eu.etaxonomy.taxeditor.specimenUuidParameter");
31 } catch (ExecutionException e) {
32 parameter = null;
33 }
34 if(parameter instanceof UUID){
35 selectedElementUUIDs.add((UUID) parameter);
36 }
37 else{
38 //if not, try with current selection
39 ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
40 if(currentSelection instanceof IStructuredSelection){
41 Iterator<?> selectionIterator = ((IStructuredSelection) currentSelection).iterator();
42 while(selectionIterator.hasNext()){
43 Object object = selectionIterator.next();
44 if(object instanceof SpecimenOrObservationBase<?>){
45 selectedElementUUIDs.add(((SpecimenOrObservationBase<?>) object).getUuid());
46 }
47 else if(object instanceof IndividualsAssociation){
48 SpecimenOrObservationBase specimen = ((IndividualsAssociation) object).getAssociatedSpecimenOrObservation();
49 if(specimen!=null){
50 selectedElementUUIDs.add(specimen.getUuid());
51 }
52 }
53 }
54 }
55 }
56 if(!selectedElementUUIDs.isEmpty()){
57 DerivateViewEditorInput input = new DerivateViewEditorInput(selectedElementUUIDs);
58 try {
59 EditorUtil.open(input);
60 } catch (PartInitException e) {
61 MessagingUtils.error(OpenDerivateViewHandler.class, "Could not open Derivative Editor", e);
62 } catch (NullPointerException npe){
63 MessagingUtils.messageDialog("Failed to open Editor", OpenDerivateViewHandler.class, "Could not open Derivative Editor. The derivate hierarchy is corrupted!", npe);
64 }
65 }
66 else{
67 MessagingUtils.informationDialog("Empty selection", "No Specimen selected.");
68 }
69 return null;
70 }
71
72 }