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