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.
10 package eu
.etaxonomy
.taxeditor
.model
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.List
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.core
.databinding
.observable
.set
.IObservableSet
;
19 import org
.eclipse
.core
.databinding
.observable
.set
.ISetChangeListener
;
20 import org
.eclipse
.core
.databinding
.observable
.set
.SetChangeEvent
;
21 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
22 import org
.eclipse
.jface
.viewers
.TreePath
;
23 import org
.eclipse
.jface
.viewers
.TreeViewer
;
24 import org
.eclipse
.jface
.viewers
.Viewer
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
27 import eu
.etaxonomy
.taxeditor
.TaxEditorPlugin
;
30 * An implementation of ITreeContentProvider which adds taxa displayed in the tree
31 * to the Editor session on an as-loaded basis. Listens for any changes to the
32 * set of session taxa, updates the TreeViewer accordingly.
34 * Could be re-written to implement ILazyTreeContentProvider at some point in the future.
35 * NO: sorting not possible with lazy tree.
41 public class TaxonomicTreeContentProvider
implements ITreeContentProvider
{
42 private static final Logger logger
= Logger
43 .getLogger(TaxonomicTreeContentProvider
.class);
45 Collection
<Taxon
> elements
= new ArrayList
<Taxon
>();
46 private IObservableSet observableSessionTaxonSet
;
50 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
53 public Object
[] getChildren(Object parentElement
) {
54 if (!(parentElement
instanceof Taxon
)) {
58 Taxon parentTaxon
= (Taxon
) parentElement
;
60 // If this returns null, the taxon's children have already
61 // been requested during this session
62 Set
<Taxon
> childTaxa
= TaxEditorPlugin
.getDefault()
63 .getSessionTaxonomicChildren(parentTaxon
);
64 if (childTaxa
== null) {
66 removeSessionTaxaListener();
68 // Request this taxon's children for the first time
69 // during this session
70 childTaxa
= parentTaxon
.getTaxonomicChildren();
71 TaxEditorPlugin
.getDefault().addSessionTaxa(childTaxa
);
73 addSessionTaxaListener();
76 return childTaxa
.toArray();
80 public Object
getParent(Object element
) {
81 if (!(element
instanceof Taxon
)) {
84 return ((Taxon
) element
).getTaxonomicParent();
88 public boolean hasChildren(Object element
) {
89 if (!(element
instanceof Taxon
)) {
92 return ((Taxon
) element
).hasTaxonomicChildren();
96 public Object
[] getElements(Object inputElement
) {
97 return elements
.toArray();
101 public void dispose() {
102 removeSessionTaxaListener();
106 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
107 if (!(viewer
instanceof TreeViewer
)) {
108 throw new IllegalArgumentException(
109 "This content provider only works with viewer of type TreeViewer"); //$NON-NLS-1$
111 this.viewer
= (TreeViewer
) viewer
;
113 if (newInput
!= null && !(newInput
instanceof Collection
)) {
114 throw new IllegalArgumentException(
115 "This content provider only works with input of type Collection<Taxon>"); //$NON-NLS-1$
119 Collection
<Taxon
> input
= (Collection
<Taxon
>) newInput
;
121 } catch (ClassCastException e
) {
122 throw new IllegalArgumentException(
123 "This content provider only works with input of type Collection<Taxon>"); //$NON-NLS-1$
127 private void setInput(Collection
<Taxon
> input
) {
129 removeSessionTaxaListener();
132 elements
.addAll(input
);
133 TaxEditorPlugin
.getDefault().addSessionTaxa(input
);
136 addSessionTaxaListener();
139 private IObservableSet
getObservableSessionTaxonSet() {
140 if (observableSessionTaxonSet
== null) {
141 observableSessionTaxonSet
= TaxEditorPlugin
.getDefault().getObservableSessionTaxa();
143 return observableSessionTaxonSet
;
146 private void addSessionTaxaListener() {
147 getObservableSessionTaxonSet().addSetChangeListener(listener
);
150 private void removeSessionTaxaListener() {
151 getObservableSessionTaxonSet().removeSetChangeListener(listener
);
154 private ISetChangeListener listener
= new ISetChangeListener() {
156 public void handleSetChange(SetChangeEvent event
) {
157 if (viewer
.getControl().isDisposed()) {
160 for (Object addition
: event
.diff
.getAdditions()) {
161 if (addition
instanceof Taxon
) {
162 Taxon taxon
= (Taxon
) addition
;
163 Taxon parentTaxon
= taxon
.getTaxonomicParent();
164 if (parentTaxon
== null) {
166 // If a new root item has been added,
167 // set it as the viewer's input
168 List
<Taxon
> newInput
= new ArrayList
<Taxon
>();
170 viewer
.setInput(newInput
);
172 viewer
.add(parentTaxon
, taxon
);
176 for (Object removal
: event
.diff
.getRemovals()) {
177 logger
.warn("removing " + CdmUtil
.getDisplayName((Taxon
) removal
));
178 if (removal
instanceof Taxon
) {
179 logger
.warn(viewer
.getTree().getItems().length
);
180 Taxon taxon
= (Taxon
) removal
;
181 // Taxon parentTaxon = taxon.getTaxonomicParent();
182 // if (parentTaxon == null) {
183 // viewer.setInput(null);
185 viewer
.remove(taxon
);
187 logger
.warn(viewer
.getTree().getItems().length
);
188 Taxon parentTaxon
= taxon
.getTaxonomicParent();
189 if (parentTaxon
== null) {
190 viewer
.setInput(null);
192 viewer
.remove(taxon
);
193 logger
.warn(viewer
.getTree().getItems().length
);