4 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
;
6 import java
.util
.ArrayList
;
10 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
11 import org
.eclipse
.jface
.viewers
.Viewer
;
13 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
14 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
15 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
16 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
17 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
18 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
19 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
20 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
21 import eu
.etaxonomy
.taxeditor
.store
.TermStore
;
24 * <p>DescriptiveContentProvider class.</p>
30 public class DescriptiveContentProvider
implements ITreeContentProvider
{
32 private static final Object
[] NO_CHILDREN
= new Object
[0];
33 private Map
<TaxonDescription
, FeatureNodeContainerTree
> featureNodeContainerCache
;
36 * <p>Constructor for DescriptiveContentProvider.</p>
38 * @param featureNodeContainerCache a {@link java.util.Map} object.
40 public DescriptiveContentProvider(Map
<TaxonDescription
, FeatureNodeContainerTree
> featureNodeContainerCache
) {
41 this.featureNodeContainerCache
= featureNodeContainerCache
;
45 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
48 public Object
[] getChildren(Object parentElement
) {
49 if (parentElement
instanceof TaxonEditorInput
) {
50 return getDescriptions((TaxonEditorInput
) parentElement
).toArray();
52 else if (parentElement
instanceof TaxonDescription
) {
53 if ( ! ((TaxonDescription
) parentElement
).isImageGallery()) {
54 TaxonDescription description
= (TaxonDescription
) parentElement
;
56 FeatureNodeContainerTree containerTree
= getContainerTreeForDesription(description
);
58 return containerTree
.getRoot().getChildren().toArray();
61 else if (parentElement
instanceof FeatureNodeContainer
){
62 FeatureNodeContainer container
= (FeatureNodeContainer
) parentElement
;
63 if(container
.isLeaf()){
64 return container
.getDescriptionElements().toArray();
66 return container
.getChildren().toArray();
73 private FeatureNodeContainerTree
getContainerTreeForDesription(TaxonDescription description
){
74 if(! featureNodeContainerCache
.containsKey(description
)){
75 FeatureNodeContainerTree containerTree
= new FeatureNodeContainerTree(description
, getFeatureTree(description
));
76 featureNodeContainerCache
.put(description
, containerTree
);
78 return featureNodeContainerCache
.get(description
);
83 public boolean hasChildren(Object element
) {
84 if (element
instanceof TaxonDescription
){
85 TaxonDescription description
= (TaxonDescription
) element
;
86 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
87 if(containerTree
!= null && containerTree
.getRoot() != null){
88 return containerTree
.getRoot().getChildren().size() != 0;
91 return getChildren(element
).length
!= 0;
95 * Retrieves the feature tree associated with the given description
97 * TODO as of now this is always the same thing because feature trees may not be associated
98 * to descriptions yet.
103 private FeatureTree
getFeatureTree(DescriptionBase description
){
104 FeatureTree featureTree
= null;
106 // TODO change this to the feature tree associated with this taxon description
107 if (description
.hasStructuredData()){
108 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
110 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForTextualDescription();
113 // create a transient tree with all features if none was selected
114 if(featureTree
== null){
115 featureTree
= FeatureTree
.NewInstance(TermStore
.getFeatures());
122 * Get all descriptions associated with the given TaxonEditorInput
124 * @param parentElement
127 private List
<DescriptionBase
> getDescriptions(TaxonEditorInput parentElement
) {
128 Taxon taxon
= parentElement
.getTaxon();
129 List
<DescriptionBase
> descriptions
= new ArrayList
<DescriptionBase
>();
130 for(DescriptionBase description
: taxon
.getDescriptions()){
131 if(! description
.isImageGallery()){
132 descriptions
.add(description
);
139 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
142 public Object
getParent(Object element
) {
147 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
150 public Object
[] getElements(Object inputElement
) {
151 return getChildren(inputElement
);
155 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
160 public void dispose() {
161 featureNodeContainerCache
.clear();
165 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
168 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}