Project

General

Profile

Download (4.74 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.ui.section.media;
12

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

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

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

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

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

    
45
	/**
46
	 * <p>Constructor for MediaRepresentationElement.</p>
47
	 *
48
	 * @param cdmFormFactory a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory} object.
49
	 * @param section a {@link eu.etaxonomy.taxeditor.ui.element.AbstractFormSection} object.
50
	 * @param element a {@link eu.etaxonomy.cdm.model.media.MediaRepresentation} object.
51
	 * @param removeListener a {@link org.eclipse.swt.events.SelectionListener} object.
52
	 * @param style a int.
53
	 */
54
	public MediaRepresentationElement(CdmFormFactory cdmFormFactory,
55
			AbstractFormSection section,
56
			MediaRepresentation element, SelectionListener removeListener,
57
			int style) {
58
		super(cdmFormFactory, section, element, removeListener, null, style);
59
	}
60

    
61
	/* (non-Javadoc)
62
	 * @see eu.etaxonomy.taxeditor.forms.section.AbstractEntityCollectionElement#createControls(eu.etaxonomy.taxeditor.forms.ICdmFormElement, int)
63
	 */
64
	/** {@inheritDoc} */
65
	@Override
66
	public void createControls(ICdmFormElement element, int style) {
67
		text_mimeType = formFactory.createTextWithLabelElement(element, "Mime Type", null, style);
68
		text_suffix = formFactory.createTextWithLabelElement(element, "Suffix", null, style);
69
		section_mediaRepresentationPart = formFactory.createMediaRepresentationPartSection(getConversationHolder(), element, style);
70
		section_mediaRepresentationPart.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
71
	}
72

    
73
	/** {@inheritDoc} */
74
	@Override
75
	public void setEntity(MediaRepresentation element) {
76
		this.entity = element;
77
		section_mediaRepresentationPart.setEntity(element);
78
		text_mimeType.setText(element.getMimeType());
79
		text_suffix.setText(element.getSuffix());
80
	}
81

    
82
	/*
83
	 * (non-Javadoc)
84
	 * @see eu.etaxonomy.taxeditor.section.AbstractEntityCollectionElement#handleEvent(java.lang.Object)
85
	 */
86
	/** {@inheritDoc} */
87
	@Override
88
	public void handleEvent(Object eventSource) {
89
		if(eventSource == text_mimeType){
90
			getEntity().setMimeType(text_mimeType.getText());
91
		}
92
		else if(eventSource == text_suffix){
93
			getEntity().setSuffix(text_suffix.getText());
94
		}
95
		// FIXME HACK automatically set the mime type to the first mediaRepresentationPart's mimetype
96
		else if(eventSource == section_mediaRepresentationPart){
97
			firePropertyChangeEvent(this);
98

    
99
			Collection<MediaRepresentationPart> imageFileElements = section_mediaRepresentationPart.getCollection(section_mediaRepresentationPart.getEntity());
100

    
101
			if(! imageFileElements.iterator().hasNext()){
102
				return;
103
			}
104

    
105
			MediaRepresentationPart mediaRepresentationPart = imageFileElements.iterator().next();
106
			if(mediaRepresentationPart == null || !(mediaRepresentationPart instanceof ImageFile)){
107
				return;
108
			}
109
			ImageFile imageFile = (ImageFile) mediaRepresentationPart;
110

    
111
			URI uri = imageFile.getUri();
112
			if(!UriUtils.isServiceAvailable(uri)){
113
				return;
114
			}
115
			try {
116
				ImageInfo imageInfo = ImageInfo.NewInstance(uri, 10000);
117
				String mimeType = imageInfo.getMimeType();
118
				text_mimeType.setText(mimeType);
119
				getEntity().setMimeType(mimeType);
120
				text_suffix.setText(imageInfo.getSuffix());
121
				getEntity().setSuffix(imageInfo.getSuffix());
122
			}
123
			//theses exceptions do not need to be logged
124
			//especially because this happens with every key stroke
125
			catch (IOException e) {
126
//				MessagingUtils.error(getClass(), e);
127
			}
128
			catch (HttpException e) {
129
//				MessagingUtils.error(getClass(), e);
130
			}
131
		}
132
	}
133
}
(4-4/8)