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
.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
;
49 public class DynamicFeatureMenuE4
{
53 public void aboutToShow(List
<MMenuElement
> items
, @Named(IServiceConstants
.ACTIVE_SELECTION
) IStructuredSelection selection
) {
54 Language globalLanguage
= PreferencesUtil
.getGlobalLanguage();
56 MMenu menu
= MMenuFactory
.INSTANCE
.createMenu();
57 menu
.setLabel(Messages
.DynamicFeatureMenuE4_new
);
60 Object selectedElement
= selection
.getFirstElement();
62 if (selectedElement
instanceof DescriptionBase
<?
>) {
63 FeatureTree
<?
> featureTree
= getFeatureTree((DescriptionBase
<?
>) selectedElement
);
65 for (FeatureNode
<?
> childNode
: featureTree
.getRootChildren()) {
66 createMenuItem(menu
, childNode
.getTerm(), globalLanguage
);
68 } else if (selectedElement
instanceof FeatureNodeContainer
) {
69 FeatureNode
<?
> featureNode
= ((FeatureNodeContainer
) selectedElement
)
72 // add the feature to the menu
73 createMenuItem(menu
, featureNode
.getTerm(), globalLanguage
);
75 // add possible children to the menu
76 for (FeatureNode
<?
> childNode
: featureNode
.getChildNodes()) {
77 createMenuItem(menu
, childNode
.getTerm(), globalLanguage
);
79 } else if (selectedElement
instanceof DescriptionElementBase
) {
80 Feature feature
= ((DescriptionElementBase
) selectedElement
)
82 createMenuItem(menu
, feature
, globalLanguage
);
86 private void createMenuItem(MMenu menu
, final DefinedTermBase feature
, Language globalLanguage
) {
87 final DefinedTermBase deproxiedFeature
= HibernateProxyHelper
.deproxy(feature
, DefinedTermBase
.class);
89 String label
= deproxiedFeature
.getLabel(globalLanguage
);
91 label
= deproxiedFeature
.getLabel();
94 label
= deproxiedFeature
.generateTitle();
97 label
= deproxiedFeature
.toString();
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
);
105 // mCommand.setCommandName(command.getName());
106 // } catch (NotDefinedException e) {
107 // e.printStackTrace();
110 menuItem
.getTransientData().put(CreateDescriptionElementOperation
.ID
+".feature.uuid", deproxiedFeature
.getUuid());
112 menuItem
.setCommand(mCommand
);
113 menu
.getChildren().add(menuItem
);
117 * Retrieves the feature tree associated with the given description
119 * TODO as of now this is always the same thing because feature trees may
120 * not be associated to descriptions yet.
125 private FeatureTree
<?
> getFeatureTree(DescriptionBase description
) {
126 FeatureTree
<?
> featureTree
= null;
128 // TODO change this to the feature tree associated with this taxon
130 if (description
.hasStructuredData()) {
131 featureTree
= PreferencesUtil
132 .getDefaultFeatureTreeForStructuredDescription();
134 featureTree
= PreferencesUtil
135 .getDefaultFeatureTreeForTextualDescription();
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());
142 featureTree
= FeatureTree
.NewInstance(terms
);
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
);