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