b24ee1704742dae6cb0d0e50b2f36f7673970a18
[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
71 if(getChildren(containerTree).length>=0){
72 return containerTree.getRoot().getChildren().toArray();
73 }
74 }
75 }
76 else if (parentElement instanceof FeatureNodeContainer){
77 FeatureNodeContainer container = (FeatureNodeContainer) parentElement;
78 if(container.isLeaf()){
79 List<DescriptionElementBase> descriptionElements = container.getDescriptionElements();
80 if(showOnlyIndividualAssociations){
81 List<DescriptionElementBase> filteredDescriptionElements = new ArrayList<DescriptionElementBase>();
82 for (DescriptionElementBase descriptionElement : descriptionElements) {
83 if(descriptionElement instanceof IndividualsAssociation){
84 filteredDescriptionElements.add(descriptionElement);
85 }
86 }
87 descriptionElements = filteredDescriptionElements;
88 }
89 return descriptionElements.toArray();
90 }else{
91 return container.getChildren().toArray();
92 }
93 }
94
95 return NO_CHILDREN;
96 }
97
98 private FeatureNodeContainerTree getContainerTreeForDesription(DescriptionBase<?> description){
99 if(! featureNodeContainerCache.containsKey(description)){
100 FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
101 featureNodeContainerCache.put(description, containerTree);
102 }
103 return featureNodeContainerCache.get(description);
104 }
105
106 /** {@inheritDoc} */
107 @Override
108 public boolean hasChildren(Object element) {
109 if (element instanceof DescriptionBase<?>){
110 DescriptionBase<?> description = (DescriptionBase<?>) element;
111 FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
112 if(containerTree != null && containerTree.getRoot() != null){
113 return containerTree.getRoot().getChildren().size() != 0;
114 }
115 }
116 return getChildren(element).length != 0;
117 }
118
119 /**
120 * Retrieves the feature tree associated with the given description
121 *
122 * TODO as of now this is always the same thing because feature trees may not be associated
123 * to descriptions yet.
124 *
125 * @param description
126 * @return
127 */
128 private FeatureTree getFeatureTree(DescriptionBase description){
129 FeatureTree featureTree = null;
130
131 // TODO change this to the feature tree associated with this taxon description
132 if (description.hasStructuredData()){
133 featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
134 }else{
135 featureTree = PreferencesUtil.getDefaultFeatureTreeForTextualDescription();
136 }
137
138 // create a transient tree with all features if none was selected
139 if(featureTree == null){
140 featureTree = FeatureTree.NewInstance(TermStore.getTerms(Feature.class));
141 }
142
143 return featureTree;
144 }
145
146 /**
147 * Get all descriptions associated with the given object
148 * @param parentElement
149 * @return
150 */
151 protected List<DescriptionBase<?>> getDescriptions(IDescribable<?> parentElement) {
152 Set<? extends DescriptionBase<?>> elementDescriptions = parentElement.getDescriptions();
153 List<DescriptionBase<?>> resultDescriptions = new ArrayList<DescriptionBase<?>>();
154 for(DescriptionBase<?> description : elementDescriptions){
155 if(! description.isImageGallery()){
156 MarkerType useMarkertype = (MarkerType) CdmStore.getService(ITermService.class).find(UsageTermCollection.uuidUseMarkerType);
157 Set<Marker> descriptionMarkers = description.getMarkers();
158 if(descriptionMarkers != null && !descriptionMarkers.isEmpty()) {
159 for (Marker marker: descriptionMarkers) {
160 if(!(marker.getMarkerType().equals(useMarkertype))) {
161 resultDescriptions.add(description);
162 }
163 }
164 }
165 else {
166 resultDescriptions.add(description);
167 }
168 }
169 }
170 return resultDescriptions;
171 }
172
173 /* (non-Javadoc)
174 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
175 */
176 /** {@inheritDoc} */
177 @Override
178 public Object getParent(Object element) {
179 return null;
180 }
181
182 /* (non-Javadoc)
183 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
184 */
185 /** {@inheritDoc} */
186 @Override
187 public Object[] getElements(Object inputElement) {
188 return getChildren(inputElement);
189 }
190
191 /* (non-Javadoc)
192 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
193 */
194 /**
195 * <p>dispose</p>
196 */
197 @Override
198 public void dispose() {
199 featureNodeContainerCache.clear();
200 }
201
202 /* (non-Javadoc)
203 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
204 */
205 /** {@inheritDoc} */
206 @Override
207 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
208
209 public void toggleShowOnlyIndividualAssociations() {
210 showOnlyIndividualAssociations = !showOnlyIndividualAssociations;
211 }
212
213 }