Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / FileUploadDialog.java @ 8d5a352e

History | View | Annotate | Download (9.6 kB)

1 ab1eb3f8 Christos Stathis
/*
2 58777026 Christos Stathis
 * Copyright 2011 GRNET S.A. All rights reserved.
3 58777026 Christos Stathis
 *
4 58777026 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 58777026 Christos Stathis
 * without modification, are permitted provided that the following
6 58777026 Christos Stathis
 * conditions are met:
7 58777026 Christos Stathis
 *
8 58777026 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 58777026 Christos Stathis
 *      disclaimer.
11 58777026 Christos Stathis
 *
12 58777026 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 58777026 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 58777026 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 58777026 Christos Stathis
 *      provided with the distribution.
16 58777026 Christos Stathis
 *
17 58777026 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 58777026 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 58777026 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 58777026 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 58777026 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 58777026 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 58777026 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 58777026 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 58777026 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 58777026 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 58777026 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 58777026 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 58777026 Christos Stathis
 *
30 58777026 Christos Stathis
 * The views and conclusions contained in the software and
31 58777026 Christos Stathis
 * documentation are those of the authors and should not be
32 58777026 Christos Stathis
 * interpreted as representing official policies, either expressed
33 58777026 Christos Stathis
 * or implied, of GRNET S.A.
34 ab1eb3f8 Christos Stathis
 */
35 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client;
36 ab1eb3f8 Christos Stathis
37 26cbae7a Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
38 26cbae7a Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
39 ab1eb3f8 Christos Stathis
40 ab1eb3f8 Christos Stathis
import com.google.gwt.core.client.GWT;
41 ab1eb3f8 Christos Stathis
import com.google.gwt.dom.client.NativeEvent;
42 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
43 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
44 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.KeyCodes;
45 28265607 Christos Stathis
import com.google.gwt.user.client.Command;
46 90f0142f Christos Stathis
import com.google.gwt.user.client.Cookies;
47 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
48 90f0142f Christos Stathis
import com.google.gwt.user.client.Window;
49 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Button;
50 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.DialogBox;
51 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FileUpload;
52 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FormPanel;
53 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
54 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
55 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
56 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
57 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Grid;
58 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
59 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Hidden;
60 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
61 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Label;
62 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
63 ab1eb3f8 Christos Stathis
64 ab1eb3f8 Christos Stathis
/**
65 ab1eb3f8 Christos Stathis
 * The 'File upload' dialog box implementation.
66 ab1eb3f8 Christos Stathis
 */
