Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.5 kB)

1
/*
2
 * Copyright 2011-2012 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.Folder;
38
import gr.grnet.pithos.web.client.rest.PutRequest;
39
import gr.grnet.pithos.web.client.rest.RestException;
40

    
41
import com.google.gwt.core.client.GWT;
42
import com.google.gwt.core.client.Scheduler;
43
import com.google.gwt.dom.client.NativeEvent;
44
import com.google.gwt.event.dom.client.ChangeEvent;
45
import com.google.gwt.event.dom.client.ChangeHandler;
46
import com.google.gwt.event.dom.client.ClickEvent;
47
import com.google.gwt.event.dom.client.ClickHandler;
48
import com.google.gwt.event.dom.client.KeyCodes;
49
import com.google.gwt.event.dom.client.KeyDownEvent;
50
import com.google.gwt.http.client.Response;
51
import com.google.gwt.i18n.client.DateTimeFormat;
52
import com.google.gwt.user.client.Command;
53
import com.google.gwt.user.client.Event.NativePreviewEvent;
54
import com.google.gwt.user.client.ui.Anchor;
55
import com.google.gwt.user.client.ui.Button;
56
import com.google.gwt.user.client.ui.DialogBox;
57
import com.google.gwt.user.client.ui.FlexTable;
58
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
59
import com.google.gwt.user.client.ui.Label;
60
import com.google.gwt.user.client.ui.TextBox;
61
import com.google.gwt.user.client.ui.VerticalPanel;
62

    
63
/**
64
 * The 'Folder properties' dialog box implementation.
65
 */
66
public class FolderPropertiesDialog extends DialogBox {
67

    
68
    protected Pithos app;
69

    
70
        /**
71
         * The widget that holds the folderName of the folder.
72
         */
73
        TextBox folderName = new TextBox();
74

    
75
        /**
76
         * A flag that denotes whether the dialog will be used to create or modify a
77
         * folder.
78
         */
79
        private final boolean create;
80

    
81
        final Folder folder;
82

    
83
        final VerticalPanel inner;
84

    
85
        /**
86
         * The widget's constructor.
87
         */
88
        public FolderPropertiesDialog(final Pithos app, boolean _create,  Folder selected) {
89
        this.app = app;
90
                Anchor close = new Anchor("close");
91
                close.addStyleName("close");
92
                close.addClickHandler(new ClickHandler() {
93
                        
94
                        @Override
95
                        public void onClick(ClickEvent event) {
96
                                hide();
97
                        }
98
                });
99

    
100
                setGlassEnabled(true);
101
                setStyleName("pithos-DialogBox");
102

    
103
                // Enable IE selection for the dialog (must disable it upon closing it)
104
                Pithos.enableIESelection();
105

    
106
                create = _create;
107
                
108
                folder = selected;
109

    
110
                // Use this opportunity to set the dialog's caption.
111
                if (create)
112
                        setText("Create folder");
113
                else
114
                        setText("Folder properties");
115

    
116
                // Outer contains inner and buttons
117
                VerticalPanel outer = new VerticalPanel();
118
                outer.add(close);
119
                // Inner contains generalPanel and permPanel
120
                inner = new VerticalPanel();
121
                inner.addStyleName("inner");
122

    
123
                VerticalPanel generalPanel = new VerticalPanel();
124
        FlexTable generalTable = new FlexTable();
125
        generalTable.setText(0, 0, "Name");
126
        generalTable.setText(1, 0, "Parent");
127
        generalTable.setText(2, 0, "Creator");
128
        generalTable.setText(3, 0, "Last modified");
129

    
130
                folderName.setText(create ? "" : folder.getName());
131
        folderName.setReadOnly(folder.isContainer() && !create);
132
        generalTable.setWidget(0, 1, folderName);
133

    
134
        final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true);
135
                folderNameNote.setVisible(false);
136
                folderNameNote.setStylePrimaryName("gss-readForAllNote");
137
                generalTable.setWidget(0, 2, folderNameNote);
138
                
139
        if (create)
140
            generalTable.setText(1, 1, folder.getName());
141
        else
142
            generalTable.setText(1, 1, folder.getParent().getName());
143
        if (create)
144
                generalTable.setText(2, 1, app.getUserID());
145
        else
146
                generalTable.setText(2, 1, folder.getOwner());
147
        DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
148
        if(folder.getLastModified() != null)
149
            generalTable.setText(3, 1, formatter.format(folder.getLastModified()));
150
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
151
        generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
152
        generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
153
        generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
154
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
155
        generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
156
        generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
157
        generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
158
        generalTable.setCellSpacing(4);
159
        generalPanel.add(generalTable);
160
        inner.add(generalPanel);
161

    
162
        outer.add(inner);
163

    
164
                // Create the 'Create/Update' button, along with a listener that hides the dialog
165
                // when the button is clicked and quits the application.
166
                String okLabel;
167
                if (create)
168
                        okLabel = "Create";
169
                else
170
                        okLabel = "Update";
171
                final Button ok = new Button(okLabel, new ClickHandler() {
172
                        @Override
173
                        public void onClick(ClickEvent event) {
174
                                createOrUpdateFolder();
175
                                closeDialog();
176
                        }
177
                });
178
                ok.addStyleName("button");
179
                outer.add(ok);
180
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
181

    
182
        folderName.addChangeHandler(new ChangeHandler() {
183
                        
184
                        @Override
185
                        public void onChange(ChangeEvent event) {
186
                                if(folderName.getText().contains("/")) {
187
                                        folderNameNote.setVisible(true);
188
                                        ok.setEnabled(false);
189
                                }
190
                                else {
191
                                        folderNameNote.setVisible(false);
192
                                        ok.setEnabled(true);
193
                                }
194
                        }
195
                });
196

    
197
        setWidget(outer);
198
        }
