Project

General

Profile

Download (3.52 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 java.util.Collection;
12

    
13
import javax.inject.Named;
14

    
15
import org.eclipse.core.runtime.IStatus;
16
import org.eclipse.core.runtime.NullProgressMonitor;
17
import org.eclipse.e4.core.di.annotations.CanExecute;
18
import org.eclipse.e4.core.di.annotations.Execute;
19
import org.eclipse.e4.core.di.annotations.Optional;
20
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
22
import org.eclipse.e4.ui.services.IServiceConstants;
23
import org.eclipse.jface.viewers.IStructuredSelection;
24
import org.eclipse.jface.wizard.WizardDialog;
25
import org.eclipse.swt.widgets.Shell;
26

    
27
import eu.etaxonomy.cdm.model.description.Feature;
28
import eu.etaxonomy.cdm.model.description.FeatureNode;
29
import eu.etaxonomy.cdm.model.description.FeatureTree;
30
import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
31
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureNodeDropAdapter;
32
import eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor;
33
import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
34
import eu.etaxonomy.taxeditor.model.MessagingUtils;
35

    
36
/**
37
 * @author pplitzner
38
 * @since Jul 12, 2017
39
 *
40
 */
41
public class AddFeatureHandler {
42

    
43
    @Execute
44
    public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
45
            @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
46
            @Named(IServiceConstants.ACTIVE_PART)MPart thisPart) {
47
        FeatureTreeEditor editor = ((FeatureTreeEditor) thisPart.getObject());
48

    
49
        if (editor.isDirty()) {
50
            if (MessagingUtils.confirmDialog(FeatureNodeDropAdapter.SAVE_CHANGES_TITLE,
51
                    FeatureNodeDropAdapter.SAVE_CHANGES_MESSAGE)){
52
                editor.save(new NullProgressMonitor());
53
            }
54
            else{
55
                return;
56
            }
57
        }
58

    
59

    
60
        AvailableFeaturesWizard wizard = new AvailableFeaturesWizard();
61
        WizardDialog dialog = new WizardDialog(shell, wizard);
62

    
63
        if (dialog.open() == IStatus.OK) {
64
            FeatureTree tree = (FeatureTree) selection.getFirstElement();
65
            Collection<Feature> additionalFeatures = wizard.getAdditionalFeatures();
66
            for (Feature feature : additionalFeatures) {
67
                FeatureNode childNode = FeatureNode.NewInstance(feature);
68
                tree.getRoot().addChild(childNode);
69
                AddFeatureOperation operation = new AddFeatureOperation(feature, tree.getRoot(), editor, editor);
70
                editor.addOperation(operation);
71
            }
72
            editor.setDirty(true);
73
            editor.getViewer().refresh();
74
            editor.getViewer().expandToLevel(selection, 1);
75
        }
76
    }
77

    
78
    @CanExecute
79
    public boolean canExecute(
80
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
81
            @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
82
            MHandledMenuItem menuItem) {
83
        boolean canExecute = false;
84
        canExecute = thisPart.getObject() instanceof FeatureTreeEditor
85
                && selection!=null
86
                && selection.size()==1
87
                && selection.getFirstElement() instanceof FeatureTree;
88
        menuItem.setVisible(canExecute);
89
        return canExecute;
90
    }
91
}
(2-2/8)