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
.handler
;
12 import org
.eclipse
.jface
.action
.ContributionItem
;
13 import org
.eclipse
.jface
.action
.IContributionItem
;
14 import org
.eclipse
.jface
.viewers
.ISelection
;
15 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
16 import org
.eclipse
.swt
.events
.SelectionEvent
;
17 import org
.eclipse
.swt
.events
.SelectionListener
;
18 import org
.eclipse
.swt
.widgets
.Event
;
19 import org
.eclipse
.swt
.widgets
.Menu
;
20 import org
.eclipse
.swt
.widgets
.MenuItem
;
21 import org
.eclipse
.ui
.ISelectionService
;
22 import org
.eclipse
.ui
.actions
.CompoundContributionItem
;
23 import org
.eclipse
.ui
.handlers
.IHandlerService
;
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
.internal
.TaxeditorEditorPlugin
;
32 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.DescriptiveViewPart
;
33 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.CreateDescriptionElementOperation
;
34 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
35 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
36 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
37 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
41 * DynamicFeatureMenu class.
48 public class DynamicFeatureMenu
extends CompoundContributionItem
{
50 private final ISelectionService selectionService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getService(ISelectionService
.class);
51 private final IHandlerService handlerService
= TaxeditorEditorPlugin
.getDefault().getWorkbench().getService(IHandlerService
.class);
57 * org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
61 protected IContributionItem
[] getContributionItems() {
63 return new IContributionItem
[] { new ContributionItem() {
65 public void fill(Menu menu
, int index
) {
67 ISelection selection
= selectionService
68 .getSelection(DescriptiveViewPart
.ID
);
70 if (selection
instanceof IStructuredSelection
) {
71 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
72 Object selectedElement
= structuredSelection
75 if (selectedElement
instanceof DescriptionBase
<?
>) {
76 FeatureTree featureTree
= getFeatureTree((DescriptionBase
<?
>) selectedElement
);
78 for (FeatureNode childNode
: featureTree
.getRootChildren()) {
79 createMenuItem(menu
, childNode
.getFeature());
82 } else if (selectedElement
instanceof FeatureNodeContainer
) {
83 FeatureNode featureNode
= ((FeatureNodeContainer
) selectedElement
)
86 // add the feature to the menu
87 createMenuItem(menu
, featureNode
.getFeature());
89 // add possible children to the menu
90 for (FeatureNode childNode
: featureNode
.getChildNodes()) {
91 createMenuItem(menu
, childNode
.getFeature());
93 } else if (selectedElement
instanceof DescriptionElementBase
) {
94 Feature feature
= ((DescriptionElementBase
) selectedElement
)
96 createMenuItem(menu
, feature
);
103 private void createMenuItem(Menu menu
, final Feature feature
) {
104 MenuItem menuItem
= new MenuItem(menu
, -1);
105 final Feature deproxiedFeature
= HibernateProxyHelper
.deproxy(feature
, Feature
.class);
107 String label
= deproxiedFeature
.getLabel(PreferencesUtil
.getGlobalLanguage());
109 menuItem
.setText(deproxiedFeature
.getLabel());
111 menuItem
.setText(label
);
113 menuItem
.addSelectionListener(new SelectionListener() {
116 public void widgetDefaultSelected(SelectionEvent e
) {
120 public void widgetSelected(SelectionEvent ev
) {
121 Event event
= new Event();
122 event
.data
= deproxiedFeature
;
124 handlerService
.executeCommand(
125 CreateDescriptionElementOperation
.ID
, event
);
126 } catch (Exception e
) {
127 MessagingUtils
.error(getClass(), e
);
135 * Retrieves the feature tree associated with the given description
137 * TODO as of now this is always the same thing because feature trees may
138 * not be associated to descriptions yet.
143 private FeatureTree
getFeatureTree(DescriptionBase description
) {
144 FeatureTree featureTree
= null;
146 // TODO change this to the feature tree associated with this taxon
148 if (description
.hasStructuredData()) {
149 featureTree
= PreferencesUtil
150 .getDefaultFeatureTreeForStructuredDescription();
152 featureTree
= PreferencesUtil
153 .getDefaultFeatureTreeForTextualDescription();
156 if (featureTree
== null) {
157 featureTree
= FeatureTree
.NewInstance(CdmStore
.getTermManager()
158 .getPreferredTerms(Feature
.class));