a7968204941b92a7fc5efa0a1f1874ede385e9c6
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / key / polytomous / handler / DeleteHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.navigation.key.polytomous.handler;
12
13 import java.util.List;
14
15 import org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.commands.common.NotDefinedException;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.IEditorReference;
22 import org.eclipse.ui.IWorkbenchPage;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.handlers.HandlerUtil;
25
26 import eu.etaxonomy.cdm.model.description.PolytomousKey;
27 import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorInput;
28 import eu.etaxonomy.taxeditor.model.MessagingUtils;
29 import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
30 import eu.etaxonomy.taxeditor.navigation.key.polytomous.PolytomousKeyViewPart;
31 import eu.etaxonomy.taxeditor.navigation.key.polytomous.operation.DeleteOperation;
32 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
33
34 /**
35 * @author n.hoffmann
36 * @created Dec 3, 2010
37 * @version 1.0
38 */
39 public class DeleteHandler extends AbstractHandler {
40
41 protected IWorkbenchPage activePage;
42 /* (non-Javadoc)
43 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
44 */
45 @Override
46 public Object execute(ExecutionEvent event) throws ExecutionException {
47 IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
48 activePage = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage();
49 List<PolytomousKey> keys = selection.toList();
50
51 if(keys.isEmpty()){
52 return null;
53 }
54 for (PolytomousKey key : keys){
55 closeObsoleteEditor(key);
56 }
57 boolean confirmation = MessagingUtils.confirmDialog("Confirm deletaion", "Do you want to delete the selected key" + (keys.size() == 1 ? "" : "s") + "?");
58
59 if(confirmation){
60
61 for(PolytomousKey key : keys){
62 try {
63 AbstractPostOperation operation = new DeleteOperation(
64 event.getCommand().getName(),
65 NavigationUtil.getUndoContext(), key,
66 (PolytomousKeyViewPart) NavigationUtil.getView(PolytomousKeyViewPart.ID, false),
67 (PolytomousKeyViewPart) NavigationUtil.getView(PolytomousKeyViewPart.ID, false));
68 NavigationUtil.executeOperation(operation);
69 } catch (NotDefinedException e) {
70 MessagingUtils.error(getClass(), e);
71 }
72 }
73 }
74
75 return null;
76 }
77
78
79 protected boolean closeObsoleteEditor(PolytomousKey key){
80 boolean result = true;
81 for (IEditorReference ref : activePage.getEditorReferences()) {
82 try {
83
84 IEditorInput input = ref.getEditorInput();
85 if (input instanceof PolytomousKeyEditorInput) {
86 PolytomousKey pKey = ((PolytomousKeyEditorInput)input).getKey();
87 //if node is a child of taxonNode then close the editor
88 if(key.equals(pKey)){
89 //if (taxonNode.equals(node)) {
90 result &= activePage.closeEditor(ref.getEditor(false), true);
91
92 }
93 }
94 } catch (PartInitException e) {
95 continue;
96 }
97 }
98 return result;
99 }
100 }