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