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;
|
12
|
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Map;
|
15
|
|
16
|
import org.eclipse.jface.action.Action;
|
17
|
import org.eclipse.jface.action.GroupMarker;
|
18
|
import org.eclipse.jface.action.IAction;
|
19
|
import org.eclipse.jface.action.IToolBarManager;
|
20
|
import org.eclipse.jface.action.MenuManager;
|
21
|
import org.eclipse.jface.resource.ImageDescriptor;
|
22
|
import org.eclipse.jface.viewers.ISelection;
|
23
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
24
|
import org.eclipse.jface.viewers.ITreeContentProvider;
|
25
|
import org.eclipse.jface.viewers.StructuredSelection;
|
26
|
import org.eclipse.jface.viewers.TreeNode;
|
27
|
import org.eclipse.jface.viewers.TreeViewer;
|
28
|
import org.eclipse.jface.viewers.Viewer;
|
29
|
import org.eclipse.swt.SWT;
|
30
|
import org.eclipse.swt.dnd.DND;
|
31
|
import org.eclipse.swt.dnd.Transfer;
|
32
|
import org.eclipse.swt.graphics.ImageData;
|
33
|
import org.eclipse.swt.widgets.Composite;
|
34
|
import org.eclipse.swt.widgets.Control;
|
35
|
import org.eclipse.swt.widgets.Menu;
|
36
|
import org.eclipse.swt.widgets.Tree;
|
37
|
import org.eclipse.ui.IWorkbenchActionConstants;
|
38
|
import org.eclipse.ui.IWorkbenchPart;
|
39
|
|
40
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
41
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
42
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
43
|
import eu.etaxonomy.cdm.model.description.IDescribable;
|
44
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
45
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
46
|
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
|
47
|
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
|
48
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
49
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
|
50
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
|
51
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
52
|
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
|
53
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
54
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
55
|
import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
|
56
|
import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
|
57
|
import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
|
58
|
|
59
|
/**
|
60
|
* <p>DescriptiveViewPart class.</p>
|
61
|
*
|
62
|
* @author n.hoffmann
|
63
|
* @created Jun 9, 2010
|
64
|
* @version 1.0
|
65
|
*/
|
66
|
public class DescriptiveViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
|
67
|
|
68
|
/** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
|
69
|
public static final String ID = "eu.etaxonomy.taxeditor.editor.view.descriptive";
|
70
|
|
71
|
protected TreeViewer viewer;
|
72
|
|
73
|
/**
|
74
|
* Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
|
75
|
* This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
|
76
|
*/
|
77
|
protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache = new HashMap<DescriptionBase<?>, FeatureNodeContainerTree>();
|
78
|
|
79
|
protected ToggleDescriptionAction showAllElementsAction;
|
80
|
|
81
|
protected ToggleDescriptionAction hideAllElementsAction;
|
82
|
|
83
|
protected int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
|
84
|
|
85
|
/** {@inheritDoc} */
|
86
|
@Override
|
87
|
public void createViewer(Composite parent) {
|
88
|
viewer = new TreeViewer(new Tree(parent, SWT.MULTI | SWT.H_SCROLL
|
89
|
| SWT.V_SCROLL | SWT.FULL_SELECTION));
|
90
|
viewer.setContentProvider(new DescriptiveContentProvider(featureNodeContainerCache));
|
91
|
viewer.setLabelProvider(new DescriptiveLabelProvider());
|
92
|
viewer.setSorter(new DescriptiveViewerSorter());
|
93
|
viewer.setAutoExpandLevel(2);
|
94
|
Transfer[] transfers = new Transfer[] { DescriptionElementTransfer.getInstance() };
|
95
|
viewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
|
96
|
this));
|
97
|
viewer.addDropSupport(dndOperations, transfers,
|
98
|
new DescriptionElementDropAdapter(viewer));
|
99
|
|
100
|
// Propagate selection from viewer
|
101
|
getSite().setSelectionProvider(viewer);
|
102
|
|
103
|
showAllElementsAction = new ToggleDescriptionAction(false);
|
104
|
hideAllElementsAction = new ToggleDescriptionAction(true);
|
105
|
|
106
|
// Add context menu to tree
|
107
|
createMenu();
|
108
|
|
109
|
createToolbar();
|
110
|
|
111
|
// set initial input
|
112
|
// if(getEditor() != null){
|
113
|
// viewer.setInput(getEditor().getEditorInput());
|
114
|
// }
|
115
|
}
|
116
|
|
117
|
/* (non-Javadoc)
|
118
|
* @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getInitialSelection()
|
119
|
*/
|
120
|
/** {@inheritDoc} */
|
121
|
@Override
|
122
|
protected ISelection getInitialSelection() {
|
123
|
if(getEditor() != null){
|
124
|
return new StructuredSelection(getEditor().getEditorInput());
|
125
|
}
|
126
|
|
127
|
return super.getInitialSelection();
|
128
|
}
|
129
|
|
130
|
protected void createToolbar() {
|
131
|
IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
|
132
|
toolBarManager.add(showAllElementsAction);
|
133
|
toolBarManager.add(hideAllElementsAction);
|
134
|
}
|
135
|
|
136
|
/*
|
137
|
* TODO add to the views menu
|
138
|
*/
|
139
|
protected void createMenu(){
|
140
|
MenuManager menuManager = new MenuManager();
|
141
|
menuManager.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
|
142
|
|
143
|
getSite().registerContextMenu(menuManager, viewer);
|
144
|
|
145
|
// FIXME for some reason this is not working
|
146
|
menuManager.add(showAllElementsAction);
|
147
|
menuManager.add(hideAllElementsAction);
|
148
|
|
149
|
Control control = viewer.getControl();
|
150
|
Menu menu = menuManager.createContextMenu(control);
|
151
|
|
152
|
control.setMenu(menu);
|
153
|
}
|
154
|
|
155
|
/** {@inheritDoc} */
|
156
|
@Override
|
157
|
protected void selectionChanged_internal(IWorkbenchPart part, ISelection selection) {
|
158
|
if(AbstractUtility.getActiveEditor() == null){
|
159
|
showEmptyPage();
|
160
|
return;
|
161
|
}
|
162
|
|
163
|
if(part == this){
|
164
|
return;
|
165
|
}
|
166
|
if(part instanceof DetailsViewPart || part instanceof SupplementalDataViewPart){
|
167
|
// do not show empty page as these views are also used to edit the description selected in this view
|
168
|
return;
|
169
|
}
|
170
|
// unpackage TreeNode of DerivateView
|
171
|
else if(part instanceof DerivateView){
|
172
|
TreeNode treeNodeOfSelection = EditorUtil.getTreeNodeOfSelection(selection);
|
173
|
if(treeNodeOfSelection!=null){
|
174
|
selection = new StructuredSelection(treeNodeOfSelection.getValue());
|
175
|
}
|
176
|
}
|
177
|
else if(part instanceof ChecklistEditor){
|
178
|
if(selection instanceof StructuredSelection && ((IStructuredSelection) selection).getFirstElement() instanceof Taxon){
|
179
|
Taxon taxon = (Taxon)((IStructuredSelection) selection).getFirstElement();
|
180
|
selection = new StructuredSelection(taxon);
|
181
|
}
|
182
|
}
|
183
|
if(selection instanceof IStructuredSelection
|
184
|
&& ((IStructuredSelection) selection).getFirstElement() instanceof IDescribable<?>
|
185
|
&& part instanceof IPartContentHasFactualData){
|
186
|
featureNodeContainerCache.clear();
|
187
|
showViewer(part, (IStructuredSelection) selection);
|
188
|
return;
|
189
|
}
|
190
|
else{
|
191
|
showEmptyPage();
|
192
|
}
|
193
|
}
|
194
|
|
195
|
/**
|
196
|
*
|
197
|
* @author n.hoffmann
|
198
|
* @created May 28, 2010
|
199
|
* @version 1.0
|
200
|
*/
|
201
|
protected class ToggleDescriptionAction extends Action{
|
202
|
private final boolean expanded;
|
203
|
|
204
|
public ToggleDescriptionAction(boolean expand){
|
205
|
super(null, IAction.AS_PUSH_BUTTON);
|
206
|
expanded = expand;
|
207
|
setImageAndTooltip();
|
208
|
}
|
209
|
|
210
|
private void setImageAndTooltip(){
|
211
|
setImageDescriptor(new ImageDescriptor() {
|
212
|
@Override
|
213
|
public ImageData getImageData() {
|
214
|
setText(expanded ? "Collapse All" : "Expand All");
|
215
|
String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
|
216
|
return ImageResources.getImage(resource).getImageData();
|
217
|
}
|
218
|
});
|
219
|
|
220
|
String toolTipText = expanded ? "Collapse all" : "Show all factual data";
|
221
|
setToolTipText(toolTipText);
|
222
|
}
|
223
|
|
224
|
@Override
|
225
|
public void run() {
|
226
|
if(expanded){
|
227
|
viewer.collapseAll();
|
228
|
}else{
|
229
|
viewer.expandAll();
|
230
|
}
|
231
|
setImageAndTooltip();
|
232
|
}
|
233
|
}
|
234
|
|
235
|
/** {@inheritDoc} */
|
236
|
@Override
|
237
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
238
|
return super.postOperation(objectAffectedByOperation);
|
239
|
}
|
240
|
|
241
|
/** {@inheritDoc} */
|
242
|
@Override
|
243
|
public void changed(Object object) {
|
244
|
for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
|
245
|
containerTree.buildTree();
|
246
|
}
|
247
|
viewer.refresh();
|
248
|
|
249
|
if(object instanceof DescriptionElementBase){
|
250
|
DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
|
251
|
DescriptionBase description = descriptionElement.getInDescription();
|
252
|
FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
|
253
|
|
254
|
FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
|
255
|
viewer.expandToLevel(featureNodeContainer, 1);
|
256
|
|
257
|
}
|
258
|
|
259
|
if(object != null){
|
260
|
StructuredSelection selection = new StructuredSelection(object);
|
261
|
viewer.setSelection(selection, true);
|
262
|
}
|
263
|
|
264
|
super.changed(object);
|
265
|
}
|
266
|
|
267
|
/** {@inheritDoc} */
|
268
|
@Override
|
269
|
public Viewer getViewer() {
|
270
|
return viewer;
|
271
|
}
|
272
|
|
273
|
/**
|
274
|
* <p>onComplete</p>
|
275
|
*
|
276
|
* @return a boolean.
|
277
|
*/
|
278
|
@Override
|
279
|
public boolean onComplete() {
|
280
|
return false;
|
281
|
}
|
282
|
}
|