Project

General

Profile

Download (5.28 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
 * <p>
41
 * DynamicFeatureMenu class.
42
 * </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 final ISelectionService selectionService = TaxeditorEditorPlugin.getDefault().getWorkbench().getService(ISelectionService.class);
51
	private final IHandlerService handlerService = TaxeditorEditorPlugin.getDefault().getWorkbench().getService(IHandlerService.class);
52

    
53
	/*
54
	 * (non-Javadoc)
55
	 *
56
	 * @see
57
	 * org.eclipse.ui.actions.CompoundContributionItem#getContributionItems()
58
	 */
59
	/** {@inheritDoc} */
60
	@Override
61
	protected IContributionItem[] getContributionItems() {
62

    
63
		return new IContributionItem[] { new ContributionItem() {
64
			@Override
65
            public void fill(Menu menu, int index) {
66

    
67
				ISelection selection = selectionService
68
						.getSelection(DescriptiveViewPart.ID);
69

    
70
				if (selection instanceof IStructuredSelection) {
71
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
72
					Object selectedElement = structuredSelection
73
							.getFirstElement();
74

    
75
					if (selectedElement instanceof DescriptionBase<?>) {
76
						FeatureTree featureTree = getFeatureTree((DescriptionBase<?>) selectedElement);
77

    
78
						for (FeatureNode childNode : featureTree.getRootChildren()) {
79
							createMenuItem(menu, childNode.getFeature());
80

    
81
						}
82
					} else if (selectedElement instanceof FeatureNodeContainer) {
83
						FeatureNode featureNode = ((FeatureNodeContainer) selectedElement)
84
								.getFeatureNode();
85

    
86
						// add the feature to the menu
87
						createMenuItem(menu, featureNode.getFeature());
88

    
89
						// add possible children to the menu
90
						for (FeatureNode childNode : featureNode.getChildNodes()) {
91
							createMenuItem(menu, childNode.getFeature());
92
						}
93
					} else if (selectedElement instanceof DescriptionElementBase) {
94
						Feature feature = ((DescriptionElementBase) selectedElement)
95
								.getFeature();
96
						createMenuItem(menu, feature);
97
					}
98
				}
99
			}
100
		} };
101
	}
102

    
103
	private void createMenuItem(Menu menu, final Feature feature) {
104
		MenuItem menuItem = new MenuItem(menu, -1);
105
		final Feature deproxiedFeature = HibernateProxyHelper.deproxy(feature, Feature.class);
106

    
107
		String label = deproxiedFeature.getLabel(PreferencesUtil.getGlobalLanguage());
108
		if (label == null){
109
			menuItem.setText(deproxiedFeature.getLabel());
110
		}else{
111
			menuItem.setText(label);
112
		}
113
		menuItem.addSelectionListener(new SelectionListener() {
114

    
115
			@Override
116
            public void widgetDefaultSelected(SelectionEvent e) {
117
			}
118

    
119
			@Override
120
            public void widgetSelected(SelectionEvent ev) {
121
				Event event = new Event();
122
				event.data = deproxiedFeature;
123
				try {
124
					handlerService.executeCommand(
125
							CreateDescriptionElementOperation.ID, event);
126
				} catch (Exception e) {
127
					MessagingUtils.error(getClass(), e);
128
				}
129
			}
130
		});
131

    
132
	}
133

    
134
	/**
135
	 * Retrieves the feature tree associated with the given description
136
	 *
137
	 * TODO as of now this is always the same thing because feature trees may
138
	 * not be associated to descriptions yet.
139
	 *
140
	 * @param description
141
	 * @return
142
	 */
143
	private FeatureTree getFeatureTree(DescriptionBase description) {
144
		FeatureTree featureTree = null;
145

    
146
		// TODO change this to the feature tree associated with this taxon
147
		// description
148
		if (description.hasStructuredData()) {
149
			featureTree = PreferencesUtil
150
					.getDefaultFeatureTreeForStructuredDescription();
151
		} else {
152
			featureTree = PreferencesUtil
153
					.getDefaultFeatureTreeForTextualDescription();
154
		}
155

    
156
		if (featureTree == null) {
157
			featureTree = FeatureTree.NewInstance(CdmStore.getTermManager()
158
					.getPreferredTerms(Feature.class));
159
		}
160

    
161
		return featureTree;
162
	}
163
}
(5-5/8)