]> git.argeo.org Git - lgpl/argeo-commons.git/blob - UploadFileWizardPage.java
8c8498fe6bba9ab41bc2a367b486c112099749ee
[lgpl/argeo-commons.git] / UploadFileWizardPage.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.eclipse.ui.specific;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.OutputStream;
24
25 import org.eclipse.jface.wizard.WizardPage;
26 import org.eclipse.rap.addons.fileupload.FileDetails;
27 import org.eclipse.rap.addons.fileupload.FileUploadEvent;
28 import org.eclipse.rap.addons.fileupload.FileUploadHandler;
29 import org.eclipse.rap.addons.fileupload.FileUploadListener;
30 import org.eclipse.rap.addons.fileupload.FileUploadReceiver;
31 import org.eclipse.rap.rwt.service.ServerPushSession;
32 import org.eclipse.rap.rwt.widgets.FileUpload;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.events.SelectionAdapter;
35 import org.eclipse.swt.events.SelectionEvent;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.widgets.Composite;
39 import org.eclipse.swt.widgets.Label;
40
41 /**
42 * RWT Specific convenience page that provides a simple interface to upload one
43 * file in a wizard context
44 *
45 * TODO Finalize clean and refactoring using the latest rap version and upload
46 * dialog addons
47 *
48 */
49 public class UploadFileWizardPage extends WizardPage {
50 // private final static Log log = LogFactory
51 // .getLog(UploadFileWizardPage.class);
52 private static final long serialVersionUID = 8251354244542973179L;
53 public final static String FILE_ITEM_TYPE = "FILE";
54 public final static String FOLDER_ITEM_TYPE = "FOLDER";
55
56 private File file;
57
58 private FileUpload fileUpload;
59 private ServerPushSession pushSession;
60 private Label fileNameLabel;
61
62 public UploadFileWizardPage() {
63 super("Import from file system");
64 setDescription("Import files from the local file system to the server");
65 }
66
67 public void createControl(Composite parent) {
68 Composite composite = new Composite(parent, SWT.NONE);
69 composite.setLayout(new GridLayout(3, false));
70 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
71 new Label(composite, SWT.NONE).setText("Pick up a file");
72
73 fileNameLabel = new Label(composite, SWT.NONE | SWT.BEGINNING);
74 fileNameLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
75 false));
76
77 fileUpload = new FileUpload(composite, SWT.NONE);
78 fileUpload.setText("Browse...");
79 fileUpload.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
80 false));
81
82 final String url = startUploadReceiver();
83 pushSession = new ServerPushSession();
84
85 fileUpload.addSelectionListener(new SelectionAdapter() {
86 private static final long serialVersionUID = 1L;
87
88 @Override
89 public void widgetSelected(SelectionEvent e) {
90 String fileName = fileUpload.getFileName();
91 fileNameLabel.setText(fileName == null ? "" : fileName);
92 pushSession.start();
93 fileUpload.submit(url);
94 }
95 });
96
97 setControl(composite);
98 }
99
100 public void performFinish() {
101 // boolean success = uploadFile.performUpload();
102 // if (!success)
103 // throw new ArgeoException("Cannot upload file named "
104 // + uploadFile.getPath());
105 }
106
107 private String startUploadReceiver() {
108 MyFileUploadReceiver receiver = new MyFileUploadReceiver();
109 FileUploadHandler uploadHandler = new FileUploadHandler(receiver);
110 uploadHandler.addUploadListener(new FileUploadListener() {
111
112 public void uploadProgress(FileUploadEvent event) {
113 // handle upload progress
114 }
115
116 public void uploadFailed(FileUploadEvent event) {
117 UploadFileWizardPage.this.setErrorMessage("upload failed: "
118 + event.getException());
119 }
120
121 public void uploadFinished(FileUploadEvent event) {
122
123 fileNameLabel.getDisplay().asyncExec(new Runnable() {
124 public void run() {
125 // UploadFileWizardPage.this.getContainer()
126 // .updateButtons();
127 pushSession.stop();
128 }
129 });
130
131 // for (FileDetails file : event.getFileDetails()) {
132 // // addToLog("received: " + file.getFileName());
133 // }
134 }
135 });
136 return uploadHandler.getUploadUrl();
137 }
138
139 private class MyFileUploadReceiver extends FileUploadReceiver {
140
141 private static final String TEMP_FILE_PREFIX = "fileupload_";
142
143 @Override
144 public void receive(InputStream dataStream, FileDetails details)
145 throws IOException {
146 File result = File.createTempFile(TEMP_FILE_PREFIX, "");
147 FileOutputStream outputStream = new FileOutputStream(result);
148 try {
149 copy(dataStream, outputStream);
150 } finally {
151 dataStream.close();
152 outputStream.close();
153 }
154 file = result;
155 }
156 }
157
158 private static void copy(InputStream inputStream, OutputStream outputStream)
159 throws IOException {
160 byte[] buffer = new byte[8192];
161 boolean finished = false;
162 while (!finished) {
163 int bytesRead = inputStream.read(buffer);
164 if (bytesRead != -1) {
165 outputStream.write(buffer, 0, bytesRead);
166 } else {
167 finished = true;
168 }
169 }
170 }
171
172 /**
173 * The full path including the directory and file drive are only returned,
174 * if the browser supports reading this properties
175 *
176 * @return The full file name of the last uploaded file including the file
177 * path as selected by the user on his local machine.
178 */
179 public String getObjectPath() {
180 return null;
181 }
182
183 public String getObjectName() {
184 return fileUpload.getFileName();
185 }
186
187 public String getObjectType() {
188 return FILE_ITEM_TYPE;
189 }
190
191 // protected void handleUploadFinished(final Upload upload) {
192 // }
193
194 /** it is caller responsability to close the stream afterwards. */
195 public InputStream getFileInputStream() throws IOException {
196 return new FileInputStream(file);
197 // InputStream fis = null;
198 //
199 // try {
200 // fis = new FileInputStream(file);
201 // return fis;
202 // } catch (Exception e) {
203 // throw new ArgeoException("Unable to retrieve file " + file, e);
204 // } finally {
205 // IOUtils.closeQuietly(fis);
206 // }
207 }
208
209 public boolean getNeedsProgressMonitor() {
210 return false;
211 }
212 }