2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.e4
.handler
;
12 import java
.util
.List
;
14 import javax
.inject
.Named
;
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
;
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
;
42 public class DynamicFeatureMenuE4
{
46 public void aboutToShow(List
<MMenuElement
> items
, @Named(IServiceConstants
.ACTIVE_SELECTION
) Object selection
) {
48 MMenu menu
= MMenuFactory
.INSTANCE
.createMenu();
52 if (selection
instanceof DescriptionBase
<?
>) {
53 FeatureTree featureTree
= getFeatureTree((DescriptionBase
<?
>) selection
);
55 for (FeatureNode childNode
: featureTree
.getRootChildren()) {
56 createMenuItem(menu
, childNode
.getFeature());
59 } else if (selection
instanceof FeatureNodeContainer
) {
60 FeatureNode featureNode
= ((FeatureNodeContainer
) selection
)
63 // add the feature to the menu
64 createMenuItem(menu
, featureNode
.getFeature());
66 // add possible children to the menu
67 for (FeatureNode childNode
: featureNode
.getChildNodes()) {
68 createMenuItem(menu
, childNode
.getFeature());
70 } else if (selection
instanceof DescriptionElementBase
) {
71 Feature feature
= ((DescriptionElementBase
) selection
)
73 createMenuItem(menu
, feature
);
77 private void createMenuItem(MMenu menu
, final Feature feature
) {
78 final Feature deproxiedFeature
= HibernateProxyHelper
.deproxy(feature
, Feature
.class);
80 String label
= deproxiedFeature
.getLabel(PreferencesUtil
.getGlobalLanguage());
82 label
= deproxiedFeature
.getLabel();
85 label
= deproxiedFeature
.generateTitle();
88 label
= deproxiedFeature
.toString();
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
);
96 // mCommand.setCommandName(command.getName());
97 // } catch (NotDefinedException e) {
98 // e.printStackTrace();
101 menuItem
.getTransientData().put(CreateDescriptionElementOperation
.ID
+".feature.uuid", feature
.getUuid());
103 menuItem
.setCommand(mCommand
);
104 menu
.getChildren().add(menuItem
);
108 * Retrieves the feature tree associated with the given description
110 * TODO as of now this is always the same thing because feature trees may
111 * not be associated to descriptions yet.
116 private FeatureTree
getFeatureTree(DescriptionBase description
) {
117 FeatureTree featureTree
= null;
119 // TODO change this to the feature tree associated with this taxon
121 if (description
.hasStructuredData()) {
122 featureTree
= PreferencesUtil
123 .getDefaultFeatureTreeForStructuredDescription();
125 featureTree
= PreferencesUtil
126 .getDefaultFeatureTreeForTextualDescription();
129 if (featureTree
== null) {
130 featureTree
= FeatureTree
.NewInstance(CdmStore
.getTermManager()
131 .getPreferredTerms(Feature
.class));