ddbb3525abc1c795239678ee843aa39b4dae0983
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / DescriptiveContentProvider.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.editor.view.descriptive;
5
6 import java.util.ArrayList;
7 import java.util.List;
8 import java.util.Map;
9 import java.util.Set;
10
11 import org.eclipse.jface.viewers.ITreeContentProvider;
12 import org.eclipse.jface.viewers.Viewer;
13
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;
29
30 /**
31 * <p>DescriptiveContentProvider class.</p>
32 *
33 * @author p.ciardelli
34 * @author n.hoffmann
35 * @version $Id: $
36 */
37 public class DescriptiveContentProvider implements ITreeContentProvider {
38
39 protected static final Object[] NO_CHILDREN = new Object[0];
40 protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache;
41 private boolean showOnlyIndividualAssociations;
42
43 public DescriptiveContentProvider(Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache) {
44 this(featureNodeContainerCache, false);
45 }
46 /**
47 * <p>Constructor for DescriptiveContentProvider.</p>
48 *
49 * @param featureNodeContainerCache a {@link java.util.Map} object.
50 */
51 public DescriptiveContentProvider(Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache, boolean showOnlyIndividualAssociations) {
52 this.featureNodeContainerCache = featureNodeContainerCache;
53 this.showOnlyIndividualAssociations = showOnlyIndividualAssociations;
54 }
55
56 /* (non-Javadoc)
57 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
58 */
59 /** {@inheritDoc} */
60 @Override
61 public Object[] getChildren(Object parentElement) {
62 if (parentElement instanceof IDescribable<?>) {
63 return getDescriptions((IDescribable<?>) parentElement).toArray();
64 }
65 else if (parentElement instanceof DescriptionBase<?>) {
66 if ( ! ((DescriptionBase<?>) parentElement).isImageGallery()) {
67 DescriptionBase<?> description = (DescriptionBase<?>) parentElement;
68
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);
76 }
77 }
78 return childrenWithChildren.toArray();
79 }
80 }
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);
90 }
91 }
92 descriptionElements = filteredDescriptionElements;
93 }
94 return descriptionElements.toArray();
95 }else{
96 return container.getChildren().toArray();
97 }
98 }
99
100 return NO_CHILDREN;
101 }
102
103 private FeatureNodeContainerTree getContainerTreeForDesription(DescriptionBase<?> description){
104 if(! featureNodeContainerCache.containsKey(description)){
105 FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
106 featureNodeContainerCache.put(description, containerTree);
107 }
108 return featureNodeContainerCache.get(description);
109 }
110
111 /** {@inheritDoc} */
112 @Override
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;
119 }
120 }
121 return getChildren(element).length != 0;
122 }
123
124 /**
125 * Retrieves the feature tree associated with the given description
126 *
127 * TODO as of now this is always the same thing because feature trees may not be associated
128 * to descriptions yet.
129 *
130 * @param description
131 * @return
132 */
133 private FeatureTree getFeatureTree(DescriptionBase description){
134 FeatureTree featureTree = null;
135
136 // TODO change this to the feature tree associated with this taxon description
137 if (description.hasStructuredData()){
138 featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
139 }else{
140 featureTree = PreferencesUtil.getDefaultFeatureTreeForTextualDescription();
141 }
142
143 // create a transient tree with all features if none was selected
144 if(featureTree == null){
145 featureTree = FeatureTree.NewInstance(TermStore.getTerms(Feature.class));
146 }
147
148 return featureTree;
149 }
150
151 /**
152 * Get all descriptions associated with the given object
153 * @param parentElement
154 * @return
155 */
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);
167 }
168 }
169 }
170 else {
171 resultDescriptions.add(description);
172 }
173 }
174 }
175 return resultDescriptions;
176 }
177
178 /* (non-Javadoc)
179 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
180 */
181 /** {@inheritDoc} */
182 @Override
183 public Object getParent(Object element) {
184 return null;
185 }
186
187 /* (non-Javadoc)
188 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
189 */
190 /** {@inheritDoc} */
191 @Override
192 public Object[] getElements(Object inputElement) {
193 return getChildren(inputElement);
194 }
195
196 /* (non-Javadoc)
197 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
198 */
199 /**
200 * <p>dispose</p>
201 */
202 @Override
203 public void dispose() {
204 featureNodeContainerCache.clear();
205 }
206
207 /* (non-Javadoc)
208 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
209 */
210 /** {@inheritDoc} */
211 @Override
212 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
213
214 public void toggleShowOnlyIndividualAssociations() {
215 showOnlyIndividualAssociations = !showOnlyIndividualAssociations;
216 }
217
218 }