Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / FolderPropertiesDialog.java @ fa5600f7

History | View | Annotate | Download (12.9 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 com.google.gwt.core.client.Scheduler;
38
import com.google.gwt.event.dom.client.KeyDownEvent;
39
import com.google.gwt.user.client.Event;
40
import gr.grnet.pithos.web.client.foldertree.Folder;
41
import gr.grnet.pithos.web.client.foldertree.Resource;
42
import gr.grnet.pithos.web.client.rest.PostCommand;
43
import gr.grnet.pithos.web.client.rest.PutRequest;
44
import gr.grnet.pithos.web.client.rest.RestException;
45
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
46

    
47
import com.google.gwt.core.client.GWT;
48
import com.google.gwt.dom.client.NativeEvent;
49
import com.google.gwt.event.dom.client.ClickEvent;
50
import com.google.gwt.event.dom.client.ClickHandler;
51
import com.google.gwt.event.dom.client.KeyCodes;
52
import com.google.gwt.http.client.URL;
53
import com.google.gwt.i18n.client.DateTimeFormat;
54
import com.google.gwt.user.client.DeferredCommand;
55
import com.google.gwt.user.client.Event.NativePreviewEvent;
56
import com.google.gwt.user.client.ui.Button;
57
import com.google.gwt.user.client.ui.CheckBox;
58
import com.google.gwt.user.client.ui.DecoratedTabPanel;
59
import com.google.gwt.user.client.ui.DialogBox;
60
import com.google.gwt.user.client.ui.FlexTable;
61
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
62
import com.google.gwt.user.client.ui.HorizontalPanel;
63
import com.google.gwt.user.client.ui.Label;
64
import com.google.gwt.user.client.ui.TabPanel;
65
import com.google.gwt.user.client.ui.TextBox;
66
import com.google.gwt.user.client.ui.VerticalPanel;
67

    
68
/**
69
 * The 'Folder properties' dialog box implementation.
70
 */
71
public class FolderPropertiesDialog extends DialogBox {
72

    
73
    private GSS app;
74

    
75
        private CheckBox readForAll;
76

    
77
        /**
78
         * The widget that holds the folderName of the folder.
79
         */
80
        private TextBox folderName = new TextBox();
81

    
82
        /**
83
         * A flag that denotes whether the dialog will be used to create or modify a
84
         * folder.
85
         */
86
        private final boolean create;
87

    
88
        final Folder folder;
89

    
90
        final TabPanel inner;
91

    
92
        /**
93
         * The widget's constructor.
94
         */
95
        public FolderPropertiesDialog(GSS app, boolean _create,  Folder selected) {
96
        this.app = app;
97
                setAnimationEnabled(true);
98

    
99
                // Enable IE selection for the dialog (must disable it upon closing it)
100
                app.enableIESelection();
101

    
102
                create = _create;
103
                
104
                folder = selected;
105

    
106
                // Use this opportunity to set the dialog's caption.
107
                if (create)
108
                        setText("Create folder");
109
                else
110
                        setText("Folder properties");
111

    
112
                // Outer contains inner and buttons
113
                VerticalPanel outer = new VerticalPanel();
114
                // Inner contains generalPanel and permPanel
115
                inner = new DecoratedTabPanel();
116
                inner.setAnimationEnabled(true);
117
                VerticalPanel generalPanel = new VerticalPanel();
118
                VerticalPanel permPanel = new VerticalPanel();
119
                final HorizontalPanel permForAll = new HorizontalPanel();
120
                final HorizontalPanel pathPanel = new HorizontalPanel();
121
                HorizontalPanel buttons = new HorizontalPanel();
122
                HorizontalPanel permButtons = new HorizontalPanel();
123

    
124
                inner.add(generalPanel, "General");
125
                if (!create)
126
                        inner.add(permPanel, "Sharing");
127
                inner.selectTab(0);
128

    
129
                FlexTable generalTable = new FlexTable();
130
                generalTable.setText(0, 0, "Name");
131
                generalTable.setText(1, 0, "Parent");
132
                generalTable.setText(2, 0, "Creator");
133
                generalTable.setText(3, 0, "Last modified");
134
                folderName.setText(create ? "" : folder.getName());
135
                generalTable.setWidget(0, 1, folderName);
136

    
137
                if (create)
138
                        generalTable.setText(1, 1, folder.getName());
139
                else
140
                        generalTable.setText(1, 1, folder.getPrefix());
141
                generalTable.setText(2, 1, "");
142
                DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
143
                if(folder.getLastModified() != null)
144
                        generalTable.setText(3, 1, formatter.format(folder.getLastModified()));
145
                generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
146
                generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
147
                generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
148
                generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
149
                generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
150
                generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
151
                generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
152
                generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
153
                generalTable.setCellSpacing(4);
154

    
155
                // Create the 'Create/Update' button, along with a listener that hides the dialog
156
                // when the button is clicked and quits the application.
157
                String okLabel;
158
                if (create)
159
                        okLabel = "Create";
160
                else
161
                        okLabel = "Update";
162
                Button ok = new Button(okLabel, new ClickHandler() {
163
                        @Override
164
                        public void onClick(ClickEvent event) {
165
                                createOrUpdateFolder();
166
                                closeDialog();
167
                        }
168
                });
169
                buttons.add(ok);
170
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
171
                // Create the 'Cancel' button, along with a listener that hides the
172
                // dialog
173
                // when the button is clicked.
174
                Button cancel = new Button("Cancel", new ClickHandler() {
175
                        @Override
176
                        public void onClick(ClickEvent event) {
177
                                closeDialog();
178
                        }
179
                });
180
                buttons.add(cancel);
181
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
182
                buttons.setSpacing(8);
183
                buttons.addStyleName("pithos-TabPanelBottom");
184

    
185
                generalPanel.add(generalTable);
186

    
187

    
188
                outer.add(inner);
189
                outer.add(buttons);
190
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
191
                outer.addStyleName("pithos-TabPanelBottom");
192

    
193
                setWidget(outer);
194

    
195
                /*if (create)
196
                        folderName.setFocus(true);
197
                else
198
                        ok.setFocus(true);*/
199
        }
200

    
201
        @Override
202
        public void center() {
203
                super.center();
204
                folderName.setFocus(true);
205
        }
206

    
207
        @Override
208
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
209
                super.onPreviewNativeEvent(preview);
210

    
211
                NativeEvent evt = preview.getNativeEvent();
212
                if (evt.getType().equals(KeyDownEvent.getType().getName()))
213
                        // Use the popup's key preview hooks to close the dialog when either
214
                        // enter or escape is pressed.
215
                        switch (evt.getKeyCode()) {
216
                                case KeyCodes.KEY_ENTER:
217
                                        closeDialog();
218
                                        createOrUpdateFolder();
219
                                        break;
220
                                case KeyCodes.KEY_ESCAPE:
221
                                        closeDialog();
222
                                        break;
223
                        }
224
        }
225

    
226

    
227
        /**
228
         * Enables IE selection prevention and hides the dialog
229
         * (we disable the prevention on creation of the dialog)
230
         */
231
        public void closeDialog() {
232
                app.preventIESelection();
233
                hide();
234
        }
235

    
236
        /**
237
         * Generate an RPC request to create a new folder.
238
         */
239
        private void createFolder() {
240
                String name = folderName.getText();
241
        String prefix = folder.getPrefix();
242
        String path = app.getApiPath() + app.getUsername() + "/" + folder.getContainer() + "/" + (prefix.length() == 0 ? "" : prefix +  "/") + name;
243
        PutRequest createFolder = new PutRequest(path) {
244
            @Override
245
            public void onSuccess(Resource result) {
246
                app.updateFolder(folder);
247
            }
248

    
249
            @Override
250
            public void onError(Throwable t) {
251
                                GWT.log("", t);
252
                                if (t instanceof RestException) {
253
                                        app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
254
                                }
255
                                else
256
                                        app.displayError("System error creating folder:" + t.getMessage());
257
            }
258
        };
259
        createFolder.setHeader("X-Auth-Token", "0000");
260
        createFolder.setHeader("Accept", "*/*");
261
        createFolder.setHeader("Content-Length", "0");
262
        createFolder.setHeader("Content-Type", "application/folder");
263
        Scheduler.get().scheduleDeferred(createFolder);
264
        }
265

    
266
        /**
267
         * Upon closing the dialog by clicking OK or pressing ENTER this method does
268
         * the actual work of modifying folder properties or creating a new Folder
269
         * depending on the value of the create field
270
         */
271
        private void createOrUpdateFolder() {
272
                if (create)
273
                        createFolder();
274
                else
275
                        updateFolder();
276

    
277
        }
278

    
279
        private void updateFolder() {
280
//                permList.updatePermissionsAccordingToInput();
281
//                Set<PermissionHolder> perms = permList.getPermissions();
282
//                JSONObject json = new JSONObject();
283
//                if(!folder.getName().equals(folderName.getText()))
284
//                        json.put("name", new JSONString(folderName.getText()));
285
//                //only update the read for all perm if the user is the owner
286
//                if (readForAll.getValue() != folder.isReadForAll())
287
//                        if (folder.getOwner().equals(app.getCurrentUserResource().getUsername()))
288
//                                json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
289
//                if (permList.hasChanges()) {
290
//                        JSONArray perma = new JSONArray();
291
//                        int i=0;
292
//                        for(PermissionHolder p : perms){
293
//                                JSONObject po = new JSONObject();
294
//                                if(p.getUser() != null)
295
//                                        po.put("user", new JSONString(p.getUser()));
296
//                                if(p.getGroup() != null)
297
//                                        po.put("group", new JSONString(p.getGroup()));
298
//                                po.put("read", JSONBoolean.getInstance(p.isRead()));
299
//                                po.put("write", JSONBoolean.getInstance(p.isWrite()));
300
//                                po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
301
//                                perma.set(i,po);
302
//                                i++;
303
//                        }
304
//                        json.put("permissions", perma);
305
//                        GWT.log(json.toString(), null);
306
//                }
307
//                PostCommand ep = new PostCommand(folder.getUri()+"?update=", json.toString(), 200){
308
//
309
//                        @Override
310
//                        public void onComplete() {
311
//                                //TODO:CELLTREE
312
//
313
//                                if(getPostBody() != null && !"".equals(getPostBody().trim())){
314
//
315
//
316
//                                        FolderResource fres = ((RestResourceWrapper) app.getTreeView().getSelection()).getResource();
317
//                                        String initialPath = fres.getUri();
318
//                                        String newPath =  getPostBody().trim();
319
//                                        fres.setUri(newPath);
320
//                                        ((RestResourceWrapper) app.getTreeView().getSelection()).getResource().setUri(newPath);
321
//                                        ((RestResourceWrapper) app.getTreeView().getSelection()).setUri(newPath);
322
//                                        app.getTreeView().updateNodeChildren(fres.getParentURI());
323
//                                        if (permList.hasChanges()) {
324
//                                                app.getTreeView().updateMySharedNode();
325
//                                        }
326
//                                        /*
327
//                                        if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
328
//                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
329
//                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
330
//                                        }*/
331
//                                }
332
//                                //app.getFolders().updateFolder( (DnDTreeItem) app.getFolders().getCurrent());
333
//
334
//                                app.get().showFileList(true);
335
//                        }
336
//
337
//                        @Override
338
//                        public void onError(Throwable t) {
339
//                                GWT.log("", t);
340
//                                if(t instanceof RestException){
341
//                                        int statusCode = ((RestException)t).getHttpStatusCode();
342
//                                        if(statusCode == 405)
343
//                                                app.displayError("You don't have the necessary permissions or" +
344
//                                                                " a folder with same name already exists");
345
//                                        else if(statusCode == 404)
346
//                                                app.displayError("Resource not found, or user specified in sharing does not exist");
347
//                                        else
348
//                                                app.displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
349
//                                }
350
//                                else
351
//                                        app.displayError("System error moifying file: "+t.getMessage());
352
//                                //TODO:CELLTREE
353
//                                //app.getFolders().updateFolder( (DnDTreeItem) app.getFolders().getCurrent());
354
//                        }
355
//                };
356
//                DeferredCommand.addCommand(ep);
357
        }
358

    
359
        public void selectTab(int _tab) {
360
                inner.selectTab(_tab);
361
        }
362
}