Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.3 kB)

1
/*
2
 * Copyright 2007, 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.GroupResource;
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
 * The 'delete group' dialog box.
44
 */
45
public class DeleteGroupDialog extends DialogBox {
46

    
47
        /**
48
         * The widget's constructor.
49
         * @param images the supplied images
50
         */
51
        public DeleteGroupDialog(final Images images) {
52
                // Use this opportunity to set the dialog's caption.
53
                setText("Delete group");
54
                setAnimationEnabled(true);
55
                final GroupResource group = (GroupResource) GSS.get().getCurrentSelection();
56
                // Create a VerticalPanel to contain the 'about' label and the 'OK'
57
                // button.
58
                final VerticalPanel outer = new VerticalPanel();
59
                final HorizontalPanel buttons = new HorizontalPanel();
60

    
61
                // Create the 'about' text and set a style name so we can style it with
62
                // CSS.
63
                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
                text.setStyleName("gss-warnMessage");
65
                outer.add(text);
66

    
67
                // Create the 'Quit' button, along with a listener that hides the dialog
68
                // when the button is clicked and quits the application.
69
                final Button ok = new Button("OK", new ClickHandler() {
70
                        @Override
71
                        public void onClick(ClickEvent event) {
72
                                deleteGroup();
73
                                hide();
74
                        }
75
                });
76
                buttons.add(ok);
77
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
78
                // Create the 'Cancel' button, along with a listener that hides the
79
                // dialog
80
                // when the button is clicked.
81
                final Button cancel = new Button("Cancel", new ClickHandler() {
82
                        @Override
83
                        public void onClick(ClickEvent event) {
84
                                hide();
85
                        }
86
                });
87
                buttons.add(cancel);
88
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
89
                buttons.setSpacing(8);
90
                buttons.setStyleName("gss-warnMessage");
91
                outer.setStyleName("gss-warnMessage");
92
                outer.add(buttons);
93
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
94
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
95
                setWidget(outer);
96
        }
97

    
98
        /**
99
         * Generate an RPC request to delete a group.
100
         *
101
         * @param userId the ID of the current user
102
         */
103
        private void deleteGroup() {
104
                final TreeItem group = GSS.get().getGroups().getCurrent();
105
                if (group == null) {
106
                        GSS.get().displayError("No group was selected!");
107
                        return;
108
                }
109
                DeleteCommand dg = new DeleteCommand(((GroupResource)group.getUserObject()).getUri()){
110
                        @Override
111
                        public void onComplete() {
112
                                GSS.get().getGroups().updateGroups();
113
                        }
114

    
115
                        @Override
116
                        public void onError(Throwable t) {
117
                                GWT.log("", t);
118
                                if(t instanceof RestException){
119
                                        int statusCode = ((RestException)t).getHttpStatusCode();
120
                                        if(statusCode == 405)
121
                                                GSS.get().displayError("You don't have the necessary permissions");
122
                                        else if(statusCode == 404)
123
                                                GSS.get().displayError("Group not found");
124
                                        else
125
                                                GSS.get().displayError("Unable to delete group:"+((RestException)t).getHttpStatusText());
126
                                }
127
                                else
128
                                        GSS.get().displayError("System error unable to delete group:"+t.getMessage());
129
                        }
130
                };
131
                DeferredCommand.addCommand(dg);
132
        }
133

    
134

    
135
        @Override
136
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
137
                super.onPreviewNativeEvent(preview);
138

    
139
                NativeEvent evt = preview.getNativeEvent();
140
                if (evt.getType().equals("keydown"))
141
                        // Use the popup's key preview hooks to close the dialog when either
142
                        // enter or escape is pressed.
143
                        switch (evt.getKeyCode()) {
144
                                case KeyCodes.KEY_ENTER:
145
                                        hide();
146
                                        deleteGroup();
147
                                        break;
148
                                case KeyCodes.KEY_ESCAPE:
149
                                        hide();
150
                                        break;
151
                        }
152
        }
153

    
154
}