Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / AddUserDialog.java @ e1218f0f

History | View | Annotate | Download (7.1 kB)

1
/*
2
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.foldertree.Resource;
38
import gr.grnet.pithos.web.client.grouptree.Group;
39
import gr.grnet.pithos.web.client.rest.PostRequest;
40
import gr.grnet.pithos.web.client.rest.RestException;
41

    
42
import com.google.gwt.core.client.GWT;
43
import com.google.gwt.core.client.Scheduler;
44
import com.google.gwt.dom.client.NativeEvent;
45
import com.google.gwt.event.dom.client.ClickEvent;
46
import com.google.gwt.event.dom.client.ClickHandler;
47
import com.google.gwt.event.dom.client.KeyCodes;
48
import com.google.gwt.event.dom.client.KeyDownEvent;
49
import com.google.gwt.http.client.Response;
50
import com.google.gwt.http.client.URL;
51
import com.google.gwt.regexp.shared.RegExp;
52
import com.google.gwt.user.client.Event.NativePreviewEvent;
53
import com.google.gwt.user.client.ui.Anchor;
54
import com.google.gwt.user.client.ui.Button;
55
import com.google.gwt.user.client.ui.DialogBox;
56
import com.google.gwt.user.client.ui.FlexTable;
57
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
58
import com.google.gwt.user.client.ui.TextBox;
59
import com.google.gwt.user.client.ui.VerticalPanel;
60

    
61
/**
62
 * The 'Folder properties' dialog box implementation.
63
 */
64
public class AddUserDialog extends DialogBox {
65

    
66
    protected Pithos app;
67

    
68
    Group group;
69
        /**
70
         * The widget that holds the folderName of the folder.
71
         */
72
        TextBox userName = new TextBox();
73

    
74
        final VerticalPanel inner;
75

    
76
        /**
77
         * The widget's constructor.
78
         */
79
        public AddUserDialog(final Pithos app, Group _group) {
80
        this.app = app;
81
        this.group = _group;
82
        
83
                Anchor close = new Anchor("close");
84
                close.addStyleName("close");
85
                close.addClickHandler(new ClickHandler() {
86
                        
87
                        @Override
88
                        public void onClick(ClickEvent event) {
89
                                hide();
90
                        }
91
                });
92

    
93
                setAnimationEnabled(true);
94
                setGlassEnabled(true);
95
                setStyleName("pithos-DialogBox");
96

    
97
                // Enable IE selection for the dialog (must disable it upon closing it)
98
                Pithos.enableIESelection();
99

    
100
                // Use this opportunity to set the dialog's caption.
101
                setText("Add user");
102

    
103
                // Outer contains inner and buttons
104
                VerticalPanel outer = new VerticalPanel();
105
                outer.add(close);
106
                // Inner contains generalPanel and permPanel
107
                inner = new VerticalPanel();
108
                inner.addStyleName("inner");
109

    
110
                VerticalPanel generalPanel = new VerticalPanel();
111
        FlexTable generalTable = new FlexTable();
112
        generalTable.setText(0, 0, "Username");
113

    
114
        generalTable.setWidget(0, 1, userName);
115

    
116
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
117
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
118
        generalTable.setCellSpacing(4);
119
        generalPanel.add(generalTable);
120
        inner.add(generalPanel);
121

    
122
        outer.add(inner);
123

    
124
                // Create the 'Create/Update' button, along with a listener that hides the dialog
125
                // when the button is clicked and quits the application.
126
                String okLabel = "Create";
127
                final Button ok = new Button(okLabel, new ClickHandler() {
128
                        @Override
129
                        public void onClick(ClickEvent event) {
130
                                addUser();
131
                                closeDialog();
132
                        }
133
                });
134
                ok.addStyleName("button");
135
                outer.add(ok);
136
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
137

    
138
        setWidget(outer);
139
        }
140

    
141
        @Override
142
        public void center() {
143
                super.center();
144
                userName.setFocus(true);
145
        }
146

    
147
        @Override
148
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
149
                super.onPreviewNativeEvent(preview);
150

    
151
                NativeEvent evt = preview.getNativeEvent();
152
                if (evt.getType().equals(KeyDownEvent.getType().getName()))
153
                        // Use the popup's key preview hooks to close the dialog when either
154
                        // enter or escape is pressed.
155
                        switch (evt.getKeyCode()) {
156
                                case KeyCodes.KEY_ENTER:
157
                                        addUser();
158
                    closeDialog();
159
                                        break;
160
                                case KeyCodes.KEY_ESCAPE:
161
                                        closeDialog();
162
                                        break;
163
                        }
164
        }
165

    
166

    
167
        /**
168
         * Enables IE selection prevention and hides the dialog
169
         * (we disable the prevention on creation of the dialog)
170
         */
171
        public void closeDialog() {
172
                Pithos.preventIESelection();
173
                hide();
174
        }
175

    
176
        /**
177
         * Generate an RPC request to create a new folder.
178
         */
179
        void addUser() {
180
                String name = userName.getText().trim();
181
                if (name.length() == 0)
182
                        return;
183
                RegExp emailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i");
184
                if (!emailValidator.test(name)) {
185
                        app.displayWarning("Username must be a valid email address");
186
                        return;
187
                }
188
                        
189
            group.addMember(name);
190
            String path = "?update=";
191
            PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUsername(), path) {
192
                        
193
                        @Override
194
                        public void onSuccess(Resource result) {
195
                                app.updateGroupNode(group);
196
                        }
197
                        
198
                        @Override
199
                        public void onError(Throwable t) {
200
                                GWT.log("", t);
201
                                app.setError(t);
202
                                if (t instanceof RestException) {
203
                                        app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText());
204
                                }
205
                                else 
206
                                        app.displayError("System error updating group:" + t.getMessage());
207
                        }
208

    
209
                        @Override
210
                        protected void onUnauthorized(Response response) {
211
                                app.sessionExpired();
212
                        }
213
                };
214
                updateGroup.setHeader("X-Auth-Token", app.getToken());
215
                String groupMembers = "";
216
                for (String u : group.getMembers())
217
                        groupMembers += (URL.encodePathSegment(u) + ",");
218
                updateGroup.setHeader("X-Account-Group-" + URL.encodePathSegment(group.getName()), groupMembers);
219
                Scheduler.get().scheduleDeferred(updateGroup);
220
        }
221
}