Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / FolderPropertiesDialog.java @ 555e8e59

History | View | Annotate | Download (12 kB)

1
/*
2
 * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package gr.ebs.gss.client;
20

    
21
import gr.ebs.gss.client.FilePropertiesDialog.Images;
22
import gr.ebs.gss.client.dnd.DnDTreeItem;
23
import gr.ebs.gss.client.rest.ExecutePost;
24
import gr.ebs.gss.client.rest.RestException;
25
import gr.ebs.gss.client.rest.resource.FolderResource;
26
import gr.ebs.gss.client.rest.resource.GroupResource;
27
import gr.ebs.gss.client.rest.resource.PermissionHolder;
28

    
29
import java.util.List;
30
import java.util.Set;
31

    
32
import com.google.gwt.core.client.GWT;
33
import com.google.gwt.i18n.client.DateTimeFormat;
34
import com.google.gwt.json.client.JSONArray;
35
import com.google.gwt.json.client.JSONBoolean;
36
import com.google.gwt.json.client.JSONObject;
37
import com.google.gwt.json.client.JSONString;
38
import com.google.gwt.user.client.DeferredCommand;
39
import com.google.gwt.user.client.ui.Button;
40
import com.google.gwt.user.client.ui.ClickListener;
41
import com.google.gwt.user.client.ui.DialogBox;
42
import com.google.gwt.user.client.ui.FlexTable;
43
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
44
import com.google.gwt.user.client.ui.HorizontalPanel;
45
import com.google.gwt.user.client.ui.KeyboardListener;
46
import com.google.gwt.user.client.ui.TabPanel;
47
import com.google.gwt.user.client.ui.TextBox;
48
import com.google.gwt.user.client.ui.VerticalPanel;
49
import com.google.gwt.user.client.ui.Widget;
50

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

    
56
        private List<GroupResource> groups = null;
57

    
58
        final PermissionsList permList;
59

    
60
        /**
61
         * The widget that holds the folderName of the folder.
62
         */
63
        private TextBox folderName = new TextBox();
64

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

    
71
        final FolderResource folder;
72

    
73
        final TabPanel inner;
74

    
75
        /**
76
         * The widget's constructor.
77
         *
78
         * @param images the image icons from the file properties dialog
79
         * @param _create true if the dialog is displayed for creating a new
80
         *            sub-folder of the selected folder, false if it is displayed
81
         *            for modifying the selected folder
82
         */
83
        public FolderPropertiesDialog(Images images, boolean _create,  final List<GroupResource> _groups) {
84
                setAnimationEnabled(true);
85

    
86
                // Enable IE selection for the dialog (must disable it upon closing it)
87
                GSS.enableIESelection();
88

    
89
                create = _create;
90
                DnDTreeItem folderItem = (DnDTreeItem)GSS.get().getFolders().getCurrent();
91
                folder = folderItem.getFolderResource();
92
                permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner());
93
                groups = _groups;
94
                // Use this opportunity to set the dialog's caption.
95
                if (create)
96
                        setText("Create folder");
97
                else
98
                        setText("Folder properties");
99

    
100
                // Outer contains inner and buttons
101
                VerticalPanel outer = new VerticalPanel();
102
                // Inner contains generalPanel and permPanel
103
                inner = new TabPanel();
104
                VerticalPanel generalPanel = new VerticalPanel();
105
                VerticalPanel permPanel = new VerticalPanel();
106
                HorizontalPanel buttons = new HorizontalPanel();
107
                HorizontalPanel permButtons = new HorizontalPanel();
108
                inner.add(generalPanel, "General");
109
                if (!create)
110
                        inner.add(permPanel, "Sharing");
111
                inner.selectTab(0);
112

    
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, "Date");
118
                folderName.setText(create ? "" : folder.getName());
119
                generalTable.setWidget(0, 1, folderName);
120
                if (create)
121
                        generalTable.setText(1, 1, folder.getName());
122
                else if(folder.getParentName() == null)
123
                        generalTable.setText(1, 1, "-");
124
                else
125
                        generalTable.setText(1, 1, folder.getParentName());
126
                generalTable.setText(2, 1, folder.getOwner());
127
                DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
128
                if(folder.getCreationDate() != null)
129
                        generalTable.setText(3, 1, formatter.format(folder.getCreationDate()));
130
                generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
131
                generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
132
                generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
133
                generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels");
134
                generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
135
                generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values");
136
                generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values");
137
                generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values");
138
                generalTable.setCellSpacing(4);
139

    
140
                // Create the 'Quit' button, along with a listener that hides the dialog
141
                // when the button is clicked and quits the application.
142
                Button ok = new Button("OK", new ClickListener() {
143

    
144
                        public void onClick(Widget sender) {
145

    
146
                                createOrUpdateFolder();
147

    
148
                                closeDialog();
149
                        }
150
                });
151
                buttons.add(ok);
152
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
153
                // Create the 'Cancel' button, along with a listener that hides the
154
                // dialog
155
                // when the button is clicked.
156
                Button cancel = new Button("Cancel", new ClickListener() {
157

    
158
                        public void onClick(Widget sender) {
159
                                closeDialog();
160
                        }
161
                });
162
                buttons.add(cancel);
163
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
164
                buttons.setSpacing(8);
165
                buttons.addStyleName("gwt-TabPanelBottom");
166

    
167
                Button add = new Button("Add Group", new ClickListener() {
168

    
169
                        public void onClick(Widget sender) {
170
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false);
171
                                dlg.center();
172
                        }
173
                });
