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 protected static final Object
[] NO_CHILDREN
= new Object
[0];
40 protected 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)
56 public Object
[] getChildren(Object parentElement
) {
57 if (parentElement
instanceof TaxonEditorInput
) {
58 return getDescriptions((TaxonEditorInput
) parentElement
).toArray();
60 else if (parentElement
instanceof TaxonDescription
) {
61 if ( ! ((TaxonDescription
) parentElement
).isImageGallery()) {
62 TaxonDescription description
= (TaxonDescription
) parentElement
;
64 FeatureNodeContainerTree containerTree
= getContainerTreeForDesription(description
);
66 return containerTree
.getRoot().getChildren().toArray();
69 else if (parentElement
instanceof FeatureNodeContainer
){
70 FeatureNodeContainer container
= (FeatureNodeContainer
) parentElement
;
71 if(container
.isLeaf()){
72 return container
.getDescriptionElements().toArray();
74 return container
.getChildren().toArray();
81 private FeatureNodeContainerTree
getContainerTreeForDesription(TaxonDescription description
){
82 if(! featureNodeContainerCache
.containsKey(description
)){
83 FeatureNodeContainerTree containerTree
= new FeatureNodeContainerTree(description
, getFeatureTree(description
));
84 featureNodeContainerCache
.put(description
, containerTree
);
86 return featureNodeContainerCache
.get(description
);
91 public boolean hasChildren(Object element
) {
92 if (element
instanceof TaxonDescription
){
93 TaxonDescription description
= (TaxonDescription
) element
;
94 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
95 if(containerTree
!= null && containerTree
.getRoot() != null){
96 return containerTree
.getRoot().getChildren().size() != 0;
99 return getChildren(element
).length
!= 0;
103 * Retrieves the feature tree associated with the given description
105 * TODO as of now this is always the same thing because feature trees may not be associated
106 * to descriptions yet.
111 private FeatureTree
getFeatureTree(DescriptionBase description
){
112 FeatureTree featureTree
= null;
114 // TODO change this to the feature tree associated with this taxon description
115 if (description
.hasStructuredData()){
116 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
118 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForTextualDescription();
121 // create a transient tree with all features if none was selected
122 if(featureTree
== null){
123 featureTree
= FeatureTree
.NewInstance(TermStore
.getTerms(Feature
.class));
130 * Get all descriptions associated with the given TaxonEditorInput
132 * @param parentElement
135 protected List
<DescriptionBase
> getDescriptions(TaxonEditorInput parentElement
) {
136 Taxon taxon
= parentElement
.getTaxon();
137 List
<DescriptionBase
> descriptions
= new ArrayList
<DescriptionBase
>();
138 for(DescriptionBase description
: taxon
.getDescriptions()){
139 if(! description
.isImageGallery()){
140 MarkerType useMarkertype
= (MarkerType
) CdmStore
.getService(ITermService
.class).find(UsageTermCollection
.uuidUseMarkerType
);
141 Set
<Marker
> descriptionMarkers
= description
.getMarkers();
142 if(descriptionMarkers
!= null && !descriptionMarkers
.isEmpty()) {
143 for (Marker marker
: descriptionMarkers
) {
144 if(!(marker
.getMarkerType().equals(useMarkertype
))) {
145 descriptions
.add(description
);
150 descriptions
.add(description
);
159 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
163 public Object
getParent(Object element
) {
168 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
172 public Object
[] getElements(Object inputElement
) {
173 return getChildren(inputElement
);
177 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
183 public void dispose() {
184 featureNodeContainerCache
.clear();
188 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
192 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}