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