Use an exponential backoff strategy for retrying rolled back transactions.
[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                         @Override\r
108                         public void onComplete() {\r
109                                 GSS.get().getGroups().updateGroups();\r
110                         }\r
111 \r
112                         @Override\r
113                         public void onError(Throwable t) {\r
114                                 GWT.log("", t);\r
115                                 if(t instanceof RestException){\r
116                                         int statusCode = ((RestException)t).getHttpStatusCode();\r
117                                         if(statusCode == 405)\r
118                                                 GSS.get().displayError("You don't have the necessary permissions");\r
119                                         else if(statusCode == 404)\r
120                                                 GSS.get().displayError("Group not found");\r
121                                         else\r
122                                                 GSS.get().displayError("Unable to delete group:"+((RestException)t).getHttpStatusText());\r
123                                 }\r
124                                 else\r
125                                         GSS.get().displayError("System error unable to delete group:"+t.getMessage());\r
126                         }\r
127                 };\r
128                 DeferredCommand.addCommand(dg);\r
129         }\r
130 \r
131         @Override\r
132         public boolean onKeyDownPreview(final char key, final int modifiers) {\r
133                 // Use the popup's key preview hooks to close the dialog when either\r
134                 // enter or escape is pressed.\r
135                 switch (key) {\r
136                         case KeyboardListener.KEY_ENTER:\r
137                                 hide();\r
138                                 deleteGroup();\r
139                                 break;\r
140                         case KeyboardListener.KEY_ESCAPE:\r
141                                 hide();\r
142                                 break;\r
143                 }\r
144 \r
145                 return true;\r
146         }\r
147 \r
148 }\r