]> git.argeo.org Git - lgpl/argeo-commons.git/blob - eclipse/runtime/org.argeo.eclipse.ui.rap/src/main/java/org/argeo/eclipse/ui/specific/ImportFileSystemWizardPage.java
Implementation of file handlers for both RCP & RAP
[lgpl/argeo-commons.git] / eclipse / runtime / org.argeo.eclipse.ui.rap / src / main / java / org / argeo / eclipse / ui / specific / ImportFileSystemWizardPage.java
1 package org.argeo.eclipse.ui.specific;
2
3 import java.io.InputStream;
4
5 import org.apache.commons.logging.Log;
6 import org.apache.commons.logging.LogFactory;
7 import org.argeo.ArgeoException;
8 import org.eclipse.jface.dialogs.MessageDialog;
9 import org.eclipse.jface.wizard.WizardPage;
10 import org.eclipse.rwt.widgets.Upload;
11 import org.eclipse.rwt.widgets.UploadAdapter;
12 import org.eclipse.rwt.widgets.UploadEvent;
13 import org.eclipse.rwt.widgets.UploadItem;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.layout.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.swt.widgets.Label;
20
21 public class ImportFileSystemWizardPage extends WizardPage {
22 private Log log = LogFactory.getLog(ImportFileSystemWizardPage.class);
23
24 private Upload uploadFile;
25
26 public ImportFileSystemWizardPage() {
27 super("Import from file system");
28 setDescription("Import files from the local file system into the JCR repository");
29 }
30
31 public void createControl(Composite parent) {
32
33 Composite composite = new Composite(parent, SWT.NONE);
34 composite.setLayout(new GridLayout(2, false));
35 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
36
37 new Label(composite, SWT.NONE).setText("Pick up a file");
38
39 uploadFile = new Upload(composite, SWT.BORDER);
40 uploadFile.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
41 uploadFile.setBrowseButtonText("Open...");
42 uploadFile.addUploadListener(new UploadManager(uploadFile));
43
44 setControl(composite);
45 }
46
47 public String getObjectPath() {
48 // NOTE Returns the full file name of the last uploaded file including
49 // the file path as selected by the user on his local machine.
50 // The full path including the directory and file drive are only
51 // returned, if the browser supports reading this properties. In Firefox
52 // 3, only the filename is returned.
53 return uploadFile.getPath();
54 }
55
56 public String getObjectName() {
57 return uploadFile.getUploadItem().getFileName();
58 }
59
60 public String getObjectType() {
61 return "nt:file";
62 }
63
64 public void performFinish() {
65 boolean success = uploadFile.performUpload();
66 if (!success)
67 throw new ArgeoException("Cannot upload file named "
68 + uploadFile.getPath());
69 }
70
71 protected void handleUploadFinished(final Upload upload) {
72 if (log.isTraceEnabled()) {
73 UploadItem uploadItem = upload.getUploadItem();
74 log.trace("filename : " + uploadItem.getFileName());
75 log.trace("content-type : " + uploadItem.getContentType());
76 }
77 }
78
79 public InputStream getFileInputStream() {
80 return uploadFile.getUploadItem().getFileInputStream();
81 }
82
83 private class UploadManager extends UploadAdapter {
84 private Upload upload;
85
86 public UploadManager(Upload upload) {
87 super();
88 this.upload = upload;
89 }
90
91 public void uploadFinished(UploadEvent uploadEvent) {
92 handleUploadFinished(upload);
93 }
94
95 public void uploadInProgress(UploadEvent uploadEvent) {
96 }
97
98 public void uploadException(UploadEvent uploadEvent) {
99 Exception exc = uploadEvent.getUploadException();
100 if (exc != null) {
101 MessageDialog.openError(Display.getCurrent().getActiveShell(),
102 "Error", exc.getMessage());
103 }
104 }
105 }
106 }