Project

General

Profile

Download (5.05 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.view.descriptive.e4.handler;
11

    
12
import java.util.List;
13

    
14
import javax.inject.Named;
15

    
16
import org.eclipse.e4.ui.di.AboutToShow;
17
import org.eclipse.e4.ui.model.application.commands.MCommand;
18
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
19
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
20
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
21
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
22
import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
23
import org.eclipse.e4.ui.services.IServiceConstants;
24
import org.eclipse.jface.viewers.IStructuredSelection;
25

    
26
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
27
import eu.etaxonomy.cdm.model.description.DescriptionBase;
28
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
29
import eu.etaxonomy.cdm.model.description.Feature;
30
import eu.etaxonomy.cdm.model.description.FeatureNode;
31
import eu.etaxonomy.cdm.model.description.FeatureTree;
32
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
33
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
34
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
35
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
36
import eu.etaxonomy.taxeditor.store.CdmStore;
37

    
38
/**
39
 *
40
 * @author pplitzner
41
 * @date 15.08.2017
42
 *
43
 */
44
public class DynamicFeatureMenuE4 {
45

    
46
    /** {@inheritDoc} */
47
    @AboutToShow
48
    public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection) {
49

    
50
        MMenu menu = MMenuFactory.INSTANCE.createMenu();
51
        menu.setLabel(Messages.DynamicFeatureMenuE4_new);
52
        items.add(menu);
53

    
54
            Object selectedElement = selection.getFirstElement();
55

    
56
        if (selectedElement instanceof DescriptionBase<?>) {
57
            FeatureTree featureTree = getFeatureTree((DescriptionBase<?>) selectedElement);
58

    
59
            for (FeatureNode childNode : featureTree.getRootChildren()) {
60
                createMenuItem(menu, childNode.getFeature());
61

    
62
            }
63
        } else if (selectedElement instanceof FeatureNodeContainer) {
64
            FeatureNode featureNode = ((FeatureNodeContainer) selectedElement)
65
                    .getFeatureNode();
66

    
67
            // add the feature to the menu
68
            createMenuItem(menu, featureNode.getFeature());
69

    
70
            // add possible children to the menu
71
            for (FeatureNode childNode : featureNode.getChildNodes()) {
72
                createMenuItem(menu, childNode.getFeature());
73
            }
74
        } else if (selectedElement instanceof DescriptionElementBase) {
75
            Feature feature = ((DescriptionElementBase) selectedElement)
76
                    .getFeature();
77
            createMenuItem(menu, feature);
78
        }
79
    }
80

    
81
    private void createMenuItem(MMenu menu, final Feature feature) {
82
        final Feature deproxiedFeature = HibernateProxyHelper.deproxy(feature, Feature.class);
83

    
84
        String label = deproxiedFeature.getLabel(PreferencesUtil.getGlobalLanguage());
85
        if (label == null){
86
            label = deproxiedFeature.getLabel();
87
        }
88
        if(label == null){
89
            label = deproxiedFeature.generateTitle();
90
        }
91
        if(label == null){
92
            label = deproxiedFeature.toString();
93
        }
94
        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
95
        menuItem.setLabel(label);
96
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
97
        mCommand.setElementId(CreateDescriptionElementOperation.ID);
98
        mCommand.setCommandName(label);
99
        //	        try {
100
        //	            mCommand.setCommandName(command.getName());
101
        //	        } catch (NotDefinedException e) {
102
        //	            e.printStackTrace();
103
        //	        }
104
        //set params
105
        menuItem.getTransientData().put(CreateDescriptionElementOperation.ID+".feature.uuid", feature.getUuid());
106

    
107
        menuItem.setCommand(mCommand);
108
        menu.getChildren().add(menuItem);
109
    }
110

    
111
	/**
112
	 * Retrieves the feature tree associated with the given description
113
	 *
114
	 * TODO as of now this is always the same thing because feature trees may
115
	 * not be associated to descriptions yet.
116
	 *
117
	 * @param description
118
	 * @return
119
	 */
120
	private FeatureTree getFeatureTree(DescriptionBase description) {
121
		FeatureTree featureTree = null;
122

    
123
		// TODO change this to the feature tree associated with this taxon
124
		// description
125
		if (description.hasStructuredData()) {
126
			featureTree = PreferencesUtil
127
					.getDefaultFeatureTreeForStructuredDescription();
128
		} else {
129
			featureTree = PreferencesUtil
130
					.getDefaultFeatureTreeForTextualDescription();
131
		}
132

    
133
		if (featureTree == null) {
134
			featureTree = FeatureTree.NewInstance(CdmStore.getTermManager()
135
					.getPreferredTerms(Feature.class));
136
		}
137

    
138
		return featureTree;
139
	}
140
}
(5-5/8)