Project

General

Profile

Download (5.8 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.description.DescriptionBase;
30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31
import eu.etaxonomy.cdm.model.description.Feature;
32
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
33
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
34
import eu.etaxonomy.cdm.model.term.FeatureNode;
35
import eu.etaxonomy.cdm.model.term.FeatureTree;
36
import eu.etaxonomy.cdm.model.term.VocabularyEnum;
37
import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
38
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
39
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
40
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
41
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
42
import eu.etaxonomy.taxeditor.store.CdmStore;
43

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

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

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

    
61
        Object selectedElement = selection.getFirstElement();
62

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

    
66
            for (FeatureNode<?> childNode : featureTree.getRootChildren()) {
67
                createMenuItem(menu, childNode.getTerm(), globalLanguage);
68
            }
69
        } else if (selectedElement instanceof FeatureNodeContainer) {
70
            FeatureNode<?> featureNode = ((FeatureNodeContainer) selectedElement)
71
                    .getFeatureNode();
72

    
73
            // add the feature to the menu
74
            createMenuItem(menu, featureNode.getTerm(), globalLanguage);
75

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

    
87
    private void createMenuItem(MMenu menu, final DefinedTermBase feature, Language globalLanguage) {
88
        final DefinedTermBase deproxiedFeature = HibernateProxyHelper.deproxy(feature, DefinedTermBase.class);
89

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

    
113
        menuItem.setCommand(mCommand);
114
        menu.getChildren().add(menuItem);
115
    }
116

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

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

    
139
		if (featureTree == null) {
140
		    featureTree = TermEditorInput.getDefaultFeatureTree();
141
		    		}
142
		if (description instanceof TaxonNameDescription){
143
		    List<Feature> terms = CdmStore.getTermManager().getPreferredTerms(CdmStore.getService(IVocabularyService.class).load(VocabularyEnum.NameFeature.getUuid()));
144
		    terms.remove(Feature.PROTOLOGUE());
145
		    featureTree = FeatureTree.NewInstance(terms);
146
		}
147

    
148
		return featureTree;
149
	}
150
}
(6-6/9)