4 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
;
6 import java
.util
.ArrayList
;
11 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
12 import org
.eclipse
.jface
.viewers
.Viewer
;
14 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
15 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
16 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
17 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
18 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
19 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
20 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
21 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
22 import eu
.etaxonomy
.taxeditor
.editor
.TaxonEditorInput
;
23 import eu
.etaxonomy
.taxeditor
.editor
.UsageTermCollection
;
24 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
25 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
26 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
27 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
28 import eu
.etaxonomy
.taxeditor
.store
.TermStore
;
31 * <p>DescriptiveContentProvider class.</p>
37 public class DescriptiveContentProvider
implements ITreeContentProvider
{
39 private static final Object
[] NO_CHILDREN
= new Object
[0];
40 private Map
<TaxonDescription
, FeatureNodeContainerTree
> featureNodeContainerCache
;
43 * <p>Constructor for DescriptiveContentProvider.</p>
45 * @param featureNodeContainerCache a {@link java.util.Map} object.
47 public DescriptiveContentProvider(Map
<TaxonDescription
, FeatureNodeContainerTree
> featureNodeContainerCache
) {
48 this.featureNodeContainerCache
= featureNodeContainerCache
;
52 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
55 public Object
[] getChildren(Object parentElement
) {
56 if (parentElement
instanceof TaxonEditorInput
) {
57 return getDescriptions((TaxonEditorInput
) parentElement
).toArray();
59 else if (parentElement
instanceof TaxonDescription
) {
60 if ( ! ((TaxonDescription
) parentElement
).isImageGallery()) {
61 TaxonDescription description
= (TaxonDescription
) parentElement
;
63 FeatureNodeContainerTree containerTree
= getContainerTreeForDesription(description
);
65 return containerTree
.getRoot().getChildren().toArray();
68 else if (parentElement
instanceof FeatureNodeContainer
){
69 FeatureNodeContainer container
= (FeatureNodeContainer
) parentElement
;
70 if(container
.isLeaf()){
71 return container
.getDescriptionElements().toArray();
73 return container
.getChildren().toArray();
80 private FeatureNodeContainerTree
getContainerTreeForDesription(TaxonDescription description
){
81 if(! featureNodeContainerCache
.containsKey(description
)){
82 FeatureNodeContainerTree containerTree
= new FeatureNodeContainerTree(description
, getFeatureTree(description
));
83 featureNodeContainerCache
.put(description
, containerTree
);
85 return featureNodeContainerCache
.get(description
);
90 public boolean hasChildren(Object element
) {
91 if (element
instanceof TaxonDescription
){
92 TaxonDescription description
= (TaxonDescription
) element
;
93 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
94 if(containerTree
!= null && containerTree
.getRoot() != null){
95 return containerTree
.getRoot().getChildren().size() != 0;
98 return getChildren(element
).length
!= 0;
102 * Retrieves the feature tree associated with the given description
104 * TODO as of now this is always the same thing because feature trees may not be associated
105 * to descriptions yet.
110 private FeatureTree
getFeatureTree(DescriptionBase description
){
111 FeatureTree featureTree
= null;
113 // TODO change this to the feature tree associated with this taxon description
114 if (description
.hasStructuredData()){
115 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
117 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForTextualDescription();
120 // create a transient tree with all features if none was selected
121 if(featureTree
== null){
122 featureTree
= FeatureTree
.NewInstance(TermStore
.getTerms(Feature
.class));
129 * Get all descriptions associated with the given TaxonEditorInput
131 * @param parentElement
134 private List
<DescriptionBase
> getDescriptions(TaxonEditorInput parentElement
) {
135 Taxon taxon
= parentElement
.getTaxon();
136 List
<DescriptionBase
> descriptions
= new ArrayList
<DescriptionBase
>();
137 for(DescriptionBase description
: taxon
.getDescriptions()){
138 if(! description
.isImageGallery()){
139 MarkerType useMarkertype
= (MarkerType
) CdmStore
.getService(ITermService
.class).find(UsageTermCollection
.uuidUseMarkerType
);
140 Set
<Marker
> descriptionMarkers
= description
.getMarkers();
141 if(descriptionMarkers
!= null && !descriptionMarkers
.isEmpty()) {
142 for (Marker marker
: descriptionMarkers
) {
143 if(!(marker
.getMarkerType().equals(useMarkertype
))) {
144 descriptions
.add(description
);
149 descriptions
.add(description
);
158 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
161 public Object
getParent(Object element
) {
166 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
169 public Object
[] getElements(Object inputElement
) {
170 return getChildren(inputElement
);
174 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
179 public void dispose() {
180 featureNodeContainerCache
.clear();
184 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
187 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}