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