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