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