p2izing the editor
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / navigation / TaxonomicTreeContentProvider.java
1 /**
2 * Copyright (C) 2007 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.navigation;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.Set;
15
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;
20
21 import eu.etaxonomy.cdm.model.taxon.Taxon;
22 import eu.etaxonomy.taxeditor.model.CdmSessionDataRepository;
23
24 /**
25 * @author p.ciardelli
26 * @created 27.06.2008
27 * @version 1.0
28 */
29 public class TaxonomicTreeContentProvider implements ITreeContentProvider {
30 private static final Logger logger = Logger
31 .getLogger(TaxonomicTreeContentProvider.class);
32
33 /*
34 * (non-Javadoc)
35 *
36 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
37 */
38 public Object[] getChildren(Object parentElement) {
39
40 String errorMessage = "getChildren(parentElement) called with non-Taxon element.";
41
42 // Make sure we are dealing with taxon objects
43 Assert.isTrue(
44 parentElement instanceof Taxon || parentElement instanceof Collection,
45 errorMessage);
46
47 // Return children for a single taxon
48 if (parentElement instanceof Taxon) {
49 return getTaxonChildren((Taxon) parentElement).toArray();
50 }
51
52 // Return children for a collection of taxa
53 List<Taxon> children = new ArrayList<Taxon>();
54 for (Object parent : (Collection) parentElement) {
55
56 Assert.isTrue(parent instanceof Taxon, errorMessage);
57 children.addAll(getTaxonChildren((Taxon) parent));
58 }
59 return children.toArray();
60 }
61
62 private Set<Taxon> getTaxonChildren(Taxon parentTaxon) {
63 Set<Taxon> children =
64 CdmSessionDataRepository.getDefault().getTaxonomicChildren(parentTaxon);
65
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());
71
72 if (isQuickNameParent(parentTaxon)) {
73 children.add(QuickNameTaxon.getInstance());
74 }
75
76 return children;
77 }
78
79 /*
80 * (non-Javadoc)
81 *
82 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
83 */
84 public Object getParent(Object element) {
85
86 Assert.isTrue(element instanceof Taxon,
87 "getParent(element) called with non-Taxon element.");
88
89 return ((Taxon) element).getTaxonomicParent();
90 }
91
92 /*
93 * (non-Javadoc)
94 *
95 * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
96 */
97 public boolean hasChildren(Object element) {
98 Assert.isTrue(element instanceof Taxon,
99 "hasChildren(element) called with non-Taxon element.");
100
101 Taxon taxon = (Taxon) element;
102
103 if (isQuickNameParent(taxon)) {
104 return true;
105 } else {
106 return taxon.getTaxonomicChildrenCount() > 0;
107 }
108 }
109
110 public boolean isQuickNameParent(Taxon taxon) {
111 return taxon.equals(QuickNameTaxon.getInstance().getTaxonomicParent());
112 }
113
114 /*
115 * (non-Javadoc)
116 *
117 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
118 */
119 public Object[] getElements(Object inputElement) {
120 return CdmSessionDataRepository.getDefault().getRootTaxa().toArray();
121 }
122
123 /*
124 * (non-Javadoc)
125 *
126 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
127 */
128 public void dispose() {}
129
130 /*
131 * (non-Javadoc)
132 *
133 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
134 * java.lang.Object, java.lang.Object)
135 */
136 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
137 // TODO Auto-generated method stub
138 }
139 }