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