2 * Copyright 2008, 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client;
21 import gr.ebs.gss.client.rest.PostCommand;
22 import gr.ebs.gss.client.rest.RestException;
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;
43 public class GroupPropertiesDialog extends DialogBox {
46 * The widget that holds the folderName of the folder.
48 private TextBox groupName = new TextBox();
51 * A flag that denotes whether the dialog will be used to create or modify a
54 private final boolean create;
57 * The widget's constructor.
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
63 public GroupPropertiesDialog(final boolean _create) {
64 setAnimationEnabled(true);
66 // Use this opportunity to set the dialog's caption.
68 setText("Create Group");
70 setText("Group properties");
71 final VerticalPanel panel = new VerticalPanel();
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);
80 panel.add(generalTable);
81 final HorizontalPanel buttons = new HorizontalPanel();
82 final Button ok = new Button("OK", new ClickHandler() {
84 public void onClick(ClickEvent event) {
85 createGroup(groupName.getText());
90 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
91 // Create the 'Cancel' button, along with a listener that hides the
93 // when the button is clicked.
94 final Button cancel = new Button("Cancel", new ClickHandler() {
96 public void onClick(ClickEvent event) {
101 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
102 buttons.setSpacing(8);
103 buttons.addStyleName("gss-TabPanelBottom");
105 panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
106 //panel.addStyleName("gss-DialogBox");
107 panel.addStyleName("gss-TabPanelBottom");
108 groupName.setFocus(true);
112 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
113 super.onPreviewNativeEvent(preview);
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:
122 createGroup( groupName.getText());
124 case KeyCodes.KEY_ESCAPE:
132 * Generate an RPC request to create a new group.
134 * @param userId the ID of the user whose namespace will be searched for
136 * @param aGroupName the name of the group to create
138 private void createGroup(String aGroupName) {
140 if (aGroupName == null || aGroupName.length() == 0) {
141 GSS.get().displayError("Empty group name!");
144 GWT.log("createGroup(" + aGroupName + ")", null);
145 PostCommand cg = new PostCommand(GSS.get().getCurrentUserResource().getGroupsPath()+"?name="+URL.encodeComponent(aGroupName), "", 201){
148 public void onComplete() {
149 GSS.get().getGroups().updateGroups();
150 GSS.get().showUserList();
154 public void onError(Throwable 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");
167 GSS.get().displayError("Unable to create group:"+((RestException)t).getHttpStatusText());
170 GSS.get().displayError("System error creating group:"+t.getMessage());
173 DeferredCommand.addCommand(cg);