Project

General

Profile

Download (9.44 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.metadata.EnabledComputedDescription;
24
import eu.etaxonomy.cdm.model.term.TermTree;
25
import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
26
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
27
import eu.etaxonomy.taxeditor.model.FeatureNodeContainerTree;
28
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
29
import eu.etaxonomy.taxeditor.store.UsageTermCollection;
30

    
31
/**
32
 * <p>DescriptiveContentProvider class.</p>
33
 *
34
 * @author p.ciardelli
35
 * @author n.hoffmann
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
	@Override
58
    public Object[] getChildren(Object parentElement) {
59
	    if(parentElement instanceof TreeNode){
60
	        parentElement = ((TreeNode) parentElement).getValue();
61
	    }
62
		if (parentElement instanceof IDescribable<?>) {
63
			return getDescriptions((IDescribable<?>) parentElement).toArray();
64
		}
65
		else if (parentElement instanceof DescriptionBase<?>) {
66
			if ( ! ((DescriptionBase<?>) parentElement).isImageGallery()) {
67
			    DescriptionBase<?> description = (DescriptionBase<?>) parentElement;
68

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

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

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

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

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

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

    
147
		return NO_CHILDREN;
148
	}
149

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

    
157
	}
158

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

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

    
182
	        TermTree<?> featureTree = null;
183

    
184
	        if (description instanceof TaxonNameDescription){
185

    
186
                return PreferencesUtil.getPreferredFeatureTreeForNameDescription(false);
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

    
202
	        return featureTree;
203
	}
204

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

    
229
				}
230
			}
231
		}
232
        return resultDescriptions;
233
    }
234

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

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

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

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

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

    
263
}
(4-4/7)