Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / DeleteGroupDialog.java @ 783db80b

History | View | Annotate | Download (5.3 kB)

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