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
|
menuItem.setText(deproxiedFeature.getLabel());
|
98
|
}else{
|
99
|
menuItem.setText(label);
|
100
|
}
|
101
|
menuItem.addSelectionListener(new SelectionListener() {
|
102
|
|
103
|
@Override
|
104
|
public void widgetDefaultSelected(SelectionEvent e) {
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public void widgetSelected(SelectionEvent ev) {
|
109
|
Event event = new Event();
|
110
|
event.data = deproxiedFeature;
|
111
|
try {
|
112
|
TaxeditorEditorPlugin.getDefault().getWorkbench().getService(IHandlerService.class).executeCommand(
|
113
|
CreateDescriptionElementOperation.ID, event);
|
114
|
} catch (Exception e) {
|
115
|
MessagingUtils.error(getClass(), e);
|
116
|
}
|
117
|
}
|
118
|
});
|
119
|
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Retrieves the feature tree associated with the given description
|
124
|
*
|
125
|
* TODO as of now this is always the same thing because feature trees may
|
126
|
* not be associated to descriptions yet.
|
127
|
*
|
128
|
* @param description
|
129
|
* @return
|
130
|
*/
|
131
|
private FeatureTree getFeatureTree(DescriptionBase description) {
|
132
|
FeatureTree featureTree = null;
|
133
|
|
134
|
// TODO change this to the feature tree associated with this taxon
|
135
|
// description
|
136
|
if (description.hasStructuredData()) {
|
137
|
featureTree = PreferencesUtil
|
138
|
.getDefaultFeatureTreeForStructuredDescription();
|
139
|
} else {
|
140
|
featureTree = PreferencesUtil
|
141
|
.getDefaultFeatureTreeForTextualDescription();
|
142
|
}
|
143
|
|
144
|
if (featureTree == null) {
|
145
|
featureTree = FeatureTree.NewInstance(CdmStore.getTermManager()
|
146
|
.getPreferredTerms(Feature.class));
|
147
|
}
|
148
|
|
149
|
return featureTree;
|
150
|
}
|
151
|
}
|