Project

General

Profile

Download (5.25 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.api.service.media.MediaInfoFileReader;
23
import eu.etaxonomy.cdm.common.URI;
24
import eu.etaxonomy.cdm.common.UriUtils;
25
import eu.etaxonomy.cdm.common.media.CdmImageInfo;
26
import eu.etaxonomy.cdm.model.media.ImageFile;
27
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
28
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
29
import eu.etaxonomy.taxeditor.store.StoreUtil;
30
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
31
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
32
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
33
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
34
import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
35
import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionElement;
36

    
37
/**
38
 * <p>MediaRepresentationElement class.</p>
39
 *
40
 * @author n.hoffmann
41
 * @created Mar 24, 2010
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
	@Override
57
	public void createControls(ICdmFormElement element, int style) {
58
		text_mimeType = formFactory.createTextWithLabelElement(element, "Mime Type", null, style);
59
		text_suffix = formFactory.createTextWithLabelElement(element, "Suffix", null, style);
60
		section_mediaRepresentationPart = formFactory.createMediaRepresentationPartSection(getConversationHolder(), element, StoreUtil.getSectionStyle(MediaRepresentationPartSection.class, MediaRepresentation.class.getCanonicalName()));
61
		section_mediaRepresentationPart.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
62
		if (entity != null){
63
			setEntity(entity);
64
		}
65
	}
66

    
67
	@Override
68
	public void setEntity(MediaRepresentation element) {
69
		this.entity = element;
70
		if (section_mediaRepresentationPart != null){
71
			section_mediaRepresentationPart.setEntity(element);
72
			text_mimeType.setText(element.getMimeType());
73
			text_suffix.setText(element.getSuffix());
74
		}
75
	}
76

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

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