Project

General

Profile

Download (5.04 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.util.Collection;
14

    
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;
21

    
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;
35

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

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

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

    
55
	@Override
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));
61
	}
62

    
63
	@Override
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());
69
	}
70

    
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
			new Job("Set mime type") {
83
                @Override
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;
88
                    }
89
                    MediaRepresentationPart mediaRepresentationPart = imageFileElements.iterator().next();
90
                    if(mediaRepresentationPart == null || !(mediaRepresentationPart instanceof ImageFile)){
91
                        return Status.CANCEL_STATUS;
92
                    }
93
                    ImageFile imageFile = (ImageFile) mediaRepresentationPart;
94

    
95
                    URI uri = imageFile.getUri();
96
                    if(!UriUtils.isServiceAvailable(uri)){
97
                        return Status.CANCEL_STATUS;
98
                    }
99
                    try {
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());
107
                        });
108
                    }
109
                    //theses exceptions do not need to be logged
110
                    //especially because this happens with every key stroke
111
                    catch (IOException|HttpException e) {
112
                        // ignore
113
                    }
114
                    return Status.OK_STATUS;
115
                }
116
            }.schedule();
117
		}
118
	}
119
}
(5-5/10)