Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (11.6 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 com.google.gwt.core.client.GWT;
38
import com.google.gwt.core.client.Scheduler;
39
import com.google.gwt.dom.client.NativeEvent;
40
import com.google.gwt.event.dom.client.*;
41
import com.google.gwt.http.client.Response;
42
import com.google.gwt.i18n.client.DateTimeFormat;
43
import com.google.gwt.user.client.Command;
44
import com.google.gwt.user.client.Event.NativePreviewEvent;
45
import com.google.gwt.user.client.ui.*;
46
import gr.grnet.pithos.web.client.foldertree.Folder;
47
import gr.grnet.pithos.web.client.rest.PutRequest;
48
import gr.grnet.pithos.web.client.rest.RestException;
49

    
50
/**
51
 * The 'Folder properties' dialog box implementation.
52
 */
53
public class FolderPropertiesDialog extends DialogBox {
54

    
55
    protected Pithos app;
56

    
57
    /**
58
     * The widget that holds the folderName of the folder.
59
     */
60
    TextBox folderName = new TextBox();
61

    
62
    /**
63
     * A flag that denotes whether the dialog will be used to create or modify a
64
     * folder.
65
     */
66
    private final boolean create;
67

    
68
    final Folder folder;
69

    
70
    final VerticalPanel inner;
71

    
72
    /**
73
     * The widget's constructor.
74
     */
75
    public FolderPropertiesDialog(final Pithos app, boolean _create, Folder selected) {
76
        this.app = app;
77
        Anchor close = new Anchor("close");
78
        close.addStyleName("close");
79
        close.addClickHandler(new ClickHandler() {
80

    
81
            @Override
82
            public void onClick(ClickEvent event) {
83
                hide();
84
            }
85
        });
86

    
87
        setGlassEnabled(true);
88
        setStyleName("pithos-DialogBox");
89

    
90
        // Enable IE selection for the dialog (must disable it upon closing it)
91
        Pithos.enableIESelection();
92

    
93
        create = _create;
94

    
95
        folder = selected;
96

    
97
        // Use this opportunity to set the dialog's caption.
98
        if(create) {
99
            setText("Create folder");
100
        }
101
        else {
102
            setText("Folder properties");
103
        }
104

    
105
        // Outer contains inner and buttons
106
        VerticalPanel outer = new VerticalPanel();
107
        outer.add(close);
108
        // Inner contains generalPanel and permPanel
109
        inner = new VerticalPanel();
110
        inner.addStyleName("inner");
111

    
112
        VerticalPanel generalPanel = new VerticalPanel();
113
        FlexTable generalTable = new FlexTable();
114
        generalTable.setText(0, 0, "Name");
115
        generalTable.setText(1, 0, "Parent");
116
        generalTable.setText(2, 0, "Creator");
117
        generalTable.setText(3, 0, "Last modified");
118

    
119
        folderName.setText(create ? "" : folder.getName());
120
        folderName.setReadOnly(folder.isContainer() && !create);
121
        generalTable.setWidget(0, 1, folderName);
122

    
123
        final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true);
124
        folderNameNote.setVisible(false);
125
        folderNameNote.setStylePrimaryName("gss-readForAllNote");
126
        generalTable.setWidget(0, 2, folderNameNote);
127

    
128
        if(create) {
129
            generalTable.setText(1, 1, folder.getName());
130
        }
131
        else {
132
            generalTable.setText(1, 1, folder.getParent().getName());
133
        }
134
        if(create) {
135
            generalTable.setText(2, 1, app.getCurrentUserDisplayNameOrID());
136
        }
137
        else {
138
            final String ownerID = folder.getOwnerID();
139
            final String displayName = app.getUserDisplayNameByID(ownerID);
140
            final String ownerDisplayName;
141
            if(displayName == null) {
142
                // FIXME: Get the actual display name and do not use the id
143
                ownerDisplayName = ownerID;
144
            }
145
            else {
146
                ownerDisplayName = displayName;
147
            }
148
            generalTable.setText(2, 1, ownerDisplayName);
149
        }
150
        DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
151
        if(folder.getLastModified() != null) {
152
            generalTable.setText(3, 1, formatter.format(folder.getLastModified()));
153
        }
154
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
155
        generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
156
        generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
157
        generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
158
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
159
        generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
160
        generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
161
        generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
162
        generalTable.setCellSpacing(4);
163
        generalPanel.add(generalTable);
164
        inner.add(generalPanel);
165

    
166
        outer.add(inner);
167

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

    
188
        folderName.addChangeHandler(new ChangeHandler() {
189

    
190
            @Override
191
            public void onChange(ChangeEvent event) {
192
                if(folderName.getText().contains("/")) {
193
                    folderNameNote.setVisible(true);
194
                    ok.setEnabled(false);
195
                }
196
                else {
197
                    folderNameNote.setVisible(false);
198
                    ok.setEnabled(true);
199
                }
200
            }
201
        });
202

    
203
        setWidget(outer);
204
    }
205

    
206
    @Override
207
    public void center() {
208
        super.center();
209
        folderName.setFocus(true);
210
    }
211

    
212
    @Override
213
    protected void onPreviewNativeEvent(NativePreviewEvent preview) {
214
        super.onPreviewNativeEvent(preview);
215

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

    
233

    
234
    /**
235
     * Enables IE selection prevention and hides the dialog
236
     * (we disable the prevention on creation of the dialog)
237
     */
238
    public void closeDialog() {
239
        Pithos.preventIESelection();
240
        hide();
241
    }
242

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

    
257
                    @Override
258
                    public void execute() {
259
                        app.updateStatistics();
260
                    }
261
                }, true);
