Statistics
| Branch: | Tag: | Revision:

root / gss / src / gr / ebs / gss / client / DeleteGroupDialog.java @ 895035a2

History | View | Annotate | Download (5 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.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
 * The 'delete group' dialog box.
41
 */
42
public class DeleteGroupDialog extends DialogBox {
43

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

    
58
                // Create the 'about' text and set a style name so we can style it with
59
                // CSS.
60
                final HTML text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to delete group '" + group.getName() + "'?</td></tr></table>");
61
                text.setStyleName("gss-warnMessage");
62
                outer.add(text);
63

    
64
                // Create the 'Quit' button, along with a listener that hides the dialog
65
                // when the button is clicked and quits the application.
66
                final Button ok = new Button("OK", new ClickListener() {
67

    
68
                        public void onClick(Widget sender) {
69
                                deleteGroup();
70
                                hide();
71
                        }
72
                });
73
                buttons.add(ok);
74
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
75
                // Create the 'Cancel' button, along with a listener that hides the
76
                // dialog
77
                // when the button is clicked.
78
                final Button cancel = new Button("Cancel", new ClickListener() {
79

    
80
                        public void onClick(Widget sender) {
81
                                hide();
82
                        }
83
                });
84
                buttons.add(cancel);
85
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
86
                buttons.setSpacing(8);
87
                buttons.setStyleName("gss-warnMessage");
88
                outer.setStyleName("gss-warnMessage");
89
                outer.add(buttons);
90
                outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
91
                outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
92
                setWidget(outer);
93
        }
94

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

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

    
129
        /*
130
         * (non-Javadoc)
131
         *
132
         * @see com.google.gwt.user.client.ui.PopupPanel#onKeyDownPreview(char, int)
133
         */
134
        public boolean onKeyDownPreview(final char key, final int modifiers) {
135
                // Use the popup's key preview hooks to close the dialog when either
136
                // enter or escape is pressed.
137
                switch (key) {
138
                        case KeyboardListener.KEY_ENTER:
139
                                hide();
140
                                deleteGroup();
141
                                break;
142
                        case KeyboardListener.KEY_ESCAPE:
143
                                hide();
144
                                break;
145
                }
146

    
147
                return true;
148
        }
149

    
150
}