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