7b30737f96d682aa6d861142b8a59274e7bfac1b
[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.Messages;
32 import eu.etaxonomy.taxeditor.editor.handler.OpenDerivateViewHandler;
33 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
34 import eu.etaxonomy.taxeditor.model.MessagingUtils;
35 import eu.etaxonomy.taxeditor.store.CdmStore;
36
37 /**
38 * @author pplitzner
39 * @date Nov 25, 2014
40 *
41 */
42 public class OpenDerivateEditorForTaxonHandler extends AbstractHandler {
43
44 private static final String COULD_NOT_OPEN_DERIVATIVE_EDITOR = Messages.OpenDerivateEditorForTaxonHandler_COULD_NOT_OPEN_EDITOR;
45
46 /* (non-Javadoc)
47 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
48 */
49 @Override
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51 ISelection currentSelection = HandlerUtil.getCurrentSelection(event);
52 if(currentSelection instanceof IStructuredSelection){
53 Iterator<?> selectionIterator = ((IStructuredSelection) currentSelection).iterator();
54 Set<UUID> derivateUuids = new HashSet<UUID>();
55 while(selectionIterator.hasNext()){
56 Object object = selectionIterator.next();
57 if(object instanceof TaxonNode){
58 TaxonNode node = (TaxonNode)object;
59 Taxon taxon = node.getTaxon();
60 List<SpecimenOrObservationBase> listByAssociatedTaxon = CdmStore.getService(IOccurrenceService.class).listByAssociatedTaxon(null, null, taxon, null, null, null, null, null);
61 for (SpecimenOrObservationBase specimenOrObservationBase : listByAssociatedTaxon) {
62 derivateUuids.add(specimenOrObservationBase.getUuid());
63 }
64 }
65 }
66 if(derivateUuids.isEmpty()){
67 MessagingUtils.warningDialog(COULD_NOT_OPEN_DERIVATIVE_EDITOR, this, Messages.OpenDerivateEditorForTaxonHandler_NO_DERIVATIVES_FOUND);
68 return null;
69 }
70 DerivateViewEditorInput input = new DerivateViewEditorInput(derivateUuids);
71 try {
72 EditorUtil.open(input);
73 } catch (PartInitException e) {
74 MessagingUtils.error(OpenDerivateViewHandler.class, COULD_NOT_OPEN_DERIVATIVE_EDITOR, e);
75 } catch (NullPointerException npe){
76 MessagingUtils.messageDialog(Messages.OpenDerivateEditorForTaxonHandler_FAILED_TO_OPEN, OpenDerivateViewHandler.class, Messages.OpenDerivateEditorForTaxonHandler_HIERARCHY_CORRUPTED, npe);
77 }
78 }
79 return null;
80 }
81
82 }