2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
35 package gr.grnet.pithos.web.client;
37 import gr.grnet.pithos.web.client.foldertree.File;
38 import gr.grnet.pithos.web.client.foldertree.Folder;
40 import com.google.gwt.core.client.Scheduler;
41 import com.google.gwt.event.dom.client.ClickEvent;
42 import com.google.gwt.event.dom.client.ClickHandler;
43 import com.google.gwt.user.client.Command;
44 import com.google.gwt.user.client.ui.Anchor;
45 import com.google.gwt.user.client.ui.Button;
46 import com.google.gwt.user.client.ui.DialogBox;
47 import com.google.gwt.user.client.ui.FileUpload;
48 import com.google.gwt.user.client.ui.FlowPanel;
49 import com.google.gwt.user.client.ui.FormPanel;
50 import com.google.gwt.user.client.ui.Grid;
51 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
52 import com.google.gwt.user.client.ui.Hidden;
53 import com.google.gwt.user.client.ui.HorizontalPanel;
54 import com.google.gwt.user.client.ui.Label;
55 import com.google.gwt.user.client.ui.VerticalPanel;
58 * The 'File upload' dialog box implementation.
60 public class FileUploadDialog extends DialogBox {
62 public static final boolean DONE = true;
65 * The Form element that performs the file upload.
67 protected final FormPanel form = new FormPanel();
69 private final FileUpload upload = new FileUpload();
71 private final Label filenameLabel = new Label();
73 private final Label foldernameLabel = new Label();
75 private Button submit;
77 protected Folder folder;
82 * The widget's constructor.
84 public FileUploadDialog(Pithos _app, Folder _folder) {
87 foldernameLabel.setText(folder.getName());
88 Anchor close = new Anchor();
89 close.addStyleName("close");
90 close.addClickHandler(new ClickHandler() {
93 public void onClick(ClickEvent event) {
97 // Set the dialog's caption.
98 setText("File upload");
99 setAnimationEnabled(true);
100 setGlassEnabled(true);
101 setStyleName("pithos-DialogBox");
102 // Since we're going to add a FileUpload widget, we'll need to set the
103 // form to use the POST method, and multipart MIME encoding.
104 form.setEncoding(FormPanel.ENCODING_MULTIPART);
105 form.setMethod(FormPanel.METHOD_POST);
107 // Create a panel to hold all of the form widgets.
108 VerticalPanel panel = new VerticalPanel();
110 form.setWidget(panel);
112 VerticalPanel inner = new VerticalPanel();
113 inner.addStyleName("inner");
115 final Hidden auth = new Hidden("X-Auth-Token");
117 upload.setName("X-Object-Data");
118 upload.setVisible(false);
119 filenameLabel.setText("");
120 filenameLabel.setVisible(false);
121 filenameLabel.setStyleName("props-labels");
122 HorizontalPanel fileUploadPanel = new HorizontalPanel();
123 fileUploadPanel.setVisible(false);
124 fileUploadPanel.add(filenameLabel);
125 fileUploadPanel.add(upload);
126 Grid generalTable = new Grid(2, 2);
127 generalTable.setText(0, 0, "Folder");
128 generalTable.setWidget(0, 1, foldernameLabel);
129 generalTable.setWidget(1, 1, fileUploadPanel);
130 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
131 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
132 generalTable.getCellFormatter().setVisible(1, 0, false);
133 generalTable.setCellSpacing(4);
135 inner.add(generalTable);
137 FlowPanel uploader = new FlowPanel();
138 uploader.getElement().setId("uploader");
142 panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
145 Scheduler.get().scheduleDeferred(new Command() {
148 public void execute() {
149 String path = app.getApiPath() + folder.getOwner() + folder.getUri();
150 setupUpload(FileUploadDialog.this, path, app.getToken());
156 private void refreshFolder() {
157 if (app.getSelectedTree().equals(app.getFolderTreeView()))
158 app.updateFolder(folder, true, new Command() {
161 public void execute() {
162 app.updateStatistics();
166 app.updateOtherSharedFolder(folder, true);
169 native void setupUpload(FileUploadDialog dlg, String path, String token) /*-{
170 $wnd.$("#uploader").pluploadQueue({
172 runtimes : 'html5, flash, gears, silverlight, browserplus, html4',
176 flash_swf_url : 'plupload/js/plupload.flash.swf',
178 // Silverlight settings
179 silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
182 Init: function(up, info) {
183 up.settings.file_data_name = "X-Object-Data";
188 BeforeUpload: function(up, file) {
189 $wnd.console.log('About to upload ' + file.name);
190 up.settings.url = path + "/" + file.name + "?X-Auth-Token=" + token;
193 FileUploaded: function(up, file, response) {
194 $wnd.console.log('File ' + file.name + ' uploaded');
195 $wnd.console.log('Response: ' + response);
198 UploadComplete: function(up, files) {
199 $wnd.console.log('All files finished');
200 dlg.@gr.grnet.pithos.web.client.FileUploadDialog::refreshFolder()();
205 dlg.@gr.grnet.pithos.web.client.FileUploadDialog::center()();