2 * Copyright (C) 2007 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
.navigation
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.core
.runtime
.Assert
;
18 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
19 import org
.eclipse
.jface
.viewers
.Viewer
;
21 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
22 import eu
.etaxonomy
.taxeditor
.model
.CdmSessionDataRepository
;
29 public class TaxonomicTreeContentProvider
implements ITreeContentProvider
{
30 private static final Logger logger
= Logger
31 .getLogger(TaxonomicTreeContentProvider
.class);
36 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
38 public Object
[] getChildren(Object parentElement
) {
40 String errorMessage
= "getChildren(parentElement) called with non-Taxon element.";
42 // Make sure we are dealing with taxon objects
44 parentElement
instanceof Taxon
|| parentElement
instanceof Collection
,
47 // Return children for a single taxon
48 if (parentElement
instanceof Taxon
) {
49 return getTaxonChildren((Taxon
) parentElement
).toArray();
52 // Return children for a collection of taxa
53 List
<Taxon
> children
= new ArrayList
<Taxon
>();
54 for (Object parent
: (Collection
) parentElement
) {
56 Assert
.isTrue(parent
instanceof Taxon
, errorMessage
);
57 children
.addAll(getTaxonChildren((Taxon
) parent
));
59 return children
.toArray();
62 private Set
<Taxon
> getTaxonChildren(Taxon parentTaxon
) {
64 CdmSessionDataRepository
.getDefault().getTaxonomicChildren(parentTaxon
);
66 // BUG - Somehow when a child taxon 1) is added with "add quick name", 2) is the
67 // parent taxon's only child, and 3) is then deleted, the QuickNameTaxon singleton
68 // shows up in the parent taxon's children after it is deleted and therefore pops
69 // back up in the tree
70 children
.remove(QuickNameTaxon
.getInstance());
72 if (isQuickNameParent(parentTaxon
)) {
73 children
.add(QuickNameTaxon
.getInstance());
82 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
84 public Object
getParent(Object element
) {
86 Assert
.isTrue(element
instanceof Taxon
,
87 "getParent(element) called with non-Taxon element.");
89 return ((Taxon
) element
).getTaxonomicParent();
95 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
97 public boolean hasChildren(Object element
) {
98 Assert
.isTrue(element
instanceof Taxon
,
99 "hasChildren(element) called with non-Taxon element.");
101 Taxon taxon
= (Taxon
) element
;
103 if (isQuickNameParent(taxon
)) {
106 return taxon
.getTaxonomicChildrenCount() > 0;
110 public boolean isQuickNameParent(Taxon taxon
) {
111 return taxon
.equals(QuickNameTaxon
.getInstance().getTaxonomicParent());
117 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
119 public Object
[] getElements(Object inputElement
) {
120 return CdmSessionDataRepository
.getDefault().getRootTaxa().toArray();
126 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
128 public void dispose() {}
133 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
134 * java.lang.Object, java.lang.Object)
136 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
137 // TODO Auto-generated method stub