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.bulkeditor.referencingobjects.e4;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.Collections;
|
15
|
import java.util.Comparator;
|
16
|
import java.util.HashMap;
|
17
|
import java.util.HashSet;
|
18
|
import java.util.List;
|
19
|
import java.util.Map;
|
20
|
import java.util.Set;
|
21
|
import java.util.UUID;
|
22
|
|
23
|
import javax.annotation.PostConstruct;
|
24
|
import javax.annotation.PreDestroy;
|
25
|
|
26
|
import org.apache.commons.lang3.StringUtils;
|
27
|
import org.apache.log4j.Logger;
|
28
|
import org.eclipse.core.runtime.IProgressMonitor;
|
29
|
import org.eclipse.core.runtime.IStatus;
|
30
|
import org.eclipse.core.runtime.Status;
|
31
|
import org.eclipse.core.runtime.jobs.Job;
|
32
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
33
|
import org.eclipse.e4.ui.services.EMenuService;
|
34
|
import org.eclipse.jface.viewers.CellEditor;
|
35
|
import org.eclipse.jface.viewers.ICellModifier;
|
36
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
37
|
import org.eclipse.jface.viewers.ITableLabelProvider;
|
38
|
import org.eclipse.jface.viewers.TableViewer;
|
39
|
import org.eclipse.jface.viewers.TableViewerColumn;
|
40
|
import org.eclipse.jface.viewers.TextCellEditor;
|
41
|
import org.eclipse.jface.viewers.TreeNode;
|
42
|
import org.eclipse.jface.viewers.Viewer;
|
43
|
import org.eclipse.swt.SWT;
|
44
|
import org.eclipse.swt.layout.GridData;
|
45
|
import org.eclipse.swt.layout.GridLayout;
|
46
|
import org.eclipse.swt.widgets.Composite;
|
47
|
import org.eclipse.swt.widgets.Display;
|
48
|
import org.eclipse.swt.widgets.Label;
|
49
|
import org.eclipse.swt.widgets.Table;
|
50
|
|
51
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
52
|
import eu.etaxonomy.cdm.api.service.IDescriptionElementService;
|
53
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
54
|
import eu.etaxonomy.cdm.api.service.IEventBaseService;
|
55
|
import eu.etaxonomy.cdm.api.service.IGroupService;
|
56
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
57
|
import eu.etaxonomy.cdm.api.service.INameService;
|
58
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
59
|
import eu.etaxonomy.cdm.api.service.IPolytomousKeyNodeService;
|
60
|
import eu.etaxonomy.cdm.api.service.IPolytomousKeyService;
|
61
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
62
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
63
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
64
|
import eu.etaxonomy.cdm.api.service.IUserService;
|
65
|
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
|
66
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
67
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
68
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
69
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
70
|
import eu.etaxonomy.cdm.model.common.EventBase;
|
71
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
72
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
73
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
74
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
75
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
76
|
import eu.etaxonomy.cdm.model.media.Media;
|
77
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
78
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
79
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
80
|
import eu.etaxonomy.cdm.model.permission.Group;
|
81
|
import eu.etaxonomy.cdm.model.permission.User;
|
82
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
83
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
84
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
85
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
86
|
import eu.etaxonomy.cdm.model.term.TermBase;
|
87
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
88
|
import eu.etaxonomy.cdm.model.term.TermTree;
|
89
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
90
|
import eu.etaxonomy.cdm.persistence.dto.AbstractTermDto;
|
91
|
import eu.etaxonomy.cdm.persistence.dto.ReferencingObjectDto;
|
92
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
93
|
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
|
94
|
import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
|
95
|
import eu.etaxonomy.cdm.persistence.dto.TermVocabularyDto;
|
96
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
97
|
import eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.ReferencingObjectsContentProvider;
|
98
|
import eu.etaxonomy.taxeditor.bulkeditor.referencingobjects.ReferencingObjectsLabelProvider;
|
99
|
import eu.etaxonomy.taxeditor.editor.IReferencingObjectsView;
|
100
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
101
|
import eu.etaxonomy.taxeditor.model.DescriptionHelper;
|
102
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
103
|
import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
|
104
|
|
105
|
/**
|
106
|
* This view loads and presents referencing objects information asynchronously.
|
107
|
*
|
108
|
* Most of the task is done in UpdateRefObjectsJob.run()
|
109
|
*
|
110
|
* @author pplitzner
|
111
|
* @author k.luther
|
112
|
* @author a.mueller
|
113
|
* @since Aug 16, 2017
|
114
|
*/
|
115
|
public class ReferencingObjectsViewE4 extends AbstractCdmEditorPartE4 implements IReferencingObjectsView{
|
116
|
|
117
|
private static final Logger logger = Logger.getLogger(ReferencingObjectsViewE4.class);
|
118
|
|
119
|
private static final List<ReferencingObjectDto> EMPTY_LIST = Arrays.asList(); //immutable empty list
|
120
|
private static final RefObjectDtoComparator COMPARATOR = new RefObjectDtoComparator();
|
121
|
|
122
|
private Label contentDescription;
|
123
|
|
124
|
private volatile UUID actualUuid; //volatile as it used used between threads but only written in the synchronized method
|
125
|
private Job currentJob = null; //this variable should only be accessed in synchronized updateToNewItem
|
126
|
|
127
|
@PostConstruct
|
128
|
public void create(Composite parent, EMenuService menuService) {
|
129
|
if (!CdmStore.isActive()){
|
130
|
return;
|
131
|
}
|
132
|
parent.setLayout(new GridLayout());
|
133
|
contentDescription = new Label(parent, SWT.NONE);
|
134
|
contentDescription.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
|
135
|
TableViewer tableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
|
136
|
| SWT.V_SCROLL | SWT.FULL_SELECTION);
|
137
|
createColumns(tableViewer);
|
138
|
tableViewer.setContentProvider(new ReferencingObjectsContentProvider());
|
139
|
tableViewer.setLabelProvider(new ReferencingObjectsLabelProvider());
|
140
|
tableViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
|
141
|
|
142
|
tableViewer.setCellModifier(new ICellModifier() {
|
143
|
|
144
|
@Override
|
145
|
public void modify(Object element, String property, Object value) {}
|
146
|
|
147
|
@Override
|
148
|
public Object getValue(Object element, String property) {
|
149
|
ITableLabelProvider tableLabelProvider = null;
|
150
|
if(tableViewer.getLabelProvider() instanceof ITableLabelProvider){
|
151
|
tableLabelProvider = (ITableLabelProvider) tableViewer.getLabelProvider();
|
152
|
}
|
153
|
Object[] columnProperties = tableViewer.getColumnProperties();
|
154
|
for (int i=0;i<columnProperties.length;i++) {
|
155
|
if(columnProperties[i].equals(property) && tableLabelProvider!=null){
|
156
|
return tableLabelProvider.getColumnText(element, i);
|
157
|
}
|
158
|
}
|
159
|
return "";
|
160
|
}
|
161
|
|
162
|
@Override
|
163
|
public boolean canModify(Object element, String property) {
|
164
|
return true;
|
165
|
}
|
166
|
});
|
167
|
|
168
|
viewer = tableViewer;
|
169
|
|
170
|
// Propagate selection from viewer
|
171
|
selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
|
172
|
viewer.addSelectionChangedListener(selectionChangedListener);
|
173
|
|
174
|
//create context menu
|
175
|
menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.bulkeditor.popupmenu.referencingobjectsview");
|
176
|
}
|
177
|
|
178
|
/**
|
179
|
* Create the columns for the table
|
180
|
* @param viewer
|
181
|
*/
|
182
|
private void createColumns(TableViewer viewer) {
|
183
|
Table table = viewer.getTable();
|
184
|
String[] titles = {"Class", "Description", "UUID", "Object ID" };
|
185
|
int[] bounds = { 100, 200, 100, 70};
|
186
|
|
187
|
CellEditor[] editors = new CellEditor[titles.length];
|
188
|
|
189
|
for (int i = 0; i < titles.length; i++) {
|
190
|
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
|
191
|
editors[i] = new TextCellEditor(table);
|
192
|
column.getColumn().setText(titles[i]);
|
193
|
column.getColumn().setWidth(bounds[i]);
|
194
|
column.getColumn().setResizable(true);
|
195
|
column.getColumn().setMoveable(true);
|
196
|
}
|
197
|
table.setHeaderVisible(true);
|
198
|
table.setLinesVisible(true);
|
199
|
|
200
|
viewer.setCellEditors(editors);
|
201
|
viewer.setColumnProperties(titles);
|
202
|
}
|
203
|
|
204
|
|
205
|
private class ItemDto {
|
206
|
String typeName;
|
207
|
String itemLabel;
|
208
|
UUID itemUuid;
|
209
|
Integer itemId;
|
210
|
Class<? extends CdmBase> itemClass;
|
211
|
|
212
|
private String bestLabel(){
|
213
|
return (StringUtils.isNotBlank(this.typeName)? this.typeName + " " : "")
|
214
|
+ "'"+(StringUtils.isNotBlank(this.itemLabel)? this.itemLabel : this.itemUuid.toString()) +"'"
|
215
|
+ (this.itemId != null? " (id=" + this.itemId+")" : "");
|
216
|
}
|
217
|
}
|
218
|
|
219
|
private class UpdateRefObjectsJob extends Job{
|
220
|
|
221
|
final private ItemDto item;
|
222
|
|
223
|
public UpdateRefObjectsJob(String name, ItemDto item) {
|
224
|
super(name);
|
225
|
this.item = item;
|
226
|
if (item.itemUuid == null){
|
227
|
throw new RuntimeException("Item uuid must always exist at this point");
|
228
|
}
|
229
|
}
|
230
|
|
231
|
@Override
|
232
|
protected IStatus run(final IProgressMonitor monitor) {
|
233
|
|
234
|
monitor.beginTask("Calculating referencing objects for " + item.itemLabel, 100);
|
235
|
|
236
|
//set to loading
|
237
|
monitor.subTask("Load empty");
|
238
|
monitor.worked(1);
|
239
|
updateView("Loading " + item.bestLabel(), EMPTY_LIST, item.itemUuid);
|
240
|
if(monitor.isCanceled()) {
|
241
|
return Status.CANCEL_STATUS;
|
242
|
}
|
243
|
|
244
|
//handle transient instance
|
245
|
if (item.itemId != null && item.itemId.equals(0)){
|
246
|
updateView("Not yet persisted: " + item.bestLabel(), EMPTY_LIST, item.itemUuid);
|
247
|
monitor.done();
|
248
|
return Status.OK_STATUS;
|
249
|
}
|
250
|
monitor.worked(1); //sum = 2
|
251
|
|
252
|
//load uninitialized DTOs from server
|
253
|
monitor.subTask("Load base data from server");
|
254
|
Set<ReferencingObjectDto> refObjectsFromServer = loadReferencingObjects(item.itemUuid, item.itemClass);
|
255
|
if (refObjectsFromServer == null){
|
256
|
updateView("An error occurred when loading " + item.bestLabel(), EMPTY_LIST, item.itemUuid);
|
257
|
return Status.CANCEL_STATUS; //TODO is this correct?, null can happen e.g. if server call throws exception
|
258
|
}
|
259
|
if(monitor.isCanceled()) {
|
260
|
return Status.CANCEL_STATUS;
|
261
|
}
|
262
|
monitor.worked(10); //sum = 12
|
263
|
|
264
|
//show count
|
265
|
monitor.subTask("Show without description");
|
266
|
int count = refObjectsFromServer.size();
|
267
|
updateView("Loading " + count + " items for " + item.bestLabel(), EMPTY_LIST, item.itemUuid);
|
268
|
monitor.worked(1); //sum = 13
|
269
|
|
270
|
//sort
|
271
|
List<ReferencingObjectDto> localDtos = sortToList(refObjectsFromServer);
|
272
|
updateView("0/" + count + " items for " + item.bestLabel(), localDtos, item.itemUuid);
|
273
|
if(monitor.isCanceled()) {
|
274
|
return Status.CANCEL_STATUS;
|
275
|
}
|
276
|
monitor.worked(2); //sum = 15
|
277
|
|
278
|
//initialize
|
279
|
monitor.subTask("Initialize");
|
280
|
initializeDtos(localDtos, item, monitor, 83); //is calling updateView itself; sum = 95
|
281
|
if(monitor.isCanceled()) {
|
282
|
return Status.CANCEL_STATUS;
|
283
|
}
|
284
|
monitor.worked(2); //sum = 100 (just in case)
|
285
|
monitor.done();
|
286
|
|
287
|
return Status.OK_STATUS;
|
288
|
}
|
289
|
}
|
290
|
|
291
|
private synchronized void updateToNewItem(final ItemDto item) {
|
292
|
if (currentJob != null){
|
293
|
currentJob.cancel();
|
294
|
}
|
295
|
Job newJob = new UpdateRefObjectsJob("Update Referencing Objects for " + item.bestLabel(), item);
|
296
|
newJob.setUser(true);
|
297
|
actualUuid = item.itemUuid;
|
298
|
currentJob = newJob;
|
299
|
currentJob.schedule();
|
300
|
}
|
301
|
|
302
|
private List<ReferencingObjectDto> sortToList(Set<ReferencingObjectDto> referencingObjectsSet) {
|
303
|
List<ReferencingObjectDto> result = new ArrayList<>(referencingObjectsSet);
|
304
|
Collections.sort(result, COMPARATOR);
|
305
|
return result;
|
306
|
}
|
307
|
|
308
|
protected boolean initializeDtos(List<ReferencingObjectDto> localDtos, ItemDto item, IProgressMonitor monitor, int work) {
|
309
|
|
310
|
IProgressMonitor subMonitor = AbstractUtility.getSubProgressMonitor(monitor, work);
|
311
|
subMonitor.beginTask("Initialize DTOs", localDtos.size());
|
312
|
int i = 100 - 20; //the first run should only include 20 records
|
313
|
int initCount = 0;
|
314
|
|
315
|
Set<ReferencingObjectDto> toInitialize = new HashSet<>();
|
316
|
for (ReferencingObjectDto dto : localDtos){
|
317
|
initCount++;
|
318
|
toInitialize.add(dto);
|
319
|
subMonitor.worked(1);
|
320
|
if (++i == 100){
|
321
|
if (monitor.isCanceled()){
|
322
|
return false;
|
323
|
}
|
324
|
initBulk(toInitialize, initCount, localDtos, item);
|
325
|
//reset
|
326
|
toInitialize = new HashSet<>();
|
327
|
i = 0;
|
328
|
}
|
329
|
}
|
330
|
//final bulk
|
331
|
if (monitor.isCanceled()){
|
332
|
return false;
|
333
|
}
|
334
|
initBulk(toInitialize, initCount, localDtos, item);
|
335
|
return true;
|
336
|
}
|
337
|
|
338
|
private void initBulk(Set<ReferencingObjectDto> toInitialize, int initCount,
|
339
|
List<ReferencingObjectDto> localDtos, ItemDto item) {
|
340
|
Set<ReferencingObjectDto> initialized = CdmStore.getCommonService().initializeReferencingObjectDtos(toInitialize, true, true, true, CdmStore.getDefaultLanguage());
|
341
|
Map<UUID,ReferencingObjectDto> map = new HashMap<>();
|
342
|
initialized.forEach(i->map.put(i.getUuid(), i));
|
343
|
toInitialize.forEach(dto->mergeInitializedData(dto, map.get(dto.getUuid())));
|
344
|
String initStr = initCount < localDtos.size()? initCount + "/" + localDtos.size() + " items": "Items";
|
345
|
updateView(initStr + " for " + item.bestLabel(), localDtos, item.itemUuid);
|
346
|
}
|
347
|
|
348
|
private void mergeInitializedData(ReferencingObjectDto to, ReferencingObjectDto from) {
|
349
|
to.setTitleCache(from.getTitleCache());
|
350
|
to.setOpenInTarget(from.getOpenInTarget());
|
351
|
to.setReferencedEntity(from.getReferencedEntity());
|
352
|
}
|
353
|
|
354
|
private Set<ReferencingObjectDto> loadReferencingObjects(UUID entity, Class objectClass) {
|
355
|
//TODO why do we need to load the referenced object here
|
356
|
CdmBase referencedObject = null;
|
357
|
try {
|
358
|
if (objectClass.getSuperclass().equals(TeamOrPersonBase.class) ){
|
359
|
referencedObject = CdmStore.getService(IAgentService.class).load(entity);
|
360
|
} else if (objectClass.getSuperclass().equals(TaxonName.class)){
|
361
|
referencedObject = CdmStore.getService(INameService.class).load(entity);
|
362
|
} else if (objectClass.getSuperclass().equals(TaxonBase.class)){
|
363
|
referencedObject = CdmStore.getService(ITaxonService.class).load(entity);
|
364
|
} else if (objectClass.equals(Reference.class)){
|
365
|
referencedObject = CdmStore.getService(IReferenceService.class).load(entity);
|
366
|
} else if (objectClass.getSuperclass().equals(SpecimenOrObservationBase.class)){
|
367
|
referencedObject = CdmStore.getService(IOccurrenceService.class).load(entity);
|
368
|
} else if (objectClass.isAssignableFrom(User.class)){
|
369
|
referencedObject = CdmStore.getService(IUserService.class).load(entity);
|
370
|
} else if (objectClass.isAssignableFrom(Group.class)){
|
371
|
referencedObject = CdmStore.getService(IGroupService.class).load(entity);
|
372
|
} else if (objectClass.isAssignableFrom(Media.class)){
|
373
|
referencedObject = CdmStore.getService(IMediaService.class).load(entity);
|
374
|
} else if (DescriptionBase.class.isAssignableFrom(objectClass)){
|
375
|
referencedObject = CdmStore.getService(IDescriptionService.class).load(entity);
|
376
|
} else if (DescriptionElementBase.class.isAssignableFrom(objectClass)){
|
377
|
referencedObject = CdmStore.getService(IDescriptionElementService.class).load(entity, null);
|
378
|
} else if (objectClass.equals(Sequence.class)){
|
379
|
referencedObject = CdmStore.getService(ISequenceService.class).load(entity, null);
|
380
|
} else if (PolytomousKey.class.isAssignableFrom(objectClass)){
|
381
|
referencedObject = CdmStore.getService(IPolytomousKeyService.class).load(entity, null);
|
382
|
} else if (PolytomousKeyNode.class.isAssignableFrom(objectClass)){
|
383
|
referencedObject = CdmStore.getService(IPolytomousKeyNodeService.class).load(entity, null);
|
384
|
} else if (DefinedTermBase.class.isAssignableFrom(objectClass)){
|
385
|
referencedObject = CdmStore.getService(ITermService.class).load(entity, null);
|
386
|
} else if (EventBase.class.isAssignableFrom(objectClass)){
|
387
|
referencedObject = CdmStore.getService(IEventBaseService.class).load(entity, null);
|
388
|
} else if (User.class.isAssignableFrom(objectClass)){
|
389
|
referencedObject = CdmStore.getService(IUserService.class).load(entity, null);
|
390
|
} else if (Group.class.isAssignableFrom(objectClass)){
|
391
|
referencedObject = CdmStore.getService(IGroupService.class).load(entity, null);
|
392
|
}
|
393
|
else if(CdmBase.class.isAssignableFrom(objectClass)){
|
394
|
referencedObject = CdmStore.getCommonService().find(objectClass, entity);
|
395
|
}
|
396
|
Set<ReferencingObjectDto> setOfReferencingObjects = null;
|
397
|
|
398
|
if (referencedObject != null){
|
399
|
setOfReferencingObjects = CdmStore.getCommonService().getReferencingObjectDtos(referencedObject);
|
400
|
return setOfReferencingObjects;
|
401
|
}
|
402
|
|
403
|
} catch (Exception e) {
|
404
|
logger.debug("Error retrieving referencing objects", e);
|
405
|
e.printStackTrace();
|
406
|
updateDescriptionLabel("The referencing objects view could not be loaded completely. Some problems occurred: " + e.getMessage());
|
407
|
}
|
408
|
return null;
|
409
|
}
|
410
|
|
411
|
/**
|
412
|
* Compares the referencing object by type and id. Using "description" for
|
413
|
* comparation has been given up to avoid initialization before comparison.
|
414
|
*/
|
415
|
static class RefObjectDtoComparator implements Comparator<ReferencingObjectDto>{
|
416
|
|
417
|
@Override
|
418
|
public int compare(ReferencingObjectDto dto1, ReferencingObjectDto dto2) {
|
419
|
int result = dto1.getType().getSimpleName().compareToIgnoreCase(dto2.getType().getSimpleName());
|
420
|
if (result == 0) {
|
421
|
result = Integer.compare(dto1.getId(),dto2.getId());
|
422
|
}
|
423
|
return result;
|
424
|
}
|
425
|
}
|
426
|
|
427
|
//not sure if it needs to be synchronized, only the local variable actualUuid is read
|
428
|
private void updateView(final String label,
|
429
|
final List<ReferencingObjectDto> referencingObjects, UUID itemUuid) {
|
430
|
|
431
|
if (this.actualUuid == itemUuid){
|
432
|
Display.getDefault().asyncExec(()->{
|
433
|
if (contentDescription != null && !contentDescription.isDisposed()){
|
434
|
contentDescription.setText(label);
|
435
|
}
|
436
|
if (viewer != null && !viewer.getControl().isDisposed()){
|
437
|
try{
|
438
|
viewer.setInput(referencingObjects);
|
439
|
viewer.refresh();
|
440
|
|
441
|
//enable/disable table
|
442
|
viewer.getControl().setEnabled(referencingObjects!=null);
|
443
|
}catch(Exception e){
|
444
|
e.printStackTrace();
|
445
|
logger.debug(e.getStackTrace());
|
446
|
updateDescriptionLabel("The referencing objects view could not be updated completely. Some Problems occurred: " + e.getMessage());
|
447
|
}
|
448
|
}
|
449
|
});
|
450
|
}
|
451
|
}
|
452
|
|
453
|
|
454
|
private void updateDescriptionLabel(final String description){
|
455
|
Display.getDefault().asyncExec(()->{
|
456
|
if(contentDescription!=null && !contentDescription.isDisposed()) {
|
457
|
contentDescription.setText(description.replace("&", "&&"));
|
458
|
}
|
459
|
}
|
460
|
);
|
461
|
}
|
462
|
|
463
|
@Override
|
464
|
protected boolean showEmptyIfNoActiveEditor(){
|
465
|
return false;
|
466
|
}
|
467
|
|
468
|
@Override
|
469
|
public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
|
470
|
if(activePart==thisPart){
|
471
|
return;
|
472
|
}
|
473
|
|
474
|
IStructuredSelection structuredSelection = createSelection(selection);
|
475
|
if(structuredSelection!=null){
|
476
|
showViewer(structuredSelection, activePart, viewer);
|
477
|
}
|
478
|
}
|
479
|
|
480
|
@Override
|
481
|
public void showViewer(IStructuredSelection selection, MPart activePart, Viewer viewer){
|
482
|
handleNewSelection(selection.getFirstElement());
|
483
|
}
|
484
|
|
485
|
//Note AM: this can probably be done better together with base class methods
|
486
|
// As I am not so familiar with this structure I only adapt it this way to be on the safe side
|
487
|
@Override
|
488
|
public void handleNewSelection(Object firstElement){
|
489
|
ItemDto dto = makeItemDto(firstElement);
|
490
|
if (dto.itemUuid == null || dto.itemClass == null || dto.itemUuid.equals(this.actualUuid)){
|
491
|
return;
|
492
|
}
|
493
|
updateToNewItem(dto);
|
494
|
}
|
495
|
|
496
|
/**
|
497
|
* Transforms the selection into uniform format (ItemDto).
|
498
|
* This method must be fast / should never require a server call
|
499
|
* as it takes place before the update Job is started and therefore is not
|
500
|
* asynchronous.
|
501
|
*/
|
502
|
public ItemDto makeItemDto(Object firstElement) {
|
503
|
|
504
|
ItemDto dto = new ItemDto();
|
505
|
if(firstElement instanceof TreeNode){
|
506
|
firstElement = ((TreeNode) firstElement).getValue();
|
507
|
}
|
508
|
if (firstElement instanceof TaxonNode && !((TaxonNode)firstElement).hasTaxon()){
|
509
|
firstElement = ((TaxonNode)firstElement).getClassification();
|
510
|
}
|
511
|
|
512
|
if (firstElement instanceof AbstractTermDto){
|
513
|
AbstractTermDto termDto = (AbstractTermDto) firstElement;
|
514
|
dto.itemLabel = termDto.getTitleCache();
|
515
|
dto.itemUuid= termDto.getUuid();
|
516
|
dto.typeName = termDto.getTermType().getLabel();
|
517
|
dto.itemId = null; // id does not yet exist in TermDto
|
518
|
if (termDto instanceof TermDto){
|
519
|
dto.itemClass = TermBase.class;
|
520
|
}else if(termDto instanceof TermTreeDto){
|
521
|
dto.itemClass = TermTree.class;
|
522
|
dto.typeName = dto.typeName + " Tree";
|
523
|
}else if(termDto instanceof TermVocabularyDto){
|
524
|
dto.itemClass = TermVocabulary.class;
|
525
|
dto.typeName = dto.typeName + " Vocabulary";
|
526
|
}else{
|
527
|
//make it an unhandled selection
|
528
|
dto.itemUuid = null;
|
529
|
}
|
530
|
}else if (firstElement instanceof TermNodeDto){
|
531
|
TermNodeDto termNodeDto = (TermNodeDto) firstElement;
|
532
|
dto.itemLabel = (termNodeDto.getTerm() != null? termNodeDto.getTerm().getTitleCache() : termNodeDto.getTreeIndex());
|
533
|
dto.itemUuid= termNodeDto.getUuid();
|
534
|
dto.typeName = termNodeDto.getType().getLabel() + " Node";
|
535
|
dto.itemId = null; //does not yet exist in TermDto
|
536
|
dto.itemClass = TermNode.class;
|
537
|
}else if(firstElement instanceof CdmBase){
|
538
|
CdmBase cdmBase = CdmBase.deproxy(firstElement, CdmBase.class);
|
539
|
String label = null;
|
540
|
if(cdmBase instanceof IdentifiableEntity){
|
541
|
label = (HibernateProxyHelper.deproxy(cdmBase, IdentifiableEntity.class)).getTitleCache();
|
542
|
}
|
543
|
else if(cdmBase instanceof DescriptionElementBase){
|
544
|
label = DescriptionHelper.getLabel(cdmBase);
|
545
|
}
|
546
|
else if (cdmBase instanceof User){
|
547
|
label = ((User)cdmBase).getUsername();
|
548
|
}else if (cdmBase instanceof TaxonNode){
|
549
|
label = ((TaxonNode)cdmBase).getTaxon().getTitleCache();
|
550
|
}
|
551
|
if (CdmUtils.isBlank(label)){
|
552
|
label = "#"+cdmBase.getId();
|
553
|
}
|
554
|
dto.typeName = cdmBase.getUserFriendlyTypeName();
|
555
|
dto.itemLabel = label;
|
556
|
dto.itemUuid= cdmBase.getUuid();
|
557
|
dto.itemClass = cdmBase.getClass();
|
558
|
dto.itemId = cdmBase.getId();
|
559
|
}else if (firstElement instanceof UuidAndTitleCache<?>){
|
560
|
UuidAndTitleCache<? extends CdmBase> element = (UuidAndTitleCache<? extends CdmBase>) firstElement;
|
561
|
dto.typeName = CdmUtils.userFriendlyClassName(element.getType());
|
562
|
dto.itemLabel = element.getTitleCache();
|
563
|
if (CdmUtils.isBlank(dto.itemLabel)){
|
564
|
dto.itemLabel = "id=" + element.getId();
|
565
|
}
|
566
|
dto.itemUuid= element.getUuid();
|
567
|
dto.itemClass = element.getType();
|
568
|
dto.itemId = element.getId();
|
569
|
}else if (firstElement instanceof String){
|
570
|
dto.typeName = "String";
|
571
|
dto.itemLabel = firstElement.toString();
|
572
|
dto.itemUuid= null;
|
573
|
dto.itemClass = null;
|
574
|
dto.itemId = null;
|
575
|
}else if (firstElement != null){
|
576
|
dto.typeName = CdmUtils.userFriendlyClassName(firstElement.getClass());
|
577
|
dto.itemLabel = firstElement.toString();
|
578
|
dto.itemUuid= null;
|
579
|
dto.itemClass = null;
|
580
|
dto.itemId = null;
|
581
|
}else{
|
582
|
dto.typeName = null;
|
583
|
dto.itemLabel = "no selection";
|
584
|
dto.itemUuid= null;
|
585
|
dto.itemClass = null;
|
586
|
dto.itemId = null;
|
587
|
}
|
588
|
return dto;
|
589
|
}
|
590
|
|
591
|
@PreDestroy
|
592
|
public void dispose() {
|
593
|
//no conversation in this view
|
594
|
}
|
595
|
|
596
|
@Override
|
597
|
public void changed(Object object) {
|
598
|
// no editing in this view
|
599
|
}
|
600
|
|
601
|
@Override
|
602
|
public boolean onComplete() {
|
603
|
return false;
|
604
|
}
|
605
|
|
606
|
@Override
|
607
|
protected String getViewName() {
|
608
|
return "Referencing Objects";
|
609
|
}
|
610
|
}
|