Merge branch 'release/3.7.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / media / MediaRepresentationElement.java
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 public MediaRepresentationElement(CdmFormFactory cdmFormFactory,
46 AbstractFormSection section,
47 MediaRepresentation element, SelectionListener removeListener,
48 int style) {
49 super(cdmFormFactory, section, element, removeListener, null, style);
50 }
51
52 /** {@inheritDoc} */
53 @Override
54 public void createControls(ICdmFormElement element, int style) {
55 text_mimeType = formFactory.createTextWithLabelElement(element, "Mime Type", null, style);
56 text_suffix = formFactory.createTextWithLabelElement(element, "Suffix", null, style);
57 section_mediaRepresentationPart = formFactory.createMediaRepresentationPartSection(getConversationHolder(), element, style);
58 section_mediaRepresentationPart.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
59 }
60
61 /** {@inheritDoc} */
62 @Override
63 public void setEntity(MediaRepresentation element) {
64 this.entity = element;
65 section_mediaRepresentationPart.setEntity(element);
66 text_mimeType.setText(element.getMimeType());
67 text_suffix.setText(element.getSuffix());
68 }
69
70 /** {@inheritDoc} */
71 @Override
72 public void handleEvent(Object eventSource) {
73 if(eventSource == text_mimeType){
74 getEntity().setMimeType(text_mimeType.getText());
75 }
76 else if(eventSource == text_suffix){
77 getEntity().setSuffix(text_suffix.getText());
78 }
79 // FIXME HACK automatically set the mime type to the first mediaRepresentationPart's mimetype
80 else if(eventSource == section_mediaRepresentationPart){
81 firePropertyChangeEvent(this);
82
83 Collection<MediaRepresentationPart> imageFileElements = section_mediaRepresentationPart.getCollection(section_mediaRepresentationPart.getEntity());
84
85 if(! imageFileElements.iterator().hasNext()){
86 return;
87 }
88
89 MediaRepresentationPart mediaRepresentationPart = imageFileElements.iterator().next();
90 if(mediaRepresentationPart == null || !(mediaRepresentationPart instanceof ImageFile)){
91 return;
92 }
93 ImageFile imageFile = (ImageFile) mediaRepresentationPart;
94
95 URI uri = imageFile.getUri();
96 if(!UriUtils.isServiceAvailable(uri)){
97 return;
98 }
99 try {
100 ImageInfo imageInfo = ImageInfo.NewInstance(uri, 10000);
101 String mimeType = imageInfo.getMimeType();
102 text_mimeType.setText(mimeType);
103 getEntity().setMimeType(mimeType);
104 text_suffix.setText(imageInfo.getSuffix());
105 getEntity().setSuffix(imageInfo.getSuffix());
106 }
107 //theses exceptions do not need to be logged
108 //especially because this happens with every key stroke
109 catch (IOException e) {
110 // MessagingUtils.error(getClass(), e);
111 }
112 catch (HttpException e) {
113 // MessagingUtils.error(getClass(), e);
114 }
115 }
116 }
117 }