Merge branch 'develop' into wset
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / util / TaxonTreeNodeContentProvider.java
1 // $Id$
2 /**
3 * Copyright (C) 2017 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.taxeditor.util;
11
12 import java.util.Collection;
13 import java.util.List;
14
15 import org.eclipse.jface.viewers.ITreeContentProvider;
16
17 import eu.etaxonomy.cdm.hibernate.HHH_9751_Util;
18 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19 import eu.etaxonomy.cdm.model.taxon.Classification;
20 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
21 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
22
23 /**
24 * @author pplitzner
25 * @date 05.09.2017
26 *
27 */
28 public class TaxonTreeNodeContentProvider implements ITreeContentProvider {
29
30 private static final Object[] NO_CHILDREN = new Object[0];
31
32 /**
33 * {@inheritDoc}
34 */
35 @Override
36 public Object[] getElements(Object inputElement) {
37 if(inputElement instanceof Collection){
38 return ((Collection) inputElement).toArray();
39 }
40 return this.getChildren(inputElement);
41 }
42
43 /**
44 * {@inheritDoc}
45 */
46 @Override
47 public Object[] getChildren(Object parentElement) {
48 Object[] children = null;
49
50 if(parentElement instanceof Classification){
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 return children != null ? children : NO_CHILDREN;
61 }
62
63 /**
64 * {@inheritDoc}
65 */
66 @Override
67 public Object getParent(Object element) {
68 if(element instanceof TaxonNode){
69 return ((TaxonNode) element).getParent();
70 }
71 return null;
72 }
73
74 /**
75 * {@inheritDoc}
76 */
77 @Override
78 public boolean hasChildren(Object element) {
79 if(element instanceof TaxonNode){
80 return ((TaxonNode) element).getCountChildren() > 0;
81 }
82 return this.getChildren(element).length > 0;
83 }
84
85 }