Project

General

Profile

Download (7 KB) Statistics
| Branch: | Tag: | Revision:
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.key.polytomous.e4.handler;
11

    
12
import javax.inject.Named;
13

    
14
import org.eclipse.core.commands.ExecutionEvent;
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.e4.core.di.annotations.CanExecute;
21
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
22
import org.eclipse.e4.ui.services.IServiceConstants;
23
import org.eclipse.jface.dialogs.MessageDialog;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25
import org.eclipse.ui.IEditorPart;
26
import org.eclipse.ui.handlers.HandlerUtil;
27

    
28
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
29
import eu.etaxonomy.taxeditor.editor.EditorUtil;
30
import eu.etaxonomy.taxeditor.editor.key.KeyEditor;
31
import eu.etaxonomy.taxeditor.editor.key.polytomous.IPolytomousKeyEditorPage;
32
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorLabels;
33
import eu.etaxonomy.taxeditor.editor.key.polytomous.handler.AbstractPolytomousKeyNodeHandler;
34
import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.DeleteNodeOperation;
35
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
36
import eu.etaxonomy.taxeditor.model.AbstractUtility;
37
import eu.etaxonomy.taxeditor.model.MessagingUtils;
38
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
39

    
40
/**
41
 * @author n.hoffmann
42
 * @created Dec 6, 2010
43
 * @version 1.0
44
 */
45
public class DeleteNodeHandlerE4 extends AbstractPolytomousKeyNodeHandler {
46

    
47

    
48
    private static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_NODE_THIS_OPERATION_IS_NOT_REVERSABLE = Messages.DeleteNodeHandler_REALLY_DELETE;
49
    private static final String CONFIRM_DELETION_OF_CHILDREN = Messages.DeleteNodeHandler_CONFIRM_DELETE;
50
    private static final String NO = Messages.DeleteNodeHandler_NO;
51
    private static final String CANCEL = Messages.DeleteNodeHandler_CANCEL;
52
    private static final String YES = Messages.DeleteNodeHandler_YES;
53
    PolytomousKeyNode nodeToBeDeleted;
54
    boolean deleteChildren;
55
    /**
56
     * @param label
57
     */
58
    public DeleteNodeHandlerE4(String label) {
59
        super(label);
60

    
61
    }
62

    
63

    
64

    
65
    public DeleteNodeHandlerE4() {
66
        super(PolytomousKeyEditorLabels.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL);
67
    }
68

    
69
    /**
70
     * {@inheritDoc}
71
     */
72
    @Override
73
    public IStatus allowOperations(ExecutionEvent event) {
74
        IEditorPart editor = HandlerUtil.getActiveEditor(event);
75
        IStructuredSelection selection = (IStructuredSelection) HandlerUtil
76
                .getCurrentSelection(event);
77
        AbstractPostOperation operation;
78
        if (editor instanceof KeyEditor) {
79
            editorPage = (IPolytomousKeyEditorPage) ((KeyEditor) editor)
80
                    .getActiveEditor();
81
        }
82

    
83

    
84
        if (selection.getFirstElement() instanceof PolytomousKeyNode) {
85
            try {
86
                String label = event.getCommand().getName();
87
                IUndoContext undoContext = EditorUtil.getUndoContext();
88
                PolytomousKeyNode node = (PolytomousKeyNode)selection.getFirstElement();
89
                nodeToBeDeleted = node;
90
                MessageDialog dialog;
91
                if (node.getChildren().size()>0){
92
                    String[] buttonLables = {YES, NO,CANCEL};
93
                    dialog = new MessageDialog(null, CONFIRM_DELETION_OF_CHILDREN, null, DO_YOU_REALLY_WANT_TO_DELETE_THE_NODE_THIS_OPERATION_IS_NOT_REVERSABLE+Messages.DeleteNodeHandler_NODE_HAS_CHILDREN, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
94
                    int returnCode = dialog.open();
95

    
96
                    if (returnCode == 0){
97
                        deleteChildren = false;
98
                    } else if (returnCode == 1){
99
                        deleteChildren = true;
100
                    } else{
101
                        return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
102
                                null);
103
                    }
104

    
105

    
106
                }else{
107
                    String[] buttonLables = {YES, CANCEL};
108
                    dialog = new MessageDialog(null, CONFIRM_DELETION_OF_CHILDREN, null, DO_YOU_REALLY_WANT_TO_DELETE_THE_NODE_THIS_OPERATION_IS_NOT_REVERSABLE, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 1);
109
                    int returnCode = dialog.open();
110

    
111
                    if (returnCode == 0){
112
                        deleteChildren = false;
113
                    } else if (returnCode == 1){
114
                       return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
115
                                null);
116
                    }
117
                }
118

    
119

    
120

    
121

    
122
            } catch (NotDefinedException e) {
123
                MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
124
            }
125
        } else {
126
                MessageDialog.openInformation(
127
                        AbstractUtility.getShell(),
128
                        PolytomousKeyEditorLabels.NO_KEY_NODE_SELECTED,
129
                        PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE
130
                        );
131
        }
132

    
133
        return Status.OK_STATUS;
134
    }
135

    
136
    /**
137
     * {@inheritDoc}
138
     */
139
    @Override
140
    public AbstractOperation prepareOperation(ExecutionEvent event) {
141
        IUndoContext undoContext = EditorUtil.getUndoContext();
142
        String label = ""; //$NON-NLS-1$
143
        IEditorPart editor = HandlerUtil.getActiveEditor(event);
144
        if (editor.isDirty()){
145
            boolean proceed = MessageDialog.openQuestion(null,
146
                    Messages.DeleteNodeHandler_SAVE_CHANGES_TITLE, Messages.DeleteNodeHandler_SAVE_CHANGES_MESSAGE);
147
            if (!proceed) {
148
                return null;
149
            }else{
150
                editor.doSave(EditorUtil.getMonitor());
151
            }
152
        }
153

    
154
        try {
155
            label = event.getCommand().getName();
156
        } catch (NotDefinedException e) {
157
            MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$
158
        }
159
        DeleteNodeOperation operation ;
160
        if (deleteChildren){
161
            operation = new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage, true);
162
        }else{
163
            operation =  new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage, false);
164
        }
165

    
166
        return operation;
167
    }
168

    
169
    /**
170
     * {@inheritDoc}
171
     */
172
    @Override
173
    public void onComplete() {
174
        // TODO Auto-generated method stub
175

    
176
    }
177

    
178
    @CanExecute
179
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
180
            MHandledMenuItem menuItem){
181
        boolean canExecute = false;
182
        canExecute = selection instanceof PolytomousKeyNode;
183
        menuItem.setVisible(canExecute);
184
        return canExecute;
185
    }
186

    
187
}
(1-1/2)