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;
|
11
|
|
12
|
import java.util.HashMap;
|
13
|
import java.util.Map;
|
14
|
|
15
|
import org.eclipse.jface.action.Action;
|
16
|
import org.eclipse.jface.action.GroupMarker;
|
17
|
import org.eclipse.jface.action.IAction;
|
18
|
import org.eclipse.jface.action.IToolBarManager;
|
19
|
import org.eclipse.jface.action.MenuManager;
|
20
|
import org.eclipse.jface.resource.ImageDescriptor;
|
21
|
import org.eclipse.jface.viewers.ISelection;
|
22
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
23
|
import org.eclipse.jface.viewers.ITreeContentProvider;
|
24
|
import org.eclipse.jface.viewers.StructuredSelection;
|
25
|
import org.eclipse.jface.viewers.TreeNode;
|
26
|
import org.eclipse.jface.viewers.TreeViewer;
|
27
|
import org.eclipse.jface.viewers.Viewer;
|
28
|
import org.eclipse.swt.SWT;
|
29
|
import org.eclipse.swt.dnd.DND;
|
30
|
import org.eclipse.swt.dnd.Transfer;
|
31
|
import org.eclipse.swt.graphics.ImageData;
|
32
|
import org.eclipse.swt.widgets.Composite;
|
33
|
import org.eclipse.swt.widgets.Control;
|
34
|
import org.eclipse.swt.widgets.Menu;
|
35
|
import org.eclipse.swt.widgets.Tree;
|
36
|
import org.eclipse.ui.IWorkbenchActionConstants;
|
37
|
import org.eclipse.ui.IWorkbenchPart;
|
38
|
import org.eclipse.ui.internal.E4PartWrapper;
|
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.MultiPageTaxonEditor;
|
49
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
50
|
import eu.etaxonomy.taxeditor.editor.view.checklist.ChecklistEditor;
|
51
|
import eu.etaxonomy.taxeditor.editor.view.media.MediaViewPart;
|
52
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
|
53
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
|
54
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
55
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
56
|
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
|
57
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
58
|
import eu.etaxonomy.taxeditor.model.ImageResources;
|
59
|
import eu.etaxonomy.taxeditor.view.AbstractCdmEditorViewPart;
|
60
|
import eu.etaxonomy.taxeditor.view.detail.DetailsViewPart;
|
61
|
import eu.etaxonomy.taxeditor.view.supplementaldata.SupplementalDataViewPart;
|
62
|
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
|
63
|
|
64
|
/**
|
65
|
* @author n.hoffmann
|
66
|
* @created Jun 9, 2010
|
67
|
* @version 1.0
|
68
|
*/
|
69
|
public class DescriptiveViewPart extends AbstractCdmEditorViewPart implements IPartContentHasDetails, IPartContentHasSupplementalData {
|
70
|
|
71
|
/** Constant <code>ID="eu.etaxonomy.taxeditor.editor.view.desc"{trunked}</code> */
|
72
|
public static final String ID = "eu.etaxonomy.taxeditor.editor.view.descriptive"; //$NON-NLS-1$
|
73
|
|
74
|
protected TreeViewer viewer;
|
75
|
|
76
|
/**
|
77
|
* Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
|
78
|
* This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
|
79
|
*/
|
80
|
protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache = new HashMap<DescriptionBase<?>, FeatureNodeContainerTree>();
|
81
|
|
82
|
protected ToggleDescriptionAction showAllElementsAction;
|
83
|
|
84
|
protected ToggleDescriptionAction hideAllElementsAction;
|
85
|
|
86
|
protected int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
|
87
|
|
88
|
private DescriptiveContentProvider provider;
|
89
|
|
90
|
/** {@inheritDoc} */
|
91
|
@Override
|
92
|
public void createViewer(Composite parent) {
|
93
|
viewer = new TreeViewer(new Tree(parent, SWT.MULTI | SWT.H_SCROLL
|
94
|
| SWT.V_SCROLL | SWT.FULL_SELECTION));
|
95
|
provider = new DescriptiveContentProvider(featureNodeContainerCache);
|
96
|
viewer.setContentProvider(provider);
|
97
|
viewer.setLabelProvider(new DescriptiveLabelProvider());
|
98
|
viewer.setSorter(new DescriptiveViewerSorter());
|
99
|
viewer.setAutoExpandLevel(2);
|
100
|
Transfer[] transfers = new Transfer[] { DescriptionElementTransfer.getInstance() };
|
101
|
// viewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
|
102
|
// this));
|
103
|
// viewer.addDropSupport(dndOperations, transfers,
|
104
|
// new DescriptionElementDropAdapter(viewer));
|
105
|
|
106
|
// Propagate selection from viewer
|
107
|
getSite().setSelectionProvider(viewer);
|
108
|
|
109
|
showAllElementsAction = new ToggleDescriptionAction(false);
|
110
|
hideAllElementsAction = new ToggleDescriptionAction(true);
|
111
|
|
112
|
// Add context menu to tree
|
113
|
createMenu();
|
114
|
|
115
|
createToolbar();
|
116
|
|
117
|
}
|
118
|
|
119
|
/** {@inheritDoc} */
|
120
|
@Override
|
121
|
protected ISelection getInitialSelection() {
|
122
|
if(getEditor() != null){
|
123
|
return new StructuredSelection(getEditor().getEditorInput());
|
124
|
}
|
125
|
|
126
|
return super.getInitialSelection();
|
127
|
}
|
128
|
|
129
|
protected void createToolbar() {
|
130
|
IToolBarManager toolBarManager = getViewSite().getActionBars().getToolBarManager();
|
131
|
toolBarManager.add(showAllElementsAction);
|
132
|
toolBarManager.add(hideAllElementsAction);
|
133
|
}
|
134
|
|
135
|
/*
|
136
|
* TODO add to the views menu
|
137
|
*/
|
138
|
protected void createMenu(){
|
139
|
MenuManager menuManager = new MenuManager();
|
140
|
menuManager.setRemoveAllWhenShown(true);
|
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 workbenchPart, ISelection selection) {
|
158
|
Object partObject = workbenchPart;
|
159
|
// if(AbstractUtility.getActiveE4Editor() == null){
|
160
|
// showEmptyPage();
|
161
|
// return;
|
162
|
// }
|
163
|
|
164
|
|
165
|
if(partObject == this){
|
166
|
return;
|
167
|
}
|
168
|
|
169
|
Object wrappedPart = WorkbenchUtility.getE4WrappedPart(partObject);
|
170
|
|
171
|
if(wrappedPart!=null){
|
172
|
partObject = wrappedPart;
|
173
|
}
|
174
|
if(part == this){
|
175
|
return;
|
176
|
}
|
177
|
|
178
|
if (partObject instanceof DetailsViewPart || partObject instanceof SupplementalDataViewPart
|
179
|
|| partObject instanceof MediaViewPart) {
|
180
|
// do not show empty page as these views are also used to edit the
|
181
|
// description selected in this view
|
182
|
return;
|
183
|
}
|
184
|
else if(partObject instanceof ChecklistEditor){
|
185
|
if(selection instanceof StructuredSelection && ((IStructuredSelection) selection).getFirstElement() instanceof Taxon){
|
186
|
Taxon taxon = (Taxon)((IStructuredSelection) selection).getFirstElement();
|
187
|
selection = new StructuredSelection(taxon);
|
188
|
}
|
189
|
}
|
190
|
//check if it is a tree node selection
|
191
|
TreeNode treeNodeOfSelection = EditorUtil.getTreeNodeOfSelection(selection);
|
192
|
if(treeNodeOfSelection!=null){
|
193
|
selection = new StructuredSelection(treeNodeOfSelection.getValue());
|
194
|
}
|
195
|
if(selection instanceof IStructuredSelection){
|
196
|
Object firstElement = ((IStructuredSelection) selection).getFirstElement();
|
197
|
if (partObject instanceof MultiPageTaxonEditor){
|
198
|
if (firstElement != ((MultiPageTaxonEditor)partObject).getTaxon()){
|
199
|
showEmptyPage();
|
200
|
return;
|
201
|
}
|
202
|
}
|
203
|
|
204
|
if(firstElement instanceof IDescribable<?>
|
205
|
&& partObject instanceof IPartContentHasFactualData
|
206
|
&& !(firstElement instanceof SpecimenOrObservationBase && partObject instanceof BulkEditor)){
|
207
|
|
208
|
featureNodeContainerCache.clear();
|
209
|
showViewer(partObject, (IStructuredSelection) selection);
|
210
|
return;
|
211
|
}
|
212
|
}
|
213
|
showEmptyPage();
|
214
|
}
|
215
|
|
216
|
@Override
|
217
|
protected String getViewName(){
|
218
|
return Messages.DescriptiveViewPart_FACTUAL_DATA;
|
219
|
}
|
220
|
|
221
|
/**
|
222
|
*
|
223
|
* @author n.hoffmann
|
224
|
* @created May 28, 2010
|
225
|
* @version 1.0
|
226
|
*/
|
227
|
protected class ToggleDescriptionAction extends Action{
|
228
|
private final boolean expanded;
|
229
|
|
230
|
public ToggleDescriptionAction(boolean expand){
|
231
|
super(null, IAction.AS_PUSH_BUTTON);
|
232
|
expanded = expand;
|
233
|
setImageAndTooltip();
|
234
|
}
|
235
|
|
236
|
private void setImageAndTooltip(){
|
237
|
setImageDescriptor(new ImageDescriptor() {
|
238
|
@Override
|
239
|
public ImageData getImageData() {
|
240
|
setText(expanded ? Messages.DescriptiveViewPart_COLLAPSE_ALL : Messages.DescriptiveViewPart_EXPAND_ALL);
|
241
|
String resource = expanded ? ImageResources.COLLAPSE_ALL : ImageResources.EXPAND_ALL;
|
242
|
return ImageResources.getImage(resource).getImageData();
|
243
|
}
|
244
|
});
|
245
|
|
246
|
String toolTipText = expanded ? Messages.DescriptiveViewPart_COLLAPSE_ALL : Messages.DescriptiveViewPart_SHOW_ALL_DATA;
|
247
|
setToolTipText(toolTipText);
|
248
|
}
|
249
|
|
250
|
@Override
|
251
|
public void run() {
|
252
|
if(expanded){
|
253
|
viewer.collapseAll();
|
254
|
}else{
|
255
|
viewer.expandAll();
|
256
|
}
|
257
|
setImageAndTooltip();
|
258
|
}
|
259
|
}
|
260
|
|
261
|
/** {@inheritDoc} */
|
262
|
@Override
|
263
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
264
|
return super.postOperation(objectAffectedByOperation);
|
265
|
}
|
266
|
|
267
|
/** {@inheritDoc} */
|
268
|
@Override
|
269
|
public void changed(Object object) {
|
270
|
for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
|
271
|
containerTree.buildTree();
|
272
|
}
|
273
|
viewer.expandToLevel(2);
|
274
|
viewer.refresh();
|
275
|
|
276
|
if(object instanceof DescriptionElementBase){
|
277
|
DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
|
278
|
DescriptionBase description = descriptionElement.getInDescription();
|
279
|
FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
|
280
|
|
281
|
FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
|
282
|
viewer.expandToLevel(featureNodeContainer, 1);
|
283
|
|
284
|
}
|
285
|
|
286
|
if(object != null){
|
287
|
StructuredSelection selection = new StructuredSelection(object);
|
288
|
viewer.setSelection(selection, true);
|
289
|
}
|
290
|
if(part instanceof BulkEditor && !(object instanceof SpecimenOrObservationBase<?>)){
|
291
|
((BulkEditor) part).forceDirty();
|
292
|
IStructuredSelection selection = (IStructuredSelection) ((BulkEditor) part).getSelectionProvider().getSelection();
|
293
|
((BulkEditor) part).changed(selection.getFirstElement());
|
294
|
|
295
|
}
|
296
|
|
297
|
if (part instanceof E4PartWrapper){
|
298
|
part = WorkbenchUtility.getE4WrappedPart(part);
|
299
|
if (part instanceof IDirtyMarkable){
|
300
|
StructuredSelection selection = new StructuredSelection(object);
|
301
|
((IDirtyMarkable)part).changed(selection.getFirstElement());
|
302
|
}
|
303
|
}
|
304
|
|
305
|
|
306
|
super.changed(object);
|
307
|
}
|
308
|
|
309
|
/** {@inheritDoc} */
|
310
|
@Override
|
311
|
public Viewer getViewer() {
|
312
|
return viewer;
|
313
|
}
|
314
|
|
315
|
@Override
|
316
|
public boolean onComplete() {
|
317
|
return false;
|
318
|
}
|
319
|
|
320
|
public void toggleShowOnlyIndividualAssociations(){
|
321
|
provider.toggleShowOnlyIndividualAssociations();
|
322
|
viewer.refresh();
|
323
|
}
|
324
|
|
325
|
|
326
|
|
327
|
}
|