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
.DescriptionElementBase
;
19 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
20 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
21 import eu
.etaxonomy
.cdm
.model
.description
.IDescribable
;
22 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
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
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
;
41 private boolean showOnlyIndividualAssociations
;
43 public DescriptiveContentProvider(Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
) {
44 this(featureNodeContainerCache
, false);
47 * <p>Constructor for DescriptiveContentProvider.</p>
49 * @param featureNodeContainerCache a {@link java.util.Map} object.
51 public DescriptiveContentProvider(Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
, boolean showOnlyIndividualAssociations
) {
52 this.featureNodeContainerCache
= featureNodeContainerCache
;
53 this.showOnlyIndividualAssociations
= showOnlyIndividualAssociations
;
57 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
61 public Object
[] getChildren(Object parentElement
) {
62 if (parentElement
instanceof IDescribable
<?
>) {
63 return getDescriptions((IDescribable
<?
>) parentElement
).toArray();
65 else if (parentElement
instanceof DescriptionBase
<?
>) {
66 if ( ! ((DescriptionBase
<?
>) parentElement
).isImageGallery()) {
67 DescriptionBase
<?
> description
= (DescriptionBase
<?
>) parentElement
;
69 FeatureNodeContainerTree containerTree
= getContainerTreeForDesription(description
);
70 List
<FeatureNodeContainer
> children
= containerTree
.getRoot().getChildren();
71 //filter out containers with no children
72 List
<FeatureNodeContainer
> childrenWithChildren
= new ArrayList
<FeatureNodeContainer
>();
73 for (FeatureNodeContainer featureNodeContainer
: children
) {
74 if(getChildren(featureNodeContainer
).length
>0){
75 childrenWithChildren
.add(featureNodeContainer
);
78 return childrenWithChildren
.toArray();
81 else if (parentElement
instanceof FeatureNodeContainer
){
82 FeatureNodeContainer container
= (FeatureNodeContainer
) parentElement
;
83 if(container
.isLeaf()){
84 List
<DescriptionElementBase
> descriptionElements
= container
.getDescriptionElements();
85 if(showOnlyIndividualAssociations
){
86 List
<DescriptionElementBase
> filteredDescriptionElements
= new ArrayList
<DescriptionElementBase
>();
87 for (DescriptionElementBase descriptionElement
: descriptionElements
) {
88 if(descriptionElement
instanceof IndividualsAssociation
){
89 filteredDescriptionElements
.add(descriptionElement
);
92 descriptionElements
= filteredDescriptionElements
;
94 return descriptionElements
.toArray();
96 return container
.getChildren().toArray();
103 private FeatureNodeContainerTree
getContainerTreeForDesription(DescriptionBase
<?
> description
){
104 if(! featureNodeContainerCache
.containsKey(description
)){
105 FeatureNodeContainerTree containerTree
= new FeatureNodeContainerTree(description
, getFeatureTree(description
));
106 featureNodeContainerCache
.put(description
, containerTree
);
108 return featureNodeContainerCache
.get(description
);
113 public boolean hasChildren(Object element
) {
114 if (element
instanceof DescriptionBase
<?
>){
115 DescriptionBase
<?
> description
= (DescriptionBase
<?
>) element
;
116 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
117 if(containerTree
!= null && containerTree
.getRoot() != null){
118 return containerTree
.getRoot().getChildren().size() != 0;
121 return getChildren(element
).length
!= 0;
125 * Retrieves the feature tree associated with the given description
127 * TODO as of now this is always the same thing because feature trees may not be associated
128 * to descriptions yet.
133 private FeatureTree
getFeatureTree(DescriptionBase description
){
134 FeatureTree featureTree
= null;
136 // TODO change this to the feature tree associated with this taxon description
137 if (description
.hasStructuredData()){
138 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
140 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForTextualDescription();
143 // create a transient tree with all features if none was selected
144 if(featureTree
== null){
145 featureTree
= FeatureTree
.NewInstance(TermStore
.getTerms(Feature
.class));
152 * Get all descriptions associated with the given object
153 * @param parentElement
156 protected List
<DescriptionBase
<?
>> getDescriptions(IDescribable
<?
> parentElement
) {
157 Set
<?
extends DescriptionBase
<?
>> elementDescriptions
= parentElement
.getDescriptions();
158 List
<DescriptionBase
<?
>> resultDescriptions
= new ArrayList
<DescriptionBase
<?
>>();
159 for(DescriptionBase
<?
> description
: elementDescriptions
){
160 if(! description
.isImageGallery()){
161 MarkerType useMarkertype
= (MarkerType
) CdmStore
.getService(ITermService
.class).find(UsageTermCollection
.uuidUseMarkerType
);
162 Set
<Marker
> descriptionMarkers
= description
.getMarkers();
163 if(descriptionMarkers
!= null && !descriptionMarkers
.isEmpty()) {
164 for (Marker marker
: descriptionMarkers
) {
165 if(!(marker
.getMarkerType().equals(useMarkertype
))) {
166 resultDescriptions
.add(description
);
171 resultDescriptions
.add(description
);
175 return resultDescriptions
;
179 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
183 public Object
getParent(Object element
) {
188 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
192 public Object
[] getElements(Object inputElement
) {
193 return getChildren(inputElement
);
197 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
203 public void dispose() {
204 featureNodeContainerCache
.clear();
208 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
212 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}
214 public void toggleShowOnlyIndividualAssociations() {
215 showOnlyIndividualAssociations
= !showOnlyIndividualAssociations
;