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