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