2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.util
;
11 import java
.util
.Collection
;
12 import java
.util
.Collections
;
13 import java
.util
.Comparator
;
14 import java
.util
.List
;
16 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
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
;
28 public class TaxonTreeNodeContentProvider
implements ITreeContentProvider
{
30 private static final Object
[] NO_CHILDREN
= new Object
[0];
32 private ITaxonNodeService taxonNodeService
;
35 public Object
[] getElements(Object inputElement
) {
36 if(inputElement
instanceof Collection
){
37 return ((Collection
<?
>) inputElement
).toArray();
39 return this.getChildren(inputElement
);
43 public Object
[] getChildren(Object parentElement
) {
44 if (taxonNodeService
== null){
45 taxonNodeService
= CdmStore
.getService(ITaxonNodeService
.class);
47 Object
[] children
= null;
49 // if(parentElement instanceof Classification){
50 // children = taxonNodeService.listChildNodesAsUuidAndTitleCache(((Classification)parentElement).getRootNode()).toArray();
51 //// children = ((Classification) parentElement).getChildNodes().toArray();
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();
60 List
<TaxonNodeDto
> childrenList
= null;
62 if(parentElement
instanceof TaxonNode
){
63 childrenList
= taxonNodeService
.listChildNodesAsTaxonNodeDto((TaxonNode
)parentElement
);
65 childrenList
= taxonNodeService
.listChildNodesAsTaxonNodeDto((TaxonNodeDto
)parentElement
);
67 Comparator
<TaxonNodeDto
> comparator
= PreferencesUtil
.getNodeComparator();
68 Collections
.sort(childrenList
, comparator
);
69 children
= childrenList
.toArray();
70 return children
!= null ? children
: NO_CHILDREN
;
74 public Object
getParent(Object element
) {
75 if (taxonNodeService
== null){
76 taxonNodeService
= CdmStore
.getService(ITaxonNodeService
.class);
78 if(element
instanceof TaxonNodeDto
){
79 if (((TaxonNodeDto
) element
).getParentUUID() != null){
80 TaxonNodeDto parentDto
= taxonNodeService
.dto(((TaxonNodeDto
) element
).getParentUUID());
81 // TaxonNode parent = taxonNodeService.load(((TaxonNodeDto) element).getParentUUID());
82 if (parentDto
!= null){
83 // return new TaxonNodeDto(parent);
92 public boolean hasChildren(Object element
) {
93 if(element
instanceof TaxonNode
){
94 return ((TaxonNode
) element
).getCountChildren() > 0;
96 if (element
instanceof TaxonNodeDto
){
97 return ((TaxonNodeDto
)element
).getTaxonomicChildrenCount()>0;
99 return this.getChildren(element
).length
> 0;