root / web_client / src / gr / grnet / pithos / web / client / FolderPropertiesDialog.java @ 9e8e14e4
History | View | Annotate | Download (16.5 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2011 Greek Research and Technology Network
|
3 |
*/
|
4 |
package gr.grnet.pithos.web.client; |
5 |
|
6 |
import gr.grnet.pithos.web.client.FilePropertiesDialog.Images; |
7 |
import gr.grnet.pithos.web.client.rest.PostCommand; |
8 |
import gr.grnet.pithos.web.client.rest.RestException; |
9 |
import gr.grnet.pithos.web.client.rest.resource.FolderResource; |
10 |
import gr.grnet.pithos.web.client.rest.resource.GroupResource; |
11 |
import gr.grnet.pithos.web.client.rest.resource.PermissionHolder; |
12 |
import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper; |
13 |
|
14 |
import java.util.List; |
15 |
import java.util.Set; |
16 |
|
17 |
import com.google.gwt.core.client.GWT; |
18 |
import com.google.gwt.dom.client.NativeEvent; |
19 |
import com.google.gwt.event.dom.client.ChangeEvent; |
20 |
import com.google.gwt.event.dom.client.ChangeHandler; |
21 |
import com.google.gwt.event.dom.client.ClickEvent; |
22 |
import com.google.gwt.event.dom.client.ClickHandler; |
23 |
import com.google.gwt.event.dom.client.KeyCodes; |
24 |
import com.google.gwt.http.client.URL; |
25 |
import com.google.gwt.i18n.client.DateTimeFormat; |
26 |
import com.google.gwt.json.client.JSONArray; |
27 |
import com.google.gwt.json.client.JSONBoolean; |
28 |
import com.google.gwt.json.client.JSONObject; |
29 |
import com.google.gwt.json.client.JSONString; |
30 |
import com.google.gwt.user.client.DeferredCommand; |
31 |
import com.google.gwt.user.client.Event.NativePreviewEvent; |
32 |
import com.google.gwt.user.client.ui.Button; |
33 |
import com.google.gwt.user.client.ui.CheckBox; |
34 |
import com.google.gwt.user.client.ui.DecoratedTabPanel; |
35 |
import com.google.gwt.user.client.ui.DialogBox; |
36 |
import com.google.gwt.user.client.ui.FlexTable; |
37 |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
38 |
import com.google.gwt.user.client.ui.HorizontalPanel; |
39 |
import com.google.gwt.user.client.ui.Label; |
40 |
import com.google.gwt.user.client.ui.TabPanel; |
41 |
import com.google.gwt.user.client.ui.TextBox; |
42 |
import com.google.gwt.user.client.ui.VerticalPanel; |
43 |
|
44 |
/**
|
45 |
* The 'Folder properties' dialog box implementation.
|
46 |
*/
|
47 |
public class FolderPropertiesDialog extends DialogBox { |
48 |
|
49 |
private List<GroupResource> groups = null; |
50 |
|
51 |
final PermissionsList permList;
|
52 |
|
53 |
private CheckBox readForAll;
|
54 |
|
55 |
/**
|
56 |
* The widget that holds the folderName of the folder.
|
57 |
*/
|
58 |
private TextBox folderName = new TextBox(); |
59 |
|
60 |
/**
|
61 |
* A flag that denotes whether the dialog will be used to create or modify a
|
62 |
* folder.
|
63 |
*/
|
64 |
private final boolean create; |
65 |
|
66 |
final FolderResource folder;
|
67 |
|
68 |
final TabPanel inner;
|
69 |
|
70 |
/**
|
71 |
* The widget's constructor.
|
72 |
*
|
73 |
* @param images the image icons from the file properties dialog
|
74 |
* @param _create true if the dialog is displayed for creating a new
|
75 |
* sub-folder of the selected folder, false if it is displayed
|
76 |
* for modifying the selected folder
|
77 |
*/
|
78 |
public FolderPropertiesDialog(Images images, boolean _create, final List<GroupResource> _groups) { |
79 |
setAnimationEnabled(true);
|
80 |
|
81 |
// Enable IE selection for the dialog (must disable it upon closing it)
|
82 |
GSS.enableIESelection(); |
83 |
|
84 |
create = _create; |
85 |
|
86 |
folder = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource(); |
87 |
permList = new PermissionsList(images, folder.getPermissions(), folder.getOwner());
|
88 |
groups = _groups; |
89 |
|
90 |
// Use this opportunity to set the dialog's caption.
|
91 |
if (create)
|
92 |
setText("Create folder");
|
93 |
else
|
94 |
setText("Folder properties");
|
95 |
|
96 |
// Outer contains inner and buttons
|
97 |
VerticalPanel outer = new VerticalPanel();
|
98 |
// Inner contains generalPanel and permPanel
|
99 |
inner = new DecoratedTabPanel();
|
100 |
inner.setAnimationEnabled(true);
|
101 |
VerticalPanel generalPanel = new VerticalPanel();
|
102 |
VerticalPanel permPanel = new VerticalPanel();
|
103 |
final HorizontalPanel permForAll = new HorizontalPanel(); |
104 |
final HorizontalPanel pathPanel = new HorizontalPanel(); |
105 |
HorizontalPanel buttons = new HorizontalPanel();
|
106 |
HorizontalPanel permButtons = new HorizontalPanel();
|
107 |
|
108 |
inner.add(generalPanel, "General");
|
109 |
if (!create)
|
110 |
inner.add(permPanel, "Sharing");
|
111 |
inner.selectTab(0);
|
112 |
|
113 |
final Label folderNameNote = new Label("Please note that slashes ('/') are not allowed in folder names.", true); |
114 |
folderNameNote.setVisible(false);
|
115 |
folderNameNote.setStylePrimaryName("pithos-readForAllNote");
|
116 |
|
117 |
FlexTable generalTable = new FlexTable();
|
118 |
generalTable.setText(0, 0, "Name"); |
119 |
generalTable.setText(1, 0, "Parent"); |
120 |
generalTable.setText(2, 0, "Creator"); |
121 |
generalTable.setText(3, 0, "Last modified"); |
122 |
folderName.setText(create ? "" : folder.getName());
|
123 |
folderName.getElement().setId("folderPropertiesDialog.textBox.name");
|
124 |
generalTable.setWidget(0, 1, folderName); |
125 |
folderName.addChangeHandler(new ChangeHandler() {
|
126 |
|
127 |
@Override
|
128 |
public void onChange(ChangeEvent event) { |
129 |
if(folderName.getText().contains("/")) |
130 |
folderNameNote.setVisible(true);
|
131 |
else
|
132 |
folderNameNote.setVisible(false);
|
133 |
|
134 |
} |
135 |
}); |
136 |
|
137 |
|
138 |
if (create)
|
139 |
generalTable.setText(1, 1, folder.getName()); |
140 |
else if(folder.getParentName() == null) |
141 |
generalTable.setText(1, 1, "-"); |
142 |
else
|
143 |
generalTable.setText(1, 1, folder.getParentName()); |
144 |
generalTable.setWidget(0, 2, folderNameNote); |
145 |
generalTable.setText(2, 1, folder.getOwner()); |
146 |
DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
|
147 |
if(folder.getModificationDate() != null) |
148 |
generalTable.setText(3, 1, formatter.format(folder.getModificationDate())); |
149 |
generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels"); |
150 |
generalTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels"); |
151 |
generalTable.getFlexCellFormatter().setStyleName(2, 0, "props-labels"); |
152 |
generalTable.getFlexCellFormatter().setStyleName(3, 0, "props-labels"); |
153 |
generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values"); |
154 |
generalTable.getFlexCellFormatter().setStyleName(1, 1, "props-values"); |
155 |
generalTable.getFlexCellFormatter().setStyleName(2, 1, "props-values"); |
156 |
generalTable.getFlexCellFormatter().setStyleName(3, 1, "props-values"); |
157 |
generalTable.setCellSpacing(4);
|
158 |
|
159 |
// Create the 'Create/Update' button, along with a listener that hides the dialog
|
160 |
// when the button is clicked and quits the application.
|
161 |
String okLabel;
|
162 |
if (create)
|
163 |
okLabel = "Create";
|
164 |
else
|
165 |
okLabel = "Update";
|
166 |
Button ok = new Button(okLabel, new ClickHandler() { |
167 |
@Override
|
168 |
public void onClick(ClickEvent event) { |
169 |
if(folderName.getText().contains("/")) |
170 |
folderNameNote.setVisible(true);
|
171 |
else {
|
172 |
folderNameNote.setVisible(false);
|
173 |
createOrUpdateFolder(); |
174 |
closeDialog(); |
175 |
} |
176 |
|
177 |
} |
178 |
}); |
179 |
ok.getElement().setId("folderPropertiesDialog.button.ok");
|
180 |
buttons.add(ok); |
181 |
buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER); |
182 |
// Create the 'Cancel' button, along with a listener that hides the
|
183 |
// dialog
|
184 |
// when the button is clicked.
|
185 |
Button cancel = new Button("Cancel", new ClickHandler() { |
186 |
@Override
|
187 |
public void onClick(ClickEvent event) { |
188 |
closeDialog(); |
189 |
} |
190 |
}); |
191 |
cancel.getElement().setId("folderPropertiesDialog.button.cancel");
|
192 |
buttons.add(cancel); |
193 |
buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
194 |
buttons.setSpacing(8);
|
195 |
buttons.addStyleName("pithos-TabPanelBottom");
|
196 |
|
197 |
Button add = new Button("Add Group", new ClickHandler() { |
198 |
@Override
|
199 |
public void onClick(ClickEvent event) { |
200 |
PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, false); |
201 |
dlg.center(); |
202 |
} |
203 |
}); |
204 |
add.getElement().setId("folderPropertiesDialog.button.addGroup");
|
205 |
permButtons.add(add); |
206 |
permButtons.setCellHorizontalAlignment(add, HasHorizontalAlignment.ALIGN_CENTER); |
207 |
|
208 |
Button addUser = new Button("Add User", new ClickHandler() { |
209 |
@Override
|
210 |
public void onClick(ClickEvent event) { |
211 |
PermissionsAddDialog dlg = new PermissionsAddDialog(groups, permList, true); |
212 |
dlg.center(); |
213 |
} |
214 |
}); |
215 |
addUser.getElement().setId("folderPropertiesDialog.button.addUser");
|
216 |
permButtons.add(addUser); |
217 |
permButtons.setCellHorizontalAlignment(addUser, HasHorizontalAlignment.ALIGN_CENTER); |
218 |
|
219 |
permButtons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER); |
220 |
permButtons.setSpacing(8);
|
221 |
permButtons.addStyleName("pithos-TabPanelBottom");
|
222 |
|
223 |
final Label readForAllNote = new Label("When this option is enabled, the folder will be readable" + |
224 |
" by everyone. By checking this option, you are certifying that you have the right to " +
|
225 |
"distribute this folder's contents and that it does not violate the Terms of Use.", true); |
226 |
readForAllNote.setVisible(false);
|
227 |
readForAllNote.setStylePrimaryName("pithos-readForAllNote");
|
228 |
|
229 |
readForAll = new CheckBox();
|
230 |
readForAll.getElement().setId("folderPropertiesDialog.checkBox.public");
|
231 |
readForAll.setValue(folder.isReadForAll()); |
232 |
readForAll.addClickHandler(new ClickHandler() {
|
233 |
@Override
|
234 |
public void onClick(ClickEvent event) { |
235 |
readForAllNote.setVisible(readForAll.getValue()); |
236 |
} |
237 |
|
238 |
}); |
239 |
|
240 |
generalPanel.add(generalTable); |
241 |
permPanel.add(permList); |
242 |
permPanel.add(permButtons); |
243 |
|
244 |
// Only show the read for all permission if the user is the owner.
|
245 |
if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername())) {
|
246 |
permForAll.add(new Label("Public")); |
247 |
permForAll.add(readForAll); |
248 |
permForAll.setSpacing(8);
|
249 |
permForAll.addStyleName("pithos-TabPanelBottom");
|
250 |
permForAll.add(readForAllNote); |
251 |
permPanel.add(permForAll); |
252 |
} |
253 |
TextBox path = new TextBox();
|
254 |
path.getElement().setId("folderPropertiesDialog.textBox.link");
|
255 |
path.setWidth("100%");
|
256 |
path.addClickHandler(new ClickHandler() {
|
257 |
@Override
|
258 |
public void onClick(ClickEvent event) { |
259 |
GSS.enableIESelection(); |
260 |
((TextBox) event.getSource()).selectAll(); |
261 |
GSS.preventIESelection(); |
262 |
} |
263 |
|
264 |
}); |
265 |
path.setText(folder.getUri()); |
266 |
path.setTitle("Use this link for sharing the folder via e-mail, IM, etc. (crtl-C/cmd-C to copy to system clipboard)");
|
267 |
path.setWidth("100%");
|
268 |
path.setReadOnly(true);
|
269 |
pathPanel.setWidth("100%");
|
270 |
pathPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); |
271 |
pathPanel.add(new Label("Link")); |
272 |
pathPanel.setSpacing(8);
|
273 |
pathPanel.addStyleName("pithos-TabPanelBottom");
|
274 |
pathPanel.add(path); |
275 |
permPanel.add(pathPanel); |
276 |
|
277 |
outer.add(inner); |
278 |
outer.add(buttons); |
279 |
outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER); |
280 |
outer.addStyleName("pithos-TabPanelBottom");
|
281 |
|
282 |
setWidget(outer); |
283 |
|
284 |
/*if (create)
|
285 |
folderName.setFocus(true);
|
286 |
else
|
287 |
ok.setFocus(true);*/
|
288 |
} |
289 |
|
290 |
@Override
|
291 |
public void center() { |
292 |
super.center();
|
293 |
folderName.setFocus(true);
|
294 |
} |
295 |
|
296 |
@Override
|
297 |
protected void onPreviewNativeEvent(NativePreviewEvent preview) { |
298 |
super.onPreviewNativeEvent(preview);
|
299 |
|
300 |
NativeEvent evt = preview.getNativeEvent(); |
301 |
if (evt.getType().equals("keydown")) |
302 |
// Use the popup's key preview hooks to close the dialog when either
|
303 |
// enter or escape is pressed.
|
304 |
switch (evt.getKeyCode()) {
|
305 |
case KeyCodes.KEY_ENTER:
|
306 |
closeDialog(); |
307 |
createOrUpdateFolder(); |
308 |
break;
|
309 |
case KeyCodes.KEY_ESCAPE:
|
310 |
closeDialog(); |
311 |
break;
|
312 |
} |
313 |
} |
314 |
|
315 |
|
316 |
/**
|
317 |
* Enables IE selection prevention and hides the dialog
|
318 |
* (we disable the prevention on creation of the dialog)
|
319 |
*/
|
320 |
public void closeDialog() { |
321 |
GSS.preventIESelection(); |
322 |
hide(); |
323 |
} |
324 |
|
325 |
/**
|
326 |
* Generate an RPC request to create a new folder.
|
327 |
*
|
328 |
* @param userId the ID of the user whose namespace will be searched for
|
329 |
* folders
|
330 |
* @param _folderName the name of the folder to create
|
331 |
*/
|
332 |
private void createFolder() { |
333 |
String name = folderName.getText();
|
334 |
if (!GSS.isValidResourceName(name)) {
|
335 |
GSS.get().displayError("The folder name '" + name + "' is invalid"); |
336 |
return;
|
337 |
} |
338 |
PostCommand ep = new PostCommand(folder.getUri() + "?new=" + |
339 |
URL.encodeComponent(name), "", 201) { |
340 |
|
341 |
@Override
|
342 |
public void onComplete() { |
343 |
//TODO:CELLTREE
|
344 |
if(folder.getUri().equals(GSS.get().getTreeView().getMyFolders().getUri())){
|
345 |
GSS.get().getTreeView().updateRootNode(); |
346 |
} |
347 |
else
|
348 |
GSS.get().getTreeView().updateNodeChildren((RestResourceWrapper) GSS.get().getTreeView().getSelection()); |
349 |
//GSS.get().getFolders().updateFolder((DnDTreeItem) GSS.get().getFolders().getCurrent());
|
350 |
} |
351 |
|
352 |
@Override
|
353 |
public void onError(Throwable t) { |
354 |
GWT.log("", t);
|
355 |
if(t instanceof RestException){ |
356 |
int statusCode = ((RestException)t).getHttpStatusCode();
|
357 |
if(statusCode == 405) |
358 |
GSS.get().displayError("You don't have the necessary" +
|
359 |
" permissions or a folder with same name " +
|
360 |
"already exists");
|
361 |
else if(statusCode == 404) |
362 |
GSS.get().displayError("Resource not found");
|
363 |
else
|
364 |
GSS.get().displayError("Unable to create folder:" +
|
365 |
((RestException)t).getHttpStatusText()); |
366 |
} |
367 |
else
|
368 |
GSS.get().displayError("System error creating folder:" +
|
369 |
t.getMessage()); |
370 |
} |
371 |
}; |
372 |
DeferredCommand.addCommand(ep); |
373 |
|
374 |
} |
375 |
|
376 |
/**
|
377 |
* Upon closing the dialog by clicking OK or pressing ENTER this method does
|
378 |
* the actual work of modifying folder properties or creating a new Folder
|
379 |
* depending on the value of the create field
|
380 |
*
|
381 |
* @param userId
|
382 |
*/
|
383 |
private void createOrUpdateFolder() { |
384 |
if (create)
|
385 |
createFolder(); |
386 |
else
|
387 |
updateFolder(); |
388 |
|
389 |
} |
390 |
|
391 |
private void updateFolder() { |
392 |
permList.updatePermissionsAccordingToInput(); |
393 |
Set<PermissionHolder> perms = permList.getPermissions();
|
394 |
JSONObject json = new JSONObject();
|
395 |
if(!folder.getName().equals(folderName.getText()))
|
396 |
json.put("name", new JSONString(folderName.getText())); |
397 |
//only update the read for all perm if the user is the owner
|
398 |
if (readForAll.getValue() != folder.isReadForAll())
|
399 |
if (folder.getOwner().equals(GSS.get().getCurrentUserResource().getUsername()))
|
400 |
json.put("readForAll", JSONBoolean.getInstance(readForAll.getValue()));
|
401 |
if (permList.hasChanges()) {
|
402 |
JSONArray perma = new JSONArray();
|
403 |
int i=0; |
404 |
for(PermissionHolder p : perms){
|
405 |
JSONObject po = new JSONObject();
|
406 |
if(p.getUser() != null) |
407 |
po.put("user", new JSONString(p.getUser())); |
408 |
if(p.getGroup() != null) |
409 |
po.put("group", new JSONString(p.getGroup())); |
410 |
po.put("read", JSONBoolean.getInstance(p.isRead()));
|
411 |
po.put("write", JSONBoolean.getInstance(p.isWrite()));
|
412 |
po.put("modifyACL", JSONBoolean.getInstance(p.isModifyACL()));
|
413 |
perma.set(i,po); |
414 |
i++; |
415 |
} |
416 |
json.put("permissions", perma);
|
417 |
GWT.log(json.toString(), null);
|
418 |
} |
419 |
PostCommand ep = new PostCommand(folder.getUri()+"?update=", json.toString(), 200){ |
420 |
|
421 |
@Override
|
422 |
public void onComplete() { |
423 |
//TODO:CELLTREE
|
424 |
|
425 |
if(getPostBody() != null && !"".equals(getPostBody().trim())){ |
426 |
|
427 |
|
428 |
FolderResource fres = ((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource(); |
429 |
String initialPath = fres.getUri();
|
430 |
String newPath = getPostBody().trim();
|
431 |
fres.setUri(newPath); |
432 |
((RestResourceWrapper) GSS.get().getTreeView().getSelection()).getResource().setUri(newPath); |
433 |
((RestResourceWrapper) GSS.get().getTreeView().getSelection()).setUri(newPath); |
434 |
GSS.get().getTreeView().updateNodeChildren(fres.getParentURI()); |
435 |
if (permList.hasChanges()) {
|
436 |
GSS.get().getTreeView().updateMySharedNode(); |
437 |
} |
438 |
/*
|
439 |
if(folderItem.getParentItem() != null && ((DnDTreeItem)folderItem.getParentItem()).getFolderResource() != null){
|
440 |
((DnDTreeItem)folderItem.getParentItem()).getFolderResource().removeSubfolderPath(initialPath);
|
441 |
((DnDTreeItem)folderItem.getParentItem()).getFolderResource().getSubfolderPaths().add(newPath);
|
442 |
}*/
|
443 |
} |
444 |
//GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
|
445 |
|
446 |
GSS.get().showFileList(true);
|
447 |
} |
448 |
|
449 |
@Override
|
450 |
public void onError(Throwable t) { |
451 |
GWT.log("", t);
|
452 |
if(t instanceof RestException){ |
453 |
int statusCode = ((RestException)t).getHttpStatusCode();
|
454 |
if(statusCode == 405) |
455 |
GSS.get().displayError("You don't have the necessary permissions or" +
|
456 |
" a folder with same name already exists");
|
457 |
else if(statusCode == 404) |
458 |
GSS.get().displayError("Resource not found, or user specified in sharing does not exist");
|
459 |
else
|
460 |
GSS.get().displayError("Unable to update folder: "+((RestException)t).getHttpStatusText());
|
461 |
} |
462 |
else
|
463 |
GSS.get().displayError("System error moifying file: "+t.getMessage());
|
464 |
//TODO:CELLTREE
|
465 |
//GSS.get().getFolders().updateFolder( (DnDTreeItem) GSS.get().getFolders().getCurrent());
|
466 |
} |
467 |
}; |
468 |
DeferredCommand.addCommand(ep); |
469 |
} |
470 |
|
471 |
public void selectTab(int _tab) { |
472 |
inner.selectTab(_tab); |
473 |
} |
474 |
|
475 |
} |