Project

General

Profile

Download (5.18 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
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.
8
 */
9

    
10
package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
11

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

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

    
39
/**
40
 *
41
 * @author n.hoffmann
42
 * @created 17.04.2009
43
 * @version 1.0
44
 */
45
public class DynamicFeatureMenu extends CompoundContributionItem {
46

    
47
	/** {@inheritDoc} */
48
	@Override
49
	protected IContributionItem[] getContributionItems() {
50

    
51
		return new IContributionItem[] { new ContributionItem() {
52
			@Override
53
            public void fill(Menu menu, int index) {
54
			    ISelectionService selectionService = TaxeditorEditorPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getSelectionService();
55

    
56
				ISelection selection = selectionService.getSelection(DescriptiveViewPart.ID);
57

    
58
				if (selection instanceof IStructuredSelection) {
59
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
60
					Object selectedElement = structuredSelection
61
							.getFirstElement();
62

    
63
					if (selectedElement instanceof DescriptionBase<?>) {
64
						FeatureTree featureTree = getFeatureTree((DescriptionBase<?>) selectedElement);
65

    
66
						for (FeatureNode childNode : featureTree.getRootChildren()) {
67
							createMenuItem(menu, childNode.getFeature());
68

    
69
						}
70
					} else if (selectedElement instanceof FeatureNodeContainer) {
71
						FeatureNode featureNode = ((FeatureNodeContainer) selectedElement)
72
								.getFeatureNode();
73

    
74
						// add the feature to the menu
75
						createMenuItem(menu, featureNode.getFeature());
76

    
77
						// add possible children to the menu
78
						for (FeatureNode childNode : featureNode.getChildNodes()) {
79
							createMenuItem(menu, childNode.getFeature());
80
						}
81
					} else if (selectedElement instanceof DescriptionElementBase) {
82
						Feature feature = ((DescriptionElementBase) selectedElement)
83
								.getFeature();
84
						createMenuItem(menu, feature);
85
					}
86
				}
87
			}
88
		} };
89
	}
90

    
91
	private void createMenuItem(Menu menu, final Feature feature) {
92
		MenuItem menuItem = new MenuItem(menu, -1);
93
		final Feature deproxiedFeature = HibernateProxyHelper.deproxy(feature, Feature.class);
94

    
95
		String label = deproxiedFeature.getLabel(PreferencesUtil.getGlobalLanguage());
96
		if (label == null){
97
			label = deproxiedFeature.getLabel();
98
		}
99
		if(label == null){
100
		    label = deproxiedFeature.generateTitle();
101
		}
102
		if(label == null){
103
		    label = deproxiedFeature.toString();
104
		}
105
		menuItem.setText(label);
106
		menuItem.addSelectionListener(new SelectionListener() {
107

    
108
			@Override
109
            public void widgetDefaultSelected(SelectionEvent e) {
110
			}
111

    
112
			@Override
113
            public void widgetSelected(SelectionEvent ev) {
114
				Event event = new Event();
115
				event.data = deproxiedFeature;
116
				try {
117
				    TaxeditorEditorPlugin.getDefault().getWorkbench().getService(IHandlerService.class).executeCommand(
118
							CreateDescriptionElementOperation.ID, event);
119
				} catch (Exception e) {
120
					MessagingUtils.error(getClass(), e);
121
				}
122
			}
123
		});
124

    
125
	}
126

    
127
	/**
128
	 * Retrieves the feature tree associated with the given description
129
	 *
130
	 * TODO as of now this is always the same thing because feature trees may
131
	 * not be associated to descriptions yet.
132
	 *
133
	 * @param description
134
	 * @return
135
	 */
136
	private FeatureTree getFeatureTree(DescriptionBase description) {
137
		FeatureTree featureTree = null;
138

    
139
		// TODO change this to the feature tree associated with this taxon
140
		// description
141
		if (description.hasStructuredData()) {
142
			featureTree = PreferencesUtil
143
					.getDefaultFeatureTreeForStructuredDescription();
144
		} else {
145
			featureTree = PreferencesUtil
146
					.getDefaultFeatureTreeForTextualDescription();
147
		}
148

    
149
		if (featureTree == null) {
150
			featureTree = FeatureTree.NewInstance(CdmStore.getTermManager()
151
					.getPreferredTerms(Feature.class));
152
		}
153

    
154
		return featureTree;
155
	}
156
}
(5-5/8)