Project

General

Profile

Download (4.24 KB) Statistics
| Branch: | Tag: | Revision:
1 ff46c6ca Patrick Plitzner
/**
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 5fec480a Katja Luther
package eu.etaxonomy.taxeditor.termtree.e4.handler;
10 ff46c6ca Patrick Plitzner
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 f57d8d08 Patrick Plitzner
import org.eclipse.e4.core.di.annotations.Optional;
16 41e1666a Patrick Plitzner
import org.eclipse.e4.ui.di.UISynchronize;
17 ff46c6ca Patrick Plitzner
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
18 72eff2a6 Patrick Plitzner
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
19 ff46c6ca Patrick Plitzner
import org.eclipse.e4.ui.services.IServiceConstants;
20 41963d8b Katja Luther
import org.eclipse.jface.viewers.ISelection;
21 ff46c6ca Patrick Plitzner
import org.eclipse.jface.viewers.IStructuredSelection;
22 41963d8b Katja Luther
import org.eclipse.jface.viewers.TreeSelection;
23 ff46c6ca Patrick Plitzner
24 2e1c204d Katja Luther
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
25 458132f7 Katja Luther
import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
26 41e1666a Patrick Plitzner
import eu.etaxonomy.taxeditor.store.StoreUtil;
27 5fec480a Katja Luther
import eu.etaxonomy.taxeditor.termtree.e4.ICharacterEditor;
28
import eu.etaxonomy.taxeditor.termtree.e4.ITermTreeEditor;
29
import eu.etaxonomy.taxeditor.termtree.e4.TermTreeEditor;
30
import eu.etaxonomy.taxeditor.termtree.e4.operation.RemoveFeatureOperation;
31 ff46c6ca Patrick Plitzner
32
/**
33
 * @author pplitzner
34
 * @since Jul 12, 2017
35
 *
36
 */
37 f6ba2ccb Katja Luther
public class RemoveTermHandler {
38 ff46c6ca Patrick Plitzner
39
    @Execute
40 72eff2a6 Patrick Plitzner
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
41 41e1666a Patrick Plitzner
            @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection, UISynchronize sync){
42 f6ba2ccb Katja Luther
        ITermTreeEditor editor = (ITermTreeEditor) thisPart.getObject();
43 cf349935 Patrick Plitzner
44 87caa5aa Katja Luther
//        if (StoreUtil.promptCheckIsDirty(editor)) {
45
//            return;
46
//        }
47 41e1666a Patrick Plitzner
        if(!StoreUtil.confirmDelete()){
48
            return;
49 cf349935 Patrick Plitzner
        }
50
51 72eff2a6 Patrick Plitzner
        for (Object selectedObject : selection.toArray()) {
52 2e1c204d Katja Luther
        	TermNodeDto featureNode = (TermNodeDto) selectedObject;
53 458132f7 Katja Luther
        	TermTreeDto tree = featureNode.getTree();
54 87caa5aa Katja Luther
        	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 41963d8b Katja Luther
            ISelection sel = editor.getViewer().getSelection();
61
            if (sel instanceof TreeSelection){
62 87caa5aa Katja Luther
//                Object o =((TreeSelection) sel).getFirstElement();
63
//                if (o instanceof TermNodeDto && ((TermNodeDto)o).getUuid().equals(featureNode.getUuid())){
64 41963d8b Katja Luther
                    TermNodeDto parent = editor.getNodeDtoForUuid(featureNode.getParentUuid());
65 87caa5aa Katja Luther
                    parent.removeChild(featureNode, false);
66
//                }
67 2e1c204d Katja Luther
            }
68 458132f7 Katja Luther
//            editor.refresh();
69 41963d8b Katja Luther
//            }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 22ac94a9 Katja Luther
                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 458132f7 Katja Luther
                }else{
81
                    ((ICharacterEditor)editor).getViewer().setInput(tree);
82
                }
83 8bf5dd93 Katja Luther
                editor.refresh();
84 41963d8b Katja Luther
//            }
85
86 2e1c204d Katja Luther
            editor.setDirty();
87 ff46c6ca Patrick Plitzner
        }
88
    }
89
90
    @CanExecute
91 72eff2a6 Patrick Plitzner
    public boolean canExecute(
92 f57d8d08 Patrick Plitzner
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
93 72eff2a6 Patrick Plitzner
            @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
94
            MHandledMenuItem menuItem) {
95 2c5756ad Patrick Plitzner
        boolean canExecute = selection!=null && !selection.isEmpty();
96 71a082e0 Patrick Plitzner
        if(selection!=null){
97
            for(Object object:selection.toArray()){
98 2e1c204d Katja Luther
                canExecute &= object instanceof TermNodeDto;
99 71a082e0 Patrick Plitzner
            }
100 ddf52a47 Patrick Plitzner
        }
101 f6ba2ccb Katja Luther
        canExecute &= thisPart.getObject() instanceof ITermTreeEditor;
102 72eff2a6 Patrick Plitzner
        menuItem.setVisible(canExecute);
103
        return canExecute;
104 ff46c6ca Patrick Plitzner
    }
105
106
}