]> git.argeo.org Git - lgpl/argeo-commons.git/blob - GenericUploadControl.java
f00196c302e6bb85dd394536793f982bd4238fc6
[lgpl/argeo-commons.git] / GenericUploadControl.java
1 package org.argeo.eclipse.ui.specific;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.apache.commons.io.FileUtils;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.events.ModifyListener;
11 import org.eclipse.swt.layout.GridData;
12 import org.eclipse.swt.layout.GridLayout;
13 import org.eclipse.swt.widgets.Button;
14 import org.eclipse.swt.widgets.Composite;
15 import org.eclipse.swt.widgets.Event;
16 import org.eclipse.swt.widgets.FileDialog;
17 import org.eclipse.swt.widgets.Listener;
18 import org.eclipse.swt.widgets.Text;
19
20 public class GenericUploadControl extends Composite {
21 private final static Log log = LogFactory
22 .getLog(GenericUploadControl.class);
23
24 private FileDialog dialog;
25 private Text filePath;
26
27 public GenericUploadControl(Composite parent, int style, String browseLabel) {
28 super(parent, style);
29 createControl(this, browseLabel);
30
31 }
32
33 private void createControl(final Composite parent, String browseLabel) {
34 Composite composite = new Composite(parent, SWT.FILL);
35 composite.setLayout(new GridLayout(2, false));
36 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
37
38 filePath = new Text(parent, SWT.BORDER | SWT.SINGLE);
39 filePath.setEnabled(false);
40
41 // Execute button
42 Button execute = new Button(parent, SWT.PUSH);
43 GridData gridData = new GridData();
44 gridData.horizontalAlignment = GridData.BEGINNING;
45 execute.setLayoutData(gridData);
46 execute.setText(browseLabel);
47
48 // Button listener
49 Listener executeListener = new Listener() {
50 public void handleEvent(Event event) {
51 dialog = new FileDialog(parent.getShell());
52 dialog.open();
53
54 }
55 };
56
57 execute.addListener(SWT.Selection, executeListener);
58 }
59
60 public boolean isControlEmpty() {
61 String path = filePath.getText();
62 if (path == null || "".equals(path.trim()))
63 return true;
64 else
65 return false;
66 }
67
68 public byte[] performUpload() {
69 String path = filePath.getText();
70 if (path != null) {
71 try {
72 File file = new File(path);
73 byte[] fileBA = FileUtils.readFileToByteArray(file);
74 return fileBA;
75 } catch (IOException e) {
76 // TODO Auto-generated catch block
77 e.printStackTrace();
78 }
79 }
80 return null;
81 }
82
83 public void addModifyListener(ModifyListener listener) {
84 filePath.addModifyListener(listener);
85 }
86
87 /**
88 * Always returns null in an RCP environment
89 */
90 public String getLastFileUploadedName() {
91 return null;
92 }
93
94 // private class UploadManager extends UploadAdapter {
95 // private Upload upload;
96 //
97 // public UploadManager(Upload upload) {
98 // super();
99 // this.upload = upload;
100 // }
101 //
102 // public void uploadFinished(UploadEvent uploadEvent) {
103 // handleUploadFinished(upload);
104 // }
105 //
106 // public void uploadInProgress(UploadEvent uploadEvent) {
107 // }
108 //
109 // public void uploadException(UploadEvent uploadEvent) {
110 // Exception exc = uploadEvent.getUploadException();
111 // if (exc != null) {
112 // MessageDialog.openError(Display.getCurrent().getActiveShell(),
113 // "Error", exc.getMessage());
114 // }
115 // }
116 //
117 // }
118 //
119
120 }