2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.ui
.section
.media
;
12 import java
.io
.IOException
;
13 import java
.util
.Collection
;
15 import org
.apache
.http
.HttpException
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.swt
.events
.SelectionListener
;
22 import eu
.etaxonomy
.cdm
.common
.URI
;
23 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
24 import eu
.etaxonomy
.cdm
.common
.media
.CdmImageInfo
;
25 import eu
.etaxonomy
.cdm
.model
.media
.ImageFile
;
26 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentation
;
27 import eu
.etaxonomy
.cdm
.model
.media
.MediaRepresentationPart
;
28 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractFormSection
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.TextWithLabelElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractEntityCollectionElement
;
37 * <p>MediaRepresentationElement class.</p>
40 * @created Mar 24, 2010
42 public class MediaRepresentationElement
extends AbstractEntityCollectionElement
<MediaRepresentation
> {
44 private MediaRepresentationPartSection section_mediaRepresentationPart
;
45 private TextWithLabelElement text_mimeType
;
46 private TextWithLabelElement text_suffix
;
48 public MediaRepresentationElement(CdmFormFactory cdmFormFactory
,
49 AbstractFormSection section
,
50 MediaRepresentation element
, SelectionListener removeListener
,
52 super(cdmFormFactory
, section
, element
, removeListener
, null, style
);
56 public void createControls(ICdmFormElement element
, int style
) {
57 text_mimeType
= formFactory
.createTextWithLabelElement(element
, "Mime Type", null, style
);
58 text_suffix
= formFactory
.createTextWithLabelElement(element
, "Suffix", null, style
);
59 section_mediaRepresentationPart
= formFactory
.createMediaRepresentationPartSection(getConversationHolder(), element
, StoreUtil
.getSectionStyle(MediaRepresentationPartSection
.class, MediaRepresentation
.class.getCanonicalName()));
60 section_mediaRepresentationPart
.setLayoutData(LayoutConstants
.FILL_HORIZONTALLY(2, 1));
64 public void setEntity(MediaRepresentation element
) {
65 this.entity
= element
;
66 section_mediaRepresentationPart
.setEntity(element
);
67 text_mimeType
.setText(element
.getMimeType());
68 text_suffix
.setText(element
.getSuffix());
72 public void handleEvent(Object eventSource
) {
73 if(eventSource
== text_mimeType
){
74 getEntity().setMimeType(text_mimeType
.getText());
76 else if(eventSource
== text_suffix
){
77 getEntity().setSuffix(text_suffix
.getText());
79 // FIXME HACK automatically set the mime type to the first mediaRepresentationPart's mimetype
80 else if(eventSource
== section_mediaRepresentationPart
){
81 firePropertyChangeEvent(this);
82 new Job("Set mime type") {
84 protected IStatus
run(IProgressMonitor monitor
) {
85 Collection
<MediaRepresentationPart
> imageFileElements
= section_mediaRepresentationPart
.getCollection(section_mediaRepresentationPart
.getEntity());
86 if(! imageFileElements
.iterator().hasNext()){
87 return Status
.CANCEL_STATUS
;
89 MediaRepresentationPart mediaRepresentationPart
= imageFileElements
.iterator().next();
90 if(mediaRepresentationPart
== null || !(mediaRepresentationPart
instanceof ImageFile
)){
91 return Status
.CANCEL_STATUS
;
93 ImageFile imageFile
= (ImageFile
) mediaRepresentationPart
;
95 URI uri
= imageFile
.getUri();
96 if(!UriUtils
.isServiceAvailable(uri
)){
97 return Status
.CANCEL_STATUS
;
100 CdmImageInfo imageInfo
= CdmImageInfo
.NewInstance(uri
, 10000);
101 String mimeType
= imageInfo
.getMimeType();
102 getEntity().setMimeType(mimeType
);
103 getEntity().setSuffix(imageInfo
.getSuffix());
104 MediaRepresentationElement
.this.getLayoutComposite().getDisplay().asyncExec(()->{
105 text_mimeType
.setText(mimeType
);
106 text_suffix
.setText(imageInfo
.getSuffix());
109 //theses exceptions do not need to be logged
110 //especially because this happens with every key stroke
111 catch (IOException
|HttpException e
) {
114 return Status
.OK_STATUS
;