Project

General

Profile

Download (5.22 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.editor.view.uses.handler;
2

    
3
//$Id$
4
/**
5
* Copyright (C) 2007 EDIT
6
* European Distributed Institute of Taxonomy 
7
* http://www.e-taxonomy.eu
8
* 
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.
11
*/
12

    
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;
25

    
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

    
41
/**
42
 * <p>DynamicFeatureMenu class.</p>
43
 *
44
 * @author n.hoffmann
45
 * @created 17.04.2009
46
 * @version 1.0
47
 */
48
public class DynamicFeatureMenu extends CompoundContributionItem {
49
	
50
	private ISelectionService selectionService = EditorUtil.getActivePart().getSite().getWorkbenchWindow().getSelectionService();
51
	private IHandlerService handlerService = (IHandlerService) EditorUtil.getService(IHandlerService.class);
52
	
53
	/* (non-Javadoc)
54
	 * @see org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
55
	 */
56
	/** {@inheritDoc} */
57
	@Override
58
	protected IContributionItem[] getContributionItems() {
59
		
60
		return new IContributionItem[] {
61
				new ContributionItem() {
62
					public void fill(Menu menu, int index){
63
								
64
						ISelection selection = selectionService.getSelection(UsesViewPart.ID);
65
						
66
						if(selection instanceof IStructuredSelection){
67
							IStructuredSelection structuredSelection = (IStructuredSelection) selection;
68
							Object selectedElement = structuredSelection.getFirstElement();
69
							
70
							if(selectedElement instanceof TaxonDescription){
71
								FeatureTree featureTree = getFeatureTree((TaxonDescription) selectedElement);
72
								
73
								for(FeatureNode childNode : featureTree.getRootChildren()){
74
									createMenuItem(menu, childNode.getFeature());
75
								}
76
							}
77
							else if(selectedElement instanceof FeatureNodeContainer){
78
								FeatureNode featureNode = ((FeatureNodeContainer) selectedElement).getFeatureNode();
79
								
80
								// add the feature to the menu
81
								createMenuItem(menu, featureNode.getFeature());
82
								
83
								// add possible children to the menu
84
								for(FeatureNode childNode : featureNode.getChildren()){
85
									createMenuItem(menu, childNode.getFeature());
86
								}
87
							}
88
							else if(selectedElement instanceof DescriptionElementBase){
89
								Feature feature = ((DescriptionElementBase) selectedElement).getFeature();
90
								createMenuItem(menu, feature);
91
							}
92
						}	
93
					}
94
				}
95
		};
96
	}	
97
	
98
	private void createMenuItem(Menu menu, final Feature feature) {
99
		MenuItem menuItem = new MenuItem(menu, -1);
100
		final Feature deproxiedFeature = (Feature) HibernateProxyHelper.deproxy(feature);
101
		menuItem.setText(deproxiedFeature.getLabel());
102
		menuItem.addSelectionListener(new SelectionListener(){
103

    
104
			public void widgetDefaultSelected(SelectionEvent e) {}
105

    
106
			public void widgetSelected(SelectionEvent ev) {
107
				Event event = new Event();
108
				event.data = deproxiedFeature;
109
				try {
110
					//handlerService.executeCommand(CreateTaxonUseOperation.ID, event);
111
				} catch (Exception e) {
112
					EditorUtil.error(getClass(), e);
113
				}
114
			}				
115
		});
116
	}
117
	
118
	/**
119
	 * Retrieves the feature tree associated with the given description
120
	 * 
121
	 * TODO as of now this is always the same thing because feature trees may not be associated 
122
	 * to descriptions yet.
123
	 * 
124
	 * @param description
125
	 * @return
126
	 */
127
	private FeatureTree getFeatureTree(DescriptionBase description){
128
		FeatureTree featureTree = null;
129
		
130
		// TODO change this to the feature tree associated with this taxon description
131
		if (description.hasStructuredData()){					
132
			featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
133
		}else{
134
			featureTree = PreferencesUtil.getDefaultFeatureTreeForTextualDescription();
135
		}
136
		
137
		if(featureTree == null){
138
			featureTree = FeatureTree.NewInstance(CdmStore.getTermManager().getPreferredFeatures());
139
		}
140
		
141
		return featureTree;
142
	}
143
}
144

    
(2-2/2)