Project

General

Profile

Download (4.27 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2017 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.featuretree.e4.handler;
10

    
11
import javax.inject.Named;
12

    
13
import org.eclipse.e4.core.di.annotations.CanExecute;
14
import org.eclipse.e4.core.di.annotations.Execute;
15
import org.eclipse.e4.core.di.annotations.Optional;
16
import org.eclipse.e4.ui.di.UISynchronize;
17
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
18
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
19
import org.eclipse.e4.ui.services.IServiceConstants;
20
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.IStructuredSelection;
22
import org.eclipse.jface.viewers.TreeSelection;
23

    
24
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
25
import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
26
import eu.etaxonomy.taxeditor.featuretree.e4.ICharacterEditor;
27
import eu.etaxonomy.taxeditor.featuretree.e4.IFeatureTreeEditor;
28
import eu.etaxonomy.taxeditor.featuretree.e4.TermTreeEditor;
29
import eu.etaxonomy.taxeditor.featuretree.e4.operation.RemoveFeatureOperation;
30
import eu.etaxonomy.taxeditor.store.StoreUtil;
31

    
32
/**
33
 * @author pplitzner
34
 * @since Jul 12, 2017
35
 *
36
 */
37
public class RemoveFeatureHandler {
38

    
39
    @Execute
40
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
41
            @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection, UISynchronize sync){
42
        IFeatureTreeEditor editor = (IFeatureTreeEditor) thisPart.getObject();
43

    
44
//        if (StoreUtil.promptCheckIsDirty(editor)) {
45
//            return;
46
//        }
47
        if(!StoreUtil.confirmDelete()){
48
            return;
49
        }
50

    
51
        for (Object selectedObject : selection.toArray()) {
52
        	TermNodeDto featureNode = (TermNodeDto) selectedObject;
53
        	TermTreeDto tree = featureNode.getTree();
54
        	if (featureNode.getUuid() != null){
55
        	    RemoveFeatureOperation operation = new RemoveFeatureOperation(featureNode.getUuid(), editor, editor);
56
        	    editor.addOperation(operation);
57
        	}else{
58
//        	    editor.removeAddOperation(featureNode);
59
        	}
60
            ISelection sel = editor.getViewer().getSelection();
61
            if (sel instanceof TreeSelection){
62
//                Object o =((TreeSelection) sel).getFirstElement();
63
//                if (o instanceof TermNodeDto && ((TermNodeDto)o).getUuid().equals(featureNode.getUuid())){
64
                    TermNodeDto parent = editor.getNodeDtoForUuid(featureNode.getParentUuid());
65
                    parent.removeChild(featureNode, false);
66
//                }
67
            }
68
//            editor.refresh();
69
//            }else{
70
//                for (Object treeDto: ((AbstractTermTreeEditor)editor).getTrees()){
71
//                    if (treeDto instanceof TermTreeDto && ((TermTreeDto)treeDto).getUuid().equals(featureNode.getTree().getUuid())){
72
//                        ((TermTreeDto)treeDto).removeChild(featureNode);
73
//                    }
74
//
75
//                }
76
                if (editor instanceof TermTreeEditor){
77
                    Object[] expandedElements = ((TermTreeEditor)editor).getViewer().getExpandedElements();
78
                    ((TermTreeEditor)editor).getViewer().setInput(((TermTreeEditor)editor).getTrees());
79
                    ((TermTreeEditor)editor).getViewer().setExpandedElements(expandedElements);
80
                }else{
81
                    ((ICharacterEditor)editor).getViewer().setInput(tree);
82
                }
83
                editor.refresh();
84
//            }
85

    
86
            editor.setDirty();
87
        }
88
    }
89

    
90
    @CanExecute
91
    public boolean canExecute(
92
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
93
            @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
94
            MHandledMenuItem menuItem) {
95
        boolean canExecute = selection!=null && !selection.isEmpty();
96
        if(selection!=null){
97
            for(Object object:selection.toArray()){
98
                canExecute &= object instanceof TermNodeDto;
99
            }
100
        }
101
        canExecute &= thisPart.getObject() instanceof IFeatureTreeEditor;
102
        menuItem.setVisible(canExecute);
103
        return canExecute;
104
    }
105

    
106
}
(10-10/11)