Project

General

Profile

Download (6.13 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.IDescribable;
21
import eu.etaxonomy.taxeditor.editor.UsageTermCollection;
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
 * <p>DescriptiveContentProvider class.</p>
30
 *
31
 * @author p.ciardelli
32
 * @author n.hoffmann
33
 * @version $Id: $
34
 */
35
public class DescriptiveContentProvider implements ITreeContentProvider {
36

    
37
	protected static final Object[] NO_CHILDREN = new Object[0];
38
	protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache;
39

    
40
	/**
41
	 * <p>Constructor for DescriptiveContentProvider.</p>
42
	 *
43
	 * @param featureNodeContainerCache a {@link java.util.Map} object.
44
	 */
45
	public DescriptiveContentProvider(Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache) {
46
		this.featureNodeContainerCache = featureNodeContainerCache;
47
	}
48

    
49
	/* (non-Javadoc)
50
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
51
	 */
52
	/** {@inheritDoc} */
53
	@Override
54
    public Object[] getChildren(Object parentElement) {
55
		if (parentElement instanceof IDescribable<?>) {
56
			return getDescriptions((IDescribable<?>) parentElement).toArray();
57
		}
58
		else if (parentElement instanceof DescriptionBase<?>) {
59
			if ( ! ((DescriptionBase<?>) parentElement).isImageGallery()) {
60
			    DescriptionBase<?> description = (DescriptionBase<?>) parentElement;
61

    
62
				FeatureNodeContainerTree containerTree = getContainerTreeForDesription(description);
63

    
64
				return containerTree.getRoot().getChildren().toArray();
65
			}
66
		}
67
		else if (parentElement instanceof FeatureNodeContainer){
68
			FeatureNodeContainer container = (FeatureNodeContainer) parentElement;
69
			if(container.isLeaf()){
70
				return container.getDescriptionElements().toArray();
71
			}else{
72
				return container.getChildren().toArray();
73
			}
74
		}
75

    
76
		return NO_CHILDREN;
77
	}
78

    
79
	private FeatureNodeContainerTree getContainerTreeForDesription(DescriptionBase<?> description){
80
		if(! featureNodeContainerCache.containsKey(description)){
81
			FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
82
			featureNodeContainerCache.put(description, containerTree);
83
		}
84
		return featureNodeContainerCache.get(description);
85
	}
86

    
87
	/** {@inheritDoc} */
88
	@Override
89
	public boolean hasChildren(Object element) {
90
		if (element instanceof DescriptionBase<?>){
91
		    DescriptionBase<?> description = (DescriptionBase<?>) element;
92
			FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
93
			if(containerTree != null && containerTree.getRoot() != null){
94
				return containerTree.getRoot().getChildren().size() != 0;
95
			}
96
		}
97
		return getChildren(element).length != 0;
98
	}
99

    
100
	/**
101
	 * Retrieves the feature tree associated with the given description
102
	 *
103
	 * TODO as of now this is always the same thing because feature trees may not be associated
104
	 * to descriptions yet.
105
	 *
106
	 * @param description
107
	 * @return
108
	 */
109
	private FeatureTree getFeatureTree(DescriptionBase description){
110
		FeatureTree featureTree = null;
111

    
112
		// TODO change this to the feature tree associated with this taxon description
113
		if (description.hasStructuredData()){
114
			featureTree = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
115
		}else{
116
			featureTree = PreferencesUtil.getDefaultFeatureTreeForTextualDescription();
117
		}
118

    
119
		// create a transient tree with all features if none was selected
120
		if(featureTree == null){
121
			featureTree = FeatureTree.NewInstance(TermStore.getTerms(Feature.class));
122
		}
123

    
124
		return featureTree;
125
	}
126

    
127
    /**
128
     * Get all descriptions associated with the given object
129
     * @param parentElement
130
     * @return
131
     */
132
    protected List<DescriptionBase<?>> getDescriptions(IDescribable<?> parentElement) {
133
        Set<? extends DescriptionBase<?>> elementDescriptions = parentElement.getDescriptions();
134
        List<DescriptionBase<?>> resultDescriptions = new ArrayList<DescriptionBase<?>>();
135
        for(DescriptionBase<?> description : elementDescriptions){
136
			if(! description.isImageGallery()){
137
				MarkerType useMarkertype = (MarkerType) CdmStore.getService(ITermService.class).find(UsageTermCollection.uuidUseMarkerType);
138
				Set<Marker> descriptionMarkers = description.getMarkers();
139
				if(descriptionMarkers != null && !descriptionMarkers.isEmpty()) {
140
					for (Marker marker: descriptionMarkers) {
141
						if(!(marker.getMarkerType().equals(useMarkertype))) {
142
							resultDescriptions.add(description);
143
						}
144
					}
145
				}
146
				else {
147
					resultDescriptions.add(description);
148
				}
149
			}
150
		}
151
        return resultDescriptions;
152
    }
153

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

    
163
	/* (non-Javadoc)
164
	 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
165
	 */
166
	/** {@inheritDoc} */
167
	@Override
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
	@Override
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
	@Override
188
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
189

    
190
}
(4-4/7)