Project

General

Profile

Download (9.31 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.TermTree;
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
 */
36
public class DescriptiveContentProvider implements ITreeContentProvider {
37

    
38
	protected static final Object[] NO_CHILDREN = new Object[0];
39
	protected Map<DescriptionBase<?>, FeatureNodeContainerTree> featureNodeContainerCache;
40
	private boolean showOnlyIndividualAssociations;
41

    
42

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

    
56
	@Override
57
    public Object[] getChildren(Object parentElement) {
58
	    if(parentElement instanceof TreeNode){
59
	        parentElement = ((TreeNode) parentElement).getValue();
60
	    }
61
		if (parentElement instanceof IDescribable<?>) {
62
			return getDescriptions((IDescribable<?>) parentElement).toArray();
63
		}
64
		else if (parentElement instanceof DescriptionBase<?>) {
65
			if ( ! ((DescriptionBase<?>) parentElement).isImageGallery()) {
66
			    DescriptionBase<?> description = (DescriptionBase<?>) parentElement;
67

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

    
93
                    @Override
94
                    public int compare(IndividualsAssociation o1, IndividualsAssociation o2) {
95
                        if(o1==null){
96
                            if(o2==null){
97
                                return 0;
98
                            }
99
                            else{
100
                                return -1;
101
                            }
102
                        }
103
                        else if(o2==null){
104
                            return 1;
105
                        }
106

    
107
                        if(o1.getAssociatedSpecimenOrObservation()==null){
108
                            if(o2.getAssociatedSpecimenOrObservation()==null){
109
                                return 0;
110
                            }
111
                            else{
112
                                return -1;
113
                            }
114
                        }
115
                        else if(o2.getAssociatedSpecimenOrObservation()==null){
116
                            return 1;
117
                        }
118

    
119
                        String titleCache1 = o1.getAssociatedSpecimenOrObservation().getTitleCache();
120
                        String titleCache2 = o2.getAssociatedSpecimenOrObservation().getTitleCache();
121

    
122
                        if(titleCache1==null){
123
                            if(titleCache2==null){
124
                                return 0;
125
                            }
126
                            else{
127
                                return -1;
128
                            }
129
                        }
130
                        if(titleCache2==null){
131
                            return 1;
132
                        }
133

    
134
                        return titleCache1.compareTo(titleCache2);
135
                    }
136
                });
137
				if(showOnlyIndividualAssociations){
138
				    descriptionElements = new ArrayList<DescriptionElementBase>(individualAssociations);
139
				}
140
                return descriptionElements.toArray();
141
			}else{
142
				return container.getChildren().toArray();
143
			}
144
		}
145

    
146
		return NO_CHILDREN;
147
	}
148

    
149
	private FeatureNodeContainerTree getContainerTreeForDesription(DescriptionBase<?> description){
150
//		if(! featureNodeContainerCache.containsKey(description)){
151
//			FeatureNodeContainerTree containerTree = new FeatureNodeContainerTree(description, getFeatureTree(description));
152
//			featureNodeContainerCache.put(description, containerTree);
153
//		}
154
//		return featureNodeContainerCache.get(description);
155
		return new FeatureNodeContainerTree(description, getFeatureTree(description));
156
	}
157

    
158
	@Override
159
	public boolean hasChildren(Object element) {
160
		if (element instanceof DescriptionBase<?>){
161
		    DescriptionBase<?> description = (DescriptionBase<?>) element;
162
			FeatureNodeContainerTree containerTree = featureNodeContainerCache.get(description);
163
			if(containerTree != null && containerTree.getRoot() != null){
164
				return containerTree.getRoot().getChildren().size() != 0;
165
			}
166
		}
167
		return getChildren(element).length != 0;
168
	}
169

    
170
	/**
171
	 * Retrieves the feature tree associated with the given description
172
	 *
173
	 * TODO as of now this is always the same thing because feature trees may not be associated
174
	 * to descriptions yet.
175
	 *
176
	 * @param description
177
	 * @return
178
	 */
179
	private TermTree getFeatureTree(DescriptionBase description){
180

    
181
	        TermTree<?> featureTree = null;
182

    
183
	        if (description instanceof TaxonNameDescription){
184

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

    
197
	        if (featureTree == null) {
198
	            featureTree = TermEditorInput.getDefaultFeatureTree();
199
	        }
200

    
201

    
202
	        return featureTree;
203

    
204

    
205

    
206
	}
207

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

    
234
	@Override
235
    public Object getParent(Object element) {
236
	    if (element instanceof FeatureNodeContainer){
237
	        return ((FeatureNodeContainer)element).getDescription();
238
	    }
239
		return null;
240
	}
241

    
242
	@Override
243
    public Object[] getElements(Object inputElement) {
244
		return getChildren(inputElement);
245
	}
246

    
247
	/**
248
	 * <p>dispose</p>
249
	 */
250
	@Override
251
    public void dispose() {
252
		featureNodeContainerCache.clear();
253
	}
254

    
255
	@Override
256
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
257

    
258
	public void toggleShowOnlyIndividualAssociations() {
259
	    showOnlyIndividualAssociations = !showOnlyIndividualAssociations;
260
    }
261

    
262
}
(4-4/7)