]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/GenericUploadControl.java
1 package org
.argeo
.eclipse
.ui
.specific
;
3 import java
.io
.InputStream
;
5 import org
.apache
.commons
.io
.IOUtils
;
6 import org
.apache
.commons
.logging
.Log
;
7 import org
.apache
.commons
.logging
.LogFactory
;
8 import org
.argeo
.ArgeoException
;
9 import org
.eclipse
.rwt
.widgets
.Upload
;
10 import org
.eclipse
.swt
.SWT
;
11 import org
.eclipse
.swt
.events
.ModifyListener
;
12 import org
.eclipse
.swt
.layout
.GridData
;
13 import org
.eclipse
.swt
.layout
.GridLayout
;
14 import org
.eclipse
.swt
.widgets
.Composite
;
16 public class GenericUploadControl
extends Composite
{
17 private final static Log log
= LogFactory
18 .getLog(GenericUploadControl
.class);
20 private Upload upload
;
22 public GenericUploadControl(Composite parent
, int style
, String browseLabel
) {
24 createControl(this, browseLabel
);
28 private void createControl(Composite parent
, String browseLabel
) {
29 parent
.setLayout(new GridLayout(1, false));
30 parent
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
32 upload
= new Upload(parent
, SWT
.BORDER
);
33 upload
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
34 upload
.setBrowseButtonText(browseLabel
);
35 // upload.addModifyListener(new UploadListener());
40 * Wrap upload.getLastFileUploaded(). Gets the name of the last uploaded
41 * file. This method can be called even if the upload has not finished yet.
43 public String
getLastFileUploadedName() {
44 return upload
.getLastFileUploaded();
47 public boolean isControlEmpty() {
48 String path
= upload
.getPath();
49 if (log
.isTraceEnabled())
50 log
.trace("UploadControl chosen path : " + path
);
51 if (path
== null || "".equals(path
.trim()))
57 public byte[] performUpload() {
58 boolean success
= upload
.performUpload();
60 if (upload
.getUploadItem().getFileSize() == -1)
61 throw new ArgeoException("File "
62 + upload
.getUploadItem().getFileName()
63 + " has not been uploaded, its size is -1");
65 InputStream inStream
= null;
68 inStream
= upload
.getUploadItem().getFileInputStream();
69 fileBA
= IOUtils
.toByteArray(inStream
);
70 } catch (Exception e
) {
71 throw new ArgeoException("Cannot read uploaded data", e
);
73 IOUtils
.closeQuietly(inStream
);
80 public void addModifyListener(ModifyListener listener
) {
81 upload
.addModifyListener(listener
);
84 // private class UploadManager extends UploadAdapter {
85 // private Upload upload;
87 // public UploadManager(Upload upload) {
89 // this.upload = upload;
92 // public void uploadFinished(UploadEvent uploadEvent) {
93 // handleUploadFinished(upload);
96 // public void uploadInProgress(UploadEvent uploadEvent) {
99 // public void uploadException(UploadEvent uploadEvent) {
100 // Exception exc = uploadEvent.getUploadException();
101 // if (exc != null) {
102 // MessageDialog.openError(Display.getCurrent().getActiveShell(),
103 // "Error", exc.getMessage());