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