1 package eu
.etaxonomy
.taxeditor
.editor
.view
.uses
.handler
;
5 * Copyright (C) 2007 EDIT
6 * European Distributed Institute of Taxonomy
7 * http://www.e-taxonomy.eu
9 * The contents of this file are subject to the Mozilla Public License Version 1.1
10 * See LICENSE.TXT at the top of this package for the full license terms.
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
.view
.descriptive
.operation
.CreateDescriptionElementOperation
;
35 import eu
.etaxonomy
.taxeditor
.editor
.view
.uses
.UsesViewPart
;
36 import eu
.etaxonomy
.taxeditor
.editor
.view
.uses
.operation
.CreateTaxonUseOperation
;
37 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
38 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
39 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
40 import eu
.etaxonomy
.taxeditor
.store
.TermStore
;
43 * <p>DynamicFeatureMenu class.</p>
49 public class DynamicFeatureMenu
extends CompoundContributionItem
{
51 private ISelectionService selectionService
= EditorUtil
.getActivePart().getSite().getWorkbenchWindow().getSelectionService();
52 private IHandlerService handlerService
= (IHandlerService
) EditorUtil
.getService(IHandlerService
.class);
55 * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
59 protected IContributionItem
[] getContributionItems() {
61 return new IContributionItem
[] {
62 new ContributionItem() {
63 public void fill(Menu menu
, int index
){
65 ISelection selection
= selectionService
.getSelection(UsesViewPart
.ID
);
67 if(selection
instanceof IStructuredSelection
){
68 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
69 Object selectedElement
= structuredSelection
.getFirstElement();
71 if(selectedElement
instanceof TaxonDescription
){
72 FeatureTree featureTree
= getFeatureTree((TaxonDescription
) selectedElement
);
74 for(FeatureNode childNode
: featureTree
.getRootChildren()){
75 createMenuItem(menu
, childNode
.getFeature());
78 else if(selectedElement
instanceof FeatureNodeContainer
){
79 FeatureNode featureNode
= ((FeatureNodeContainer
) selectedElement
).getFeatureNode();
81 // add the feature to the menu
82 createMenuItem(menu
, featureNode
.getFeature());
84 // add possible children to the menu
85 for(FeatureNode childNode
: featureNode
.getChildren()){
86 createMenuItem(menu
, childNode
.getFeature());
89 else if(selectedElement
instanceof DescriptionElementBase
){
90 Feature feature
= ((DescriptionElementBase
) selectedElement
).getFeature();
91 createMenuItem(menu
, feature
);
99 private void createMenuItem(Menu menu
, final Feature feature
) {
100 MenuItem menuItem
= new MenuItem(menu
, -1);
101 final Feature deproxiedFeature
= (Feature
) HibernateProxyHelper
.deproxy(feature
);
102 menuItem
.setText(deproxiedFeature
.getLabel());
103 menuItem
.addSelectionListener(new SelectionListener(){
105 public void widgetDefaultSelected(SelectionEvent e
) {}
107 public void widgetSelected(SelectionEvent ev
) {
108 Event event
= new Event();
109 event
.data
= deproxiedFeature
;
111 //handlerService.executeCommand(CreateTaxonUseOperation.ID, event);
112 } catch (Exception e
) {
113 EditorUtil
.error(getClass(), e
);
120 * Retrieves the feature tree associated with the given description
122 * TODO as of now this is always the same thing because feature trees may not be associated
123 * to descriptions yet.
128 private FeatureTree
getFeatureTree(DescriptionBase description
){
129 FeatureTree featureTree
= null;
131 // TODO change this to the feature tree associated with this taxon description
132 if (description
.hasStructuredData()){
133 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
135 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForTextualDescription();
138 if(featureTree
== null){
139 //featureTree = FeatureTree.NewInstance(CdmStore.getTermManager().getPreferredFeatures());
140 featureTree
= FeatureTree
.NewInstance(CdmStore
.getTermManager().getPreferredTerms(Feature
.class));