Project

General

Profile

Download (5.74 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.net.URI;
13

    
14
import org.apache.commons.io.FileUtils;
15
import org.eclipse.core.runtime.IProgressMonitor;
16
import org.eclipse.core.runtime.IStatus;
17
import org.eclipse.core.runtime.Status;
18
import org.eclipse.core.runtime.jobs.Job;
19
import org.eclipse.swt.events.SelectionListener;
20

    
21
import eu.etaxonomy.cdm.common.media.ImageInfo;
22
import eu.etaxonomy.cdm.model.media.ImageFile;
23
import eu.etaxonomy.taxeditor.store.StoreUtil;
24
import eu.etaxonomy.taxeditor.ui.element.AbstractFormSection;
25
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
26
import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
27
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
28
import eu.etaxonomy.taxeditor.ui.element.ImageElement;
29
import eu.etaxonomy.taxeditor.ui.element.KeyValueViewerElement;
30
import eu.etaxonomy.taxeditor.ui.element.NumberWithLabelElement;
31

    
32
/**
33
 * @author n.hoffmann
34
 * @created Mar 26, 2010
35
 */
36
public class ImageFileElement extends MediaRepresentationPartElement<ImageFile> {
37

    
38
    private final class LoadImageJob extends Job{
39

    
40
        private URI uri;
41
        private boolean updateDimensions;
42

    
43
        public LoadImageJob(URI uri, boolean updateDimensions) {
44
            super("Load image");
45
            this.uri = uri;
46
            this.updateDimensions = updateDimensions;
47
        }
48

    
49
        @Override
50
        protected IStatus run(IProgressMonitor monitor) {
51
            try{
52
                ImageInfo imageInfo = ImageInfo.NewInstanceWithMetaData(uri, 10000);
53
                ImageFileElement.this.getLayoutComposite().getDisplay().asyncExec(()->{
54
                    element_keyValue.setInput(imageInfo.getMetaData());
55
                    try {
56
                        disposeImage();
57
                        element_image = formFactory.createImageElement(parentFormElement, uri, style);
58
                        element_image.initImageUri(uri);
59
                    } catch (Exception e) {
60
                        handleException();
61
                    }
62
                    element_image.loadImage();
63
                    if(uri == null){
64
                        return;
65
                    }
66
                    if(updateDimensions){
67
                        text_size.setText(FileUtils.byteCountToDisplaySize(imageInfo.getLength()));
68
                        // KLUDGE this is not save for very large files, because of the int cast.
69
                        // But then, I don't think we will handle such large files in the near future
70
                        getEntity().setSize((int) imageInfo.getLength());
71

    
72
                        text_height.setNumber(imageInfo.getHeight());
73
                        getEntity().setHeight(imageInfo.getHeight());
74

    
75
                        text_width.setNumber(imageInfo.getWidth());
76
                        getEntity().setWidth(imageInfo.getWidth());
77
                    }
78
                    StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
79
                });
80
            }
81
            catch (Exception e) {
82
                handleException();
83
            }
84
            return Status.OK_STATUS;
85
        }
86

    
87
    }
88

    
89
	private NumberWithLabelElement text_height;
90
	private NumberWithLabelElement text_width;
91
	private KeyValueViewerElement element_keyValue;
92
	private ImageElement element_image;
93
    private ICdmFormElement parentFormElement;
94
    private int style;
95

    
96
	public ImageFileElement(CdmFormFactory cdmFormFactory,
97
			AbstractFormSection section, ImageFile element,
98
			SelectionListener removeListener, int style) {
99
		super(cdmFormFactory, section, element, removeListener, style);
100
	}
101

    
102
	@Override
103
	public void createControls(ICdmFormElement formElement, int style) {
104
		super.createControls(formElement, style);
105

    
106
        this.parentFormElement = formElement;
107
        this.style = style;
108

    
109
		text_height = formFactory.createFloatTextWithLabelElement(formElement, "Height", null, style);
110
		text_height.setEnabled(false);
111
		text_width = formFactory.createFloatTextWithLabelElement(formElement, "Width", null, style);
112
		text_width.setEnabled(false);
113
		element_keyValue = formFactory.createKeyValueViewerElement(formElement, "Key", "Value", null);
114
	}
115

    
116
	@Override
117
	public void setEntity(ImageFile entity) {
118
		super.setEntity(entity);
119
		text_height.setNumber(entity.getHeight());
120
		text_width.setNumber(entity.getWidth());
121
		try {
122
            new LoadImageJob(getEntity().getUri(), true).schedule();
123
		} catch (Exception e) {
124
		    e.printStackTrace();
125
			handleException();
126
		}
127
	}
128

    
129
	@Override
130
	public void handleEvent(Object eventSource) {
131
		if(eventSource == text_uri){
132
			try {
133
			    URI uri = text_uri.parseText();
134
				getEntity().setUri(uri);
135
				if(uri==null){
136
				    //buffer URI if parsing error occurred
137
				    MediaDetailElement parentMediaDetailElement = getParentMediaDetailElement();
138
				    if(parentMediaDetailElement!=null){
139
				        parentMediaDetailElement.setUriBuffer(text_uri.getText());
140
				    }
141
				}
142
				new LoadImageJob(getEntity().getUri(), true).schedule();
143
				firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
144
			} catch (Exception e) {
145
				handleException();
146
			} finally {
147
				firePropertyChangeEvent(this);
148
			}
149
		}
150
	}
151

    
152
	protected void handleException() {
153
	    if(!getLayoutComposite().isDisposed()){
154
	        getLayoutComposite().getDisplay().asyncExec(()->{
155
	            disposeImage();
156
	            StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
157
	        });
158
	    }
159
	}
160

    
161
    private void disposeImage(){
162
        if(element_image!=null){
163
            element_image.dispose();
164
            element_image = null;
165
        }
166
    }
167
}
(2-2/10)