1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2017 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
|
package eu.etaxonomy.taxeditor.editor.view.descriptive.e4;
|
11
|
|
12
|
import java.util.Arrays;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
import javax.annotation.PostConstruct;
|
18
|
import javax.inject.Inject;
|
19
|
|
20
|
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
|
21
|
import org.eclipse.e4.core.contexts.IEclipseContext;
|
22
|
import org.eclipse.e4.core.di.annotations.Optional;
|
23
|
import org.eclipse.e4.ui.di.UIEventTopic;
|
24
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
25
|
import org.eclipse.e4.ui.services.EMenuService;
|
26
|
import org.eclipse.jface.viewers.AbstractTreeViewer;
|
27
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
28
|
import org.eclipse.jface.viewers.ITreeContentProvider;
|
29
|
import org.eclipse.jface.viewers.StructuredSelection;
|
30
|
import org.eclipse.jface.viewers.TreeNode;
|
31
|
import org.eclipse.jface.viewers.TreeViewer;
|
32
|
import org.eclipse.jface.viewers.Viewer;
|
33
|
import org.eclipse.swt.SWT;
|
34
|
import org.eclipse.swt.dnd.DND;
|
35
|
import org.eclipse.swt.dnd.Transfer;
|
36
|
import org.eclipse.swt.widgets.Composite;
|
37
|
import org.eclipse.swt.widgets.Tree;
|
38
|
|
39
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
40
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
41
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
42
|
import eu.etaxonomy.cdm.model.description.IDescribable;
|
43
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
44
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
45
|
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
|
46
|
import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
|
47
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
48
|
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
|
49
|
import eu.etaxonomy.taxeditor.editor.view.concept.e4.ConceptViewPartE4;
|
50
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptionElementDragListener;
|
51
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptionElementDropAdapter;
|
52
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptionElementTransfer;
|
53
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveContentProvider;
|
54
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveLabelProvider;
|
55
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewerSorter;
|
56
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.NameDescriptionFilter;
|
57
|
import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
|
58
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
59
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
|
60
|
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
|
61
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
62
|
import eu.etaxonomy.taxeditor.model.IPartContentHasFactualData;
|
63
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
64
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
65
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
66
|
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
|
67
|
import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
|
68
|
import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
|
69
|
import eu.etaxonomy.taxeditor.workbench.part.ICollapsableExpandable;
|
70
|
|
71
|
/**
|
72
|
* @author pplitzner
|
73
|
* @date 15.08.2017
|
74
|
*/
|
75
|
public class FactualDataPartE4 extends AbstractCdmEditorPartE4
|
76
|
implements IPartContentHasDetails, IPartContentHasSupplementalData, ICdmEntitySessionEnabled,
|
77
|
ICollapsableExpandable {
|
78
|
|
79
|
/**
|
80
|
* Maps {@link FeatureNodeContainerTree} to their corresponding {@link TaxonDescritpion}.<br>
|
81
|
* This serves as input for the {@link ITreeContentProvider} of the {@link TreeViewer}
|
82
|
*/
|
83
|
protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache = new HashMap<DescriptionBase<?>, FeatureNodeContainerTree>();
|
84
|
|
85
|
protected int dndOperations = DND.DROP_COPY | DND.DROP_MOVE;
|
86
|
|
87
|
private DescriptiveContentProvider provider;
|
88
|
|
89
|
@PostConstruct
|
90
|
public void create(Composite parent, EMenuService menuService, MPart thisPart,
|
91
|
IEclipseContext context) {
|
92
|
TreeViewer treeViewer = new TreeViewer(new Tree(parent, SWT.H_SCROLL
|
93
|
| SWT.V_SCROLL | SWT.FULL_SELECTION | SWT.MULTI));
|
94
|
|
95
|
this.thisPart = thisPart;
|
96
|
|
97
|
provider = new DescriptiveContentProvider(featureNodeContainerCache);
|
98
|
treeViewer.setContentProvider(provider);
|
99
|
treeViewer.setLabelProvider(new DescriptiveLabelProvider());
|
100
|
treeViewer.setSorter(new DescriptiveViewerSorter());
|
101
|
treeViewer.setFilters(new NameDescriptionFilter());
|
102
|
treeViewer.setAutoExpandLevel(2);
|
103
|
Transfer[] transfers = new Transfer[] { DescriptionElementTransfer.getInstance() };
|
104
|
treeViewer.addDragSupport(dndOperations, transfers, new DescriptionElementDragListener(
|
105
|
this));
|
106
|
|
107
|
|
108
|
viewer = treeViewer;
|
109
|
|
110
|
// Propagate selection from viewer
|
111
|
selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
|
112
|
viewer.addSelectionChangedListener(selectionChangedListener);
|
113
|
DescriptionElementDropAdapter dropListener = new DescriptionElementDropAdapter(viewer);
|
114
|
ContextInjectionFactory.inject(dropListener, context);
|
115
|
|
116
|
treeViewer.addDropSupport(dndOperations, transfers,
|
117
|
dropListener);
|
118
|
//create context menu
|
119
|
menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.factualDataView");
|
120
|
|
121
|
}
|
122
|
|
123
|
@Override
|
124
|
public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
|
125
|
if(activePart==thisPart){
|
126
|
return;
|
127
|
}
|
128
|
|
129
|
Object partObject = createPartObject(activePart);
|
130
|
|
131
|
|
132
|
|
133
|
if ((partObject instanceof DetailsPartE4 && !(selection instanceof TaxonName))|| partObject instanceof SupplementalDataPartE4
|
134
|
|| partObject instanceof MediaViewPartE4 || partObject instanceof ConceptViewPartE4 || ((partObject instanceof AbstractCdmEditorPartE4) &&(((AbstractCdmEditorPartE4)partObject).getViewer().getInput() instanceof DescriptionElementBase))) {
|
135
|
// do not show empty page as these views are also used to edit the
|
136
|
// description selected in this view
|
137
|
return;
|
138
|
}
|
139
|
|
140
|
|
141
|
if(partObject instanceof IPartContentHasFactualData ){
|
142
|
|
143
|
if (!viewer.getControl().isDisposed()){
|
144
|
viewer.getControl().setEnabled(isEnabled());
|
145
|
|
146
|
}
|
147
|
|
148
|
IStructuredSelection structuredSelection = createSelection(selection);
|
149
|
if(structuredSelection==null || structuredSelection.isEmpty()){
|
150
|
showEmptyPage();
|
151
|
return;
|
152
|
}
|
153
|
|
154
|
Object firstElement = structuredSelection.getFirstElement();
|
155
|
if (partObject instanceof TaxonNameEditorE4){
|
156
|
if (firstElement != ((TaxonNameEditorE4)partObject).getTaxon()){
|
157
|
showEmptyPage();
|
158
|
return;
|
159
|
}
|
160
|
}
|
161
|
|
162
|
if(firstElement instanceof TreeNode){
|
163
|
firstElement = ((TreeNode) firstElement).getValue();
|
164
|
}
|
165
|
if(firstElement instanceof IDescribable<?>
|
166
|
&& partObject instanceof IPartContentHasFactualData
|
167
|
&& !(firstElement instanceof SpecimenOrObservationBase && partObject instanceof BulkEditorE4)){
|
168
|
// && !(firstElement instanceof TaxonName && partObject instanceof BulkEditorE4)){
|
169
|
if (firstElement instanceof TaxonName ){
|
170
|
thisPart.setLabel(Messages.NameFactsDataView_Lable);
|
171
|
}else{
|
172
|
thisPart.setLabel(Messages.FactualDataView_Lable);
|
173
|
}
|
174
|
// featureNodeContainerCache.clear();
|
175
|
showViewer(structuredSelection, activePart, viewer);
|
176
|
if ( viewer.getControl() != null &&!this.viewer.getControl().isDisposed()){
|
177
|
this.viewer.getControl().setEnabled(isEnabled());
|
178
|
}
|
179
|
|
180
|
return;
|
181
|
}
|
182
|
}
|
183
|
showEmptyPage();
|
184
|
return;
|
185
|
}
|
186
|
|
187
|
@Override
|
188
|
protected String getViewName(){
|
189
|
return Messages.DescriptiveViewPart_FACTUAL_DATA;
|
190
|
}
|
191
|
|
192
|
@Override
|
193
|
public void changed(Object object) {
|
194
|
Object[] expandedObjects = null;
|
195
|
if (!viewer.getControl().isDisposed()){
|
196
|
expandedObjects = ((TreeViewer)viewer).getExpandedElements();
|
197
|
}else{
|
198
|
return;
|
199
|
}
|
200
|
for(FeatureNodeContainerTree containerTree : featureNodeContainerCache.values()){
|
201
|
containerTree.buildTree();
|
202
|
}
|
203
|
|
204
|
viewer.refresh();
|
205
|
super.changed(object);
|
206
|
((TreeViewer) viewer).setExpandedElements(expandedObjects);
|
207
|
if(object instanceof DescriptionElementBase){
|
208
|
DescriptionElementBase descriptionElement = (DescriptionElementBase) object;
|
209
|
DescriptionBase description = descriptionElement.getInDescription();
|
210
|
FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
|
211
|
FeatureNodeContainer featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
|
212
|
if (featureNodeContainer == null){
|
213
|
containerTree = new FeatureNodeContainerTree(description, provider.getFeatureTree(description));
|
214
|
featureNodeContainerCache.put(description, containerTree);
|
215
|
featureNodeContainer = containerTree.getFeatureNodeContainerForDescriptionElement(descriptionElement);
|
216
|
}
|
217
|
((AbstractTreeViewer) viewer).expandToLevel(featureNodeContainer, 2);
|
218
|
StructuredSelection selection = new StructuredSelection(object);
|
219
|
viewer.setSelection(selection, true);
|
220
|
}
|
221
|
|
222
|
|
223
|
|
224
|
}
|
225
|
|
226
|
@Inject
|
227
|
@Optional
|
228
|
private void updateView(@UIEventTopic(WorkbenchEventConstants.SAVE_TAXON)boolean refresh){
|
229
|
if(refresh){
|
230
|
Object[] expandedObjects = ((TreeViewer)viewer).getExpandedElements();
|
231
|
viewer.refresh();
|
232
|
((TreeViewer)viewer).collapseAll();
|
233
|
if (expandedObjects.length>0){
|
234
|
((TreeViewer)viewer).setExpandedElements(expandedObjects);
|
235
|
}
|
236
|
}
|
237
|
}
|
238
|
|
239
|
|
240
|
@Inject
|
241
|
@Optional
|
242
|
private void updateView(@UIEventTopic(WorkbenchEventConstants.CURRENT_ACTIVE_EDITOR)ITaxonEditor editor){
|
243
|
if(editor == null){
|
244
|
featureNodeContainerCache.clear();
|
245
|
}
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* {@inheritDoc}
|
250
|
*/
|
251
|
@Override
|
252
|
public void collapse() {
|
253
|
if(viewer instanceof TreeViewer) {
|
254
|
((TreeViewer) viewer).collapseAll();
|
255
|
}
|
256
|
}
|
257
|
|
258
|
/**
|
259
|
* {@inheritDoc}
|
260
|
*/
|
261
|
@Override
|
262
|
public void expand() {
|
263
|
if(viewer instanceof TreeViewer) {
|
264
|
((TreeViewer) viewer).expandAll();
|
265
|
}
|
266
|
}
|
267
|
|
268
|
public void toggleShowOnlyIndividualAssociations(){
|
269
|
provider.toggleShowOnlyIndividualAssociations();
|
270
|
viewer.refresh();
|
271
|
}
|
272
|
|
273
|
@Override
|
274
|
public ICdmEntitySession getCdmEntitySession() {
|
275
|
if(selectionProvidingPart != null && selectionProvidingPart instanceof ICdmEntitySessionEnabled) {
|
276
|
return ((ICdmEntitySessionEnabled) selectionProvidingPart).getCdmEntitySession();
|
277
|
}
|
278
|
return null;
|
279
|
}
|
280
|
|
281
|
@Override
|
282
|
public List<? extends CdmBase> getRootEntities() {
|
283
|
return Arrays.asList((CdmBase)getViewer().getInput());
|
284
|
}
|
285
|
@Override
|
286
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
287
|
return null;
|
288
|
}
|
289
|
|
290
|
@Override
|
291
|
protected void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
|
292
|
Object[] expandedObjects = null;
|
293
|
if (!((TreeViewer)this.getViewer()).getControl().isDisposed()){
|
294
|
expandedObjects = ((TreeViewer)this.getViewer()).getExpandedElements();
|
295
|
}
|
296
|
super.showViewer(selection, activePart, viewer);
|
297
|
if (expandedObjects == null || expandedObjects.length == 0){
|
298
|
((TreeViewer)this.viewer).expandToLevel(2);
|
299
|
}else{
|
300
|
((TreeViewer)this.viewer).setExpandedElements(expandedObjects);
|
301
|
}
|
302
|
}
|
303
|
|
304
|
|
305
|
|
306
|
}
|