Project

General

Profile

Download (6.1 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.operations.AbstractOperation;
15
import org.eclipse.core.commands.operations.IUndoContext;
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.core.runtime.Status;
18
import org.eclipse.e4.core.di.annotations.CanExecute;
19
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
20
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
21
import org.eclipse.e4.ui.services.IServiceConstants;
22
import org.eclipse.jface.dialogs.MessageDialog;
23
import org.eclipse.jface.viewers.IStructuredSelection;
24
import org.eclipse.swt.widgets.Shell;
25

    
26
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
27
import eu.etaxonomy.taxeditor.editor.EditorUtil;
28
import eu.etaxonomy.taxeditor.editor.key.polytomous.PolytomousKeyEditorLabels;
29
import eu.etaxonomy.taxeditor.editor.key.polytomous.e4.PolytomousKeyListEditorE4;
30
import eu.etaxonomy.taxeditor.editor.key.polytomous.operation.DeleteNodeOperation;
31
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
32
import eu.etaxonomy.taxeditor.model.AbstractUtility;
33

    
34
/**
35
 * @author n.hoffmann
36
 * @created Dec 6, 2010
37
 * @version 1.0
38
 */
39
public class DeleteNodeHandlerE4 extends AbstractPolytomousKeyNodeHandlerE4 {
40

    
41

    
42
    private static final String DO_YOU_REALLY_WANT_TO_DELETE_THE_NODE_THIS_OPERATION_IS_NOT_REVERSABLE = Messages.DeleteNodeHandler_REALLY_DELETE;
43
    private static final String CONFIRM_DELETION_OF_CHILDREN = Messages.DeleteNodeHandler_CONFIRM_DELETE;
44
    private static final String NO = Messages.DeleteNodeHandler_NO;
45
    private static final String CANCEL = Messages.DeleteNodeHandler_CANCEL;
46
    private static final String YES = Messages.DeleteNodeHandler_YES;
47
    PolytomousKeyNode nodeToBeDeleted;
48
    boolean deleteChildren;
49

    
50
    public DeleteNodeHandlerE4(String label) {
51
        super(label);
52
    }
53

    
54
    public DeleteNodeHandlerE4() {
55
        super(PolytomousKeyEditorLabels.DELETE_NODE_POLYTOMOUS_KEY_NODE_LABEL);
56
    }
57

    
58
    @Override
59
    public IStatus allowOperations(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
60
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
61
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
62
            MHandledMenuItem menuItem) {
63

    
64
        if (selection.getFirstElement() instanceof PolytomousKeyNode) {
65
            label = menuItem.getLocalizedLabel();
66
            PolytomousKeyNode node = (PolytomousKeyNode)selection.getFirstElement();
67
            nodeToBeDeleted = node;
68
            MessageDialog dialog;
69
            if (node.getChildren().size()>0){
70
                String[] buttonLables = {YES, NO,CANCEL};
71
                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);
72
                int returnCode = dialog.open();
73

    
74
                if (returnCode == 0){
75
                    deleteChildren = false;
76
                } else if (returnCode == 1){
77
                    deleteChildren = true;
78
                } else{
79
                    return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
80
                            null);
81
                }
82

    
83

    
84
            }else{
85
                String[] buttonLables = {YES, CANCEL};
86
                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);
87
                int returnCode = dialog.open();
88

    
89
                if (returnCode == 0){
90
                    deleteChildren = false;
91
                } else if (returnCode == 1){
92
                   return new Status(IStatus.CANCEL, "unknown", //$NON-NLS-1$
93
                            null);
94
                }
95
            }
96
        } else {
97
                MessageDialog.openInformation(
98
                        shell,
99
                        PolytomousKeyEditorLabels.NO_KEY_NODE_SELECTED,
100
                        PolytomousKeyEditorLabels.NO_KEY_NODE_FOR_INSERT_NODE_SELECTED_MESSAGE
101
                        );
102
        }
103

    
104
        return Status.OK_STATUS;
105
    }
106

    
107
    @Override
108
    public AbstractOperation prepareOperation(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
109
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
110
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
111
            MHandledMenuItem menuItem) {
112
        IUndoContext undoContext = EditorUtil.getUndoContext();
113
        PolytomousKeyListEditorE4 editor = (PolytomousKeyListEditorE4) activePart.getObject();
114
        if (editor.isDirty()){
115
            boolean proceed = MessageDialog.openQuestion(null,
116
                    Messages.DeleteNodeHandler_SAVE_CHANGES_TITLE, Messages.DeleteNodeHandler_SAVE_CHANGES_MESSAGE);
117
            if (!proceed) {
118
                return null;
119
            }else{
120
                editor.doSave(AbstractUtility.getMonitor());
121
            }
122
        }
123

    
124
        label = menuItem.getLocalizedLabel();
125
        DeleteNodeOperation operation ;
126
        if (deleteChildren){
127
            operation = new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage, true);
128
        }else{
129
            operation =  new DeleteNodeOperation(label, undoContext, nodeToBeDeleted, editorPage, false);
130
        }
131

    
132
        return operation;
133
    }
134

    
135
    @Override
136
    public void onComplete() {
137
    }
138

    
139
    /**
140
     * {@inheritDoc}
141
     */
142
    @Override
143
    protected Object getTrigger() {
144
        return this;
145
    }
146

    
147
    @CanExecute
148
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
149
            MHandledMenuItem menuItem){
150
        boolean canExecute = false;
151
        canExecute = selection instanceof PolytomousKeyNode;
152
        menuItem.setVisible(canExecute);
153
        return canExecute;
154
    }
155

    
156
}
(4-4/6)