Project

General

Profile

Download (5.11 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.model.MessagingUtils;
37
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
38
import eu.etaxonomy.taxeditor.store.CdmStore;
39

    
40
/**
41
 * <p>
42
 * DynamicFeatureMenu class.
43
 * </p>
44
 *
45
 * @author n.hoffmann
46
 * @created 17.04.2009
47
 * @version 1.0
48
 */
49
public class DynamicFeatureMenu extends CompoundContributionItem {
50

    
51
	private final ISelectionService selectionService = EditorUtil.getActivePart()
52
			.getSite().getWorkbenchWindow().getSelectionService();
53
	private final IHandlerService handlerService = (IHandlerService) EditorUtil
54
			.getService(IHandlerService.class);
55

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

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

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

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

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

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

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

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

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

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

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

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

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

    
131
	}
132

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

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

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

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