67 26cbae7a Christos Stathis
public class FileUploadDialog extends DialogBox {
68 ab1eb3f8 Christos Stathis
69 26cbae7a Christos Stathis
    public static final boolean DONE = true;
70 ab1eb3f8 Christos Stathis
71 ab1eb3f8 Christos Stathis
        /**
72 ab1eb3f8 Christos Stathis
         * The Form element that performs the file upload.
73 ab1eb3f8 Christos Stathis
         */
74 10791b56 Christos Stathis
    protected final FormPanel form = new FormPanel();
75 ab1eb3f8 Christos Stathis
76 ab1eb3f8 Christos Stathis
        private final FileUpload upload = new FileUpload();
77 ab1eb3f8 Christos Stathis
78 26cbae7a Christos Stathis
        private final Label filenameLabel = new Label();
79 ab1eb3f8 Christos Stathis
80 26cbae7a Christos Stathis
    private final Label foldernameLabel = new Label();
81 ab1eb3f8 Christos Stathis
82 26cbae7a Christos Stathis
    private Button submit;
83 ab1eb3f8 Christos Stathis
84 26cbae7a Christos Stathis
        protected Folder folder;
85 ab1eb3f8 Christos Stathis
86 f55cf326 Christos Stathis
    protected Pithos app;
87 ab1eb3f8 Christos Stathis
88 ab1eb3f8 Christos Stathis
        /**
89 ab1eb3f8 Christos Stathis
         * The widget's constructor.
90 ab1eb3f8 Christos Stathis
         */
91 ab1eb3f8 Christos Stathis
        public FileUploadDialog() {
92 ab1eb3f8 Christos Stathis
                // Set the dialog's caption.
93 ab1eb3f8 Christos Stathis
                setText("File upload");
94 ab1eb3f8 Christos Stathis
                setAnimationEnabled(true);
95 ab1eb3f8 Christos Stathis
                // Since we're going to add a FileUpload widget, we'll need to set the
96 ab1eb3f8 Christos Stathis
                // form to use the POST method, and multipart MIME encoding.
97 ab1eb3f8 Christos Stathis
                form.setEncoding(FormPanel.ENCODING_MULTIPART);
98 ab1eb3f8 Christos Stathis
                form.setMethod(FormPanel.METHOD_POST);
99 ab1eb3f8 Christos Stathis
100 ab1eb3f8 Christos Stathis
                // Create a panel to hold all of the form widgets.
101 ab1eb3f8 Christos Stathis
                VerticalPanel panel = new VerticalPanel();
102 ab1eb3f8 Christos Stathis
                form.setWidget(panel);
103 26cbae7a Christos Stathis
104 26cbae7a Christos Stathis
        final Hidden auth = new Hidden("X-Auth-Token", "");
105 26cbae7a Christos Stathis
        panel.add(auth);
106 0737a2a4 Christos Stathis
                upload.setName("X-Object-Data");
107 ab1eb3f8 Christos Stathis
                filenameLabel.setText("");
108 ab1eb3f8 Christos Stathis
                filenameLabel.setVisible(false);
109 ab1eb3f8 Christos Stathis
                filenameLabel.setStyleName("props-labels");
110 26cbae7a Christos Stathis
                HorizontalPanel fileUploadPanel = new HorizontalPanel();
111 26cbae7a Christos Stathis
                fileUploadPanel.add(filenameLabel);
112 26cbae7a Christos Stathis
                fileUploadPanel.add(upload);
113 ab1eb3f8 Christos Stathis
                Grid generalTable = new Grid(2, 2);
114 ab1eb3f8 Christos Stathis
                generalTable.setText(0, 0, "Folder");
115 26cbae7a Christos Stathis
        generalTable.setWidget(0, 1, foldernameLabel);
116 ab1eb3f8 Christos Stathis
                generalTable.setText(1, 0, "File");
117 26cbae7a Christos Stathis
                generalTable.setWidget(1, 1, fileUploadPanel);
118 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
119 26cbae7a Christos Stathis
        generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
120 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
121 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
122 ab1eb3f8 Christos Stathis
                generalTable.setCellSpacing(4);
123 ab1eb3f8 Christos Stathis
124 ab1eb3f8 Christos Stathis
                panel.add(generalTable);
125 ab1eb3f8 Christos Stathis
126 ab1eb3f8 Christos Stathis
                // Create a panel to hold the buttons.
127 ab1eb3f8 Christos Stathis
                HorizontalPanel buttons = new HorizontalPanel();
128 ab1eb3f8 Christos Stathis
129 ab1eb3f8 Christos Stathis
                // Create the 'upload' button, along with a listener that submits the
130 ab1eb3f8 Christos Stathis
                // form.
131 26cbae7a Christos Stathis
                submit = new Button("Upload", new ClickHandler() {
132 ab1eb3f8 Christos Stathis
                        @Override
133 10791b56 Christos Stathis
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
134 ab1eb3f8 Christos Stathis
                                prepareAndSubmit();
135 ab1eb3f8 Christos Stathis
                        }
136 ab1eb3f8 Christos Stathis
                });
137 ab1eb3f8 Christos Stathis
                buttons.add(submit);
138 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
139 ab1eb3f8 Christos Stathis
                // Create the 'Cancel' button, along with a listener that hides the
140 ab1eb3f8 Christos Stathis
                // dialog when the button is clicked.
141 ab1eb3f8 Christos Stathis
                final Button cancel = new Button("Cancel", new ClickHandler() {
142 ab1eb3f8 Christos Stathis
                        @Override
143 10791b56 Christos Stathis
                        public void onClick(@SuppressWarnings("unused") ClickEvent event) {
144 ab1eb3f8 Christos Stathis
                                hide();
145 ab1eb3f8 Christos Stathis
                        }
146 ab1eb3f8 Christos Stathis
                });
147 ab1eb3f8 Christos Stathis
                buttons.add(cancel);
148 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
149 ab1eb3f8 Christos Stathis
                buttons.setSpacing(8);
150 26cbae7a Christos Stathis
        panel.add(buttons);
151 26cbae7a Christos Stathis
        panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
152 ab1eb3f8 Christos Stathis
153 ab1eb3f8 Christos Stathis
                // Add an event handler to the form.
154 ab1eb3f8 Christos Stathis
                form.addSubmitHandler(new SubmitHandler() {
155 ab1eb3f8 Christos Stathis
156 ab1eb3f8 Christos Stathis
                        @Override
157 10791b56 Christos Stathis
                        public void onSubmit(@SuppressWarnings("unused") SubmitEvent event) {
158 26cbae7a Christos Stathis
                auth.setValue(app.getToken()); //This is done here because the app object is not available in the constructor
159 90f0142f Christos Stathis
                Cookies.setCookie("X-Auth-Token", app.getToken(), null, Window.Location.getHostName(), app.getApiPath(), false);
160 ab1eb3f8 Christos Stathis
                        }
161 ab1eb3f8 Christos Stathis
                });
162 ab1eb3f8 Christos Stathis
                form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
163 ab1eb3f8 Christos Stathis
164 ab1eb3f8 Christos Stathis
                        @Override
165 ab1eb3f8 Christos Stathis
                        public void onSubmitComplete(SubmitCompleteEvent event) {
166 ab1eb3f8 Christos Stathis
                                // When the form submission is successfully completed, this
167 ab1eb3f8 Christos Stathis
                                // event is fired. Assuming the service returned a response
168 ab1eb3f8 Christos Stathis
                                // of type text/html, we can get the result text here (see
169 ab1eb3f8 Christos Stathis
                                // the FormPanel documentation for further explanation).
170 ab1eb3f8 Christos Stathis
                                String results = event.getResults();
171 ab1eb3f8 Christos Stathis
172 ab1eb3f8 Christos Stathis
                                // Unfortunately the results are never empty, even in
173 ab1eb3f8 Christos Stathis
                                // the absense of errors, so we have to check for '<pre></pre>'.
174 0737a2a4 Christos Stathis
                                if (results != null && results.length() > 0 && !results.equalsIgnoreCase("<pre></pre>")) {
175 ab1eb3f8 Christos Stathis
                                        GWT.log(results, null);
176 26cbae7a Christos Stathis
                                        app.displayError(results);
177 ab1eb3f8 Christos Stathis
                                }
178 5fb15774 Christos Stathis
                                if (app.getSelectedTree().equals(app.getFolderTreeView()))
179 28265607 Christos Stathis
                                        app.updateFolder(folder, true, new Command() {
180 28265607 Christos Stathis
                                                
181 28265607 Christos Stathis
                                                @Override
182 28265607 Christos Stathis
                                                public void execute() {
183 28265607 Christos Stathis
                                                        app.updateStatistics();
184 28265607 Christos Stathis
                                                }
185 28265607 Christos Stathis
                                        });
186 5fb15774 Christos Stathis
                                else
187 5fb15774 Christos Stathis
                                        app.updateOtherSharedFolder(folder, true);
188 26cbae7a Christos Stathis
                                hide();
189 ab1eb3f8 Christos Stathis
                        }
190 ab1eb3f8 Christos Stathis
                });
