Project

General

Profile

Download (5.44 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.TaxonNameDescription;
32
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
33
import eu.etaxonomy.cdm.model.term.TermNode;
34
import eu.etaxonomy.cdm.model.term.TermTree;
35
import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
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

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

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

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

    
58
        Object selectedElement = selection.getFirstElement();
59

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

    
63
            for (TermNode<?> childNode : featureTree.getRootChildren()) {
64
                createMenuItem(menu, childNode.getTerm(), globalLanguage);
65
            }
66
        } else if (selectedElement instanceof FeatureNodeContainer) {
67
            TermNode<?> featureNode = ((FeatureNodeContainer) selectedElement)
68
                    .getFeatureNode();
69

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

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

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

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

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

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

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

    
136
		if (featureTree == null) {
137
		    featureTree = TermEditorInput.getDefaultFeatureTree();
138
		}
139
		if (description instanceof TaxonNameDescription){
140
		    featureTree = TermEditorInput.getPreferredNameFeatureTree();
141

    
142
		}
143

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