fix for #3998
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / key / polytomous / handler / DeleteNodeHandler.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.editor.key.polytomous.handler;
12
13 import org.eclipse.core.commands.AbstractHandler;
14 import org.eclipse.core.commands.ExecutionEvent;
15 import org.eclipse.core.commands.ExecutionException;
16 import org.eclipse.core.commands.common.NotDefinedException;
17 import org.eclipse.core.commands.operations.IUndoContext;
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.ui.IEditorPart;
21 import org.eclipse.ui.handlers.HandlerUtil;
22
23 import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
24 import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
25 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26 import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
27 import eu.etaxonomy.taxeditor.editor.EditorUtil;
28 import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
29 import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
30 import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.DeleteNodeOperation;
31 import eu.etaxonomy.taxeditor.model.MessagingUtils;
32 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34
35 /**
36 * @author n.hoffmann
37 * @created Dec 6, 2010
38 * @version 1.0
39 */
40 public class DeleteNodeHandler extends AbstractHandler {
41
42 /*
43 * (non-Javadoc)
44 *
45 * @see
46 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
47 * ExecutionEvent)
48 */
49 @Override
50 public Object execute(ExecutionEvent event) throws ExecutionException {
51 IEditorPart editor = HandlerUtil.getActiveEditor(event);
52
53 if (editor.isDirty()){
54 boolean proceed = MessageDialog.openQuestion(null,
55 "Save changes", "You have made changes that must be saved before you can delete the node. Would you like to proceed?");
56 if (!proceed) {
57 return null;
58 }else{
59 editor.doSave(EditorUtil.getMonitor());
60 }
61 }
62 if (editor instanceof KeyEditor) {
63 IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
64 .getActiveEditor();
65
66 IStructuredSelection selection = (IStructuredSelection) HandlerUtil
67 .getCurrentSelection(event);
68
69 if (selection.getFirstElement() instanceof PolytomousKeyNode) {
70 try {
71 String label = event.getCommand().getName();
72 IUndoContext undoContext = EditorUtil.getUndoContext();
73
74 for (Object element : selection.toArray()) {
75 PolytomousKeyNode keyNode = HibernateProxyHelper.deproxy(element, PolytomousKeyNode.class);
76
77 AbstractPostOperation operation = new DeleteNodeOperation(
78 label, undoContext, keyNode, editorPage);
79 EditorUtil.executeOperation(operation);
80 }
81
82 } catch (NotDefinedException e) {
83 MessagingUtils.warn(getClass(), "Command name not set.");
84 }
85 } else {
86 MessageDialog.openInformation(
87 EditorUtil.getShell(),
88 "No Key Node Selected",
89 "Please right-click on a specific key node to delete a key node.");
90 }
91 }
92
93 return null;
94 }
95
96 }