Project

General

Profile

Download (6.07 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
import java.util.Set;
10

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

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

    
30
/**
31
 * <p>DescriptiveContentProvider class.</p>
32
 *
33
 * @author p.ciardelli
34
 * @author n.hoffmann
35
 * @version $Id: $
36
 */
37
public class DescriptiveContentProvider implements ITreeContentProvider {
38
	
39
	protected static final Object[] NO_CHILDREN = new Object[0];
40
	protected Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache;
41
	
42
	/**
43
	 * <p>Constructor for DescriptiveContentProvider.</p>
44
	 *
45
	 * @param featureNodeContainerCache a {@link java.util.Map} object.
46
	 */
47
	public DescriptiveContentProvider(Map<TaxonDescription, FeatureNodeContainerTree> featureNodeContainerCache) {
48
		this.featureNodeContainerCache = featureNodeContainerCache;
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
	protected List<DescriptionBase> getDescriptions(TaxonEditorInput parentElement) {
135
		Taxon taxon = parentElement.getTaxon();
136
		List<DescriptionBase> descriptions = new ArrayList<DescriptionBase>();
137
		for(DescriptionBase description : taxon.getDescriptions()){
138
			if(! description.isImageGallery()){
139
				MarkerType useMarkertype = (MarkerType) CdmStore.getService(ITermService.class).find(UsageTermCollection.uuidUseMarkerType);
140
				Set<Marker> descriptionMarkers = description.getMarkers();
141
				if(descriptionMarkers != null && !descriptionMarkers.isEmpty()) {
142
					for (Marker marker: descriptionMarkers) {
143
						if(!(marker.getMarkerType().equals(useMarkertype))) {
144
							descriptions.add(description);
145
						}
146
					}
147
				}
148
				else {
149
					descriptions.add(description);
150
				}
151
			}
152
			
153
		}			
154
		return descriptions;
155
	}
156

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

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

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