Project

General

Profile

« Previous | Next » 

Revision 0d4ff026

Added by Alex Theys almost 12 years ago

AT: committing changes to the TaxEditor Post second round of code review

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/uses/UsesContentProvider.java
27 27
import eu.etaxonomy.cdm.model.taxon.Taxon;
28 28
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
29 29
import eu.etaxonomy.taxeditor.editor.UsageTermCollection;
30
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveContentProvider;
30 31
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
31 32
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
32 33
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
......
39 40
 * @created mar 13, 2012
40 41
 * @version 1.0
41 42
 */
42
public class UsesContentProvider implements ITreeContentProvider {
43
public class UsesContentProvider extends DescriptiveContentProvider implements ITreeContentProvider {
43 44

  
44
	private static final Object[] NO_CHILDREN = new Object[0];
45
	private Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache;
46 45
	Set<MarkerType> markerTypes = new HashSet<MarkerType>();
47 46
		
48 47
	/**
......
51 50
	 * @param featureNodeContainerCache a {@link java.util.Map} object.
52 51
	 */
53 52
	public UsesContentProvider(Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache) {
53
		super(featureNodeContainerCache);
54 54
		this.featureNodeContainerCache = featureNodeContainerCache;
55 55
	}
56
	
57

  
58
	/* (non-Javadoc)
59
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
60
	 */
61
	/** {@inheritDoc} */
62
	public Object[] getChildren(Object parentElement) {
63
		if (parentElement instanceof TaxonEditorInput) {
64
			return getDescriptions((TaxonEditorInput) parentElement).toArray(); 
65
		}
66
		else if (parentElement instanceof TaxonDescription) {
67
			if ( ! ((TaxonDescription) parentElement).isImageGallery()) {
68
				TaxonDescription description = (TaxonDescription) parentElement;
69
				
70
				FeatureNodeContainerTree containerTree = getContainerTreeForDesription(description);
71
				
72
				return containerTree.getRoot().getChildren().toArray();
73
			}
74
		}
75
		else if (parentElement instanceof FeatureNodeContainer){
76
			FeatureNodeContainer container = (FeatureNodeContainer) parentElement;
77
			if(container.isLeaf()){
78
				return container.getDescriptionElements().toArray();
79
			}else{
80
				return container.getChildren().toArray();
81
			}
82
		}
83
		
84
		return NO_CHILDREN;
85
	}
86
	
87
	private FeatureNodeContainerTree getContainerTreeForDesription(TaxonDescription description){
88
		if(! featureNodeContainerCache.containsKey(description)){
89
			FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
90
			featureNodeContainerCache.put(description, containerTree);
91
		}
92
		return featureNodeContainerCache.get(description);
93
	}
94

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

  
135 57
	/**
136 58
	 * Get all descriptions associated with the given TaxonEditorInput
......
138 60
	 * @param parentElement
139 61
	 * @return
140 62
	 */
141
	private List<DescriptionBase> getDescriptions(TaxonEditorInput parentElement) {
63
	@Override
64
	protected List<DescriptionBase> getDescriptions(TaxonEditorInput parentElement) {
142 65
		this.markerTypes.addAll(CdmStore.getTermManager().getPreferredTerms(MarkerType.class));
143 66
		Taxon taxon = parentElement.getTaxon();
144 67
		List<DescriptionBase> descriptions = new ArrayList<DescriptionBase>();
......
159 82
		return descriptions;
160 83
	}
161 84

  
162
	/* (non-Javadoc)
163
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
164
	 */
165
	/** {@inheritDoc} */
166
	public Object getParent(Object element) {
167
		return null;
168
	}
169

  
170
	/* (non-Javadoc)
171
	 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
172
	 */
173
	/** {@inheritDoc} */
174
	public Object[] getElements(Object inputElement) {
175
		return getChildren(inputElement);
176
	}
177
	
178
	/* (non-Javadoc)
179
	 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
180
	 */
181
	/**
182
	 * <p>dispose</p>
183
	 */
184
	public void dispose() {
185
		featureNodeContainerCache.clear();
186
	}
187

  
188
	/* (non-Javadoc)
189
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
190
	 */
191
	/** {@inheritDoc} */
192
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}	
193

  
194 85
}

Also available in: Unified diff