Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / FileUploadDialog.java @ 749068ba

History | View | Annotate | Download (9.5 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
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.
16
 *
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.
29
 *
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.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.foldertree.File;
38
import gr.grnet.pithos.web.client.foldertree.Folder;
39

    
40
import com.google.gwt.core.client.GWT;
41
import com.google.gwt.dom.client.NativeEvent;
42
import com.google.gwt.event.dom.client.ClickEvent;
43
import com.google.gwt.event.dom.client.ClickHandler;
44
import com.google.gwt.event.dom.client.KeyCodes;
45
import com.google.gwt.user.client.Event.NativePreviewEvent;
46
import com.google.gwt.user.client.ui.Button;
47
import com.google.gwt.user.client.ui.DialogBox;
48
import com.google.gwt.user.client.ui.FileUpload;
49
import com.google.gwt.user.client.ui.FormPanel;
50
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
51
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
52
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
53
import com.google.gwt.user.client.ui.FormPanel.SubmitHandler;
54
import com.google.gwt.user.client.ui.Grid;
55
import com.google.gwt.user.client.ui.HTML;
56
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
57
import com.google.gwt.user.client.ui.Hidden;
58
import com.google.gwt.user.client.ui.HorizontalPanel;
59
import com.google.gwt.user.client.ui.Label;
60
import com.google.gwt.user.client.ui.VerticalPanel;
61

    
62
/**
63
 * The 'File upload' dialog box implementation.
64
 */
65
public class FileUploadDialog extends DialogBox {
66

    
67
    public static final boolean DONE = true;
68

    
69
        /**
70
         * The Form element that performs the file upload.
71
         */
72
        private final FormPanel form = new FormPanel();
73

    
74
        private final FileUpload upload = new FileUpload();
75

    
76
        private final Label filenameLabel = new Label();
77

    
78
    private final Label foldernameLabel = new Label();
79

    
80
    private Button submit;
81

    
82
        protected Folder folder;
83

    
84
    protected Pithos app;
85

    
86
        /**
87
         * The widget's constructor.
88
         */
89
        public FileUploadDialog() {
90
                // Set the dialog's caption.
91
                setText("File upload");
92
                setAnimationEnabled(true);
93
                // Since we're going to add a FileUpload widget, we'll need to set the
94
                // form to use the POST method, and multipart MIME encoding.
95
                form.setEncoding(FormPanel.ENCODING_MULTIPART);
96
                form.setMethod(FormPanel.METHOD_POST);
97

    
98
                // Create a panel to hold all of the form widgets.
99
                VerticalPanel panel = new VerticalPanel();
100
                form.setWidget(panel);
101
                final HTML info = new HTML("You may select a file to upload. Install" +
102
                                " <a href='http://gears.google.com/' target='_blank'>Google " +
103
                                "Gears</a><br> for uploading multiple files simultaneously.");
104
                info.addStyleName("pithos-uploadNote");
105
                panel.add(info);
106

    
107
        final Hidden auth = new Hidden("X-Auth-Token", "");
108
        panel.add(auth);
109
                upload.setName("X-Object-Data");
110
                filenameLabel.setText("");
111
                filenameLabel.setVisible(false);
112
                filenameLabel.setStyleName("props-labels");
113
                HorizontalPanel fileUploadPanel = new HorizontalPanel();
114
                fileUploadPanel.add(filenameLabel);
115
                fileUploadPanel.add(upload);
116
                Grid generalTable = new Grid(2, 2);
117
                generalTable.setText(0, 0, "Folder");
118
        generalTable.setWidget(0, 1, foldernameLabel);
119
                generalTable.setText(1, 0, "File");
120
                generalTable.setWidget(1, 1, fileUploadPanel);
121
                generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
122
        generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
123
                generalTable.getCellFormatter().setStyleName(1, 0, "props-labels");
124
                generalTable.getCellFormatter().setStyleName(1, 1, "props-values");
125
                generalTable.setCellSpacing(4);
126

    
127
                panel.add(generalTable);
128

    
129
                // Create a panel to hold the buttons.
130
                HorizontalPanel buttons = new HorizontalPanel();
131

    
132
                // Create the 'upload' button, along with a listener that submits the
133
                // form.
134
                submit = new Button("Upload", new ClickHandler() {
135
                        @Override
136
                        public void onClick(ClickEvent event) {
137
                                prepareAndSubmit();
138
                        }
139
                });
140
                buttons.add(submit);
141
                buttons.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_CENTER);
142
                // Create the 'Cancel' button, along with a listener that hides the
143
                // dialog when the button is clicked.
144
                final Button cancel = new Button("Cancel", new ClickHandler() {
145
                        @Override
146
                        public void onClick(ClickEvent event) {
147
                                hide();
148
                        }
149
                });
150
                buttons.add(cancel);
151
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
152
                buttons.setSpacing(8);
153
                buttons.addStyleName("pithos-DialogBox");
154
        panel.add(buttons);
155
        panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
156

    
157
                // Add an event handler to the form.
158
                form.addSubmitHandler(new SubmitHandler() {
159

    
160
                        @Override
161
                        public void onSubmit(SubmitEvent event) {
162
                auth.setValue(app.getToken()); //This is done here because the app object is not available in the constructor
163
                        }
164
                });
165
                form.addSubmitCompleteHandler(new SubmitCompleteHandler() {
166

    
167
                        @Override
168
                        public void onSubmitComplete(SubmitCompleteEvent event) {
169
                                // When the form submission is successfully completed, this
170
                                // event is fired. Assuming the service returned a response
171
                                // of type text/html, we can get the result text here (see
172
                                // the FormPanel documentation for further explanation).
173
                                String results = event.getResults();
174

    
175
                                // Unfortunately the results are never empty, even in
176
                                // the absense of errors, so we have to check for '<pre></pre>'.
177
                                if (results != null && results.length() > 0 && !results.equalsIgnoreCase("<pre></pre>")) {
178
                                        GWT.log(results, null);
179
                                        app.displayError(results);
180
                                }
181
                app.updateFolder(folder);
182
                                hide();
183
                        }
184
                });
185

    
186

    
187
                panel.addStyleName("pithos-DialogBox");
188
                addStyleName("pithos-DialogBox");
189
                setWidget(form);
190
        }
191

    
192
        @Override
193
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
194
                super.onPreviewNativeEvent(preview);
195

    
196
                NativeEvent evt = preview.getNativeEvent();
197
                if (evt.getType().equals("keydown"))
198
                        // Use the popup's key preview hooks to close the dialog when either
199
                        // enter or escape is pressed.
200
                        switch (evt.getKeyCode()) {
201
                                case KeyCodes.KEY_ENTER:
202
                                        prepareAndSubmit();
203
                                        break;
204
                                case KeyCodes.KEY_ESCAPE:
205
                                        hide();
206
                                        break;
207
                        }
208
        }
