534e9b59ee28b975cb9273cf68df50441078cc34
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / uses / UsesContentProvider.java
1 package eu.etaxonomy.taxeditor.editor.view.uses;
2
3 import java.util.ArrayList;
4 import java.util.HashSet;
5 import java.util.List;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.UUID;
9
10 import org.eclipse.jface.viewers.ITreeContentProvider;
11
12 import eu.etaxonomy.cdm.api.service.ITermService;
13 import eu.etaxonomy.cdm.model.common.Marker;
14 import eu.etaxonomy.cdm.model.common.MarkerType;
15 import eu.etaxonomy.cdm.model.description.DescriptionBase;
16 import eu.etaxonomy.cdm.model.description.Feature;
17 import eu.etaxonomy.cdm.model.description.FeatureTree;
18 import eu.etaxonomy.cdm.model.description.TaxonDescription;
19 import eu.etaxonomy.cdm.model.taxon.Taxon;
20 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
21 import eu.etaxonomy.taxeditor.editor.UsageTermCollection;
22 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveContentProvider;
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;
27
28 /**
29 * UsesContentProvider Class
30 * @author a.theys
31 * @created mar 13, 2012
32 * @version 1.0
33 */
34 public class UsesContentProvider extends DescriptiveContentProvider implements ITreeContentProvider {
35
36 private static final Object[] NO_CHILDREN = new Object[0];
37 private Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache;
38 Set<MarkerType> markerTypes = new HashSet<MarkerType>();
39
40 /**
41 * <p>Constructor for DescriptiveContentProvider.</p>
42 *
43 * @param featureNodeContainerCache a {@link java.util.Map} object.
44 */
45 public UsesContentProvider(Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache) {
46 super(featureNodeContainerCache);
47 this.featureNodeContainerCache = featureNodeContainerCache;
48 }
49
50
51 private FeatureNodeContainerTree getContainerTreeForDesription(TaxonDescription description){
52 if(! featureNodeContainerCache.containsKey(description)){
53 FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
54 featureNodeContainerCache.put(description, containerTree);
55 }
56 return featureNodeContainerCache.get(description);
57 }
58
59 /**
60 * Retrieves the feature tree associated with the given description
61 *
62 * TODO as of now this is always the same thing because feature trees may not be associated
63 * to descriptions yet.
64 *
65 * @param description
66 * @return
67 */
68 private FeatureTree getFeatureTree(DescriptionBase description){
69 FeatureTree featureTree = null;
70
71 // TODO change this to the feature tree associated with this taxon description
72 if (description.hasStructuredData()){
73 featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
74 }else{
75 featureTree = PreferencesUtil.getDefaultFeatureTreeForTextualDescription();
76 }
77
78 // create a transient tree with all features if none was selected
79 if(featureTree == null){
80 featureTree = FeatureTree.NewInstance(TermStore.getTerms(Feature.class));
81 }
82
83 return featureTree;
84 }
85
86 /**
87 * Get all descriptions associated with the given TaxonEditorInput
88 *
89 * @param parentElement
90 * @return
91 */
92 private List<DescriptionBase> getDescriptions(TaxonEditorInput parentElement) {
93 this.markerTypes.addAll(CdmStore.getTermManager().getPreferredTerms(MarkerType.class));
94 Taxon taxon = parentElement.getTaxon();
95 List<DescriptionBase> descriptions = new ArrayList<DescriptionBase>();
96
97 for(DescriptionBase description : taxon.getDescriptions()){
98 if(! description.isImageGallery()){
99 MarkerType useMarkertype = (MarkerType) CdmStore.getService(ITermService.class).find(UsageTermCollection.uuidUseRecordFeature);
100 Set<Marker> descriptionMarkers = description.getMarkers();
101 if(descriptionMarkers != null) {
102 for (Marker marker: descriptionMarkers) {
103 //TODO Use the GetbyUUID method
104 if(marker.getMarkerType().equals(useMarkertype)) {
105 descriptions.add(description);
106 }
107 }
108 }
109 }
110 }
111 return descriptions;
112 }
113
114 }