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.featuretree.e4;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import javax.annotation.PostConstruct;
|
20
|
import javax.annotation.PreDestroy;
|
21
|
import javax.inject.Inject;
|
22
|
|
23
|
import org.eclipse.core.runtime.IProgressMonitor;
|
24
|
import org.eclipse.e4.core.di.annotations.Optional;
|
25
|
import org.eclipse.e4.ui.di.Focus;
|
26
|
import org.eclipse.e4.ui.di.Persist;
|
27
|
import org.eclipse.e4.ui.di.UIEventTopic;
|
28
|
import org.eclipse.e4.ui.di.UISynchronize;
|
29
|
import org.eclipse.e4.ui.model.application.ui.MDirtyable;
|
30
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
31
|
import org.eclipse.e4.ui.services.EMenuService;
|
32
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
33
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
34
|
import org.eclipse.jface.util.LocalSelectionTransfer;
|
35
|
import org.eclipse.jface.viewers.ISelection;
|
36
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
37
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
38
|
import org.eclipse.jface.viewers.SelectionChangedEvent;
|
39
|
import org.eclipse.jface.viewers.StructuredSelection;
|
40
|
import org.eclipse.jface.viewers.TreeViewer;
|
41
|
import org.eclipse.swt.SWT;
|
42
|
import org.eclipse.swt.dnd.DND;
|
43
|
import org.eclipse.swt.dnd.Transfer;
|
44
|
import org.eclipse.swt.events.KeyAdapter;
|
45
|
import org.eclipse.swt.events.KeyEvent;
|
46
|
import org.eclipse.swt.layout.FillLayout;
|
47
|
import org.eclipse.swt.widgets.Composite;
|
48
|
import org.eclipse.ui.IMemento;
|
49
|
|
50
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
51
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
52
|
import eu.etaxonomy.cdm.api.service.ITermNodeService;
|
53
|
import eu.etaxonomy.cdm.api.service.ITermTreeService;
|
54
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
55
|
import eu.etaxonomy.cdm.model.term.TermNode;
|
56
|
import eu.etaxonomy.cdm.model.term.TermType;
|
57
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
58
|
import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
|
59
|
import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
|
60
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
61
|
import eu.etaxonomy.taxeditor.editor.definedterm.FeatureTreeViewerComparator;
|
62
|
import eu.etaxonomy.taxeditor.editor.definedterm.TermTransfer;
|
63
|
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
|
64
|
import eu.etaxonomy.taxeditor.featuretree.TermNodeDtoTransfer;
|
65
|
import eu.etaxonomy.taxeditor.featuretree.TermTreeContentProvider;
|
66
|
import eu.etaxonomy.taxeditor.featuretree.TermTreeLabelProvider;
|
67
|
import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
|
68
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
69
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
70
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
71
|
import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
|
72
|
import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
|
73
|
import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
|
74
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
75
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
76
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
77
|
import eu.etaxonomy.taxeditor.store.AppModelId;
|
78
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
79
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
80
|
import eu.etaxonomy.taxeditor.workbench.part.IE4ViewerPart;
|
81
|
|
82
|
/**
|
83
|
*
|
84
|
* @author pplitzner
|
85
|
* @date 06.06.2017
|
86
|
*
|
87
|
*/
|
88
|
public class TermTreeEditor<T extends DefinedTermBase>
|
89
|
implements ITermTreeEditor, ISelectionChangedListener,
|
90
|
IE4ViewerPart, IPartContentHasDetails, IPartContentHasSupplementalData,
|
91
|
IContextListener, IConversationEnabled, IDirtyMarkable {
|
92
|
|
93
|
public static final List<String> TREE_PROPERTY_PATH = Arrays.asList(new String[] {
|
94
|
"root", //$NON-NLS-1$
|
95
|
"root.children", //$NON-NLS-1$
|
96
|
"root.children.inapplicableIf", //$NON-NLS-1$
|
97
|
"root.children.inapplicableIf.feature", //$NON-NLS-1$
|
98
|
"root.children.inapplicableIf.state", //$NON-NLS-1$
|
99
|
"root.children.onlyApplicableIf", //$NON-NLS-1$
|
100
|
"root.children.onlyApplicableIf.feature", //$NON-NLS-1$
|
101
|
"root.children.onlyApplicableIf.state", //$NON-NLS-1$
|
102
|
});
|
103
|
|
104
|
private ConversationHolder conversation;
|
105
|
|
106
|
private ICdmEntitySession cdmEntitySession;
|
107
|
|
108
|
@Inject
|
109
|
private ESelectionService selService;
|
110
|
|
111
|
@Inject
|
112
|
private MDirtyable dirty;
|
113
|
|
114
|
@Inject
|
115
|
private UISynchronize sync;
|
116
|
|
117
|
@Inject
|
118
|
private MPart thisPart;
|
119
|
|
120
|
private TreeViewer viewer;
|
121
|
|
122
|
Map<UUID,TermTreeDto> trees;
|
123
|
|
124
|
Map<UUID, TermNodeDto> uuidTermMap = new HashMap<>();
|
125
|
List<AbstractPostOperation<TermNode>> operationList = new ArrayList<>();
|
126
|
List<TermNodeDto> listToUpdate = new ArrayList<>();
|
127
|
|
128
|
TermType termType;
|
129
|
|
130
|
@Inject
|
131
|
public TermTreeEditor() {
|
132
|
CdmStore.getContextManager().addContextListener(this);
|
133
|
}
|
134
|
|
135
|
@PostConstruct
|
136
|
public void createControl(Composite parent, EMenuService menuService){
|
137
|
if (CdmStore.isActive()){
|
138
|
initSession();
|
139
|
}
|
140
|
else{
|
141
|
return;
|
142
|
}
|
143
|
parent.setLayout(new FillLayout());
|
144
|
viewer = new TreeViewer(parent);
|
145
|
TermTreeContentProvider contentProvider = new TermTreeContentProvider();
|
146
|
|
147
|
viewer.setContentProvider(contentProvider);
|
148
|
viewer.setLabelProvider(new TermTreeLabelProvider());
|
149
|
|
150
|
int ops = DND.DROP_COPY | DND.DROP_MOVE;
|
151
|
Transfer[] transfers = new Transfer[] {
|
152
|
TermNodeDtoTransfer.getInstance(),
|
153
|
TermTransfer.getInstance(),
|
154
|
LocalSelectionTransfer.getTransfer()};
|
155
|
viewer.addDragSupport(ops, transfers, new TermNodeDtoDragListener(viewer));
|
156
|
viewer.addDropSupport(ops, transfers, new TermTreeDtoDropAdapter(this, viewer, sync));
|
157
|
viewer.addSelectionChangedListener(this);
|
158
|
viewer.getTree().addKeyListener(new KeyAdapter() {
|
159
|
@Override
|
160
|
public void keyPressed(KeyEvent e) {
|
161
|
if(e.stateMask == SWT.MOD1 && e.keyCode == 'c'){
|
162
|
copy(viewer.getStructuredSelection());
|
163
|
}
|
164
|
else if(e.stateMask == SWT.MOD1 && e.keyCode == 'v'){
|
165
|
paste(viewer.getStructuredSelection());
|
166
|
}
|
167
|
}
|
168
|
});
|
169
|
|
170
|
updateTrees();
|
171
|
viewer.setComparator(new FeatureTreeViewerComparator());
|
172
|
viewer.setInput(getTrees());
|
173
|
contentProvider.setUuidTermMap(uuidTermMap);
|
174
|
//create context menu
|
175
|
menuService.registerContextMenu(viewer.getControl(), AppModelId.POPUPMENU_EU_ETAXONOMY_TAXEDITOR_STORE_POPUPMENU_FEATURETREEEDITOR);
|
176
|
}
|
177
|
|
178
|
public void init(TermType type, String label){
|
179
|
this.termType = type;
|
180
|
updateTrees();
|
181
|
viewer.setComparator(new FeatureTreeViewerComparator());
|
182
|
viewer.setInput(getTrees());
|
183
|
((TermTreeContentProvider)viewer.getContentProvider()).setUuidTermMap(uuidTermMap);
|
184
|
thisPart.setLabel(label);
|
185
|
}
|
186
|
|
187
|
// protected abstract List<TermTreeDto> getTrees();
|
188
|
|
189
|
public void paste(IStructuredSelection selection) {
|
190
|
if (StoreUtil.promptCheckIsDirty(this)) {
|
191
|
return;
|
192
|
}
|
193
|
|
194
|
ISelection clipBoardSelection = LocalSelectionTransfer.getTransfer().getSelection();
|
195
|
Object firstElement = selection.getFirstElement();
|
196
|
TermNodeDto parentNode = null;
|
197
|
if(firstElement instanceof TermNodeDto){
|
198
|
parentNode = (TermNodeDto) firstElement;
|
199
|
}
|
200
|
else if(firstElement instanceof TermTreeDto){
|
201
|
parentNode = ((TermTreeDto)firstElement).getRoot();
|
202
|
}
|
203
|
if(parentNode!=null){
|
204
|
|
205
|
TermNodeDto copiedNode = (TermNodeDto) ((IStructuredSelection)clipBoardSelection).getFirstElement();
|
206
|
boolean isDuplicate = this.checkDuplicates(copiedNode.getTerm().getUuid(), parentNode.getTree().getUuid());
|
207
|
if (isDuplicate){
|
208
|
MessagingUtils.informationDialog(Messages.AddFeatureHandler_Duplicates_not_allowed, Messages.AddFeatureHandler_Duplicates_not_allowed_message + "\n"+copiedNode.getTerm().getTitleCache());
|
209
|
return;
|
210
|
}
|
211
|
TermNodeDto newDto = new TermNodeDto(copiedNode.getTerm(), parentNode, 0, parentNode.getTree(), null, null, null);
|
212
|
this.refresh();
|
213
|
this.setDirty();
|
214
|
AddFeatureOperation operation = new AddFeatureOperation(copiedNode.getTerm().getUuid(), parentNode, this, this);
|
215
|
// AbstractUtility.executeOperation(operation, sync);
|
216
|
this.addOperation(operation);
|
217
|
}
|
218
|
|
219
|
}
|
220
|
|
221
|
public void copy(IStructuredSelection selection) {
|
222
|
LocalSelectionTransfer.getTransfer().setSelection(selection);
|
223
|
}
|
224
|
|
225
|
private void initSession(){
|
226
|
if(conversation == null){
|
227
|
conversation = CdmStore.createConversation();
|
228
|
}
|
229
|
if(cdmEntitySession==null){
|
230
|
cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
|
231
|
}
|
232
|
}
|
233
|
|
234
|
private void clearSession() {
|
235
|
if(conversation!=null){
|
236
|
conversation.close();
|
237
|
conversation = null;
|
238
|
}
|
239
|
if(cdmEntitySession != null) {
|
240
|
cdmEntitySession.dispose();
|
241
|
cdmEntitySession = null;
|
242
|
}
|
243
|
dirty.setDirty(false);
|
244
|
}
|
245
|
|
246
|
public void setDirty(boolean isDirty){
|
247
|
this.dirty.setDirty(isDirty);
|
248
|
}
|
249
|
@Override
|
250
|
public void setDirty(){
|
251
|
this.dirty.setDirty(true);
|
252
|
}
|
253
|
|
254
|
@Override
|
255
|
public boolean isDirty(){
|
256
|
return dirty.isDirty();
|
257
|
}
|
258
|
|
259
|
@Override
|
260
|
public void selectionChanged(SelectionChangedEvent event) {
|
261
|
//propagate selection
|
262
|
selService.setSelection(event.getSelection());
|
263
|
}
|
264
|
|
265
|
@Focus
|
266
|
public void focus(){
|
267
|
if(viewer!=null){
|
268
|
viewer.getControl().setFocus();
|
269
|
}
|
270
|
if(conversation!=null && !conversation.isBound()){
|
271
|
conversation.bind();
|
272
|
}
|
273
|
if(cdmEntitySession != null) {
|
274
|
cdmEntitySession.bind();
|
275
|
}
|
276
|
}
|
277
|
|
278
|
@Override
|
279
|
public void refresh(){
|
280
|
viewer.refresh();
|
281
|
}
|
282
|
|
283
|
@Override
|
284
|
public TreeViewer getViewer(){
|
285
|
return viewer;
|
286
|
}
|
287
|
|
288
|
@Override
|
289
|
public IStructuredSelection getSelection() {
|
290
|
return (IStructuredSelection) viewer.getSelection();
|
291
|
}
|
292
|
|
293
|
@Override
|
294
|
public ConversationHolder getConversationHolder() {
|
295
|
return conversation;
|
296
|
}
|
297
|
|
298
|
@Override
|
299
|
@Persist
|
300
|
public void save(IProgressMonitor monitor){
|
301
|
if (!conversation.isBound()) {
|
302
|
conversation.bind();
|
303
|
}
|
304
|
|
305
|
// commit the conversation and start a new transaction immediately
|
306
|
Object[] expandedElements = this.viewer.getExpandedElements();
|
307
|
conversation.commit(true);
|
308
|
if (operationList != null && !operationList.isEmpty()){
|
309
|
for (AbstractPostOperation<TermNode> operation: operationList){
|
310
|
AbstractUtility.executeOperation(operation, sync);
|
311
|
}
|
312
|
operationList.clear();
|
313
|
}
|
314
|
|
315
|
CdmStore.getService(ITermNodeService.class).saveTermNodeDtoList(listToUpdate);
|
316
|
listToUpdate.clear();
|
317
|
List<TermTreeDto> rootEntities = getRootEntities();
|
318
|
CdmStore.getService(ITermTreeService.class).saveOrUpdateTermTreeDtoList(rootEntities);
|
319
|
initializeTrees();
|
320
|
conversation.commit(true);
|
321
|
|
322
|
this.setDirty(false);
|
323
|
// this.viewer.setExpandedElements(expandedElements);
|
324
|
}
|
325
|
|
326
|
private void initializeTrees() {
|
327
|
Object[] expandedElements = viewer.getExpandedElements();
|
328
|
viewer.getTree().removeAll();
|
329
|
updateTrees();
|
330
|
viewer.setInput(getTrees());
|
331
|
viewer.setExpandedElements(expandedElements);
|
332
|
|
333
|
}
|
334
|
|
335
|
@PreDestroy
|
336
|
public void dispose(){
|
337
|
selService.setSelection(null);
|
338
|
clearSession();
|
339
|
}
|
340
|
|
341
|
@Override
|
342
|
public ICdmEntitySession getCdmEntitySession() {
|
343
|
return cdmEntitySession;
|
344
|
}
|
345
|
|
346
|
@Override
|
347
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
348
|
List<String> propertyPaths = Arrays.asList(new String[] {
|
349
|
"children", //$NON-NLS-1$
|
350
|
"feature", //$NON-NLS-1$
|
351
|
"featureTree", //$NON-NLS-1$
|
352
|
});
|
353
|
Map<Object, List<String>> propertyPathMap =
|
354
|
new HashMap<Object, List<String>>();
|
355
|
propertyPathMap.put(TermNode.class,propertyPaths);
|
356
|
return propertyPathMap;
|
357
|
}
|
358
|
|
359
|
@Override
|
360
|
public List<TermTreeDto> getRootEntities() {
|
361
|
return (List<TermTreeDto>) viewer.getInput();
|
362
|
}
|
363
|
|
364
|
@Override
|
365
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
366
|
}
|
367
|
|
368
|
@Override
|
369
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
370
|
//close view when workbench closes
|
371
|
try{
|
372
|
thisPart.getContext().get(EPartService.class).hidePart(thisPart);
|
373
|
}
|
374
|
catch(Exception e){
|
375
|
//nothing
|
376
|
}
|
377
|
}
|
378
|
|
379
|
@Override
|
380
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
381
|
}
|
382
|
|
383
|
@Override
|
384
|
public void contextRefresh(IProgressMonitor monitor) {
|
385
|
}
|
386
|
|
387
|
@Override
|
388
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
389
|
}
|
390
|
|
391
|
@Override
|
392
|
public void update(CdmDataChangeMap arg0) {
|
393
|
}
|
394
|
|
395
|
@Override
|
396
|
public void changed(Object element) {
|
397
|
dirty.setDirty(true);
|
398
|
viewer.refresh();
|
399
|
}
|
400
|
|
401
|
@Override
|
402
|
public void forceDirty() {
|
403
|
dirty.setDirty(true);
|
404
|
}
|
405
|
|
406
|
@Override
|
407
|
public boolean postOperation(Object objectAffectedByOperation) {
|
408
|
initializeTrees();
|
409
|
viewer.refresh();
|
410
|
if(objectAffectedByOperation instanceof TermNodeDto){
|
411
|
TermNodeDto node = (TermNodeDto)objectAffectedByOperation;
|
412
|
// viewer.expandToLevel(((TermRelationBase) node).getGraph(), 1); find a solution for dto editor
|
413
|
}
|
414
|
if(objectAffectedByOperation!=null){
|
415
|
StructuredSelection selection = new StructuredSelection(objectAffectedByOperation);
|
416
|
viewer.setSelection(selection);
|
417
|
}
|
418
|
return true;
|
419
|
}
|
420
|
|
421
|
@Override
|
422
|
public boolean onComplete() {
|
423
|
return false;
|
424
|
}
|
425
|
|
426
|
@Override
|
427
|
public TermNodeDto getNodeDtoForUuid(UUID nodeUuid){
|
428
|
return uuidTermMap.get(nodeUuid);
|
429
|
}
|
430
|
|
431
|
protected void addAllNodesToMap(TermNodeDto root){
|
432
|
if (!uuidTermMap.containsKey(root.getUuid())){
|
433
|
uuidTermMap.put(root.getUuid(), root);
|
434
|
if (root.getTerm() != null){
|
435
|
getTreeDtoForUuid(root.getTree().getUuid()).addTerm(root.getTerm());
|
436
|
}
|
437
|
}
|
438
|
for (TermNodeDto child: root.getChildren()){
|
439
|
uuidTermMap.put(child.getUuid(), child);
|
440
|
if (child.getTerm() != null){
|
441
|
getTreeDtoForUuid(child.getTree().getUuid()).addTerm(child.getTerm());
|
442
|
}
|
443
|
if (child.getChildren() != null && !child.getChildren().isEmpty()){
|
444
|
addAllNodesToMap(child);
|
445
|
}
|
446
|
}
|
447
|
}
|
448
|
|
449
|
/**
|
450
|
* @param term
|
451
|
* @param treeUuid
|
452
|
*/
|
453
|
@Override
|
454
|
public boolean checkDuplicates(UUID termUuid, UUID treeUuid) {
|
455
|
TermTreeDto tree = this.getTreeDtoForUuid(treeUuid);
|
456
|
for (TermDto dto: tree.getTerms()){
|
457
|
if (dto.getUuid().equals(termUuid)) {
|
458
|
return true;
|
459
|
}
|
460
|
}
|
461
|
return false;
|
462
|
}
|
463
|
|
464
|
protected void updateTrees(){
|
465
|
uuidTermMap.clear();
|
466
|
if (trees != null && !trees.isEmpty()){
|
467
|
trees.clear();
|
468
|
}
|
469
|
if (trees == null){
|
470
|
trees = new HashMap<>();
|
471
|
}
|
472
|
List<TermTreeDto> treeList = CdmStore.getService(ITermTreeService.class).listTermTreeDtosByTermType(getTermType());
|
473
|
treeList.stream().forEach(tree -> trees.put(tree.getUuid(), tree));
|
474
|
// trees.addAll(CdmStore.getService(ITermTreeService.class).listTermTreeDtosByTermType(getTermType()));
|
475
|
for (TermTreeDto tree: trees.values()){
|
476
|
addAllNodesToMap(tree.getRoot());
|
477
|
}
|
478
|
((TermTreeContentProvider<T>)viewer.getContentProvider()).setUuidTermMap(uuidTermMap);
|
479
|
// viewer.refresh();
|
480
|
|
481
|
}
|
482
|
|
483
|
public List<TermTreeDto> getTrees(){
|
484
|
List<TermTreeDto> treeList = new ArrayList<>(trees.values());
|
485
|
return treeList;
|
486
|
}
|
487
|
|
488
|
public void putTree(TermTreeDto tree){
|
489
|
trees.put(tree.getUuid(), tree);
|
490
|
|
491
|
}
|
492
|
|
493
|
public void removeTree(TermTreeDto tree){
|
494
|
trees.remove(tree.getUuid());
|
495
|
|
496
|
}
|
497
|
|
498
|
@Override
|
499
|
public TermTreeDto getTreeDtoForUuid(UUID treeUuid){
|
500
|
return trees.get(treeUuid);
|
501
|
}
|
502
|
|
503
|
@Override
|
504
|
public void setTreeDtoForUuid(TermTreeDto tree){
|
505
|
trees.put(tree.getUuid(), tree);
|
506
|
}
|
507
|
|
508
|
@Override
|
509
|
public void setNodeDtoForUuid(TermNodeDto node){
|
510
|
this.uuidTermMap.put(node.getUuid(), node);
|
511
|
}
|
512
|
|
513
|
@Override
|
514
|
public void addOperation(AbstractPostOperation operation) {
|
515
|
operationList.add(operation);
|
516
|
}
|
517
|
|
518
|
@Inject
|
519
|
@Optional
|
520
|
private void addSaveCandidate(@UIEventTopic(WorkbenchEventConstants.ADD_SAVE_CANDIDATE) UUID cdmbaseUuid) {
|
521
|
for (UUID uuid: uuidTermMap.keySet()) {
|
522
|
if (uuid.equals(cdmbaseUuid)){
|
523
|
listToUpdate.add(uuidTermMap.get(uuid));
|
524
|
}
|
525
|
|
526
|
}
|
527
|
}
|
528
|
|
529
|
@Override
|
530
|
public TermType getTermType() {
|
531
|
return termType;
|
532
|
}
|
533
|
|
534
|
|
535
|
}
|