Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / concept / handler / DeleteConceptRelationHandler.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.view.concept.handler;
11
12 import java.util.HashSet;
13 import java.util.Iterator;
14 import java.util.Set;
15
16 import org.eclipse.core.commands.AbstractHandler;
17 import org.eclipse.core.commands.ExecutionEvent;
18 import org.eclipse.core.commands.ExecutionException;
19 import org.eclipse.core.commands.common.NotDefinedException;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.IStructuredSelection;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
26 import eu.etaxonomy.taxeditor.editor.EditorUtil;
27 import eu.etaxonomy.taxeditor.editor.Page;
28 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
29 import eu.etaxonomy.taxeditor.editor.view.concept.operation.DeleteConceptRelationOperation;
30 import eu.etaxonomy.taxeditor.model.MessagingUtils;
31 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
32 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
33
34 /**
35 * @author n.hoffmann
36 * @created Jan 25, 2011
37 * @version 1.0
38 */
39 public class DeleteConceptRelationHandler extends AbstractHandler {
40
41 /* (non-Javadoc)
42 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
43 */
44 @Override
45 public Object execute(ExecutionEvent event) throws ExecutionException {
46 TaxonNameEditor editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(
47 Page.NAME);
48
49 ISelection selection = HandlerUtil.getActiveSite(event).getSelectionProvider().getSelection();
50
51 Set<TaxonRelationship> relations = new HashSet<TaxonRelationship>();
52
53 if(selection instanceof IStructuredSelection){
54 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
55 Iterator iterator = structuredSelection.iterator();
56
57 while (iterator.hasNext()){
58 Object element = iterator.next();
59 if(element instanceof TaxonRelationship){
60 relations.add((TaxonRelationship) element);
61 }
62 }
63 }
64
65 IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
66 IPostOperationEnabled postOperationEnabled = (activePart instanceof IPostOperationEnabled) ? (IPostOperationEnabled) activePart : null;
67
68 try {
69 AbstractPostOperation operation = new DeleteConceptRelationOperation(event.getCommand().getName(),
70 editor.getUndoContext(), editor.getTaxon(), relations, postOperationEnabled);
71 EditorUtil.executeOperation(operation);
72 } catch (NotDefinedException e) {
73 MessagingUtils.warn(getClass(), "Command name not set");
74 }
75
76 return null;
77 }
78 }