199

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

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

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

    
225

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

    
235
        /**
236
         * Generate an RPC request to create a new folder.
237
         */
238
        private void createFolder() {
239
                String name = folderName.getText().trim();
240
                if (name.length() == 0)
241
                        return;
242
        String path = folder.getUri() + "/" + name;
243
        PutRequest createFolder = new PutRequest(app.getApiPath(), folder.getOwner(), path) {
244
            @Override
245
            public void onSuccess(Resource result) {
246
                app.updateFolder(folder, true, new Command() {
247
                                        
248
                                        @Override
249
                                        public void execute() {
250
                                                app.updateStatistics();
251
                                        }
252
                                }, true);
253
            }
254

    
255
            @Override
256
            public void onError(Throwable t) {
257
                                GWT.log("", t);
258
                                app.setError(t);
259
                                if (t instanceof RestException) {
260
                                        app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
261
                                }
262
                                else
263
                                        app.displayError("System error creating folder:" + t.getMessage());
264
            }
265

    
266
                        @Override
267
                        protected void onUnauthorized(Response response) {
268
                                app.sessionExpired();
269
                        }
270
       };
271
        createFolder.setHeader("X-Auth-Token", app.getUserToken());
272
        createFolder.setHeader("Accept", "*/*");
273
        createFolder.setHeader("Content-Length", "0");
274
        createFolder.setHeader("Content-Type", "application/directory");
275
        Scheduler.get().scheduleDeferred(createFolder);
276
        }
277

    
278
        /**
279
         * Upon closing the dialog by clicking OK or pressing ENTER this method does
280
         * the actual work of modifying folder properties or creating a new Folder
281
         * depending on the value of the create field
282
         */
283
        protected void createOrUpdateFolder() {
284
                if (create)
285
                        createFolder();
286
                else
287
                        updateFolder();
288

    
289
        }
290

    
291
        private void updateFolder() {
292
        final String newName = folderName.getText().trim();
293
        if (newName.length() == 0)
294
                return;
295
        if (!folder.isContainer() && !folder.getName().equals(newName)) {
296
            final String path = folder.getParent().getUri() + "/" + newName;
297
            app.copyFolder(folder, folder.getOwner(), path, true, new Command() {
298
                                
299
                                @Override
300
                                public void execute() {
301
                    app.updateFolder(folder.getParent(), false, new Command() {
302
                                    
303
                                    @Override
304
                                    public void execute() {
305
                                            app.updateMySharedRoot();
306
                                    }
307
                            }, true);
308
                                }
309
                        });
310
        }
311
        else
312
            app.updateFolder(folder.getParent(), false, new Command() {
313
                            
314
                            @Override
315
                            public void execute() {
316
                                    app.updateMySharedRoot();
317
                            }
318
                    }, true);
319
        }
320
}