Project

General

Profile

Download (5.23 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.common.Language;
28
import eu.etaxonomy.cdm.model.description.DescriptionBase;
29
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30
import eu.etaxonomy.cdm.model.description.Feature;
31
import eu.etaxonomy.cdm.model.description.FeatureNode;
32
import eu.etaxonomy.cdm.model.description.FeatureTree;
33
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
34
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
35
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
36
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
37
import eu.etaxonomy.taxeditor.store.CdmStore;
38

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

    
47
    /** {@inheritDoc} */
48
    @AboutToShow
49
    public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection) {
50
        Language globalLanguage = PreferencesUtil.getGlobalLanguage();
51

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

    
56
        Object selectedElement = selection.getFirstElement();
57

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

    
61
            for (FeatureNode childNode : featureTree.getRootChildren()) {
62
                createMenuItem(menu, childNode.getFeature(), globalLanguage);
63
            }
64
        } else if (selectedElement instanceof FeatureNodeContainer) {
65
            FeatureNode featureNode = ((FeatureNodeContainer) selectedElement)
66
                    .getFeatureNode();
67

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

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

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

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

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

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

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

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

    
139
		return featureTree;
140
	}
141
}
(6-6/9)