Project

General

Profile

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

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

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

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

    
21
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
22
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
23
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
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.bulkeditor.BulkEditor;
33
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
34
import eu.etaxonomy.taxeditor.model.MessagingUtils;
35

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

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

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

    
51
		if (parentElement instanceof TaxonEditorInput || parentElement instanceof Taxon) {
52
			
53
			Taxon taxon = null;
54
			if (parentElement instanceof TaxonEditorInput){
55
				taxon = ((TaxonEditorInput) parentElement).getTaxon(); 
56
			} else{
57
				taxon = HibernateProxyHelper.deproxy(parentElement, Taxon.class);
58
			}
59
			 
60
			if(taxon == null){
61
				MessagingUtils.error(getClass(), "Taxon is null", null);
62
				return NO_CHILDREN;
63
			}
64
			HashSet<DescriptionBase> imageGalleries = new HashSet<DescriptionBase>();
65
			for(DescriptionBase description : taxon.getDescriptions()){
66
				if(description.isImageGallery()){
67
					imageGalleries.add(description);
68
				}
69
			}
70
			return imageGalleries.toArray();
71
		}
72
		else if (parentElement instanceof DescriptionBase) {
73
			if (((DescriptionBase) parentElement).isImageGallery()) {
74
				List<Media> images =  getImages((DescriptionBase) parentElement);
75
				if (images != null){
76
					return images.toArray();
77
				} 
78
				return null;
79
				
80
			}
81
		}
82
		else if (parentElement instanceof DerivedUnit){
83
			try {
84
				DerivedUnitFacade facade = DerivedUnitFacade.NewInstance((DerivedUnit) parentElement);
85

    
86
				SpecimenDescription derivedUnitImageGallery = facade.getDerivedUnitImageGallery(false);
87

    
88
				if(derivedUnitImageGallery != null){
89
					return Collections.singleton(derivedUnitImageGallery).toArray();
90
				}
91

    
92
			} catch (DerivedUnitFacadeNotSupportedException e) {
93
				MessagingUtils.error(this.getClass(), "DerivedUnitFacadeNotSupportedException when trying to instantiate DerivedUnitFacade", e);
94
			}
95
		}
96
		else if (parentElement instanceof FieldUnit){
97
		        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, (FieldUnit) parentElement);
98

    
99
		        SpecimenDescription fieldObjectImageGallery = facade.getFieldObjectImageGallery(false);
100

    
101
		        if(fieldObjectImageGallery != null){
102
		            return Collections.singleton(fieldObjectImageGallery).toArray();
103
		        }
104
		}
105
		return NO_CHILDREN;
106
	}
107

    
108
	/** {@inheritDoc} */
109
	@Override
110
    public Object getParent(Object element) {
111
		// TODO Auto-generated method stub
112
		return null;
113
	}
114

    
115
	/** {@inheritDoc} */
116
	@Override
117
    public boolean hasChildren(Object element) {
118
		if (getChildren(element)== null) {
119
			return false;
120
		}
121
		return (getChildren(element).length > 0);
122
	}
123

    
124
	/** {@inheritDoc} */
125
	@Override
126
    public Object[] getElements(Object inputElement) {
127
		return getChildren(inputElement);
128
	}
129

    
130
	/**
131
	 * <p>dispose</p>
132
	 */
133
	@Override
134
    public void dispose() {}
135

    
136
	/** {@inheritDoc} */
137
	@Override
138
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {}
139

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

    
143
		Set<DescriptionElementBase> elements = description.getElements();
144
		if (elements != null) {
145
			// by definition, image galleries have only one description element
146
			if(elements.size() > 1){
147
				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);
148
			}
149
			if (!elements.isEmpty()){
150
				DescriptionElementBase element = elements.iterator().next();
151
				return element.getMedia();
152
			}
153

    
154
			
155
		}
156
		return null;
157
	}
158

    
159
	private SpecimenDescription createDerivedUnitFacadeImageGallery(DerivedUnitFacade facade){
160
		SpecimenDescription description = SpecimenDescription.NewInstance();
161
		description.setImageGallery(true);
162

    
163

    
164

    
165
		return description;
166
	}
167
}
(1-1/3)