- added context menu option to open multiple specimen IndividualsAssociations from...
[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 /* (non-Javadoc)
25 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
26 */
27 @Override
28 public Object execute(ExecutionEvent event) throws ExecutionException {
29 ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
30 if(currentSelection instanceof IStructuredSelection){
31 Iterator<?> selectionIterator = ((IStructuredSelection) currentSelection).iterator();
32 Set<UUID> selectedElementUUIDs = new HashSet<UUID>();
33 while(selectionIterator.hasNext()){
34 Object object = selectionIterator.next();
35 if(object instanceof SpecimenOrObservationBase<?>){
36 selectedElementUUIDs.add(((SpecimenOrObservationBase<?>) object).getUuid());
37 }
38 else if(object instanceof IndividualsAssociation){
39 SpecimenOrObservationBase specimen = ((IndividualsAssociation) object).getAssociatedSpecimenOrObservation();
40 if(specimen!=null){
41 selectedElementUUIDs.add(specimen.getUuid());
42 }
43 }
44 }
45 DerivateViewEditorInput input = new DerivateViewEditorInput(selectedElementUUIDs);
46 try {
47 EditorUtil.open(input);
48 } catch (PartInitException e) {
49 MessagingUtils.error(OpenDerivateViewHandler.class, "Could not open Derivate Editor", e);
50 } catch (NullPointerException npe){
51 MessagingUtils.messageDialog("Failed to open Editor", OpenDerivateViewHandler.class, "Could not open Derivate Editor. The derivate hierarchy is corrupted!", npe);
52 }
53 }
54 return null;
55 }
56
57 }