3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.handler
;
13 import org
.eclipse
.jface
.action
.ContributionItem
;
14 import org
.eclipse
.jface
.action
.IContributionItem
;
15 import org
.eclipse
.jface
.viewers
.ISelection
;
16 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
17 import org
.eclipse
.swt
.events
.SelectionEvent
;
18 import org
.eclipse
.swt
.events
.SelectionListener
;
19 import org
.eclipse
.swt
.widgets
.Event
;
20 import org
.eclipse
.swt
.widgets
.Menu
;
21 import org
.eclipse
.swt
.widgets
.MenuItem
;
22 import org
.eclipse
.ui
.ISelectionService
;
23 import org
.eclipse
.ui
.actions
.CompoundContributionItem
;
24 import org
.eclipse
.ui
.handlers
.IHandlerService
;
26 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
27 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
28 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
29 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
30 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
31 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
33 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
34 import eu
.etaxonomy
.taxeditor
.editor
.UsageTermCollection
;
35 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.DescriptiveViewPart
;
36 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.CreateDescriptionElementOperation
;
37 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
38 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
39 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
43 * DynamicFeatureMenu class.
50 public class DynamicFeatureMenu
extends CompoundContributionItem
{
52 private ISelectionService selectionService
= EditorUtil
.getActivePart()
53 .getSite().getWorkbenchWindow().getSelectionService();
54 private IHandlerService handlerService
= (IHandlerService
) EditorUtil
55 .getService(IHandlerService
.class);
61 * org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
65 protected IContributionItem
[] getContributionItems() {
67 return new IContributionItem
[] { new ContributionItem() {
68 public void fill(Menu menu
, int index
) {
70 ISelection selection
= selectionService
71 .getSelection(DescriptiveViewPart
.ID
);
73 if (selection
instanceof IStructuredSelection
) {
74 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
75 Object selectedElement
= structuredSelection
78 if (selectedElement
instanceof TaxonDescription
) {
79 FeatureTree featureTree
= getFeatureTree((TaxonDescription
) selectedElement
);
81 for (FeatureNode childNode
: featureTree
83 if (!(childNode
.getFeature().getUuid()
84 .equals(UsageTermCollection
.uuidUseRecordFeature
))
85 && !(childNode
.getFeature().getUuid()
86 .equals(UsageTermCollection
.uuidUseSummaryFeature
))) {
87 createMenuItem(menu
, childNode
.getFeature());
90 } else if (selectedElement
instanceof FeatureNodeContainer
) {
91 FeatureNode featureNode
= ((FeatureNodeContainer
) selectedElement
)
94 // add the feature to the menu
95 createMenuItem(menu
, featureNode
.getFeature());
97 // add possible children to the menu
98 for (FeatureNode childNode
: featureNode
.getChildren()) {
99 createMenuItem(menu
, childNode
.getFeature());
101 } else if (selectedElement
instanceof DescriptionElementBase
) {
102 Feature feature
= ((DescriptionElementBase
) selectedElement
)
104 createMenuItem(menu
, feature
);
111 private void createMenuItem(Menu menu
, final Feature feature
) {
112 MenuItem menuItem
= new MenuItem(menu
, -1);
113 final Feature deproxiedFeature
= (Feature
) HibernateProxyHelper
117 menuItem
.setText(deproxiedFeature
.getLabel());
118 menuItem
.addSelectionListener(new SelectionListener() {
120 public void widgetDefaultSelected(SelectionEvent e
) {
123 public void widgetSelected(SelectionEvent ev
) {
124 Event event
= new Event();
125 event
.data
= deproxiedFeature
;
127 handlerService
.executeCommand(
128 CreateDescriptionElementOperation
.ID
, event
);
129 } catch (Exception e
) {
130 EditorUtil
.error(getClass(), e
);
138 * Retrieves the feature tree associated with the given description
140 * TODO as of now this is always the same thing because feature trees may
141 * not be associated to descriptions yet.
146 private FeatureTree
getFeatureTree(DescriptionBase description
) {
147 FeatureTree featureTree
= null;
149 // TODO change this to the feature tree associated with this taxon
151 if (description
.hasStructuredData()) {
152 featureTree
= PreferencesUtil
153 .getDefaultFeatureTreeForStructuredDescription();
155 featureTree
= PreferencesUtil
156 .getDefaultFeatureTreeForTextualDescription();
159 if (featureTree
== null) {
160 featureTree
= FeatureTree
.NewInstance(CdmStore
.getTermManager()
161 .getPreferredTerms(Feature
.class));