Project

General

Profile

Download (6.37 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.editor.view.uses;
2

    
3
import java.util.ArrayList;
4
import java.util.HashSet;
5
import java.util.List;
6
import java.util.Map;
7
import java.util.Set;
8
import java.util.UUID;
9

    
10
import org.eclipse.jface.viewers.ITreeContentProvider;
11
import org.eclipse.jface.viewers.Viewer;
12

    
13
import eu.etaxonomy.cdm.api.service.ITermService;
14
import eu.etaxonomy.cdm.model.common.Marker;
15
import eu.etaxonomy.cdm.model.common.MarkerType;
16
import eu.etaxonomy.cdm.model.description.DescriptionBase;
17
import eu.etaxonomy.cdm.model.description.Feature;
18
import eu.etaxonomy.cdm.model.description.FeatureTree;
19
import eu.etaxonomy.cdm.model.description.TaxonDescription;
20
import eu.etaxonomy.cdm.model.taxon.Taxon;
21
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
22
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
23
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
24
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
25
import eu.etaxonomy.taxeditor.store.CdmStore;
26
import eu.etaxonomy.taxeditor.store.TermStore;
27

    
28
/**
29
 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
30
 *
31
 * @author a.theys	
32
 * @created mar 13, 2012
33
 * @version 1.0
34
 */
35
public class UsesContentProvider implements ITreeContentProvider {
36

    
37
	private static final Object[] NO_CHILDREN = new Object[0];
38
	private Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache;
39
	Set<MarkerType> markerTypes = new HashSet<MarkerType>();
40
		
41
	/**
42
	 * <p>Constructor for DescriptiveContentProvider.</p>
43
	 *
44
	 * @param featureNodeContainerCache a {@link java.util.Map} object.
45
	 */
46
	public UsesContentProvider(Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache) {
47
		this.featureNodeContainerCache = featureNodeContainerCache;
48
	}
49
	
50

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

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

    
128
	/**
129
	 * Get all descriptions associated with the given TaxonEditorInput
130
	 * 
131
	 * @param parentElement
132
	 * @return
133
	 */
134
	private List<DescriptionBase> getDescriptions(TaxonEditorInput parentElement) {
135
		this.markerTypes.addAll(CdmStore.getTermManager().getPreferredTerms(MarkerType.class));
136
		Taxon taxon = parentElement.getTaxon();
137
		List<DescriptionBase> descriptions = new ArrayList<DescriptionBase>();
138
		
139
	for(DescriptionBase description : taxon.getDescriptions()){
140
			if(! description.isImageGallery()){
141
				MarkerType useMarkertype = (MarkerType) CdmStore.getService(ITermService.class).find(UUID.fromString("2e6e42d9-e92a-41f4-899b-03c0ac64f039"));
142
				Set<Marker> descriptionMarkers = description.getMarkers();
143
				if(descriptionMarkers != null) {
144
					for (Marker marker: descriptionMarkers) {
145
						//TODO Use the GetbyUUID method
146
						if(marker.getMarkerType().equals(useMarkertype)) {
147
							descriptions.add(description);
148
						}
149
					}
150
				}
151
			}
152
		}			
153
		return descriptions;
154
	}
155

    
156
	/* (non-Javadoc)
157
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
158
	 */
159
	/** {@inheritDoc} */
160
	public Object getParent(Object element) {
161
		return null;
162
	}
163

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

    
182
	/* (non-Javadoc)
183
	 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
184
	 */
185
	/** {@inheritDoc} */
186
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}	
187

    
188
}
(1-1/3)