262
            }
263

    
264
            @Override
265
            public void onError(Throwable t) {
266
                GWT.log("", t);
267
                app.setError(t);
268
                if(t instanceof RestException) {
269
                    app.displayError("Unable to create folder:" + ((RestException) t).getHttpStatusText());
270
                }
271
                else {
272
                    app.displayError("System error creating folder:" + t.getMessage());
273
                }
274
            }
275

    
276
            @Override
277
            protected void onUnauthorized(Response response) {
278
                app.sessionExpired();
279
            }
280
        };
281
        createFolder.setHeader("X-Auth-Token", app.getUserToken());
282
        createFolder.setHeader("Accept", "*/*");
283
        createFolder.setHeader("Content-Length", "0");
284
        createFolder.setHeader("Content-Type", "application/directory");
285
        Scheduler.get().scheduleDeferred(createFolder);
286
    }
287

    
288
    /**
289
     * Upon closing the dialog by clicking OK or pressing ENTER this method does
290
     * the actual work of modifying folder properties or creating a new Folder
291
     * depending on the value of the create field
292
     */
293
    protected void createOrUpdateFolder() {
294
        if(create) {
295
            createFolder();
296
        }
297
        else {
298
            updateFolder();
299
        }
300

    
301
    }
302

    
303
    private void updateFolder() {
304
        final String newName = folderName.getText().trim();
305
        if(newName.length() == 0) {
306
            return;
307
        }
308
        if(!folder.isContainer() && !folder.getName().equals(newName)) {
309
            final String path = folder.getParent().getUri() + "/" + newName;
310
            app.copyFolder(folder, folder.getOwnerID(), path, true, new Command() {
311

    
312
                @Override
313
                public void execute() {
314
                    app.updateFolder(folder.getParent(), false, new Command() {
315

    
316
                        @Override
317
                        public void execute() {
318
                            app.updateMySharedRoot();
319
                        }
320
                    }, true);
321
                }
322
            });
323
        }
324
        else {
325
            app.updateFolder(folder.getParent(), false, new Command() {
326

    
327
                @Override
328
                public void execute() {
329
                    app.updateMySharedRoot();
330
                }
331
            }, true);
332
        }
333
    }
334
}