Project

General

Profile

Download (5.76 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 org.apache.commons.io.FileUtils;
13
import org.eclipse.core.runtime.IProgressMonitor;
14
import org.eclipse.core.runtime.IStatus;
15
import org.eclipse.core.runtime.Status;
16
import org.eclipse.core.runtime.jobs.Job;
17
import org.eclipse.swt.events.SelectionListener;
18

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

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

    
37
    private final class LoadImageJob extends Job{
38

    
39
        private URI uri;
40
        private boolean updateDimensions;
41

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

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

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

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

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

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

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

    
104
        this.parentFormElement = formElement;
105
        this.style = style;
106

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

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

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

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

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