Project

General

Profile

Download (9.04 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.FeatureTree;
21
import eu.etaxonomy.cdm.model.description.IDescribable;
22
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
23
import eu.etaxonomy.taxeditor.editor.definedterm.input.TermEditorInput;
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.UsageTermCollection;
28

    
29
/**
30
 * <p>DescriptiveContentProvider class.</p>
31
 *
32
 * @author p.ciardelli
33
 * @author n.hoffmann
34
 * @version $Id: $
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
	/* (non-Javadoc)
57
	 * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
58
	 */
59
	/** {@inheritDoc} */
60
	@Override
61
    public Object[] getChildren(Object parentElement) {
62
	    if(parentElement instanceof TreeNode){
63
	        parentElement = ((TreeNode) parentElement).getValue();
64
	    }
65
		if (parentElement instanceof IDescribable<?>) {
66
			return getDescriptions((IDescribable<?>) parentElement).toArray();
67
		}
68
		else if (parentElement instanceof DescriptionBase<?>) {
69
			if ( ! ((DescriptionBase<?>) parentElement).isImageGallery()) {
70
			    DescriptionBase<?> description = (DescriptionBase<?>) parentElement;
71

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

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

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

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

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

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

    
150
		return NO_CHILDREN;
151
	}
152

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

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

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

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

    
193
		// create a transient tree with all features if none was selected
194
		if(featureTree == null){
195
			featureTree = TermEditorInput.getDefaultFeatureTree();
196
		}
197

    
198
		return featureTree;
199
	}
200

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

    
227
	/** {@inheritDoc} */
228
	@Override
229
    public Object getParent(Object element) {
230
		return null;
231
	}
232

    
233
	/** {@inheritDoc} */
234
	@Override
235
    public Object[] getElements(Object inputElement) {
236
		return getChildren(inputElement);
237
	}
238

    
239
	/**
240
	 * <p>dispose</p>
241
	 */
242
	@Override
243
    public void dispose() {
244
		featureNodeContainerCache.clear();
245
	}
246

    
247
	/** {@inheritDoc} */
248
	@Override
249
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
250

    
251
	public void toggleShowOnlyIndividualAssociations() {
252
	    showOnlyIndividualAssociations = !showOnlyIndividualAssociations;
253
    }
254

    
255
}
(4-4/6)