209

    
210
        /**
211
         * Make any last minute checks and start the upload.
212
         */
213
        protected void prepareAndSubmit() {
214
        if (upload.getFilename().length() == 0) {
215
            app.displayError("You must select a file!");
216
            return;
217
        }
218
        final String fname = getFilename(upload.getFilename());
219
        String apath = app.getApiPath() + app.getUsername() + folder.getUri() + "/" + fname;
220
        form.setAction(apath);
221
        submit.setEnabled(false);
222
        upload.setVisible(false);
223
        filenameLabel.setText(fname);
224
        filenameLabel.setVisible(true);
225

    
226
                if (getFileForName(fname) == null) {
227
            form.submit();
228
                }
229
                else {
230
                        // We are going to update an existing file, so show a confirmation dialog.
231
                        ConfirmationDialog confirm = new ConfirmationDialog("Are you sure " +
232
                                        "you want to update " + fname + "?", "Update") {
233

    
234
                                @Override
235
                                public void cancel() {
236
                                        FileUploadDialog.this.hide();
237
                                }
238

    
239
                                @Override
240
                                public void confirm() {
241
                                        form.submit();
242
                                }
243

    
244
                        };
245
                        confirm.center();
246
                }
247
        }
248

    
249
    /**
250
         * Returns the file name from a potential full path argument. Apparently IE
251
         * insists on sending the full path name of a file when uploading, forcing
252
         * us to trim the extra path info. Since this is only observed on Windows we
253
         * get to check for a single path separator value.
254
         *
255
         * @param name the potentially full path name of a file
256
         * @return the file name without extra path information
257
         */
258
        protected String getFilename(String name) {
259
                int pathSepIndex = name.lastIndexOf("\\");
260
                if (pathSepIndex == -1) {
261
                        pathSepIndex = name.lastIndexOf("/");
262
                        if (pathSepIndex == -1)
263
                                return name;
264
                }
265
                return name.substring(pathSepIndex + 1);
266
        }
267

    
268
        protected File getFileForName(String name){
269
                for (File f : folder.getFiles())
270
                        if (!f.isInTrash() && f.getName().equals(name))
271
                                return f;
272
                return null;
273
        }
274

    
275
    public void setApp(Pithos app) {
276
        this.app = app;
277
    }
278

    
279
    public void setFolder(Folder folder) {
280
        this.folder = folder;
281
        foldernameLabel.setText(folder.getName());
282
    }
283
}