307f026dec5f03645fa36425a7f5f0b7fc078735
[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 import org.eclipse.jface.viewers.Viewer;
12
13 import eu.etaxonomy.cdm.api.service.IDescriptionService;
14 import eu.etaxonomy.cdm.model.common.Marker;
15 import eu.etaxonomy.cdm.model.common.MarkerType;
16 import eu.etaxonomy.cdm.model.description.DescriptionBase;
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.cdm.persistence.dao.common.IDefinedTermDao;
21 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
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;
27
28 public class UsesContentProvider implements ITreeContentProvider {
29
30 private static final Object[] NO_CHILDREN = new Object[0];
31 private Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache;
32 Set<MarkerType> markerTypes = new HashSet<MarkerType>();
33
34 /**
35 * <p>Constructor for DescriptiveContentProvider.</p>
36 *
37 * @param featureNodeContainerCache a {@link java.util.Map} object.
38 */
39 public UsesContentProvider(Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache) {
40 //MarkerType markerType = (MarkerType)this.definedTermDao.findByUuid(UUID.fromString("b4b1b2ab-89a8-4ce6-8110-d60b8b1bc433")); //Marker "complete"
41 //this.markerTypes.addAll(CdmStore.getTermManager().getPreferredMarkerTypes());
42 this.featureNodeContainerCache = featureNodeContainerCache;
43 }
44
45
46
47 /* (non-Javadoc)
48 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
49 */
50 /** {@inheritDoc} */
51 public Object[] getChildren(Object parentElement) {
52 if (parentElement instanceof TaxonEditorInput) {
53 return getDescriptions((TaxonEditorInput) parentElement).toArray();
54 }
55 else if (parentElement instanceof TaxonDescription) {
56 if ( ! ((TaxonDescription) parentElement).isImageGallery()) {
57 TaxonDescription description = (TaxonDescription) parentElement;
58
59 FeatureNodeContainerTree containerTree = getContainerTreeForDesription(description);
60
61 return containerTree.getRoot().getChildren().toArray();
62 }
63 }
64 else if (parentElement instanceof FeatureNodeContainer){
65 FeatureNodeContainer container = (FeatureNodeContainer) parentElement;
66 if(container.isLeaf()){
67 return container.getDescriptionElements().toArray();
68 }else{
69 return container.getChildren().toArray();
70 }
71 }
72
73 return NO_CHILDREN;
74 }
75
76 private FeatureNodeContainerTree getContainerTreeForDesription(TaxonDescription description){
77 if(! featureNodeContainerCache.containsKey(description)){
78 FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
79 featureNodeContainerCache.put(description, containerTree);
80 }
81 return featureNodeContainerCache.get(description);
82 }
83
84 /** {@inheritDoc} */
85 @Override
86 public boolean hasChildren(Object element) {
87 if (element instanceof TaxonDescription){
88 TaxonDescription description = (TaxonDescription) element;
89 FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
90 if(containerTree != null && containerTree.getRoot() != null){
91 return containerTree.getRoot().getChildren().size() != 0;
92 }
93 }
94 return getChildren(element).length != 0;
95 }
96
97 /**
98 * Retrieves the feature tree associated with the given description
99 *
100 * TODO as of now this is always the same thing because feature trees may not be associated
101 * to descriptions yet.
102 *
103 * @param description
104 * @return
105 */
106 private FeatureTree getFeatureTree(DescriptionBase description){
107 FeatureTree featureTree = null;
108
109 // TODO change this to the feature tree associated with this taxon description
110 if (description.hasStructuredData()){
111 featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
112 }else{
113 featureTree = PreferencesUtil.getDefaultFeatureTreeForTextualDescription();
114 }
115
116 // create a transient tree with all features if none was selected
117 if(featureTree == null){
118 featureTree = FeatureTree.NewInstance(TermStore.getFeatures());
119 }
120
121 return featureTree;
122 }
123
124 /**
125 * Get all descriptions associated with the given TaxonEditorInput
126 *
127 * @param parentElement
128 * @return
129 */
130 //CdmStore.getService(IDescriptionService.class).listTaxonDescriptions(input.getTaxonNode().getTaxon(), null, null, null, null, null, null)
131 private List<DescriptionBase> getDescriptions(TaxonEditorInput parentElement) {
132 this.markerTypes.addAll(CdmStore.getTermManager().getPreferredMarkerTypes());
133 Taxon taxon = parentElement.getTaxon();
134 Set<MarkerType> typesToDisdplay = new HashSet<MarkerType>();
135 List<DescriptionBase> descriptions = new ArrayList<DescriptionBase>();
136 List<TaxonDescription> usesDescriptions = new ArrayList<TaxonDescription>();
137
138 /*for (MarkerType markerType : markerTypes) {
139 if (markerType.toString().equals("complete")) {
140 typesToDisdplay.add(markerType);
141 }
142 }*/
143 //usesDescriptions = CdmStore.getService(IDescriptionService.class).listTaxonDescriptions(taxon, null, null, typesToDisdplay, null, null, null);
144 //for(DescriptionBase description : CdmStore.getService(IDescriptionService.class).listTaxonDescriptions(taxon, null, null, typesToDisdplay, null, null, null)){
145 for(DescriptionBase description : taxon.getDescriptions()){
146 if(! description.isImageGallery()){
147 Set<Marker> descriptionMarkers = description.getMarkers();
148 if(descriptionMarkers != null) {
149 for (Marker marker: descriptionMarkers) {
150 if(marker.getMarkerType().getTitleCache().equals("use")) {
151 descriptions.add(description);
152 }
153 }
154 }
155 }
156 }
157 return descriptions;
158 }
159
160 /* (non-Javadoc)
161 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
162 */
163 /** {@inheritDoc} */
164 public Object getParent(Object element) {
165 return null;
166 }
167
168 /* (non-Javadoc)
169 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
170 */
171 /** {@inheritDoc} */
172 public Object[] getElements(Object inputElement) {
173 return getChildren(inputElement);
174 }
175
176 /* (non-Javadoc)
177 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
178 */
179 /**
180 * <p>dispose</p>
181 */
182 public void dispose() {
183 featureNodeContainerCache.clear();
184 }
185
186 /* (non-Javadoc)
187 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
188 */
189 /** {@inheritDoc} */
190 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
191
192 }