d35b444afee071ee9812e2a8a553a65badbe59b6
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / e4 / handler / DynamicFeatureMenuE4.java
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
25 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
26 import eu.etaxonomy.cdm.model.description.DescriptionBase;
27 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
28 import eu.etaxonomy.cdm.model.description.Feature;
29 import eu.etaxonomy.cdm.model.description.FeatureNode;
30 import eu.etaxonomy.cdm.model.description.FeatureTree;
31 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
32 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
33 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35
36 /**
37 *
38 * @author pplitzner
39 * @date 15.08.2017
40 *
41 */
42 public class DynamicFeatureMenuE4 {
43
44 /** {@inheritDoc} */
45 @AboutToShow
46 public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
47
48 MMenu menu = MMenuFactory.INSTANCE.createMenu();
49 menu.setLabel("New");
50 items.add(menu);
51
52 if (selection instanceof DescriptionBase<?>) {
53 FeatureTree featureTree = getFeatureTree((DescriptionBase<?>) selection);
54
55 for (FeatureNode childNode : featureTree.getRootChildren()) {
56 createMenuItem(menu, childNode.getFeature());
57
58 }
59 } else if (selection instanceof FeatureNodeContainer) {
60 FeatureNode featureNode = ((FeatureNodeContainer) selection)
61 .getFeatureNode();
62
63 // add the feature to the menu
64 createMenuItem(menu, featureNode.getFeature());
65
66 // add possible children to the menu
67 for (FeatureNode childNode : featureNode.getChildNodes()) {
68 createMenuItem(menu, childNode.getFeature());
69 }
70 } else if (selection instanceof DescriptionElementBase) {
71 Feature feature = ((DescriptionElementBase) selection)
72 .getFeature();
73 createMenuItem(menu, feature);
74 }
75 }
76
77 private void createMenuItem(MMenu menu, final Feature feature) {
78 final Feature deproxiedFeature = HibernateProxyHelper.deproxy(feature, Feature.class);
79
80 String label = deproxiedFeature.getLabel(PreferencesUtil.getGlobalLanguage());
81 if (label == null){
82 label = deproxiedFeature.getLabel();
83 }
84 if(label == null){
85 label = deproxiedFeature.generateTitle();
86 }
87 if(label == null){
88 label = deproxiedFeature.toString();
89 }
90 MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
91 menuItem.setLabel(label);
92 MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
93 mCommand.setElementId(CreateDescriptionElementOperation.ID);
94 mCommand.setCommandName(label);
95 // try {
96 // mCommand.setCommandName(command.getName());
97 // } catch (NotDefinedException e) {
98 // e.printStackTrace();
99 // }
100 //set params
101 menuItem.getTransientData().put(CreateDescriptionElementOperation.ID+".feature.uuid", feature.getUuid());
102
103 menuItem.setCommand(mCommand);
104 menu.getChildren().add(menuItem);
105 }
106
107 /**
108 * Retrieves the feature tree associated with the given description
109 *
110 * TODO as of now this is always the same thing because feature trees may
111 * not be associated to descriptions yet.
112 *
113 * @param description
114 * @return
115 */
116 private FeatureTree getFeatureTree(DescriptionBase description) {
117 FeatureTree featureTree = null;
118
119 // TODO change this to the feature tree associated with this taxon
120 // description
121 if (description.hasStructuredData()) {
122 featureTree = PreferencesUtil
123 .getDefaultFeatureTreeForStructuredDescription();
124 } else {
125 featureTree = PreferencesUtil
126 .getDefaultFeatureTreeForTextualDescription();
127 }
128
129 if (featureTree == null) {
130 featureTree = FeatureTree.NewInstance(CdmStore.getTermManager()
131 .getPreferredTerms(Feature.class));
132 }
133
134 return featureTree;
135 }
136 }