Project

General

Profile

« Previous | Next » 

Revision ca1b4808

Added by Patrick Plitzner almost 5 years ago

ref #8129 Load images asynchronously in media details view

View differences:

eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/media/MediaDetailElement.java
8 8
*/
9 9
package eu.etaxonomy.taxeditor.ui.section.media;
10 10

  
11
import java.io.IOException;
11 12
import java.net.URI;
12 13
import java.util.List;
13 14
import java.util.Set;
14 15

  
16
import org.apache.http.HttpException;
15 17
import org.apache.sanselan.ImageReadException;
18
import org.eclipse.core.runtime.IProgressMonitor;
19
import org.eclipse.core.runtime.IStatus;
20
import org.eclipse.core.runtime.Status;
21
import org.eclipse.core.runtime.jobs.Job;
16 22
import org.eclipse.swt.SWT;
17 23
import org.eclipse.swt.widgets.Label;
18 24

  
......
42 48
 */
43 49
public class MediaDetailElement extends AbstractCdmDetailElement<Media>{
44 50

  
51
    /**
52
     * @author pplitzner
53
     * @since Jul 11, 2019
54
     *
55
     */
56
    private final class LoadImageJob extends Job {
57
        private URI uri;
58
        private LoadImageJob(URI uri, String name) {
59
            super(name);
60
            this.uri = uri;
61
        }
62

  
63
        @Override
64
        protected IStatus run(IProgressMonitor monitor) {
65
            ImageInfo imageInfo;
66
            try {
67
                //first check if uri refers to an actual (non-image) file
68
                UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
69
                imageInfo = ImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
70
                MediaDetailElement.this.getLayoutComposite().getDisplay().asyncExec(()->{
71
                    singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
72
                    if(singleMediaRepresentationPart.isInstanceOf(ImageFile.class)){
73
                        ImageFile image = CdmBase.deproxy(singleMediaRepresentationPart, ImageFile.class);
74
                        image.setHeight(imageInfo.getHeight());
75
                        image.setWidth(imageInfo.getWidth());
76
                    }
77
                    singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
78
                    singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
79
                    element_image = formFactory.createImageElement(parentFormElement, uri, style);
80
                    StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
81
                    try {
82
                        element_image.initImageUri(uri);
83
                    } catch (IOException | HttpException e) {
84
                        exception(e, uri);
85
                    }
86
                    element_image.loadImage();
87
                    disposeErrorLabel();
88
                });
89
            } catch (IOException|HttpException e){
90
               exception(e, uri);
91
            }
92
            return Status.OK_STATUS;
93
        }
94
    }
95

  
45 96
    private UriWithLabelElement textUri;
46 97
    private MediaRepresentationSection section_mediaRepresentation;
47 98

  
......
89 140
                uriBuffer=textUri.getText();
90 141
            }
91 142
            else{
92
                createImageElement(uri);
143
                new LoadImageJob(uri, "loadImage").schedule();
93 144
            }
94 145
        }
95 146
    }
96 147

  
97
    private void createImageElement(URI uri) {
98
        ImageInfo imageInfo;
99
        try {
100
            if(uri == null){
101
                return;
148
    private void exception(Exception e, URI uri){
149
        getLayoutComposite().getDisplay().asyncExec(()->{
150
            if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
151
                disposeErrorLabel();
152
                handleException(uri, "No preview available for this file type");
102 153
            }
103
            //first check if uri refers to an actual (non-image) file
104
            UriUtils.getInputStream(uri);// will fail with a FileNotFoundException if not
105
            imageInfo = ImageInfo.NewInstance(uri, 10000);//will fail when it is no image file
106
            singleMediaRepresentationPart.setSize((int) imageInfo.getLength());
107
            if(singleMediaRepresentationPart.isInstanceOf(ImageFile.class)){
108
            	ImageFile image = CdmBase.deproxy(singleMediaRepresentationPart, ImageFile.class);
109
                image.setHeight(imageInfo.getHeight());
110
                image.setWidth(imageInfo.getWidth());
154
            else{
155
                disposeErrorLabel();
156
                handleException(uri, "No file found");
111 157
            }
112
            singleMediaRepresentationPart.getMediaRepresentation().setMimeType(imageInfo.getMimeType());
113
            singleMediaRepresentationPart.getMediaRepresentation().setSuffix(imageInfo.getSuffix());
114
            element_image = formFactory.createImageElement(parentFormElement, uri, style);
115
            element_image.initImageUri(uri);
116
            element_image.loadImage();
117
    		disposeErrorLabel();
118
        } catch (Exception e){
119
        	if(e.getCause()!=null && e.getCause().getClass().equals(ImageReadException.class)){
120
        		disposeErrorLabel();
121
        		handleException(uri, "No preview available for this file type");
122
        	}
123
        	else{
124
        		disposeErrorLabel();
125
        		handleException(uri, "No file found");
126
        	}
127
        }
158
        });
128 159
    }
129 160

  
130 161
	private void disposeErrorLabel() {
......
182 213
            }
183 214
            textUri.getLayoutComposite().layout();
184 215

  
185
            createImageElement(singleMediaRepresentationPart.getUri());
216
            new LoadImageJob(singleMediaRepresentationPart.getUri(), "loadImage").schedule();
186 217
        }
187 218
    }
188 219

  

Also available in: Unified diff