Project

General

Profile

Download (3.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2009 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
package eu.etaxonomy.taxeditor.editor.definedterm.e4.handler;
10

    
11
import java.util.UUID;
12

    
13
import javax.inject.Named;
14

    
15
import org.eclipse.core.commands.operations.IUndoContext;
16
import org.eclipse.e4.core.di.annotations.CanExecute;
17
import org.eclipse.e4.core.di.annotations.Execute;
18
import org.eclipse.e4.ui.di.UISynchronize;
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.model.application.ui.menu.MMenuItem;
22
import org.eclipse.e4.ui.services.IServiceConstants;
23
import org.eclipse.jface.dialogs.MessageDialog;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25

    
26
import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
27
import eu.etaxonomy.cdm.persistence.dto.TermDto;
28
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
29
import eu.etaxonomy.taxeditor.editor.definedterm.e4.DefinedTermEditorE4;
30
import eu.etaxonomy.taxeditor.editor.definedterm.operation.DeleteTermBaseOperation;
31
import eu.etaxonomy.taxeditor.model.AbstractUtility;
32
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
33
import eu.etaxonomy.taxeditor.store.StoreUtil;
34

    
35
/**
36
 *
37
 * @author pplitzner
38
 * @since Aug 22, 2017
39
 *
40
 */
41
public class DeleteTermBaseHandlerE4 {
42

    
43
    @Execute
44
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
45
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MMenuItem menuItem,
46
            UISynchronize sync) {
47

    
48
        DefinedTermEditorE4 termEditor = (DefinedTermEditorE4) activePart.getObject();
49

    
50
        if (termEditor.isDirty()){
51
            boolean proceed = MessageDialog.openQuestion(null,
52
                    "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
53
            if (proceed) {
54
                termEditor.save(null);
55
            } else {
56
                return;
57
            }
58
        }
59
        String label = menuItem.getLocalizedLabel();
60
        IUndoContext undoContext = StoreUtil.getUndoContext();
61
        AbstractPostOperation operation =
62
                new DeleteTermBaseOperation(label,
63
                        undoContext,
64
                        (AbstractTermDto) selection.getFirstElement(),
65
                        termEditor.getDefinedTermEditorInput(),
66
                        termEditor);
67
        AbstractUtility.executeOperation(operation, sync);
68
    }
69

    
70
    @CanExecute
71
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
72
            MHandledMenuItem menuItem){
73
        boolean canExecute = false;
74
        Object firstElement = selection.getFirstElement();
75
        canExecute = selection.size()==1
76
                && (firstElement instanceof AbstractTermDto);
77
        UUID vocabularyUuid = null;
78
        if(firstElement instanceof TermDto){
79
            vocabularyUuid = ((TermDto) firstElement).getVocabularyUuid();
80
        }else if(firstElement instanceof TermVocabularyDto){
81
            vocabularyUuid = ((TermVocabularyDto) firstElement).getUuid();
82
        }
83
        canExecute &= vocabularyUuid!=null;
84
        menuItem.setVisible(canExecute);
85
        return canExecute;
86
    }
87
}
(3-3/4)