Don't show Upload and Paste options in My Shared and Other's Shared pseudo-folders.
[pithos] / src / gr / ebs / gss / client / GroupPropertiesDialog.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;
20
21 import gr.ebs.gss.client.rest.PostCommand;
22 import gr.ebs.gss.client.rest.RestException;
23
24 import com.google.gwt.core.client.GWT;
25 import com.google.gwt.http.client.URL;
26 import com.google.gwt.user.client.DeferredCommand;
27 import com.google.gwt.user.client.ui.Button;
28 import com.google.gwt.user.client.ui.ClickListener;
29 import com.google.gwt.user.client.ui.DialogBox;
30 import com.google.gwt.user.client.ui.Grid;
31 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
32 import com.google.gwt.user.client.ui.HorizontalPanel;
33 import com.google.gwt.user.client.ui.KeyboardListener;
34 import com.google.gwt.user.client.ui.TextBox;
35 import com.google.gwt.user.client.ui.VerticalPanel;
36 import com.google.gwt.user.client.ui.Widget;
37
38 /**
39  * @author kman
40  */
41 public class GroupPropertiesDialog extends DialogBox {
42
43         /**
44          * The widget that holds the folderName of the folder.
45          */
46         private TextBox groupName = new TextBox();
47
48         /**
49          * A flag that denotes whether the dialog will be used to create or modify a
50          * folder.
51          */
52         private final boolean create;
53
54         /**
55          * The widget's constructor.
56          *
57          * @param _create true if the dialog is displayed for creating a new
58          *            sub-folder of the selected folder, false if it is displayed
59          *            for modifying the selected folder
60          */
61         public GroupPropertiesDialog(final boolean _create) {
62                 setAnimationEnabled(true);
63                 create = _create;
64                 // Use this opportunity to set the dialog's caption.
65                 if (create)
66                         setText("Create Group");
67                 else
68                         setText("Group properties");
69                 final VerticalPanel panel = new VerticalPanel();
70                 setWidget(panel);
71                 final Grid generalTable = new Grid(1, 2);
72                 generalTable.setText(0, 0, "Group Name");
73                 generalTable.setWidget(0, 1, groupName);
74                 generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
75                 generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
76                 generalTable.setCellSpacing(4);
77
78                 panel.add(generalTable);
79                 final HorizontalPanel buttons = new HorizontalPanel();
80                 final Button ok = new Button("OK", new ClickListener() {
81
82                         public void onClick(Widget sender) {
83                                 createGroup(groupName.getText());
84                                 hide();
85                         }
86                 });
87                 buttons.add(ok);
88                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
89                 // Create the 'Cancel' button, along with a listener that hides the
90                 // dialog
91                 // when the button is clicked.
92                 final Button cancel = new Button("Cancel", new ClickListener() {
93
94                         public void onClick(Widget sender) {
95                                 hide();
96                         }
97                 });
98                 buttons.add(cancel);
99                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
100                 buttons.setSpacing(8);
101                 buttons.addStyleName("gwt-TabPanelBottom");
102                 panel.add(buttons);
103                 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
104                 //panel.addStyleName("gss-DialogBox");
105                 panel.addStyleName("gwt-TabPanelBottom");
106                 groupName.setFocus(true);
107         }
108
109         @Override
110         public boolean onKeyDownPreview(final char key, final int modifiers) {
111                 // Use the popup's key preview hooks to close the dialog when either
112                 // enter or escape is pressed.
113                 switch (key) {
114                         case KeyboardListener.KEY_ENTER:
115                                 hide();
116                                 createGroup( groupName.getText());
117                                 break;
118                         case KeyboardListener.KEY_ESCAPE:
119                                 hide();
120                                 break;
121                 }
122
123                 return true;
124         }
125         /**
126          * Generate an RPC request to create a new group.
127          *
128          * @param userId the ID of the user whose namespace will be searched for
129          *            groups
130          * @param aGroupName the name of the group to create
131          */
132         private void createGroup(String aGroupName) {
133
134                 if (aGroupName == null || aGroupName.length() == 0) {
135                         GSS.get().displayError("Empty group name!");
136                         return;
137                 }
138                 GWT.log("createGroup(" + aGroupName + ")", null);
139                 PostCommand cg = new PostCommand(GSS.get().getCurrentUserResource().getGroupsPath()+"?name="+URL.encodeComponent(aGroupName), "", 201){
140
141                         @Override
142                         public void onComplete() {
143                                 GSS.get().getGroups().updateGroups();
144                                 GSS.get().showUserList();
145                         }
146
147                         @Override
148                         public void onError(Throwable t) {
149                                 GWT.log("", t);
150                                 if(t instanceof RestException){
151                                         int statusCode = ((RestException)t).getHttpStatusCode();
152                                         if(statusCode == 405)
153                                                 GSS.get().displayError("You don't have the necessary permissions");
154                                         else if(statusCode == 404)
155                                                 GSS.get().displayError("Resource does not exist");
156                                         else if(statusCode == 409)
157                                                 GSS.get().displayError("A group with the same name already exists");
158                                         else if(statusCode == 413)
159                                                 GSS.get().displayError("Your quota has been exceeded");
160                                         else
161                                                 GSS.get().displayError("Unable to create group:"+((RestException)t).getHttpStatusText());
162                                 }
163                                 else
164                                         GSS.get().displayError("System error creating group:"+t.getMessage());
165                         }
166                 };
167                 DeferredCommand.addCommand(cg);
168
169         }
170 }