Project

General

Profile

Download (5.1 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.core.runtime.IProgressMonitor;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.Status;
20
import org.eclipse.core.runtime.jobs.Job;
21
import org.eclipse.swt.events.SelectionListener;
22

    
23
import eu.etaxonomy.cdm.common.UriUtils;
24
import eu.etaxonomy.cdm.common.media.ImageInfo;
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;
35

    
36
/**
37
 * <p>MediaRepresentationElement class.</p>
38
 *
39
 * @author n.hoffmann
40
 * @created Mar 24, 2010
41
 * @version 1.0
42
 */
43
public class MediaRepresentationElement extends AbstractEntityCollectionElement<MediaRepresentation> {
44

    
45
	private MediaRepresentationPartSection section_mediaRepresentationPart;
46
	private TextWithLabelElement text_mimeType;
47
	private TextWithLabelElement text_suffix;
48

    
49
	public MediaRepresentationElement(CdmFormFactory cdmFormFactory,
50
			AbstractFormSection section,
51
			MediaRepresentation element, SelectionListener removeListener,
52
			int style) {
53
		super(cdmFormFactory, section, element, removeListener, null, style);
54
	}
55

    
56
	/** {@inheritDoc} */
57
	@Override
58
	public void createControls(ICdmFormElement element, int style) {
59
		text_mimeType = formFactory.createTextWithLabelElement(element, "Mime Type", null, style);
60
		text_suffix = formFactory.createTextWithLabelElement(element, "Suffix", null, style);
61
		section_mediaRepresentationPart = formFactory.createMediaRepresentationPartSection(getConversationHolder(), element, StoreUtil.getSectionStyle(MediaRepresentationPartSection.class, MediaRepresentation.class.getCanonicalName()));
62
		section_mediaRepresentationPart.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
63
	}
64

    
65
	/** {@inheritDoc} */
66
	@Override
67
	public void setEntity(MediaRepresentation element) {
68
		this.entity = element;
69
		section_mediaRepresentationPart.setEntity(element);
70
		text_mimeType.setText(element.getMimeType());
71
		text_suffix.setText(element.getSuffix());
72
	}
73

    
74
	/** {@inheritDoc} */
75
	@Override
76
	public void handleEvent(Object eventSource) {
77
		if(eventSource == text_mimeType){
78
			getEntity().setMimeType(text_mimeType.getText());
79
		}
80
		else if(eventSource == text_suffix){
81
			getEntity().setSuffix(text_suffix.getText());
82
		}
83
		// FIXME HACK automatically set the mime type to the first mediaRepresentationPart's mimetype
84
		else if(eventSource == section_mediaRepresentationPart){
85
			firePropertyChangeEvent(this);
86
			new Job("Set mime type") {
87
                @Override
88
                protected IStatus run(IProgressMonitor monitor) {
89
                    Collection<MediaRepresentationPart> imageFileElements = section_mediaRepresentationPart.getCollection(section_mediaRepresentationPart.getEntity());
90
                    if(! imageFileElements.iterator().hasNext()){
91
                        return Status.CANCEL_STATUS;
92
                    }
93
                    MediaRepresentationPart mediaRepresentationPart = imageFileElements.iterator().next();
94
                    if(mediaRepresentationPart == null || !(mediaRepresentationPart instanceof ImageFile)){
95
                        return Status.CANCEL_STATUS;
96
                    }
97
                    ImageFile imageFile = (ImageFile) mediaRepresentationPart;
98

    
99
                    URI uri = imageFile.getUri();
100
                    if(!UriUtils.isServiceAvailable(uri)){
101
                        return Status.CANCEL_STATUS;
102
                    }
103
                    try {
104
                        ImageInfo imageInfo = ImageInfo.NewInstance(uri, 10000);
105
                        String mimeType = imageInfo.getMimeType();
106
                        getEntity().setMimeType(mimeType);
107
                        getEntity().setSuffix(imageInfo.getSuffix());
108
                        MediaRepresentationElement.this.getLayoutComposite().getDisplay().asyncExec(()->{
109
                            text_mimeType.setText(mimeType);
110
                            text_suffix.setText(imageInfo.getSuffix());
111
                        });
112
                    }
113
                    //theses exceptions do not need to be logged
114
                    //especially because this happens with every key stroke
115
                    catch (IOException|HttpException e) {
116
                        // ignore
117
                    }
118
                    return Status.OK_STATUS;
119
                }
120
            }.schedule();
121
		}
122
	}
123
}
(5-5/10)