Automated merge with https://gss.googlecode.com/hg/
[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.dom.client.NativeEvent;\r
28 import com.google.gwt.event.dom.client.ClickEvent;\r
29 import com.google.gwt.event.dom.client.ClickHandler;\r
30 import com.google.gwt.event.dom.client.KeyCodes;\r
31 import com.google.gwt.user.client.DeferredCommand;\r
32 import com.google.gwt.user.client.Event.NativePreviewEvent;\r
33 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
34 import com.google.gwt.user.client.ui.Button;\r
35 import com.google.gwt.user.client.ui.DialogBox;\r
36 import com.google.gwt.user.client.ui.HTML;\r
37 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
38 import com.google.gwt.user.client.ui.HorizontalPanel;\r
39 import com.google.gwt.user.client.ui.TreeItem;\r
40 import com.google.gwt.user.client.ui.VerticalPanel;\r
41 \r
42 /**\r
43  * The 'delete group' dialog box.\r
44  */\r
45 public class DeleteGroupDialog extends DialogBox {\r
46 \r
47         /**\r
48          * The widget's constructor.\r
49          * @param images the supplied images\r
50          */\r
51         public DeleteGroupDialog(final Images images) {\r
52                 // Use this opportunity to set the dialog's caption.\r
53                 setText("Delete group");\r
54                 setAnimationEnabled(true);\r
55                 final GroupResource group = (GroupResource) GSS.get().getCurrentSelection();\r
56                 // Create a VerticalPanel to contain the 'about' label and the 'OK'\r
57                 // button.\r
58                 final VerticalPanel outer = new VerticalPanel();\r
59                 final HorizontalPanel buttons = new HorizontalPanel();\r
60 \r
61                 // Create the 'about' text and set a style name so we can style it with\r
62                 // CSS.\r
63                 final HTML text = new HTML("<table><tr><td>" + AbstractImagePrototype.create(images.warn()).getHTML() + "</td><td>" + "Are you sure you want to delete group '" + group.getName() + "'?</td></tr></table>");\r
64                 text.setStyleName("gss-warnMessage");\r
65                 outer.add(text);\r
66 \r
67                 // Create the 'Quit' button, along with a listener that hides the dialog\r
68                 // when the button is clicked and quits the application.\r
69                 final Button ok = new Button("OK", new ClickHandler() {\r
70                         @Override\r
71                         public void onClick(ClickEvent event) {\r
72                                 deleteGroup();\r
73                                 hide();\r
74                         }\r
75                 });\r
76                 buttons.add(ok);\r
77                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);\r
78                 // Create the 'Cancel' button, along with a listener that hides the\r
79                 // dialog\r
80                 // when the button is clicked.\r
81                 final Button cancel = new Button("Cancel", new ClickHandler() {\r
82                         @Override\r
83                         public void onClick(ClickEvent event) {\r
84                                 hide();\r
85                         }\r
86                 });\r
87                 buttons.add(cancel);\r
88                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);\r
89                 buttons.setSpacing(8);\r
90                 buttons.setStyleName("gss-warnMessage");\r
91                 outer.setStyleName("gss-warnMessage");\r
92                 outer.add(buttons);\r
93                 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);\r
94                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);\r
95                 setWidget(outer);\r
96         }\r
97 \r
98         /**\r
99          * Generate an RPC request to delete a group.\r
100          *\r
101          * @param userId the ID of the current user\r
102          */\r
103         private void deleteGroup() {\r
104                 final TreeItem group = GSS.get().getGroups().getCurrent();\r
105                 if (group == null) {\r
106                         GSS.get().displayError("No group was selected!");\r
107                         return;\r
108                 }\r
109                 DeleteCommand dg = new DeleteCommand(((GroupResource)group.getUserObject()).getUri()){\r
110                         @Override\r
111                         public void onComplete() {\r
112                                 GSS.get().getGroups().updateGroups();\r
113                         }\r
114 \r
115                         @Override\r
116                         public void onError(Throwable t) {\r
117                                 GWT.log("", t);\r
118                                 if(t instanceof RestException){\r
119                                         int statusCode = ((RestException)t).getHttpStatusCode();\r
120                                         if(statusCode == 405)\r
121                                                 GSS.get().displayError("You don't have the necessary permissions");\r
122                                         else if(statusCode == 404)\r
123                                                 GSS.get().displayError("Group not found");\r
124                                         else\r
125                                                 GSS.get().displayError("Unable to delete group:"+((RestException)t).getHttpStatusText());\r
126                                 }\r
127                                 else\r
128                                         GSS.get().displayError("System error unable to delete group:"+t.getMessage());\r
129                         }\r
130                 };\r
131                 DeferredCommand.addCommand(dg);\r
132         }\r
133 \r
134 \r
135         @Override\r
136         protected void onPreviewNativeEvent(NativePreviewEvent preview) {\r
137                 super.onPreviewNativeEvent(preview);\r
138 \r
139                 NativeEvent evt = preview.getNativeEvent();\r
140                 if (evt.getType().equals("keydown"))\r
141                         // Use the popup's key preview hooks to close the dialog when either\r
142                         // enter or escape is pressed.\r
143                         switch (evt.getKeyCode()) {\r
144                                 case KeyCodes.KEY_ENTER:\r
145                                         hide();\r
146                                         deleteGroup();\r
147                                         break;\r
148                                 case KeyCodes.KEY_ESCAPE:\r
149                                         hide();\r
150                                         break;\r
151                         }\r
152         }\r
153 \r
154 }\r