Use Lytebox (a Lightbox clone) for quick viewing images.
[pithos] / src / gr / ebs / gss / client / DeleteUserDialog.java
1 /*
2  * Copyright 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 package gr.ebs.gss.client;
20
21 import gr.ebs.gss.client.MessagePanel.Images;
22 import gr.ebs.gss.client.rest.DeleteCommand;
23 import gr.ebs.gss.client.rest.RestException;
24 import gr.ebs.gss.client.rest.resource.GroupUserResource;
25
26 import com.google.gwt.core.client.GWT;
27 import com.google.gwt.user.client.DeferredCommand;
28 import com.google.gwt.user.client.ui.Button;
29 import com.google.gwt.user.client.ui.ClickListener;
30 import com.google.gwt.user.client.ui.DialogBox;
31 import com.google.gwt.user.client.ui.HTML;
32 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
33 import com.google.gwt.user.client.ui.HorizontalPanel;
34 import com.google.gwt.user.client.ui.KeyboardListener;
35 import com.google.gwt.user.client.ui.TreeItem;
36 import com.google.gwt.user.client.ui.VerticalPanel;
37 import com.google.gwt.user.client.ui.Widget;
38
39
40 /**
41  * @author kman
42  *
43  */
44 public class DeleteUserDialog extends DialogBox {
45
46         /**
47          * The widget's constructor.
48          * @param images the supplied images
49          */
50         public DeleteUserDialog(final Images images) {
51                 // Use this opportunity to set the dialog's caption.
52                 setText("Delete user");
53                 setAnimationEnabled(true);
54                 final GroupUserResource group = (GroupUserResource) GSS.get().getCurrentSelection();
55                 // Create a VerticalPanel to contain the 'about' label and the 'OK'
56                 // button.
57                 final VerticalPanel outer = new VerticalPanel();
58                 final HorizontalPanel buttons = new HorizontalPanel();
59
60                 // Create the 'about' text and set a style name so we can style it with
61                 // CSS.
62                 final HTML text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to remove user '" + group.getName() + "'?</td></tr></table>");
63                 text.setStyleName("gss-warnMessage");
64                 outer.add(text);
65
66                 // Create the 'Quit' button, along with a listener that hides the dialog
67                 // when the button is clicked and quits the application.
68                 final Button ok = new Button("OK", new ClickListener() {
69
70                         public void onClick(Widget sender) {
71                                 deleteUser();
72                                 hide();
73                         }
74                 });
75                 buttons.add(ok);
76                 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
77                 // Create the 'Cancel' button, along with a listener that hides the
78                 // dialog
79                 // when the button is clicked.
80                 final Button cancel = new Button("Cancel", new ClickListener() {
81
82                         public void onClick(Widget sender) {
83                                 hide();
84                         }
85                 });
86                 buttons.add(cancel);
87                 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
88                 buttons.setSpacing(8);
89                 buttons.setStyleName("gss-warnMessage");
90                 outer.setStyleName("gss-warnMessage");
91                 outer.add(buttons);
92                 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
93                 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
94                 setWidget(outer);
95         }
96
97         /**
98          * Generate an RPC request to delete a group.
99          *
100          * @param userId the ID of the current user
101          */
102         private void deleteUser() {
103                 final TreeItem user = GSS.get().getGroups().getCurrent();
104                 final TreeItem group = user.getParentItem();
105                 if (group == null) {
106                         GSS.get().displayError("No user was selected!");
107                         return;
108                 }
109                 final GroupUserResource memberR = (GroupUserResource) user.getUserObject();
110                 DeleteCommand du = new DeleteCommand(memberR.getUri()){
111
112                         @Override
113                         public void onComplete() {
114                                 GSS.get().getGroups().updateGroups();
115                         }
116
117                         @Override
118                         public void onError(Throwable t) {
119                                 GWT.log("", t);
120                                 if(t instanceof RestException){
121                                         int statusCode = ((RestException)t).getHttpStatusCode();
122                                         if(statusCode == 405)
123                                                 GSS.get().displayError("You don't have the necessary permissions");
124                                         else if(statusCode == 404)
125                                                 GSS.get().displayError("User not found");
126                                         else
127                                                 GSS.get().displayError("Unable to delete user:"+((RestException)t).getHttpStatusText());
128                                 }
129                                 else
130                                         GSS.get().displayError("System error unable to delete user:"+t.getMessage());
131                         }
132                 };
133                 DeferredCommand.addCommand(du);
134
135         }
136
137         @Override
138         public boolean onKeyDownPreview(final char key, final int modifiers) {
139                 // Use the popup's key preview hooks to close the dialog when either
140                 // enter or escape is pressed.
141                 switch (key) {
142                         case KeyboardListener.KEY_ENTER:
143                                 hide();
144                                 deleteUser();
145                                 break;
146                         case KeyboardListener.KEY_ESCAPE:
147                                 hide();
148                                 break;
149                 }
150
151                 return true;
152         }
153
154 }
155