Project

General

Profile

Download (4.64 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.taxeditor.editor.view.media;
12

    
13
import java.util.Collections;
14
import java.util.HashSet;
15
import java.util.List;
16
import java.util.Set;
17

    
18
import org.eclipse.core.runtime.Assert;
19
import org.eclipse.jface.viewers.ITreeContentProvider;
20
import org.eclipse.jface.viewers.Viewer;
21

    
22
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
23
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
24
import eu.etaxonomy.cdm.model.description.DescriptionBase;
25
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
27
import eu.etaxonomy.cdm.model.media.Media;
28
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
29
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
30
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
31
import eu.etaxonomy.cdm.model.taxon.Taxon;
32
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
33
import eu.etaxonomy.taxeditor.model.MessagingUtils;
34

    
35
/**
36
 * <p>MediaContentProvider class.</p>
37
 *
38
 * @author n.hoffmann
39
 * @created Jun 15, 2010
40
 * @version 1.0
41
 */
42
public class MediaContentProvider implements ITreeContentProvider{
43

    
44
	private static final Object[] NO_CHILDREN = new Object[0];
45

    
46
	/** {@inheritDoc} */
47
	@Override
48
    public Object[] getChildren(Object parentElement) {
49

    
50
		if (parentElement instanceof TaxonEditorInput) {
51
			Taxon taxon = ((TaxonEditorInput) parentElement).getTaxon();
52
			if(taxon == null){
53
				MessagingUtils.error(getClass(), "Taxon is null", null);
54
				return NO_CHILDREN;
55
			}
56
			HashSet<DescriptionBase> imageGalleries = new HashSet<DescriptionBase>();
57
			for(DescriptionBase description : taxon.getDescriptions()){
58
				if(description.isImageGallery()){
59
					imageGalleries.add(description);
60
				}
61
			}
62
			return imageGalleries.toArray();
63
		}
64
		else if (parentElement instanceof DescriptionBase) {
65
			if (((DescriptionBase) parentElement).isImageGallery()) {
66
				return getImages((DescriptionBase) parentElement).toArray();
67
			}
68
		}
69
		else if (parentElement instanceof DerivedUnit){
70
			try {
71
				DerivedUnitFacade facade = DerivedUnitFacade.NewInstance((DerivedUnit) parentElement);
72

    
73
				SpecimenDescription derivedUnitImageGallery = facade.getDerivedUnitImageGallery(false);
74

    
75
				if(derivedUnitImageGallery != null){
76
					return Collections.singleton(derivedUnitImageGallery).toArray();
77
				}
78

    
79
			} catch (DerivedUnitFacadeNotSupportedException e) {
80
				MessagingUtils.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e);
81
			}
82
		}
83
		else if (parentElement instanceof FieldUnit){
84
		        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, (FieldUnit) parentElement);
85

    
86
		        SpecimenDescription fieldObjectImageGallery = facade.getFieldObjectImageGallery(false);
87

    
88
		        if(fieldObjectImageGallery != null){
89
		            return Collections.singleton(fieldObjectImageGallery).toArray();
90
		        }
91
		}
92
		return NO_CHILDREN;
93
	}
94

    
95
	/** {@inheritDoc} */
96
	@Override
97
    public Object getParent(Object element) {
98
		// TODO Auto-generated method stub
99
		return null;
100
	}
101

    
102
	/** {@inheritDoc} */
103
	@Override
104
    public boolean hasChildren(Object element) {
105
		return (getChildren(element).length > 0);
106
	}
107

    
108
	/** {@inheritDoc} */
109
	@Override
110
    public Object[] getElements(Object inputElement) {
111
		return getChildren(inputElement);
112
	}
113

    
114
	/**
115
	 * <p>dispose</p>
116
	 */
117
	@Override
118
    public void dispose() {}
119

    
120
	/** {@inheritDoc} */
121
	@Override
122
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
123

    
124
	private List<Media> getImages(DescriptionBase description){
125
		Assert.isTrue(description.isImageGallery(), "Description should have the imageGallery flag set.");
126

    
127
		Set<DescriptionElementBase> elements = description.getElements();
128
		if (elements != null) {
129
			// by definition, image galleries have only one description element
130
			if(elements.size() > 1){
131
				MessagingUtils.error(this.getClass(), "There should be one and only one description element to hold the images. Found: " + elements.size() + " InDescription ID: " + description.getUuid(), null);
132
			}
133

    
134
			DescriptionElementBase element = elements.iterator().next();
135

    
136
			return element.getMedia();
137
		}
138
		return null;
139
	}
140

    
141
	private SpecimenDescription createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade){
142
		SpecimenDescription description = SpecimenDescription.NewInstance();
143
		description.setImageGallery(true);
144

    
145

    
146

    
147
		return description;
148
	}
149
}
(1-1/3)