Project

General

Profile

Download (5.29 KB) Statistics
| Branch: | Tag: | Revision:
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.ExecutionEvent;
14
import org.eclipse.core.commands.ExecutionException;
15
import org.eclipse.core.commands.common.NotDefinedException;
16
import org.eclipse.core.commands.operations.AbstractOperation;
17
import org.eclipse.core.commands.operations.IUndoContext;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.Status;
20
import org.eclipse.jface.dialogs.MessageDialog;
21
import org.eclipse.jface.viewers.IStructuredSelection;
22
import org.eclipse.ui.IEditorPart;
23
import org.eclipse.ui.handlers.HandlerUtil;
24

    
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.PolytomousKeyEditorLabels;
31
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyListEditor;
32
import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.DeleteNodeOperation;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
35

    
36
/**
37
 * @author n.hoffmann
38
 * @created Dec 6, 2010
39
 * @version 1.0
40
 */
41
public class DeleteNodeHandler extends AbstractPolytomousKeyNodeHandler {
42

    
43

    
44
    PolytomousKeyNode nodeToBeDeleted;
45
    /**
46
     * @param label
47
     */
48
    public DeleteNodeHandler(String label) {
49
        super(label);
50

    
51
    }
52

    
53

    
54

    
55
    public DeleteNodeHandler() {
56
        super(PolytomousKeyEditorLabels.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL);
57
    }
58

    
59
	/*
60
	 * (non-Javadoc)
61
	 *
62
	 * @see
63
	 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
64
	 * ExecutionEvent)
65
	 */
66
	@Override
67
	public Object execute(ExecutionEvent event) throws ExecutionException {
68
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
69

    
70
		if (editor.isDirty()){
71
			boolean proceed = MessageDialog.openQuestion(null,
72
					"Save changes", "You have made changes that must be saved before you can delete the node. Would you like to proceed?");
73
			if (!proceed) {
74
				return null;
75
			}else{
76
				editor.doSave(EditorUtil.getMonitor());
77
			}
78
		}
79
		if (editor instanceof KeyEditor) {
80
			IPolytomousKeyEditorPage editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
81
					.getActiveEditor();
82

    
83
			IStructuredSelection selection = (IStructuredSelection) HandlerUtil
84
					.getCurrentSelection(event);
85

    
86
			if (selection.getFirstElement() instanceof PolytomousKeyNode) {
87
				try {
88
					String label = event.getCommand().getName();
89
					IUndoContext undoContext = EditorUtil.getUndoContext();
90

    
91
					for (Object element : selection.toArray()) {
92
						PolytomousKeyNode keyNode = HibernateProxyHelper.deproxy(element, PolytomousKeyNode.class);
93

    
94
						AbstractPostOperation operation = new DeleteNodeOperation(
95
								label, undoContext, keyNode, editorPage);
96
						EditorUtil.executeOperation(operation);
97
					}
98

    
99
				} catch (NotDefinedException e) {
100
					MessagingUtils.warn(getClass(), "Command name not set.");
101
				}
102
			} else {
103
	                MessageDialog.openInformation(
104
	                        EditorUtil.getShell(),
105
	                        "No Key Node Selected",
106
	                        "Please right-click on a specific key node to delete a key node.");
107
			}
108
		}
109

    
110
		return null;
111
	}
112

    
113
    /**
114
     * {@inheritDoc}
115
     */
116
    @Override
117
    public IStatus allowOperations(ExecutionEvent event) {
118
        IEditorPart editor = HandlerUtil.getActiveEditor(event);
119

    
120
        if (editor instanceof KeyEditor) {
121
            editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
122
                    .getActiveEditor();
123

    
124
            if (editorPage instanceof PolytomousKeyListEditor) {
125
                PolytomousKeyListEditor klEditor = (PolytomousKeyListEditor) editorPage;
126
                IStructuredSelection selection = (IStructuredSelection) HandlerUtil.getCurrentSelection(event);
127
                    if (selection.getFirstElement() instanceof PolytomousKeyNode) {
128
                        nodeToBeDeleted = (PolytomousKeyNode) selection.getFirstElement();
129
                    } else {
130
                        return new Status(IStatus.ERROR,
131
                                "unknown",
132
                                PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_CHILD_SELECTED_MESSAGE);
133
                    }
134
                }
135

    
136
        }
137
        return Status.OK_STATUS;
138
    }
139

    
140
    /**
141
     * {@inheritDoc}
142
     */
143
    @Override
144
    public AbstractOperation prepareOperation(ExecutionEvent event) {
145
        IUndoContext undoContext = EditorUtil.getUndoContext();
146
        String label = "";
147
        try {
148
            label = event.getCommand().getName();
149
        } catch (NotDefinedException e) {
150
            MessagingUtils.warn(getClass(), "Command name not set.");
151
        }
152
        return new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage);
153
    }
154

    
155
    /**
156
     * {@inheritDoc}
157
     */
158
    @Override
159
    public void onComplete() {
160
        // TODO Auto-generated method stub
161

    
162
    }
163

    
164
}
(4-4/8)