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