Project

General

Profile

Download (5.63 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.api.service.IVocabularyService;
27
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.common.VocabularyEnum;
30
import eu.etaxonomy.cdm.model.description.DescriptionBase;
31
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32
import eu.etaxonomy.cdm.model.description.Feature;
33
import eu.etaxonomy.cdm.model.description.FeatureNode;
34
import eu.etaxonomy.cdm.model.description.FeatureTree;
35
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
36
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
37
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
38
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
39
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
40
import eu.etaxonomy.taxeditor.store.CdmStore;
41

    
42
/**
43
 *
44
 * @author pplitzner
45
 * @date 15.08.2017
46
 *
47
 */
48
public class DynamicFeatureMenuE4 {
49

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

    
55
        MMenu menu = MMenuFactory.INSTANCE.createMenu();
56
        menu.setLabel(Messages.DynamicFeatureMenuE4_new);
57
        items.add(menu);
58

    
59
        Object selectedElement = selection.getFirstElement();
60

    
61
        if (selectedElement instanceof DescriptionBase<?>) {
62
            FeatureTree featureTree = getFeatureTree((DescriptionBase<?>) selectedElement);
63

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

    
71
            // add the feature to the menu
72
            createMenuItem(menu, featureNode.getFeature(), globalLanguage);
73

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

    
85
    private void createMenuItem(MMenu menu, final Feature feature, Language globalLanguage) {
86
        final Feature deproxiedFeature = HibernateProxyHelper.deproxy(feature, Feature.class);
87

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

    
111
        menuItem.setCommand(mCommand);
112
        menu.getChildren().add(menuItem);
113
    }
114

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

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

    
137
		if (featureTree == null) {
138
			featureTree = FeatureTree.NewInstance(CdmStore.getTermManager()
139
					.getPreferredTerms(Feature.class));
140
		}
141
		if (description instanceof TaxonNameDescription){
142
		    featureTree = FeatureTree.NewInstance(CdmStore.getTermManager().getPreferredTerms(CdmStore.getService(IVocabularyService.class).load(VocabularyEnum.NameFeature.getUuid())));
143
		}
144

    
145
		return featureTree;
146
	}
147
}
(6-6/9)