191 ab1eb3f8 Christos Stathis
192 ab1eb3f8 Christos Stathis
                setWidget(form);
193 ab1eb3f8 Christos Stathis
        }
194 ab1eb3f8 Christos Stathis
195 ab1eb3f8 Christos Stathis
        @Override
196 ab1eb3f8 Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
197 ab1eb3f8 Christos Stathis
                super.onPreviewNativeEvent(preview);
198 ab1eb3f8 Christos Stathis
199 ab1eb3f8 Christos Stathis
                NativeEvent evt = preview.getNativeEvent();
200 ab1eb3f8 Christos Stathis
                if (evt.getType().equals("keydown"))
201 ab1eb3f8 Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
202 ab1eb3f8 Christos Stathis
                        // enter or escape is pressed.
203 ab1eb3f8 Christos Stathis
                        switch (evt.getKeyCode()) {
204 ab1eb3f8 Christos Stathis
                                case KeyCodes.KEY_ENTER:
205 ab1eb3f8 Christos Stathis
                                        prepareAndSubmit();
206 ab1eb3f8 Christos Stathis
                                        break;
207 ab1eb3f8 Christos Stathis
                                case KeyCodes.KEY_ESCAPE:
208 26cbae7a Christos Stathis
                                        hide();
209 ab1eb3f8 Christos Stathis
                                        break;
210 ab1eb3f8 Christos Stathis
                        }
211 ab1eb3f8 Christos Stathis
        }
212 ab1eb3f8 Christos Stathis
213 ab1eb3f8 Christos Stathis
        /**
214 ab1eb3f8 Christos Stathis
         * Make any last minute checks and start the upload.
215 ab1eb3f8 Christos Stathis
         */
