Project

General

Profile

Download (9.33 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.Collections;
8
import java.util.Comparator;
9
import java.util.List;
10
import java.util.Map;
11
import java.util.Set;
12

    
13
import org.eclipse.jface.viewers.ITreeContentProvider;
14
import org.eclipse.jface.viewers.TreeNode;
15
import org.eclipse.jface.viewers.Viewer;
16

    
17
import eu.etaxonomy.cdm.model.common.Marker;
18
import eu.etaxonomy.cdm.model.description.DescriptionBase;
19
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
20
import eu.etaxonomy.cdm.model.description.IDescribable;
21
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
22
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
23
import eu.etaxonomy.cdm.model.term.FeatureTree;
24
import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
25
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
26
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
27
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
28
import eu.etaxonomy.taxeditor.store.UsageTermCollection;
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<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache;
41
	private boolean showOnlyIndividualAssociations;
42

    
43

    
44
	public DescriptiveContentProvider(Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache) {
45
	    this(featureNodeContainerCache, false);
46
	}
47
	/**
48
	 * <p>Constructor for DescriptiveContentProvider.</p>
49
	 *
50
	 * @param featureNodeContainerCache a {@link java.util.Map} object.
51
	 */
52
	public DescriptiveContentProvider(Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache, boolean showOnlyIndividualAssociations) {
53
		this.featureNodeContainerCache = featureNodeContainerCache;
54
		this.showOnlyIndividualAssociations = showOnlyIndividualAssociations;
55
	}
56

    
57
	/* (non-Javadoc)
58
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
59
	 */
60
	/** {@inheritDoc} */
61
	@Override
62
    public Object[] getChildren(Object parentElement) {
63
	    if(parentElement instanceof TreeNode){
64
	        parentElement = ((TreeNode) parentElement).getValue();
65
	    }
66
		if (parentElement instanceof IDescribable<?>) {
67
			return getDescriptions((IDescribable<?>) parentElement).toArray();
68
		}
69
		else if (parentElement instanceof DescriptionBase<?>) {
70
			if ( ! ((DescriptionBase<?>) parentElement).isImageGallery()) {
71
			    DescriptionBase<?> description = (DescriptionBase<?>) parentElement;
72

    
73
				FeatureNodeContainerTree containerTree = getContainerTreeForDesription(description);
74
				List<FeatureNodeContainer> children = containerTree.getRoot().getChildren();
75
				//filter out containers with no children
76
				List<FeatureNodeContainer> childrenWithChildren = new ArrayList<FeatureNodeContainer>();
77
				for (FeatureNodeContainer featureNodeContainer : children) {
78
				    if(getChildren(featureNodeContainer).length>0){
79
				        childrenWithChildren.add(featureNodeContainer);
80
				    }
81
                }
82
				return childrenWithChildren.toArray();
83
			}
84
		}
85
		else if (parentElement instanceof FeatureNodeContainer){
86
			FeatureNodeContainer container = (FeatureNodeContainer) parentElement;
87
			if(container.isLeaf()){
88
				List<DescriptionElementBase> descriptionElements = container.getDescriptionElements();
89
				List<IndividualsAssociation> individualAssociations = new ArrayList<IndividualsAssociation>();
90
				for (DescriptionElementBase descriptionElement : descriptionElements) {
91
				    if(descriptionElement instanceof IndividualsAssociation){
92
				        individualAssociations.add((IndividualsAssociation) descriptionElement);
93
				    }
94
				}
95
				//sort individual associations by title cache of associated specimens
96
				Collections.sort(individualAssociations, new Comparator<IndividualsAssociation>() {
97

    
98
                    @Override
99
                    public int compare(IndividualsAssociation o1, IndividualsAssociation o2) {
100
                        if(o1==null){
101
                            if(o2==null){
102
                                return 0;
103
                            }
104
                            else{
105
                                return -1;
106
                            }
107
                        }
108
                        else if(o2==null){
109
                            return 1;
110
                        }
111

    
112
                        if(o1.getAssociatedSpecimenOrObservation()==null){
113
                            if(o2.getAssociatedSpecimenOrObservation()==null){
114
                                return 0;
115
                            }
116
                            else{
117
                                return -1;
118
                            }
119
                        }
120
                        else if(o2.getAssociatedSpecimenOrObservation()==null){
121
                            return 1;
122
                        }
123

    
124
                        String titleCache1 = o1.getAssociatedSpecimenOrObservation().getTitleCache();
125
                        String titleCache2 = o2.getAssociatedSpecimenOrObservation().getTitleCache();
126

    
127
                        if(titleCache1==null){
128
                            if(titleCache2==null){
129
                                return 0;
130
                            }
131
                            else{
132
                                return -1;
133
                            }
134
                        }
135
                        if(titleCache2==null){
136
                            return 1;
137
                        }
138

    
139
                        return titleCache1.compareTo(titleCache2);
140
                    }
141
                });
142
				if(showOnlyIndividualAssociations){
143
				    descriptionElements = new ArrayList<DescriptionElementBase>(individualAssociations);
144
				}
145
                return descriptionElements.toArray();
146
			}else{
147
				return container.getChildren().toArray();
148
			}
149
		}
150

    
151
		return NO_CHILDREN;
152
	}
153

    
154
	private FeatureNodeContainerTree getContainerTreeForDesription(DescriptionBase<?> description){
155
		if(! featureNodeContainerCache.containsKey(description)){
156
			FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
157
			featureNodeContainerCache.put(description, containerTree);
158
		}
159
		return featureNodeContainerCache.get(description);
160
	}
161

    
162
	/** {@inheritDoc} */
163
	@Override
164
	public boolean hasChildren(Object element) {
165
		if (element instanceof DescriptionBase<?>){
166
		    DescriptionBase<?> description = (DescriptionBase<?>) element;
167
			FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
168
			if(containerTree != null && containerTree.getRoot() != null){
169
				return containerTree.getRoot().getChildren().size() != 0;
170
			}
171
		}
172
		return getChildren(element).length != 0;
173
	}
174

    
175
	/**
176
	 * Retrieves the feature tree associated with the given description
177
	 *
178
	 * TODO as of now this is always the same thing because feature trees may not be associated
179
	 * to descriptions yet.
180
	 *
181
	 * @param description
182
	 * @return
183
	 */
184
	private FeatureTree getFeatureTree(DescriptionBase description){
185

    
186
	        FeatureTree<?> featureTree = null;
187

    
188
	        // TODO change this to the feature tree associated with this taxon
189
	        // description
190
	        if (description.hasStructuredData()) {
191
	            featureTree = PreferencesUtil
192
	                    .getDefaultFeatureTreeForStructuredDescription();
193
	        } else {
194
	            featureTree = PreferencesUtil
195
	                    .getDefaultFeatureTreeForTextualDescription();
196
	        }
197

    
198
	        if (featureTree == null) {
199
	            featureTree = TermEditorInput.getDefaultFeatureTree();
200
	                    }
201
	        if (description instanceof TaxonNameDescription){
202

    
203
	            featureTree = TermEditorInput.getDefaultNameFeatureTree();
204
	        }
205

    
206
	        return featureTree;
207

    
208

    
209

    
210
	}
211

    
212
    /**
213
     * Get all descriptions associated with the given object
214
     * @param parentElement
215
     * @return
216
     */
217
    protected List<DescriptionBase<?>> getDescriptions(IDescribable<?> parentElement) {
218
        Set<? extends DescriptionBase<?>> elementDescriptions = parentElement.getDescriptions();
219
        List<DescriptionBase<?>> resultDescriptions = new ArrayList<DescriptionBase<?>>();
220
        for(DescriptionBase<?> description : elementDescriptions){
221
			if(! description.isImageGallery()){
222
				Set<Marker> descriptionMarkers = description.getMarkers();
223
				if(descriptionMarkers != null && !descriptionMarkers.isEmpty()) {
224
					for (Marker marker: descriptionMarkers) {
225
						if(marker.getMarkerType() != null && !(marker.getMarkerType().getUuid().equals(UsageTermCollection.uuidUseMarkerType))) {
226
							resultDescriptions.add(description);
227
						}
228
					}
229
				}
230
				else {
231
					resultDescriptions.add(description);
232
				}
233
			}
234
		}
235
        return resultDescriptions;
236
    }
237

    
238
	/** {@inheritDoc} */
239
	@Override
240
    public Object getParent(Object element) {
241
		return null;
242
	}
243

    
244
	/** {@inheritDoc} */
245
	@Override
246
    public Object[] getElements(Object inputElement) {
247
		return getChildren(inputElement);
248
	}
249

    
250
	/**
251
	 * <p>dispose</p>
252
	 */
253
	@Override
254
    public void dispose() {
255
		featureNodeContainerCache.clear();
256
	}
257

    
258
	/** {@inheritDoc} */
259
	@Override
260
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
261

    
262
	public void toggleShowOnlyIndividualAssociations() {
263
	    showOnlyIndividualAssociations = !showOnlyIndividualAssociations;
264
    }
265

    
266
}
(4-4/7)