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