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