Project

General

Profile

Download (5.36 KB) Statistics
| Branch: | Tag: | Revision:
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.Feature;
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.model.FeatureNodeContainerTree;
21
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
22
import eu.etaxonomy.taxeditor.store.TermStore;
23

    
24
/**
25
 * <p>DescriptiveContentProvider class.</p>
26
 *
27
 * @author p.ciardelli
28
 * @author n.hoffmann
29
 * @version $Id: $
30
 */
31
public class DescriptiveContentProvider implements ITreeContentProvider {
32
	
33
	private static final Object[] NO_CHILDREN = new Object[0];
34
	private Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache;
35
	
36
	/**
37
	 * <p>Constructor for DescriptiveContentProvider.</p>
38
	 *
39
	 * @param featureNodeContainerCache a {@link java.util.Map} object.
40
	 */
41
	public DescriptiveContentProvider(Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache) {
42
		this.featureNodeContainerCache = featureNodeContainerCache;
43
	}
44
	
45
	/* (non-Javadoc)
46
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
47
	 */
48
	/** {@inheritDoc} */
49
	public Object[] getChildren(Object parentElement) {
50
		if (parentElement instanceof TaxonEditorInput) {
51
			return getDescriptions((TaxonEditorInput) parentElement).toArray(); 
52
		}
53
		else if (parentElement instanceof TaxonDescription) {
54
			if ( ! ((TaxonDescription) parentElement).isImageGallery()) {
55
				TaxonDescription description = (TaxonDescription) parentElement;
56
				
57
				FeatureNodeContainerTree containerTree = getContainerTreeForDesription(description);
58
				
59
				return containerTree.getRoot().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
	private FeatureNodeContainerTree getContainerTreeForDesription(TaxonDescription description){
75
		if(! featureNodeContainerCache.containsKey(description)){
76
			FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
77
			featureNodeContainerCache.put(description, containerTree);
78
		}
79
		return featureNodeContainerCache.get(description);
80
	}
81

    
82
	/** {@inheritDoc} */
83
	@Override
84
	public boolean hasChildren(Object element) {
85
		if (element instanceof TaxonDescription){
86
			TaxonDescription description = (TaxonDescription) element;
87
			FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
88
			if(containerTree != null && containerTree.getRoot() != null){
89
				return containerTree.getRoot().getChildren().size() != 0;
90
			}
91
		} 
92
		return getChildren(element).length != 0;
93
	}
94
	
95
	/**
96
	 * Retrieves the feature tree associated with the given description
97
	 * 
98
	 * TODO as of now this is always the same thing because feature trees may not be associated 
99
	 * to descriptions yet.
100
	 * 
101
	 * @param description
102
	 * @return
103
	 */
104
	private FeatureTree getFeatureTree(DescriptionBase description){
105
		FeatureTree featureTree = null;
106
		
107
		// TODO change this to the feature tree associated with this taxon description
108
		if (description.hasStructuredData()){					
109
			featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
110
		}else{
111
			featureTree = PreferencesUtil.getDefaultFeatureTreeForTextualDescription();
112
		}
113
		
114
		// create a transient tree with all features if none was selected
115
		if(featureTree == null){
116
			featureTree = FeatureTree.NewInstance(TermStore.getTerms(Feature.class));
117
		}
118
		
119
		return featureTree;
120
	}
121

    
122
	/**
123
	 * Get all descriptions associated with the given TaxonEditorInput
124
	 * 
125
	 * @param parentElement
126
	 * @return
127
	 */
128
	private List<DescriptionBase> getDescriptions(TaxonEditorInput parentElement) {
129
		Taxon taxon = parentElement.getTaxon();
130
		List<DescriptionBase> descriptions = new ArrayList<DescriptionBase>();
131
		for(DescriptionBase description : taxon.getDescriptions()){
132
			if(! description.isImageGallery()){
133
				descriptions.add(description);
134
			}
135
		}			
136
		return descriptions;
137
	}
138

    
139
	/* (non-Javadoc)
140
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
141
	 */
142
	/** {@inheritDoc} */
143
	public Object getParent(Object element) {
144
		return null;
145
	}
146

    
147
	/* (non-Javadoc)
148
	 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
149
	 */
150
	/** {@inheritDoc} */
151
	public Object[] getElements(Object inputElement) {
152
		return getChildren(inputElement);
153
	}
154
	
155
	/* (non-Javadoc)
156
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
157
	 */
158
	/**
159
	 * <p>dispose</p>
160
	 */
161
	public void dispose() {
162
		featureNodeContainerCache.clear();
163
	}
164

    
165
	/* (non-Javadoc)
166
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
167
	 */
168
	/** {@inheritDoc} */
169
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}	
170
	
171
}
(4-4/7)