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.util;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.Collections;
|
13
|
import java.util.Comparator;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.eclipse.jface.viewers.ITreeContentProvider;
|
17
|
|
18
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
19
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
20
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
21
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
22
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
23
|
|
24
|
/**
|
25
|
* @author pplitzner
|
26
|
* @date 05.09.2017
|
27
|
*/
|
28
|
public class TaxonTreeNodeContentProvider implements ITreeContentProvider {
|
29
|
|
30
|
private static final Object[] NO_CHILDREN = new Object[0];
|
31
|
|
32
|
private ITaxonNodeService taxonNodeService;
|
33
|
|
34
|
@Override
|
35
|
public Object[] getElements(Object inputElement) {
|
36
|
if(inputElement instanceof Collection){
|
37
|
return ((Collection<?>) inputElement).toArray();
|
38
|
}
|
39
|
return this.getChildren(inputElement);
|
40
|
}
|
41
|
|
42
|
@Override
|
43
|
public Object[] getChildren(Object parentElement) {
|
44
|
if (taxonNodeService == null){
|
45
|
taxonNodeService = CdmStore.getService(ITaxonNodeService.class);
|
46
|
}
|
47
|
Object[] children = null;
|
48
|
|
49
|
// if(parentElement instanceof Classification){
|
50
|
// children = taxonNodeService.listChildNodesAsUuidAndTitleCache(((Classification)parentElement).getRootNode()).toArray();
|
51
|
//// children = ((Classification) parentElement).getChildNodes().toArray();
|
52
|
// }
|
53
|
// //taxon node
|
54
|
// if(parentElement instanceof ITaxonTreeNode){
|
55
|
// ITaxonTreeNode treeNode = (ITaxonTreeNode) HibernateProxyHelper.deproxy(parentElement);
|
56
|
// List<TaxonNode> childrenSet = treeNode.getChildNodes();
|
57
|
// HHH_9751_Util.removeAllNull(childrenSet);
|
58
|
// children = childrenSet.toArray();
|
59
|
// }
|
60
|
List<TaxonNodeDto> childrenList = null;
|
61
|
|
62
|
if(parentElement instanceof TaxonNode){
|
63
|
childrenList = taxonNodeService.listChildNodesAsTaxonNodeDto((TaxonNode)parentElement);
|
64
|
}else {
|
65
|
childrenList = taxonNodeService.listChildNodesAsTaxonNodeDto((TaxonNodeDto)parentElement);
|
66
|
}
|
67
|
Comparator<TaxonNodeDto> comparator = PreferencesUtil.getNodeComparator();
|
68
|
Collections.sort(childrenList, comparator);
|
69
|
children = childrenList.toArray();
|
70
|
return children != null ? children : NO_CHILDREN;
|
71
|
}
|
72
|
|
73
|
|
74
|
|
75
|
@Override
|
76
|
public Object getParent(Object element) {
|
77
|
if (taxonNodeService == null){
|
78
|
taxonNodeService = CdmStore.getService(ITaxonNodeService.class);
|
79
|
}
|
80
|
if(element instanceof TaxonNodeDto){
|
81
|
if (((TaxonNodeDto) element).getParentUUID() != null){
|
82
|
TaxonNodeDto parentDto = taxonNodeService.dto(((TaxonNodeDto) element).getParentUUID());
|
83
|
// TaxonNode parent = taxonNodeService.load(((TaxonNodeDto) element).getParentUUID());
|
84
|
if (parentDto != null){
|
85
|
// return new TaxonNodeDto(parent);
|
86
|
return parentDto;
|
87
|
}
|
88
|
}
|
89
|
}
|
90
|
return null;
|
91
|
}
|
92
|
|
93
|
@Override
|
94
|
public boolean hasChildren(Object element) {
|
95
|
if(element instanceof TaxonNode){
|
96
|
return ((TaxonNode) element).getCountChildren() > 0;
|
97
|
}
|
98
|
if (element instanceof TaxonNodeDto){
|
99
|
return ((TaxonNodeDto)element).getTaxonomicChildrenCount()>0;
|
100
|
}
|
101
|
return this.getChildren(element).length > 0;
|
102
|
}
|
103
|
}
|