Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.7 kB)

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.rest.PostCommand;
22
import gr.ebs.gss.client.rest.RestException;
23

    
24
import com.google.gwt.core.client.GWT;
25
import com.google.gwt.dom.client.NativeEvent;
26
import com.google.gwt.event.dom.client.ClickEvent;
27
import com.google.gwt.event.dom.client.ClickHandler;
28
import com.google.gwt.event.dom.client.KeyCodes;
29
import com.google.gwt.http.client.URL;
30
import com.google.gwt.user.client.DeferredCommand;
31
import com.google.gwt.user.client.Event.NativePreviewEvent;
32
import com.google.gwt.user.client.ui.Button;
33
import com.google.gwt.user.client.ui.DialogBox;
34
import com.google.gwt.user.client.ui.Grid;
35
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
36
import com.google.gwt.user.client.ui.HorizontalPanel;
37
import com.google.gwt.user.client.ui.TextBox;
38
import com.google.gwt.user.client.ui.VerticalPanel;
39

    
40
/**
41
 * @author kman
42
 */
43
public class GroupPropertiesDialog extends DialogBox {
44

    
45
        /**
46
         * The widget that holds the folderName of the folder.
47
         */
48
        private TextBox groupName = new TextBox();
49

    
50
        /**
51
         * A flag that denotes whether the dialog will be used to create or modify a
52
         * folder.
53
         */
54
        private final boolean create;
55

    
56
        /**
57
         * The widget's constructor.
58
         *
59
         * @param _create true if the dialog is displayed for creating a new
60
         *            sub-folder of the selected folder, false if it is displayed
61
         *            for modifying the selected folder
62
         */
63
        public GroupPropertiesDialog(final boolean _create) {
64
                setAnimationEnabled(true);
65
                create = _create;
66
                // Use this opportunity to set the dialog's caption.
67
                if (create)
68
                        setText("Create Group");
69
                else
70
                        setText("Group properties");
71
                final VerticalPanel panel = new VerticalPanel();
72
                setWidget(panel);
73
                final Grid generalTable = new Grid(1, 2);
74
                generalTable.setText(0, 0, "Group Name");
75
                generalTable.setWidget(0, 1, groupName);
76
                generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
77
                generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
78
                generalTable.setCellSpacing(4);
79

    
80
                panel.add(generalTable);
81
                final HorizontalPanel buttons = new HorizontalPanel();
82
                final Button ok = new Button("OK", new ClickHandler() {
83
                        @Override
84
                        public void onClick(ClickEvent event) {
85
                                createGroup(groupName.getText());
86
                                hide();
87
                        }
88
                });
89
                buttons.add(ok);
90
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
91
                // Create the 'Cancel' button, along with a listener that hides the
92
                // dialog
93
                // when the button is clicked.
94
                final Button cancel = new Button("Cancel", new ClickHandler() {
95
                        @Override
96
                        public void onClick(ClickEvent event) {
97
                                hide();
98
                        }
99
                });
100
                buttons.add(cancel);
101
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
102
                buttons.setSpacing(8);
103
                buttons.addStyleName("gss-TabPanelBottom");
104
                panel.add(buttons);
105
                panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
106
                //panel.addStyleName("gss-DialogBox");
107
                panel.addStyleName("gss-TabPanelBottom");                
108
        }
109
        @Override
110
        public void center() {
111
                super.center();
112
                groupName.setFocus(true);
113
        }
114

    
115
        @Override
116
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
117
                super.onPreviewNativeEvent(preview);
118

    
119
                NativeEvent evt = preview.getNativeEvent();
120
                if (evt.getType().equals("keydown"))
121
                        // Use the popup's key preview hooks to close the dialog when either
122
                        // enter or escape is pressed.
123
                        switch (evt.getKeyCode()) {
124
                                case KeyCodes.KEY_ENTER:
125
                                        hide();
126
                                        createGroup( groupName.getText());
127
                                        break;
128
                                case KeyCodes.KEY_ESCAPE:
129
                                        hide();
130
                                        break;
131
                        }
132
        }
133

    
134

    
135
        /**
136
         * Generate an RPC request to create a new group.
137
         *
138
         * @param userId the ID of the user whose namespace will be searched for
139
         *            groups
140
         * @param aGroupName the name of the group to create
141
         */
142
        private void createGroup(String aGroupName) {
143

    
144
                if (aGroupName == null || aGroupName.length() == 0) {
145
                        GSS.get().displayError("Empty group name!");
146
                        return;
147
                }
148
                GWT.log("createGroup(" + aGroupName + ")", null);
149
                PostCommand cg = new PostCommand(GSS.get().getCurrentUserResource().getGroupsPath()+"?name="+URL.encodeComponent(aGroupName), "", 201){
150

    
151
                        @Override
152
                        public void onComplete() {
153
                                GSS.get().getGroups().updateGroups();
154
                                GSS.get().showUserList();
155
                        }
156

    
157
                        @Override
158
                        public void onError(Throwable t) {
159
                                GWT.log("", t);
160
                                if(t instanceof RestException){
161
                                        int statusCode = ((RestException)t).getHttpStatusCode();
162
                                        if(statusCode == 405)
163
                                                GSS.get().displayError("You don't have the necessary permissions");
164
                                        else if(statusCode == 404)
165
                                                GSS.get().displayError("Resource does not exist");
166
                                        else if(statusCode == 409)
167
                                                GSS.get().displayError("A group with the same name already exists");
168
                                        else if(statusCode == 413)
169
                                                GSS.get().displayError("Your quota has been exceeded");
170
                                        else
171
                                                GSS.get().displayError("Unable to create group:"+((RestException)t).getHttpStatusText());
172
                                }
173
                                else
174
                                        GSS.get().displayError("System error creating group:"+t.getMessage());
175
                        }
176
                };
177
                DeferredCommand.addCommand(cg);
178

    
179
        }
180
}