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
.view
.descriptive
.DescriptiveViewPart
;
35 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.CreateDescriptionElementOperation
;
36 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
37 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
38 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
41 * <p>DynamicFeatureMenu class.</p>
47 public class DynamicFeatureMenu
extends CompoundContributionItem
{
49 private ISelectionService selectionService
= EditorUtil
.getActivePart().getSite().getWorkbenchWindow().getSelectionService();
50 private IHandlerService handlerService
= (IHandlerService
) EditorUtil
.getService(IHandlerService
.class);
53 * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
57 protected IContributionItem
[] getContributionItems() {
59 return new IContributionItem
[] {
60 new ContributionItem() {
61 public void fill(Menu menu
, int index
){
63 ISelection selection
= selectionService
.getSelection(DescriptiveViewPart
.ID
);
65 if(selection
instanceof IStructuredSelection
){
66 IStructuredSelection structuredSelection
= (IStructuredSelection
) selection
;
67 Object selectedElement
= structuredSelection
.getFirstElement();
69 if(selectedElement
instanceof TaxonDescription
){
70 FeatureTree featureTree
= getFeatureTree((TaxonDescription
) selectedElement
);
72 for(FeatureNode childNode
: featureTree
.getRootChildren()){
73 createMenuItem(menu
, childNode
.getFeature());
76 else if(selectedElement
instanceof FeatureNodeContainer
){
77 FeatureNode featureNode
= ((FeatureNodeContainer
) selectedElement
).getFeatureNode();
79 // add the feature to the menu
80 createMenuItem(menu
, featureNode
.getFeature());
82 // add possible children to the menu
83 for(FeatureNode childNode
: featureNode
.getChildren()){
84 createMenuItem(menu
, childNode
.getFeature());
87 else if(selectedElement
instanceof DescriptionElementBase
){
88 Feature feature
= ((DescriptionElementBase
) selectedElement
).getFeature();
89 createMenuItem(menu
, feature
);
97 private void createMenuItem(Menu menu
, final Feature feature
) {
98 MenuItem menuItem
= new MenuItem(menu
, -1);
99 final Feature deproxiedFeature
= (Feature
) HibernateProxyHelper
.deproxy(feature
);
100 menuItem
.setText(deproxiedFeature
.getLabel());
101 menuItem
.addSelectionListener(new SelectionListener(){
103 public void widgetDefaultSelected(SelectionEvent e
) {}
105 public void widgetSelected(SelectionEvent ev
) {
106 Event event
= new Event();
107 event
.data
= deproxiedFeature
;
109 handlerService
.executeCommand(CreateDescriptionElementOperation
.ID
, event
);
110 } catch (Exception e
) {
111 EditorUtil
.error(getClass(), e
);
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 not be associated
121 * 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 description
130 if (description
.hasStructuredData()){
131 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
133 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForTextualDescription();
136 if(featureTree
== null){
137 featureTree
= FeatureTree
.NewInstance(CdmStore
.getTermManager().getPreferredTerms(Feature
.class));