Updated copyright notice
[pithos] / web_client / src / gr / grnet / pithos / web / client / commands / NewFolderCommand.java
1 /*
2  *  Copyright (c) 2011 Greek Research and Technology Network
3  */
4 package gr.grnet.pithos.web.client.commands;
5
6 import gr.grnet.pithos.web.client.FileMenu.Images;
7 import gr.grnet.pithos.web.client.FolderPropertiesDialog;
8 import gr.grnet.pithos.web.client.GSS;
9 import gr.grnet.pithos.web.client.rest.GetCommand;
10 import gr.grnet.pithos.web.client.rest.MultipleGetCommand;
11 import gr.grnet.pithos.web.client.rest.resource.GroupResource;
12 import gr.grnet.pithos.web.client.rest.resource.GroupsResource;
13 import gr.grnet.pithos.web.client.rest.resource.RestResource;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.user.client.Command;
20 import com.google.gwt.user.client.DeferredCommand;
21 import com.google.gwt.user.client.IncrementalCommand;
22 import com.google.gwt.user.client.ui.PopupPanel;
23
24 /**
25  * Display the 'new folder' dialog for creating a new folder.
26  *
27  */
28 public class NewFolderCommand implements Command{
29         private PopupPanel containerPanel;
30         final Images images;
31
32         private List<GroupResource> groups = null;
33
34         /**
35          * @param aContainerPanel
36          * @param newImages the images of the new folder dialog
37          */
38         public NewFolderCommand(PopupPanel aContainerPanel, final Images newImages){
39                 containerPanel = aContainerPanel;
40                 images=newImages;
41         }
42
43         @Override
44         public void execute() {
45                 containerPanel.hide();
46                 getGroups();
47                 DeferredCommand.addCommand(new IncrementalCommand() {
48
49                         @Override
50                         public boolean execute() {
51                                 boolean res = canContinue();
52                                 if (res) {
53                                         displayNewFolder();
54                                         return false;
55                                 }
56                                 return true;
57                         }
58
59                 });
60         }
61
62         private boolean canContinue() {
63                 if (groups == null)
64                         return false;
65                 return true;
66         }
67
68         void displayNewFolder() {
69                 RestResource currentFolder = GSS.get().getTreeView().getSelection();
70                 if (currentFolder == null) {
71                         GSS.get().displayError("You have to select the parent folder first");
72                         return;
73                 }
74                 FolderPropertiesDialog dlg = new FolderPropertiesDialog(images, true,  groups);
75                 dlg.center();
76         }
77
78         private void getGroups() {
79                 GetCommand<GroupsResource> gg = new GetCommand<GroupsResource>(GroupsResource.class, GSS.get().getCurrentUserResource().getGroupsPath(), null){
80
81                         @Override
82                         public void onComplete() {
83                                 GroupsResource res = getResult();
84                                 MultipleGetCommand<GroupResource> ga = new MultipleGetCommand<GroupResource>(GroupResource.class, res.getGroupPaths().toArray(new String[]{}), null){
85
86                                         @Override
87                                         public void onComplete() {
88                                                 List<GroupResource> groupList = getResult();
89                                                 groups = groupList;
90                                         }
91
92                                         @Override
93                                         public void onError(Throwable t) {
94                                                 GWT.log("", t);
95                                                 GSS.get().displayError("Unable to fetch groups");
96                                                 groups = new ArrayList<GroupResource>();
97                                         }
98
99                                         @Override
100                                         public void onError(String p, Throwable throwable) {
101                                                 GWT.log("Path:"+p, throwable);
102                                         }
103                                 };
104                                 DeferredCommand.addCommand(ga);
105                         }
106
107                         @Override
108                         public void onError(Throwable t) {
109                                 GWT.log("", t);
110                                 GSS.get().displayError("Unable to fetch groups");
111                                 groups = new ArrayList<GroupResource>();
112                         }
113                 };
114                 DeferredCommand.addCommand(gg);
115         }
116
117 }