Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / DeleteUserDialog.java @ 0e64bec2

History | View | Annotate | Download (5.2 kB)

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