4 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collections
;
8 import java
.util
.Comparator
;
13 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
14 import org
.eclipse
.jface
.viewers
.Viewer
;
16 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
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
.model
.FeatureNodeContainer
;
24 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainerTree
;
25 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
26 import eu
.etaxonomy
.taxeditor
.store
.TermStore
;
27 import eu
.etaxonomy
.taxeditor
.store
.UsageTermCollection
;
30 * <p>DescriptiveContentProvider class.</p>
36 public class DescriptiveContentProvider
implements ITreeContentProvider
{
38 protected static final Object
[] NO_CHILDREN
= new Object
[0];
39 protected Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
;
40 private boolean showOnlyIndividualAssociations
;
42 public DescriptiveContentProvider(Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
) {
43 this(featureNodeContainerCache
, false);
46 * <p>Constructor for DescriptiveContentProvider.</p>
48 * @param featureNodeContainerCache a {@link java.util.Map} object.
50 public DescriptiveContentProvider(Map
<DescriptionBase
<?
>, FeatureNodeContainerTree
> featureNodeContainerCache
, boolean showOnlyIndividualAssociations
) {
51 this.featureNodeContainerCache
= featureNodeContainerCache
;
52 this.showOnlyIndividualAssociations
= showOnlyIndividualAssociations
;
56 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
60 public Object
[] getChildren(Object parentElement
) {
61 if (parentElement
instanceof IDescribable
<?
>) {
62 return getDescriptions((IDescribable
<?
>) parentElement
).toArray();
64 else if (parentElement
instanceof DescriptionBase
<?
>) {
65 if ( ! ((DescriptionBase
<?
>) parentElement
).isImageGallery()) {
66 DescriptionBase
<?
> description
= (DescriptionBase
<?
>) parentElement
;
68 FeatureNodeContainerTree containerTree
= getContainerTreeForDesription(description
);
69 List
<FeatureNodeContainer
> children
= containerTree
.getRoot().getChildren();
70 //filter out containers with no children
71 List
<FeatureNodeContainer
> childrenWithChildren
= new ArrayList
<FeatureNodeContainer
>();
72 for (FeatureNodeContainer featureNodeContainer
: children
) {
73 if(getChildren(featureNodeContainer
).length
>0){
74 childrenWithChildren
.add(featureNodeContainer
);
77 return childrenWithChildren
.toArray();
80 else if (parentElement
instanceof FeatureNodeContainer
){
81 FeatureNodeContainer container
= (FeatureNodeContainer
) parentElement
;
82 if(container
.isLeaf()){
83 List
<DescriptionElementBase
> descriptionElements
= container
.getDescriptionElements();
84 List
<IndividualsAssociation
> individualAssociations
= new ArrayList
<IndividualsAssociation
>();
85 for (DescriptionElementBase descriptionElement
: descriptionElements
) {
86 if(descriptionElement
instanceof IndividualsAssociation
){
87 individualAssociations
.add((IndividualsAssociation
) descriptionElement
);
90 //sort individual associations by title cache of associated specimens
91 Collections
.sort(individualAssociations
, new Comparator
<IndividualsAssociation
>() {
94 public int compare(IndividualsAssociation o1
, IndividualsAssociation o2
) {
95 if(o1
==null || o1
.getAssociatedSpecimenOrObservation()==null){
98 if(o2
==null || o2
.getAssociatedSpecimenOrObservation()==null){
101 return o1
.getAssociatedSpecimenOrObservation().compareTo(o2
.getAssociatedSpecimenOrObservation());
104 if(showOnlyIndividualAssociations
){
105 descriptionElements
= new ArrayList
<DescriptionElementBase
>(individualAssociations
);
107 return descriptionElements
.toArray();
109 return container
.getChildren().toArray();
116 private FeatureNodeContainerTree
getContainerTreeForDesription(DescriptionBase
<?
> description
){
117 if(! featureNodeContainerCache
.containsKey(description
)){
118 FeatureNodeContainerTree containerTree
= new FeatureNodeContainerTree(description
, getFeatureTree(description
));
119 featureNodeContainerCache
.put(description
, containerTree
);
121 return featureNodeContainerCache
.get(description
);
126 public boolean hasChildren(Object element
) {
127 if (element
instanceof DescriptionBase
<?
>){
128 DescriptionBase
<?
> description
= (DescriptionBase
<?
>) element
;
129 FeatureNodeContainerTree containerTree
= featureNodeContainerCache
.get(description
);
130 if(containerTree
!= null && containerTree
.getRoot() != null){
131 return containerTree
.getRoot().getChildren().size() != 0;
134 return getChildren(element
).length
!= 0;
138 * Retrieves the feature tree associated with the given description
140 * TODO as of now this is always the same thing because feature trees may not be associated
141 * to descriptions yet.
146 private FeatureTree
getFeatureTree(DescriptionBase description
){
147 FeatureTree featureTree
= null;
149 // TODO change this to the feature tree associated with this taxon description
150 if (description
.hasStructuredData()){
151 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForStructuredDescription();
153 featureTree
= PreferencesUtil
.getDefaultFeatureTreeForTextualDescription();
156 // create a transient tree with all features if none was selected
157 if(featureTree
== null){
158 featureTree
= FeatureTree
.NewInstance(TermStore
.getTerms(Feature
.class));
165 * Get all descriptions associated with the given object
166 * @param parentElement
169 protected List
<DescriptionBase
<?
>> getDescriptions(IDescribable
<?
> parentElement
) {
170 Set
<?
extends DescriptionBase
<?
>> elementDescriptions
= parentElement
.getDescriptions();
171 List
<DescriptionBase
<?
>> resultDescriptions
= new ArrayList
<DescriptionBase
<?
>>();
172 for(DescriptionBase
<?
> description
: elementDescriptions
){
173 if(! description
.isImageGallery()){
174 Set
<Marker
> descriptionMarkers
= description
.getMarkers();
175 if(descriptionMarkers
!= null && !descriptionMarkers
.isEmpty()) {
176 for (Marker marker
: descriptionMarkers
) {
177 if(marker
.getMarkerType() != null && !(marker
.getMarkerType().getUuid().equals(UsageTermCollection
.uuidUseMarkerType
))) {
178 resultDescriptions
.add(description
);
183 resultDescriptions
.add(description
);
187 return resultDescriptions
;
192 public Object
getParent(Object element
) {
198 public Object
[] getElements(Object inputElement
) {
199 return getChildren(inputElement
);
206 public void dispose() {
207 featureNodeContainerCache
.clear();
212 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {}
214 public void toggleShowOnlyIndividualAssociations() {
215 showOnlyIndividualAssociations
= !showOnlyIndividualAssociations
;