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

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

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

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

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

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

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

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

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

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