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
;
24 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
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
.definedterm
.input
.TermEditorInput
;
38 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
39 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.CreateDescriptionElementOperation
;
40 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
41 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
42 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
50 public class DynamicFeatureMenuE4
{
54 public void aboutToShow(List
<MMenuElement
> items
, @Named(IServiceConstants
.ACTIVE_SELECTION
) IStructuredSelection selection
) {
55 Language globalLanguage
= PreferencesUtil
.getGlobalLanguage();
57 MMenu menu
= MMenuFactory
.INSTANCE
.createMenu();
58 menu
.setLabel(Messages
.DynamicFeatureMenuE4_new
);
61 Object selectedElement
= selection
.getFirstElement();
63 if (selectedElement
instanceof DescriptionBase
<?
>) {
64 FeatureTree
<?
> featureTree
= getFeatureTree((DescriptionBase
<?
>) selectedElement
);
66 for (FeatureNode
<?
> childNode
: featureTree
.getRootChildren()) {
67 createMenuItem(menu
, childNode
.getTerm(), globalLanguage
);
69 } else if (selectedElement
instanceof FeatureNodeContainer
) {
70 FeatureNode
<?
> featureNode
= ((FeatureNodeContainer
) selectedElement
)
73 // add the feature to the menu
74 createMenuItem(menu
, featureNode
.getTerm(), globalLanguage
);
76 // add possible children to the menu
77 for (FeatureNode
<?
> childNode
: featureNode
.getChildNodes()) {
78 createMenuItem(menu
, childNode
.getTerm(), globalLanguage
);
80 } else if (selectedElement
instanceof DescriptionElementBase
) {
81 Feature feature
= ((DescriptionElementBase
) selectedElement
)
83 createMenuItem(menu
, feature
, globalLanguage
);
87 private void createMenuItem(MMenu menu
, final DefinedTermBase feature
, Language globalLanguage
) {
88 final DefinedTermBase deproxiedFeature
= HibernateProxyHelper
.deproxy(feature
, DefinedTermBase
.class);
90 String label
= deproxiedFeature
.getLabel(globalLanguage
);
92 label
= deproxiedFeature
.getLabel();
95 label
= deproxiedFeature
.generateTitle();
98 label
= deproxiedFeature
.toString();
100 MHandledMenuItem menuItem
= MMenuFactory
.INSTANCE
.createHandledMenuItem();
101 menuItem
.setLabel(label
);
102 MCommand mCommand
= MCommandsFactory
.INSTANCE
.createCommand();
103 mCommand
.setElementId(CreateDescriptionElementOperation
.ID
);
104 mCommand
.setCommandName(label
);
106 // mCommand.setCommandName(command.getName());
107 // } catch (NotDefinedException e) {
108 // e.printStackTrace();
111 menuItem
.getTransientData().put(CreateDescriptionElementOperation
.ID
+".feature.uuid", deproxiedFeature
.getUuid());
113 menuItem
.setCommand(mCommand
);
114 menu
.getChildren().add(menuItem
);
118 * Retrieves the feature tree associated with the given description
120 * TODO as of now this is always the same thing because feature trees may
121 * not be associated to descriptions yet.
126 private FeatureTree
<?
> getFeatureTree(DescriptionBase description
) {
127 FeatureTree
<?
> featureTree
= null;
129 // TODO change this to the feature tree associated with this taxon
131 if (description
.hasStructuredData()) {
132 featureTree
= PreferencesUtil
133 .getDefaultFeatureTreeForStructuredDescription();
135 featureTree
= PreferencesUtil
136 .getDefaultFeatureTreeForTextualDescription();
139 if (featureTree
== null) {
140 featureTree
= TermEditorInput
.getDefaultFeatureTree();
142 if (description
instanceof TaxonNameDescription
){
143 List
<Feature
> terms
= CdmStore
.getTermManager().getPreferredTerms(CdmStore
.getService(IVocabularyService
.class).load(VocabularyEnum
.NameFeature
.getUuid()));
144 terms
.remove(Feature
.PROTOLOGUE());
145 featureTree
= FeatureTree
.NewInstance(terms
);