174
                permButtons.add(add);
175
                permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER);
176

    
177
                Button addUser = new Button("Add User", new ClickListener() {
178

    
179
                        public void onClick(Widget sender) {
180
                                PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true);
181
                                dlg.center();
182
                        }
183
                });
184
                permButtons.add(addUser);
185
                permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER);
186

    
187
                permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
188
                permButtons.setSpacing(8);
189
                permButtons.addStyleName("gwt-TabPanelBottom");
190

    
191
                generalPanel.add(generalTable);
192
                permPanel.add(permList);
193
                permPanel.add(permButtons);
194
                outer.add(inner);
195
                outer.add(buttons);
196
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
197
                outer.addStyleName("gwt-TabPanelBottom");
198

    
199
                setWidget(outer);
200

    
201
                /*if (create)
202
                        folderName.setFocus(true);
203
                else
204
                        ok.setFocus(true);*/
205
        }
206

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

    
213
        @Override
214
        public boolean onKeyDownPreview(char key, int modifiers) {
215
                // Use the popup's key preview hooks to close the dialog when either
216
                // enter or escape is pressed.
217
                switch (key) {
218
                        case KeyboardListener.KEY_ENTER:
219
                                closeDialog();
220
                                createOrUpdateFolder();
221
                                break;
222
                        case KeyboardListener.KEY_ESCAPE:
223
                                closeDialog();
224
                                break;
225
                }
226

    
227
                return true;
228
        }
229

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

    
239
        /**
240
         * Generate an RPC request to create a new folder.
241
         *
242
         * @param userId the ID of the user whose namespace will be searched for
243
         *            folders
244
         * @param _folderName the name of the folder to create
245
         */
246
        private void createFolder() {
247
                ExecutePost ep = new ExecutePost(folder.getUri()+"?new="+folderName.getText(),"", 201){
248

    
249
                        @Override
250
                        public void onComplete() {
251
                                GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
252
                        }
253

    
254
                        @Override
255
                        public void onError(Throwable t) {
256
                                GWT.log("", t);
257
                                if(t instanceof RestException){
258
                                        int statusCode = ((RestException)t).getHttpStatusCode();
259
                                        if(statusCode == 405)
260
                                                GSS.get().displayError("You don't have the necessary permissions or a folder with same name already exists");
261
                                        else if(statusCode == 404)
262
                                                GSS.get().displayError("Resource not found");
263
                                        else
264
                                                GSS.get().displayError("Unable to create folder:"+((RestException)t).getHttpStatusText());
265
                                }
266
                                else
267
                                        GSS.get().displayError("System error creating folder:"+t.getMessage());
268
                        }
269
                };
270
                DeferredCommand.addCommand(ep);
271

    
272
        }
273

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

    
287
        }
288

    
289
        private void updateFolder() {
290
                permList.updatePermissionsAccordingToInput();
291
                Set<PermissionHolder> perms = permList.getPermissions();
292
                JSONObject json = new JSONObject();
293
                if(!folder.getName().equals(folderName.getText()))
294
                        json.put("name", new JSONString(folderName.getText()));
295
                JSONArray perma = new JSONArray();
296
                int i=0;
297
                for(PermissionHolder p : perms){
298
                        JSONObject po = new JSONObject();
299
                        if(p.getUser() != null)
300
                                po.put("user", new JSONString(p.getUser()));
301
                        if(p.getGroup() != null)
302
                                po.put("group", new JSONString(p.getGroup()));
303
                        po.put("read", JSONBoolean.getInstance(p.isRead()));
304
                        po.put("write", JSONBoolean.getInstance(p.isWrite()));
305
                        po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
306
                        perma.set(i,po);
307
                        i++;
308
                }
309
                json.put("permissions", perma);
310
                GWT.log(json.toString(), null);
311
                ExecutePost ep = new ExecutePost(folder.getUri()+"?update=", json.toString(), 200){
312

    
313
                        @Override
314
                        public void onComplete() {
315
                                if(getPostBody() != null && !"".equals(getPostBody().trim())){
316
                                        DnDTreeItem folderItem = (DnDTreeItem) GSS.get().getFolders().getCurrent();
317
                                        FolderResource fres = folderItem.getFolderResource();
318
                                        String initialPath = fres.getUri();
319
                                        String newPath =  getPostBody().trim();
320
                                        fres.setUri(newPath);
321

    
322
                                        if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
323
                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
324
                                                ((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
325
                                        }
326
                                }
327
                                GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
328
                                GSS.get().showFileList(true);
329
                        }
330

    
331
                        @Override
332
                        public void onError(Throwable t) {
333
                                GWT.log("", t);
334
                                if(t instanceof RestException){
335
                                        int statusCode = ((RestException)t).getHttpStatusCode();
336
                                        if(statusCode == 405)
337
                                                GSS.get().displayError("You don't have the necessary permissions or" +
338
                                                                " a folder with same name already exists");
339
                                        else if(statusCode == 404)
340
                                                GSS.get().displayError("Resource not found, or user specified in sharing does not exist");
341
                                        else
342
                                                GSS.get().displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
343
                                }
344
                                else
345
                                        GSS.get().displayError("System error moifying file: "+t.getMessage());
346
                        }
347
                };
348
                DeferredCommand.addCommand(ep);
349
        }
350

    
351
        public void selectTab(int _tab) {
352
                inner.selectTab(_tab);
353
        }
354

    
355
}