216 26cbae7a Christos Stathis
        protected void prepareAndSubmit() {
217 26cbae7a Christos Stathis
        if (upload.getFilename().length() == 0) {
218 26cbae7a Christos Stathis
            app.displayError("You must select a file!");
219 26cbae7a Christos Stathis
            return;
220 26cbae7a Christos Stathis
        }
221 26cbae7a Christos Stathis
        final String fname = getFilename(upload.getFilename());
222 5fb15774 Christos Stathis
        String apath = app.getApiPath() + folder.getOwner() + folder.getUri() + "/" + fname;
223 26cbae7a Christos Stathis
        form.setAction(apath);
224 26cbae7a Christos Stathis
        submit.setEnabled(false);
225 26cbae7a Christos Stathis
        upload.setVisible(false);
226 26cbae7a Christos Stathis
        filenameLabel.setText(fname);
227 26cbae7a Christos Stathis
        filenameLabel.setVisible(true);
228 26cbae7a Christos Stathis
229 ab1eb3f8 Christos Stathis
                if (getFileForName(fname) == null) {
230 0737a2a4 Christos Stathis
            form.submit();
231 ab1eb3f8 Christos Stathis
                }
232 ab1eb3f8 Christos Stathis
                else {
233 ab1eb3f8 Christos Stathis
                        // We are going to update an existing file, so show a confirmation dialog.
234 ab1eb3f8 Christos Stathis
                        ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
235 ab1eb3f8 Christos Stathis
                                        "you want to update " + fname + "?", "Update") {
236 ab1eb3f8 Christos Stathis
237 ab1eb3f8 Christos Stathis
                                @Override
238 ab1eb3f8 Christos Stathis
                                public void cancel() {
239 ab1eb3f8 Christos Stathis
                                        FileUploadDialog.this.hide();
240 ab1eb3f8 Christos Stathis
                                }
241 ab1eb3f8 Christos Stathis
242 ab1eb3f8 Christos Stathis
                                @Override
243 ab1eb3f8 Christos Stathis
                                public void confirm() {
244 ab1eb3f8 Christos Stathis
                                        form.submit();
245 ab1eb3f8 Christos Stathis
                                }
246 ab1eb3f8 Christos Stathis
247 ab1eb3f8 Christos Stathis
                        };
248 ab1eb3f8 Christos Stathis
                        confirm.center();
249 ab1eb3f8 Christos Stathis
                }
250 ab1eb3f8 Christos Stathis
        }
251 ab1eb3f8 Christos Stathis
252 26cbae7a Christos Stathis
    /**
253 ab1eb3f8 Christos Stathis
         * Returns the file name from a potential full path argument. Apparently IE
254 ab1eb3f8 Christos Stathis
         * insists on sending the full path name of a file when uploading, forcing
255 ab1eb3f8 Christos Stathis
         * us to trim the extra path info. Since this is only observed on Windows we
256 ab1eb3f8 Christos Stathis
         * get to check for a single path separator value.
257 ab1eb3f8 Christos Stathis
         *
258 ab1eb3f8 Christos Stathis
         * @param name the potentially full path name of a file
259 ab1eb3f8 Christos Stathis
         * @return the file name without extra path information
260 ab1eb3f8 Christos Stathis
         */
261 ab1eb3f8 Christos Stathis
        protected String getFilename(String name) {
262 ab1eb3f8 Christos Stathis
                int pathSepIndex = name.lastIndexOf("\\");
263 ab1eb3f8 Christos Stathis
                if (pathSepIndex == -1) {
264 ab1eb3f8 Christos Stathis
                        pathSepIndex = name.lastIndexOf("/");
265 ab1eb3f8 Christos Stathis
                        if (pathSepIndex == -1)
266 ab1eb3f8 Christos Stathis
                                return name;
267 ab1eb3f8 Christos Stathis
                }
268 ab1eb3f8 Christos Stathis
                return name.substring(pathSepIndex + 1);
269 ab1eb3f8 Christos Stathis
        }
270 ab1eb3f8 Christos Stathis
271 26cbae7a Christos Stathis
        protected File getFileForName(String name){
272 26cbae7a Christos Stathis
                for (File f : folder.getFiles())
273 0393c0ed Christos Stathis
                        if (f.getName().equals(name))
274 ab1eb3f8 Christos Stathis
                                return f;
275 ab1eb3f8 Christos Stathis
                return null;
276 ab1eb3f8 Christos Stathis
        }
277 ab1eb3f8 Christos Stathis
278 f55cf326 Christos Stathis
    public void setApp(Pithos app) {
279 26cbae7a Christos Stathis
        this.app = app;
280 26cbae7a Christos Stathis
    }
281 ab1eb3f8 Christos Stathis
282 26cbae7a Christos Stathis
    public void setFolder(Folder folder) {
283 26cbae7a Christos Stathis
        this.folder = folder;
284 26cbae7a Christos Stathis
        foldernameLabel.setText(folder.getName());
285 26cbae7a Christos Stathis
    }
286 ab1eb3f8 Christos Stathis
}