2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.eclipse
.ui
.specific
;
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
;
25 import org
.eclipse
.jface
.wizard
.WizardPage
;
26 import org
.eclipse
.rap
.fileupload
.FileDetails
;
27 import org
.eclipse
.rap
.fileupload
.FileUploadEvent
;
28 import org
.eclipse
.rap
.fileupload
.FileUploadHandler
;
29 import org
.eclipse
.rap
.fileupload
.FileUploadListener
;
30 import org
.eclipse
.rap
.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
;
42 * RWT Specific convenience page that provides a simple interface to upload one
43 * file in a wizard context
45 * TODO Finalize clean and refactoring using the latest rap version and upload
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";
58 private FileUpload fileUpload
;
59 private ServerPushSession pushSession
;
60 private Label fileNameLabel
;
62 public UploadFileWizardPage() {
63 super("Import from file system");
64 setDescription("Import files from the local file system to the server");
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");
73 fileNameLabel
= new Label(composite
, SWT
.NONE
| SWT
.BEGINNING
);
74 fileNameLabel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true,
77 fileUpload
= new FileUpload(composite
, SWT
.NONE
);
78 fileUpload
.setText("Browse...");
79 fileUpload
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, false,
82 final String url
= startUploadReceiver();
83 pushSession
= new ServerPushSession();
85 fileUpload
.addSelectionListener(new SelectionAdapter() {
86 private static final long serialVersionUID
= 1L;
89 public void widgetSelected(SelectionEvent e
) {
90 String fileName
= fileUpload
.getFileName();
91 fileNameLabel
.setText(fileName
== null ?
"" : fileName
);
93 fileUpload
.submit(url
);
97 setControl(composite
);
100 public void performFinish() {
101 // boolean success = uploadFile.performUpload();
103 // throw new ArgeoException("Cannot upload file named "
104 // + uploadFile.getPath());
107 private String
startUploadReceiver() {
108 MyFileUploadReceiver receiver
= new MyFileUploadReceiver();
109 FileUploadHandler uploadHandler
= new FileUploadHandler(receiver
);
110 uploadHandler
.addUploadListener(new FileUploadListener() {
112 public void uploadProgress(FileUploadEvent event
) {
113 // handle upload progress
116 public void uploadFailed(FileUploadEvent event
) {
117 UploadFileWizardPage
.this.setErrorMessage("upload failed: "
118 + event
.getException());
121 public void uploadFinished(FileUploadEvent event
) {
123 fileNameLabel
.getDisplay().asyncExec(new Runnable() {
125 // UploadFileWizardPage.this.getContainer()
131 // for (FileDetails file : event.getFileDetails()) {
132 // // addToLog("received: " + file.getFileName());
136 return uploadHandler
.getUploadUrl();
139 private class MyFileUploadReceiver
extends FileUploadReceiver
{
141 private static final String TEMP_FILE_PREFIX
= "fileupload_";
144 public void receive(InputStream dataStream
, FileDetails details
)
146 File result
= File
.createTempFile(TEMP_FILE_PREFIX
, "");
147 FileOutputStream outputStream
= new FileOutputStream(result
);
149 copy(dataStream
, outputStream
);
152 outputStream
.close();
158 private static void copy(InputStream inputStream
, OutputStream outputStream
)
160 byte[] buffer
= new byte[8192];
161 boolean finished
= false;
163 int bytesRead
= inputStream
.read(buffer
);
164 if (bytesRead
!= -1) {
165 outputStream
.write(buffer
, 0, bytesRead
);
173 * The full path including the directory and file drive are only returned,
174 * if the browser supports reading this properties
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.
179 public String
getObjectPath() {
183 public String
getObjectName() {
184 return fileUpload
.getFileName();
187 public String
getObjectType() {
188 return FILE_ITEM_TYPE
;
191 // protected void handleUploadFinished(final Upload upload) {
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;
200 // fis = new FileInputStream(file);
202 // } catch (Exception e) {
203 // throw new ArgeoException("Unable to retrieve file " + file, e);
205 // IOUtils.closeQuietly(fis);
209 public boolean getNeedsProgressMonitor() {