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");
110 public void center() {
112 groupName.setFocus(true);
116 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
117 super.onPreviewNativeEvent(preview);
119 NativeEvent evt = preview.getNativeEvent();
120 if (evt.getType().equals("keydown"))
121 // Use the popup's key preview hooks to close the dialog when either
122 // enter or escape is pressed.
123 switch (evt.getKeyCode()) {
124 case KeyCodes.KEY_ENTER:
126 createGroup( groupName.getText());
128 case KeyCodes.KEY_ESCAPE:
136 * Generate an RPC request to create a new group.
138 * @param userId the ID of the user whose namespace will be searched for
140 * @param aGroupName the name of the group to create
142 private void createGroup(String aGroupName) {
144 if (aGroupName == null || aGroupName.length() == 0) {
145 GSS.get().displayError("Empty group name!");
148 GWT.log("createGroup(" + aGroupName + ")", null);
149 PostCommand cg = new PostCommand(GSS.get().getCurrentUserResource().getGroupsPath()+"?name="+URL.encodeComponent(aGroupName), "", 201){
152 public void onComplete() {
153 GSS.get().getGroups().updateGroups();
154 GSS.get().showUserList();
158 public void onError(Throwable t) {
160 if(t instanceof RestException){
161 int statusCode = ((RestException)t).getHttpStatusCode();
162 if(statusCode == 405)
163 GSS.get().displayError("You don't have the necessary permissions");
164 else if(statusCode == 404)
165 GSS.get().displayError("Resource does not exist");
166 else if(statusCode == 409)
167 GSS.get().displayError("A group with the same name already exists");
168 else if(statusCode == 413)
169 GSS.get().displayError("Your quota has been exceeded");
171 GSS.get().displayError("Unable to create group:"+((RestException)t).getHttpStatusText());
174 GSS.get().displayError("System error creating group:"+t.getMessage());
177 DeferredCommand.addCommand(cg);