ab74d55aa71945cc35d8b087279c8a4d84958ebe
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / derivate / handler / OpenDerivateEditorForTaxonHandler.java
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.editor.view.derivate.handler;
11
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.List;
15 import java.util.Set;
16 import java.util.UUID;
17
18 import org.eclipse.core.commands.AbstractHandler;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.handlers.HandlerUtil;
25
26 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
27 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
30 import eu.etaxonomy.taxeditor.editor.EditorUtil;
31 import eu.etaxonomy.taxeditor.editor.handler.OpenDerivateViewHandler;
32 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
33 import eu.etaxonomy.taxeditor.model.MessagingUtils;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35
36 /**
37 * @author pplitzner
38 * @date Nov 25, 2014
39 *
40 */
41 public class OpenDerivateEditorForTaxonHandler extends AbstractHandler {
42
43 /* (non-Javadoc)
44 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
45 */
46 @Override
47 public Object execute(ExecutionEvent event) throws ExecutionException {
48 ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
49 if(currentSelection instanceof IStructuredSelection){
50 Iterator<?> selectionIterator = ((IStructuredSelection) currentSelection).iterator();
51 Set<UUID> derivateUuids = new HashSet<UUID>();
52 while(selectionIterator.hasNext()){
53 Object object = selectionIterator.next();
54 if(object instanceof TaxonNode){
55 TaxonNode node = (TaxonNode)object;
56 Taxon taxon = node.getTaxon();
57 List<SpecimenOrObservationBase> listByAssociatedTaxon = CdmStore.getService(IOccurrenceService.class).listByAssociatedTaxon(null, null, taxon, null, null, null, null, null);
58 for (SpecimenOrObservationBase specimenOrObservationBase : listByAssociatedTaxon) {
59 derivateUuids.add(specimenOrObservationBase.getUuid());
60 }
61 }
62 }
63 if(derivateUuids.isEmpty()){
64 MessagingUtils.warningDialog("Could not open Derivate Editor", this, "No Derivates found");
65 return null;
66 }
67 DerivateViewEditorInput input = new DerivateViewEditorInput(derivateUuids);
68 try {
69 EditorUtil.open(input);
70 } catch (PartInitException e) {
71 MessagingUtils.error(OpenDerivateViewHandler.class, "Could not open Derivate Editor", e);
72 } catch (NullPointerException npe){
73 MessagingUtils.messageDialog("Failed to open Editor", OpenDerivateViewHandler.class, "Could not open Derivate Editor. The derivate hierarchy is corrupted!", npe);
74 }
75 }
76 return null;
77 }
78
79 }