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
.IDescribable
;
21 import eu
.etaxonomy
.taxeditor
.editor
.UsageTermCollection
;
22 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
23 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
24 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
25 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
26 import eu
.etaxonomy
.taxeditor
.store
.TermStore
;
29 * <p>DescriptiveContentProvider class.</p>
35 public class DescriptiveContentProvider
implements ITreeContentProvider
{
37 protected static final Object
[] NO_CHILDREN
= new Object
[0];
38 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
;
41 * <p>Constructor for DescriptiveContentProvider.</p>
43 * @param featureNodeContainerCache a {@link java.util.Map} object.
45 public DescriptiveContentProvider(Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
) {
46 this.featureNodeContainerCache
= featureNodeContainerCache
;
50 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
54 public Object
[] getChildren(Object parentElement
) {
55 if (parentElement
instanceof IDescribable
<?
>) {
56 return getDescriptions((IDescribable
<?
>) parentElement
).toArray();
58 else if (parentElement
instanceof DescriptionBase
<?
>) {
59 if ( ! ((DescriptionBase
<?
>) parentElement
).isImageGallery()) {
60 DescriptionBase
<?
> description
= (DescriptionBase
<?
>) parentElement
;
62 FeatureNodeContainerTree containerTree
= getContainerTreeForDesription(description
);
64 return containerTree
.getRoot().getChildren().toArray();
67 else if (parentElement
instanceof FeatureNodeContainer
){
68 FeatureNodeContainer container
= (FeatureNodeContainer
) parentElement
;
69 if(container
.isLeaf()){
70 return container
.getDescriptionElements().toArray();
72 return container
.getChildren().toArray();
79 private FeatureNodeContainerTree
getContainerTreeForDesription(DescriptionBase
<?
> description
){
80 if(! featureNodeContainerCache
.containsKey(description
)){
81 FeatureNodeContainerTree containerTree
= new FeatureNodeContainerTree(description
, getFeatureTree(description
));
82 featureNodeContainerCache
.put(description
, containerTree
);
84 return featureNodeContainerCache
.get(description
);
89 public boolean hasChildren(Object element
) {
90 if (element
instanceof DescriptionBase
<?
>){
91 DescriptionBase
<?
> description
= (DescriptionBase
<?
>) element
;
92 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
93 if(containerTree
!= null && containerTree
.getRoot() != null){
94 return containerTree
.getRoot().getChildren().size() != 0;
97 return getChildren(element
).length
!= 0;
101 * Retrieves the feature tree associated with the given description
103 * TODO as of now this is always the same thing because feature trees may not be associated
104 * to descriptions yet.
109 private FeatureTree
getFeatureTree(DescriptionBase description
){
110 FeatureTree featureTree
= null;
112 // TODO change this to the feature tree associated with this taxon description
113 if (description
.hasStructuredData()){
114 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
116 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForTextualDescription();
119 // create a transient tree with all features if none was selected
120 if(featureTree
== null){
121 featureTree
= FeatureTree
.NewInstance(TermStore
.getTerms(Feature
.class));
128 * Get all descriptions associated with the given TaxonEditorInput
130 * @param parentElement
133 protected List
<DescriptionBase
> getDescriptions(IDescribable
<?
> parentElement
) {
134 Set
<?
extends DescriptionBase
<?
>> elementDescriptions
= parentElement
.getDescriptions();
135 List
<DescriptionBase
> resultDescriptions
= new ArrayList
<DescriptionBase
>();
136 for(DescriptionBase description
: elementDescriptions
){
137 if(! description
.isImageGallery()){
138 MarkerType useMarkertype
= (MarkerType
) CdmStore
.getService(ITermService
.class).find(UsageTermCollection
.uuidUseMarkerType
);
139 Set
<Marker
> descriptionMarkers
= description
.getMarkers();
140 if(descriptionMarkers
!= null && !descriptionMarkers
.isEmpty()) {
141 for (Marker marker
: descriptionMarkers
) {
142 if(!(marker
.getMarkerType().equals(useMarkertype
))) {
143 resultDescriptions
.add(description
);
148 resultDescriptions
.add(description
);
153 return resultDescriptions
;
157 * @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)
170 public Object
[] getElements(Object inputElement
) {
171 return getChildren(inputElement
);
175 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
181 public void dispose() {
182 featureNodeContainerCache
.clear();
186 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
190 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}