]> git.argeo.org Git - lgpl/argeo-commons.git/blob - GenericUploadControl.java
48806b496aa5138575a0f8da5c3f91ee4f441b00
[lgpl/argeo-commons.git] / GenericUploadControl.java
1 package org.argeo.eclipse.ui.specific;
2
3 import java.io.InputStream;
4
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;
15
16 public class GenericUploadControl extends Composite {
17 private final static Log log = LogFactory
18 .getLog(GenericUploadControl.class);
19
20 private Upload upload;
21
22 public GenericUploadControl(Composite parent, int style, String browseLabel) {
23 super(parent, style);
24 createControl(this, browseLabel);
25
26 }
27
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));
31
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());
36 parent.pack();
37 }
38
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()))
44 return true;
45 else
46 return false;
47 }
48
49 public byte[] performUpload() {
50 boolean success = upload.performUpload();
51 if (success) {
52 if (upload.getUploadItem().getFileSize() == -1)
53 throw new ArgeoException("File "
54 + upload.getUploadItem().getFileName()
55 + " has not been uploaded, its size is -1");
56
57 InputStream inStream = null;
58 byte[] fileBA = null;
59 try {
60 inStream = upload.getUploadItem().getFileInputStream();
61 fileBA = IOUtils.toByteArray(inStream);
62 } catch (Exception e) {
63 throw new ArgeoException("Cannot read uploaded data", e);
64 } finally {
65 IOUtils.closeQuietly(inStream);
66 }
67 return fileBA;
68 }
69 return null;
70 }
71
72 public void addModifyListener(ModifyListener listener) {
73 upload.addModifyListener(listener);
74 }
75
76 // private class UploadManager extends UploadAdapter {
77 // private Upload upload;
78 //
79 // public UploadManager(Upload upload) {
80 // super();
81 // this.upload = upload;
82 // }
83 //
84 // public void uploadFinished(UploadEvent uploadEvent) {
85 // handleUploadFinished(upload);
86 // }
87 //
88 // public void uploadInProgress(UploadEvent uploadEvent) {
89 // }
90 //
91 // public void uploadException(UploadEvent uploadEvent) {
92 // Exception exc = uploadEvent.getUploadException();
93 // if (exc != null) {
94 // MessageDialog.openError(Display.getCurrent().getActiveShell(),
95 // "Error", exc.getMessage());
96 // }
97 // }
98 //
99 // }
100 //
101
102 }