Project

General

Profile

Download (4 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.ui.section.media;
11

    
12
import java.io.IOException;
13
import java.net.URI;
14
import java.util.Collection;
15

    
16
import org.apache.http.HttpException;
17
import org.eclipse.swt.events.SelectionListener;
18

    
19
import eu.etaxonomy.cdm.common.UriUtils;
20
import eu.etaxonomy.cdm.common.media.ImageInfo;
21
import eu.etaxonomy.cdm.model.media.ImageFile;
22
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
23
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
24
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
25
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
26
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
27
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
28
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
29
import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionElement;
30

    
31
/**
32
 * <p>MediaRepresentationElement class.</p>
33
 *
34
 * @author n.hoffmann
35
 * @created Mar 24, 2010
36
 * @version 1.0
37
 */
38
public class MediaRepresentationElement extends AbstractEntityCollectionElement<MediaRepresentation> {
39

    
40
	private MediaRepresentationPartSection section_mediaRepresentationPart;
41
	private TextWithLabelElement text_mimeType;
42
	private TextWithLabelElement text_suffix;
43

    
44
	public MediaRepresentationElement(CdmFormFactory cdmFormFactory,
45
			AbstractFormSection section,
46
			MediaRepresentation element, SelectionListener removeListener,
47
			int style) {
48
		super(cdmFormFactory, section, element, removeListener, null, style);
49
	}
50

    
51
	/** {@inheritDoc} */
52
	@Override
53
	public void createControls(ICdmFormElement element, int style) {
54
		text_mimeType = formFactory.createTextWithLabelElement(element, "Mime Type", null, style);
55
		text_suffix = formFactory.createTextWithLabelElement(element, "Suffix", null, style);
56
		section_mediaRepresentationPart = formFactory.createMediaRepresentationPartSection(getConversationHolder(), element, style);
57
		section_mediaRepresentationPart.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
58
	}
59

    
60
	/** {@inheritDoc} */
61
	@Override
62
	public void setEntity(MediaRepresentation element) {
63
		this.entity = element;
64
		section_mediaRepresentationPart.setEntity(element);
65
		text_mimeType.setText(element.getMimeType());
66
		text_suffix.setText(element.getSuffix());
67
	}
68

    
69
	/** {@inheritDoc} */
70
	@Override
71
	public void handleEvent(Object eventSource) {
72
		if(eventSource == text_mimeType){
73
			getEntity().setMimeType(text_mimeType.getText());
74
		}
75
		else if(eventSource == text_suffix){
76
			getEntity().setSuffix(text_suffix.getText());
77
		}
78
		// FIXME HACK automatically set the mime type to the first mediaRepresentationPart's mimetype
79
		else if(eventSource == section_mediaRepresentationPart){
80
			firePropertyChangeEvent(this);
81

    
82
			Collection<MediaRepresentationPart> imageFileElements = section_mediaRepresentationPart.getCollection(section_mediaRepresentationPart.getEntity());
83

    
84
			if(! imageFileElements.iterator().hasNext()){
85
				return;
86
			}
87

    
88
			MediaRepresentationPart mediaRepresentationPart = imageFileElements.iterator().next();
89
			if(mediaRepresentationPart == null || !(mediaRepresentationPart instanceof ImageFile)){
90
				return;
91
			}
92
			ImageFile imageFile = (ImageFile) mediaRepresentationPart;
93

    
94
			URI uri = imageFile.getUri();
95
			if(!UriUtils.isServiceAvailable(uri)){
96
				return;
97
			}
98
			try {
99
				ImageInfo imageInfo = ImageInfo.NewInstance(uri, 10000);
100
				String mimeType = imageInfo.getMimeType();
101
				text_mimeType.setText(mimeType);
102
				getEntity().setMimeType(mimeType);
103
				text_suffix.setText(imageInfo.getSuffix());
104
				getEntity().setSuffix(imageInfo.getSuffix());
105
			}
106
			//theses exceptions do not need to be logged
107
			//especially because this happens with every key stroke
108
			catch (IOException e) {
109
//				MessagingUtils.error(getClass(), e);
110
			}
111
			catch (HttpException e) {
112
//				MessagingUtils.error(getClass(), e);
113
			}
114
		}
115
	}
116
}
(5-5/10)