Project

General

Profile

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

    
11
package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
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.taxeditor.editor.EditorUtil;
33
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
34
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
35
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
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 = EditorUtil.getActivePart()
51
			.getSite().getWorkbenchWindow().getSelectionService();
52
	private final IHandlerService handlerService = (IHandlerService) EditorUtil
53
			.getService(IHandlerService.class);
54

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

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

    
69
				ISelection selection = selectionService
70
						.getSelection(DescriptiveViewPart.ID);
71

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

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

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

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

    
88
						// add the feature to the menu
89
						createMenuItem(menu, featureNode.getFeature());
90

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

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

    
110
		menuItem.setText(deproxiedFeature.getLabel());
111
		menuItem.addSelectionListener(new SelectionListener() {
112

    
113
			@Override
114
            public void widgetDefaultSelected(SelectionEvent e) {
115
			}
116

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

    
130
	}
131

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

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

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

    
159
		return featureTree;
160
	}
161
}
(5-5/7)