fix default feature tree for facts and name facts
[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 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.l10n.Messages;
38 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
39 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
40 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
41 import eu.etaxonomy.taxeditor.store.CdmStore;
42
43 /**
44 *
45 * @author pplitzner
46 * @date 15.08.2017
47 *
48 */
49 public class DynamicFeatureMenuE4 {
50
51 /** {@inheritDoc} */
52 @AboutToShow
53 public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection) {
54 Language globalLanguage = PreferencesUtil.getGlobalLanguage();
55
56 MMenu menu = MMenuFactory.INSTANCE.createMenu();
57 menu.setLabel(Messages.DynamicFeatureMenuE4_new);
58 items.add(menu);
59
60 Object selectedElement = selection.getFirstElement();
61
62 if (selectedElement instanceof DescriptionBase<?>) {
63 FeatureTree<?> featureTree = getFeatureTree((DescriptionBase<?>) selectedElement);
64
65 for (FeatureNode<?> childNode : featureTree.getRootChildren()) {
66 createMenuItem(menu, childNode.getTerm(), globalLanguage);
67 }
68 } else if (selectedElement instanceof FeatureNodeContainer) {
69 FeatureNode<?> featureNode = ((FeatureNodeContainer) selectedElement)
70 .getFeatureNode();
71
72 // add the feature to the menu
73 createMenuItem(menu, featureNode.getTerm(), globalLanguage);
74
75 // add possible children to the menu
76 for (FeatureNode<?> childNode : featureNode.getChildNodes()) {
77 createMenuItem(menu, childNode.getTerm(), globalLanguage);
78 }
79 } else if (selectedElement instanceof DescriptionElementBase) {
80 Feature feature = ((DescriptionElementBase) selectedElement)
81 .getFeature();
82 createMenuItem(menu, feature, globalLanguage);
83 }
84 }
85
86 private void createMenuItem(MMenu menu, final DefinedTermBase feature, Language globalLanguage) {
87 final DefinedTermBase deproxiedFeature = HibernateProxyHelper.deproxy(feature, DefinedTermBase.class);
88
89 String label = deproxiedFeature.getLabel(globalLanguage);
90 if (label == null){
91 label = deproxiedFeature.getLabel();
92 }
93 if(label == null){
94 label = deproxiedFeature.generateTitle();
95 }
96 if(label == null){
97 label = deproxiedFeature.toString();
98 }
99 MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
100 menuItem.setLabel(label);
101 MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
102 mCommand.setElementId(CreateDescriptionElementOperation.ID);
103 mCommand.setCommandName(label);
104 // try {
105 // mCommand.setCommandName(command.getName());
106 // } catch (NotDefinedException e) {
107 // e.printStackTrace();
108 // }
109 //set params
110 menuItem.getTransientData().put(CreateDescriptionElementOperation.ID+".feature.uuid", deproxiedFeature.getUuid());
111
112 menuItem.setCommand(mCommand);
113 menu.getChildren().add(menuItem);
114 }
115
116 /**
117 * Retrieves the feature tree associated with the given description
118 *
119 * TODO as of now this is always the same thing because feature trees may
120 * not be associated to descriptions yet.
121 *
122 * @param description
123 * @return
124 */
125 private FeatureTree<?> getFeatureTree(DescriptionBase description) {
126 FeatureTree<?> featureTree = null;
127
128 // TODO change this to the feature tree associated with this taxon
129 // description
130 if (description.hasStructuredData()) {
131 featureTree = PreferencesUtil
132 .getDefaultFeatureTreeForStructuredDescription();
133 } else {
134 featureTree = PreferencesUtil
135 .getDefaultFeatureTreeForTextualDescription();
136 }
137
138 if (featureTree == null) {
139 List<Feature> terms = CdmStore.getTermManager().getPreferredTerms(CdmStore.getService(IVocabularyService.class).load(VocabularyEnum.Feature.getUuid()), null);
140 terms.remove(Feature.IMAGE());
141
142 featureTree = FeatureTree.NewInstance(terms);
143
144 }
145 if (description instanceof TaxonNameDescription){
146 List<Feature> terms = CdmStore.getTermManager().getPreferredTerms(CdmStore.getService(IVocabularyService.class).load(VocabularyEnum.NameFeature.getUuid()));
147 terms.remove(Feature.PROTOLOGUE());
148 featureTree = FeatureTree.NewInstance(terms);
149 }
150
151 return featureTree;
152 }
153 }