Remove the redundant gss top-level directory.
[pithos] / src / gr / ebs / gss / client / DeleteGroupDialog.java
1 /*\r
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.\r
3  *\r
4  * This file is part of GSS.\r
5  *\r
6  * GSS is free software: you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation, either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * GSS is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.\r
18  */\r
19 package gr.ebs.gss.client;\r
20 \r
21 import gr.ebs.gss.client.MessagePanel.Images;\r
22 import gr.ebs.gss.client.rest.DeleteCommand;\r
23 import gr.ebs.gss.client.rest.RestException;\r
24 import gr.ebs.gss.client.rest.resource.GroupResource;\r
25 \r
26 import com.google.gwt.core.client.GWT;\r
27 import com.google.gwt.user.client.DeferredCommand;\r
28 import com.google.gwt.user.client.ui.Button;\r
29 import com.google.gwt.user.client.ui.ClickListener;\r
30 import com.google.gwt.user.client.ui.DialogBox;\r
31 import com.google.gwt.user.client.ui.HTML;\r
32 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
33 import com.google.gwt.user.client.ui.HorizontalPanel;\r
34 import com.google.gwt.user.client.ui.KeyboardListener;\r
35 import com.google.gwt.user.client.ui.TreeItem;\r
36 import com.google.gwt.user.client.ui.VerticalPanel;\r
37 import com.google.gwt.user.client.ui.Widget;\r
38 \r
39 /**\r
40  * The 'delete group' dialog box.\r
41  */\r
42 public class DeleteGroupDialog extends DialogBox {\r
43 \r
44         /**\r
45          * The widget's constructor.\r
46          * @param images the supplied images\r
47          */\r
48         public DeleteGroupDialog(final Images images) {\r
49                 // Use this opportunity to set the dialog's caption.\r
50                 setText("Delete group");\r
51                 setAnimationEnabled(true);\r
52                 final GroupResource group = (GroupResource) GSS.get().getCurrentSelection();\r
53                 // Create a VerticalPanel to contain the 'about' label and the 'OK'\r
54                 // button.\r
55                 final VerticalPanel outer = new VerticalPanel();\r
56                 final HorizontalPanel buttons = new HorizontalPanel();\r
57 \r
58                 // Create the 'about' text and set a style name so we can style it with\r
59                 // CSS.\r
60                 final HTML text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to delete group '" + group.getName() + "'?</td></tr></table>");\r
61                 text.setStyleName("gss-warnMessage");\r
62                 outer.add(text);\r
63 \r
64                 // Create the 'Quit' button, along with a listener that hides the dialog\r
65                 // when the button is clicked and quits the application.\r
66                 final Button ok = new Button("OK", new ClickListener() {\r
67 \r
68                         public void onClick(Widget sender) {\r
69                                 deleteGroup();\r
70                                 hide();\r
71                         }\r
72                 });\r
73                 buttons.add(ok);\r
74                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);\r
75                 // Create the 'Cancel' button, along with a listener that hides the\r
76                 // dialog\r
77                 // when the button is clicked.\r
78                 final Button cancel = new Button("Cancel", new ClickListener() {\r
79 \r
80                         public void onClick(Widget sender) {\r
81                                 hide();\r
82                         }\r
83                 });\r
84                 buttons.add(cancel);\r
85                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
86                 buttons.setSpacing(8);\r
87                 buttons.setStyleName("gss-warnMessage");\r
88                 outer.setStyleName("gss-warnMessage");\r
89                 outer.add(buttons);\r
90                 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);\r
91                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
92                 setWidget(outer);\r
93         }\r
94 \r
95         /**\r
96          * Generate an RPC request to delete a group.\r
97          *\r
98          * @param userId the ID of the current user\r
99          */\r
100         private void deleteGroup() {\r
101                 final TreeItem group = GSS.get().getGroups().getCurrent();\r
102                 if (group == null) {\r
103                         GSS.get().displayError("No group was selected!");\r
104                         return;\r
105                 }\r
106                 DeleteCommand dg = new DeleteCommand(((GroupResource)group.getUserObject()).getUri()){\r
107                         public void onComplete() {\r
108                                 GSS.get().getGroups().updateGroups();\r
109                         }\r
110 \r
111                         public void onError(Throwable t) {\r
112                                 GWT.log("", t);\r
113                                 if(t instanceof RestException){\r
114                                         int statusCode = ((RestException)t).getHttpStatusCode();\r
115                                         if(statusCode == 405)\r
116                                                 GSS.get().displayError("You don't have the necessary permissions");\r
117                                         else if(statusCode == 404)\r
118                                                 GSS.get().displayError("Group not found");\r
119                                         else\r
120                                                 GSS.get().displayError("Unable to delete group:"+((RestException)t).getHttpStatusText());\r
121                                 }\r
122                                 else\r
123                                         GSS.get().displayError("System error unable to delete group:"+t.getMessage());\r
124                         }\r
125                 };\r
126                 DeferredCommand.addCommand(dg);\r
127         }\r
128 \r
129         /*\r
130          * (non-Javadoc)\r
131          *\r
132          * @see com.google.gwt.user.client.ui.PopupPanel#onKeyDownPreview(char, int)\r
133          */\r
134         public boolean onKeyDownPreview(final char key, final int modifiers) {\r
135                 // Use the popup's key preview hooks to close the dialog when either\r
136                 // enter or escape is pressed.\r
137                 switch (key) {\r
138                         case KeyboardListener.KEY_ENTER:\r
139                                 hide();\r
140                                 deleteGroup();\r
141                                 break;\r
142                         case KeyboardListener.KEY_ESCAPE:\r
143                                 hide();\r
144                                 break;\r
145                 }\r
146 \r
147                 return true;\r
148         }\r
149 \r
150 }\r