Project

General

Profile

Download (6.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.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
 * @author pplitzner
43
 * @date 15.08.2017
44
 */
45
public class DynamicFeatureMenuE4 {
46

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

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

    
55
        Object selectedElement = selection.getFirstElement();
56

    
57
        if (selectedElement instanceof DescriptionBase<?>) {
58
            if (((DescriptionBase<?>) selectedElement).isComputed()){
59
                items.remove(menu);
60
                return;
61
            }
62
            TermTree<?> featureTree = getFeatureTree((DescriptionBase<?>) selectedElement);
63

    
64
            for (TermNode<?> childNode : featureTree.getRootChildren()) {
65
                createMenuItem(menu, childNode.getTerm(), globalLanguage);
66
            }
67
        } else if (selectedElement instanceof FeatureNodeContainer) {
68
            if (((FeatureNodeContainer) selectedElement).getDescription().isComputed()){
69
                items.remove(menu);
70
                return;
71
            }
72
            TermNode<?> featureNode = ((FeatureNodeContainer) selectedElement)
73
                    .getFeatureNode();
74

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

    
78
            // add possible children to the menu
79
            for (TermNode<?> childNode : featureNode.getChildNodes()) {
80
                TermTree<?> featureTree = getFeatureTree(((FeatureNodeContainer) selectedElement).getDescription());
81
                featureTree = PreferencesUtil.getPreferredFeatureTreeForNameDescription(false);
82

    
83
                if (featureTree != null && featureTree.getRootChildren().contains(childNode.getTerm() )){
84
                        createMenuItem(menu, childNode.getTerm(), globalLanguage);
85
                }
86
            }
87

    
88
        } else if (selectedElement instanceof DescriptionElementBase) {
89
            if (((DescriptionElementBase) selectedElement).getInDescription().isComputed()){
90
                items.remove(menu);
91
                return;
92
            }
93
            Feature feature = ((DescriptionElementBase) selectedElement)
94
                    .getFeature();
95
            createMenuItem(menu, feature, globalLanguage);
96
        }
97
    }
98

    
99
    private void createMenuItem(MMenu menu, final DefinedTermBase feature, Language globalLanguage) {
100
        final DefinedTermBase<?> deproxiedFeature = HibernateProxyHelper.deproxy(feature, DefinedTermBase.class);
101

    
102
        String label = deproxiedFeature.getLabel(globalLanguage);
103
        if (label == null){
104
            label = deproxiedFeature.getLabel();
105
        }
106
        if(label == null){
107
            label = deproxiedFeature.generateTitle();
108
        }
109
        if(label == null){
110
            label = deproxiedFeature.toString();
111
        }
112
        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
113
        menuItem.setLabel(label);
114
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
115
        mCommand.setElementId(CreateDescriptionElementOperation.ID);
116
        mCommand.setCommandName(label);
117
        //	        try {
118
        //	            mCommand.setCommandName(command.getName());
119
        //	        } catch (NotDefinedException e) {
120
        //	            e.printStackTrace();
121
        //	        }
122
        //set params
123
        menuItem.getTransientData().put(CreateDescriptionElementOperation.ID+".feature.uuid", deproxiedFeature.getUuid());
124

    
125
        menuItem.setCommand(mCommand);
126
        menu.getChildren().add(menuItem);
127
    }
128

    
129
	/**
130
	 * Retrieves the feature tree associated with the given description
131
	 *
132
	 * TODO as of now this is always the same thing because feature trees may
133
	 * not be associated to descriptions yet.
134
	 *
135
	 * @param description
136
	 * @return
137
	 */
138
	private TermTree<?> getFeatureTree(DescriptionBase description) {
139
		TermTree<?> featureTree = null;
140

    
141
		// TODO change this to the feature tree associated with this taxon
142
		// description
143
		if (description.hasStructuredData()) {
144
			featureTree = PreferencesUtil
145
					.getDefaultFeatureTreeForStructuredDescription();
146
		} else {
147
			featureTree = PreferencesUtil
148
					.getDefaultFeatureTreeForTextualDescription();
149
		}
150

    
151
		if (featureTree == null) {
152
		    featureTree = TermEditorInput.getPreferredTaxonFeatureTree(false);
153
		}
154
		if (description instanceof TaxonNameDescription){
155
		    featureTree = TermEditorInput.getPreferredNameFeatureTree(false);
156

    
157
		}
158

    
159
		return featureTree;
160
	}
161
}
(6-6/11)