]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - GenericUploadControl.java
48806b496aa5138575a0f8da5c3f91ee4f441b00
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());
39 public boolean isControlEmpty() {
40 String path
= upload
.getPath();
41 if (log
.isTraceEnabled())
42 log
.trace("UploadControl chosen path : " + path
);
43 if (path
== null || "".equals(path
.trim()))
49 public byte[] performUpload() {
50 boolean success
= upload
.performUpload();
52 if (upload
.getUploadItem().getFileSize() == -1)
53 throw new ArgeoException("File "
54 + upload
.getUploadItem().getFileName()
55 + " has not been uploaded, its size is -1");
57 InputStream inStream
= null;
60 inStream
= upload
.getUploadItem().getFileInputStream();
61 fileBA
= IOUtils
.toByteArray(inStream
);
62 } catch (Exception e
) {
63 throw new ArgeoException("Cannot read uploaded data", e
);
65 IOUtils
.closeQuietly(inStream
);
72 public void addModifyListener(ModifyListener listener
) {
73 upload
.addModifyListener(listener
);
76 // private class UploadManager extends UploadAdapter {
77 // private Upload upload;
79 // public UploadManager(Upload upload) {
81 // this.upload = upload;
84 // public void uploadFinished(UploadEvent uploadEvent) {
85 // handleUploadFinished(upload);
88 // public void uploadInProgress(UploadEvent uploadEvent) {
91 // public void uploadException(UploadEvent uploadEvent) {
92 // Exception exc = uploadEvent.getUploadException();
94 // MessageDialog.openError(Display.getCurrent().getActiveShell(),
95 // "Error", exc.getMessage());