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.EditorUtil;
|
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 = 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 = HibernateProxyHelper.deproxy(feature, Feature.class);
|
108
|
|
109
|
String label = deproxiedFeature.getLabel(PreferencesUtil.getGlobalLanguage());
|
110
|
if (label == null){
|
111
|
menuItem.setText(deproxiedFeature.getLabel());
|
112
|
}else{
|
113
|
menuItem.setText(label);
|
114
|
}
|
115
|
menuItem.addSelectionListener(new SelectionListener() {
|
116
|
|
117
|
@Override
|
118
|
public void widgetDefaultSelected(SelectionEvent e) {
|
119
|
}
|
120
|
|
121
|
@Override
|
122
|
public void widgetSelected(SelectionEvent ev) {
|
123
|
Event event = new Event();
|
124
|
event.data = deproxiedFeature;
|
125
|
try {
|
126
|
handlerService.executeCommand(
|
127
|
CreateDescriptionElementOperation.ID, event);
|
128
|
} catch (Exception e) {
|
129
|
MessagingUtils.error(getClass(), e);
|
130
|
}
|
131
|
}
|
132
|
});
|
133
|
|
134
|
}
|
135
|
|
136
|
/**
|
137
|
* Retrieves the feature tree associated with the given description
|
138
|
*
|
139
|
* TODO as of now this is always the same thing because feature trees may
|
140
|
* not be associated to descriptions yet.
|
141
|
*
|
142
|
* @param description
|
143
|
* @return
|
144
|
*/
|
145
|
private FeatureTree getFeatureTree(DescriptionBase description) {
|
146
|
FeatureTree featureTree = null;
|
147
|
|
148
|
// TODO change this to the feature tree associated with this taxon
|
149
|
// description
|
150
|
if (description.hasStructuredData()) {
|
151
|
featureTree = PreferencesUtil
|
152
|
.getDefaultFeatureTreeForStructuredDescription();
|
153
|
} else {
|
154
|
featureTree = PreferencesUtil
|
155
|
.getDefaultFeatureTreeForTextualDescription();
|
156
|
}
|
157
|
|
158
|
if (featureTree == null) {
|
159
|
featureTree = FeatureTree.NewInstance(CdmStore.getTermManager()
|
160
|
.getPreferredTerms(Feature.class));
|
161
|
}
|
162
|
|
163
|
return featureTree;
|
164
|
}
|
165
|
}
|