]> 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
Add a method to get LastFileUpdated name in rap environments.
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui.rap / src / main / java / org / argeo / eclipse / ui / specific / 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 /**
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.
42 */
43 public String getLastFileUploadedName() {
44 return upload.getLastFileUploaded();
45 }
46
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()))
52 return true;
53 else
54 return false;
55 }
56
57 public byte[] performUpload() {
58 boolean success = upload.performUpload();
59 if (success) {
60 if (upload.getUploadItem().getFileSize() == -1)
61 throw new ArgeoException("File "
62 + upload.getUploadItem().getFileName()
63 + " has not been uploaded, its size is -1");
64
65 InputStream inStream = null;
66 byte[] fileBA = null;
67 try {
68 inStream = upload.getUploadItem().getFileInputStream();
69 fileBA = IOUtils.toByteArray(inStream);
70 } catch (Exception e) {
71 throw new ArgeoException("Cannot read uploaded data", e);
72 } finally {
73 IOUtils.closeQuietly(inStream);
74 }
75 return fileBA;
76 }
77 return null;
78 }
79
80 public void addModifyListener(ModifyListener listener) {
81 upload.addModifyListener(listener);
82 }
83
84 // private class UploadManager extends UploadAdapter {
85 // private Upload upload;
86 //
87 // public UploadManager(Upload upload) {
88 // super();
89 // this.upload = upload;
90 // }
91 //
92 // public void uploadFinished(UploadEvent uploadEvent) {
93 // handleUploadFinished(upload);
94 // }
95 //
96 // public void uploadInProgress(UploadEvent uploadEvent) {
97 // }
98 //
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());
104 // }
105 // }
106 //
107 // }
108 //
109
110 }