Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / InvitationsDialog.java @ 2ba08401

History | View | Annotate | Download (10.3 kB)

1
/*
2
 * Copyright 2011 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.rest.PostRequest;
39

    
40
import com.google.gwt.core.client.GWT;
41
import com.google.gwt.core.client.Scheduler;
42
import com.google.gwt.dom.client.NativeEvent;
43
import com.google.gwt.event.dom.client.ClickEvent;
44
import com.google.gwt.event.dom.client.ClickHandler;
45
import com.google.gwt.event.dom.client.KeyCodes;
46
import com.google.gwt.http.client.Response;
47
import com.google.gwt.user.client.Event.NativePreviewEvent;
48
import com.google.gwt.user.client.ui.Anchor;
49
import com.google.gwt.user.client.ui.Button;
50
import com.google.gwt.user.client.ui.DialogBox;
51
import com.google.gwt.user.client.ui.FlexTable;
52
import com.google.gwt.user.client.ui.HTML;
53
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
54
import com.google.gwt.user.client.ui.HorizontalPanel;
55
import com.google.gwt.user.client.ui.Image;
56
import com.google.gwt.user.client.ui.TextBox;
57
import com.google.gwt.user.client.ui.VerticalPanel;
58
import com.google.gwt.user.client.ui.Widget;
59

    
60

    
61
/**
62
 * A dialog box that displays info about invitations
63
 */
64
public class InvitationsDialog extends DialogBox {
65
        private static final int INV_PER_PAGE = 5;
66
        
67
        Pithos app;
68
        VerticalPanel messagesPanel;
69
        int rows = 0;
70
        FlexTable sentInvitationsTable;        
71
        /**
72
         * The current displayed page of sent invitations 
73
         */
74
        int currentPage = 0;
75
        HorizontalPanel pagerPanel;        
76
        /**
77
         * The widget constructor.
78
         */
79
        public InvitationsDialog(Pithos _app, final Invitations inv) {
80
                this.app = _app;
81
                Anchor close = new Anchor();
82
                close.addStyleName("close");
83
                close.addClickHandler(new ClickHandler() {
84
                        
85
                        @Override
86
                        public void onClick(ClickEvent event) {
87
                                hide();
88
                        }
89
                });
90
                // Set the dialog's caption.
91
                setText("Invite friends");
92
                setAnimationEnabled(true);
93
                setGlassEnabled(true);
94
                setStyleName("pithos-DialogBox");
95
                VerticalPanel outer = new VerticalPanel();
96
                outer.add(close);
97

    
98
                VerticalPanel inner = new VerticalPanel();
99
                inner.addStyleName("inner");
100
                // Create the text and set a style name so we can style it with CSS.
101
                HTML text = new HTML("You have <span class='pithos-leftInvitationsNumber'>" + inv.getInvitationsLeft() + "</span> invitations left");
102
                text.addStyleName("pithos-invitationsLeft");
103
                inner.add(text);
104
                
105
                HorizontalPanel split = new HorizontalPanel();
106

    
107
                VerticalPanel left = new VerticalPanel();
108
                left.addStyleName("pithos-sendInvitationsPanel");
109
                left.setVisible(inv.getInvitationsLeft() > 0);
110

    
111
                HorizontalPanel sendLabelPanel = new HorizontalPanel();
112
                HTML sendLabel = new HTML("Send new invitations");
113
                sendLabel.addStyleName("pithos-sendInvitationsTitle");
114
                sendLabelPanel.add(sendLabel);
115
                Image plus = new Image("images/plus.png");
116
                plus.addStyleName("pithos-addInvitationImg");
117
                sendLabelPanel.add(plus);
118
                left.add(sendLabelPanel);
119

    
120
                messagesPanel = new VerticalPanel();
121
                messagesPanel.setSpacing(5);
122
                left.add(messagesPanel);
123
                
124
                final FlexTable table = new FlexTable();
125
                left.add(table);
126
                plus.addClickHandler(new ClickHandler() {
127
                        
128
                        @Override
129
                        public void onClick(ClickEvent event) {
130
                                if (rows == 0) {
131
                                        table.setHTML(0, 0, "Name <span class='eg'>e.g. John Smith</span>");
132
                                        table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
133
                                        table.setText(0, 1, "Email");
134
                                        table.getFlexCellFormatter().setStyleName(0, 1, "props-labels");
135
                                        rows++;
136
                                }
137
                                table.setWidget(rows, 0, new TextBox());
138
                                table.getFlexCellFormatter().setStyleName(1, 0, "props-values");
139

    
140
                                table.setWidget(rows, 1, new TextBox());
141
                                table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
142
                                
143
                                Image delete = new Image("images/delete.png");
144
                                delete.addStyleName("pithos-invitationDeleteImg");
145
                                delete.addClickHandler(new ClickHandler() {
146
                                        
147
                                        @Override
148
                                        public void onClick(ClickEvent event) {
149
                                                table.removeRow(table.getCellForEvent(event).getRowIndex());
150
                                        }
151
                                });
152
                                table.setWidget(rows, 2, delete);
153
                                
154
                                if (rows > 1)
155
                                        table.getRowFormatter().addStyleName(rows, "pithos-invitationFormRow");
156
                                
157
                                rows++;
158
                        }
159
                });
160

    
161
                Button send = new Button("send invitations", new ClickHandler() {
162
                        @Override
163
                        public void onClick(ClickEvent event) {
164
                                int rowCount = table.getRowCount();
165
                                
166
                                for (int i = 1; i<rowCount; i++) {
167
                                        String name = ((TextBox) table.getWidget(i, 0)).getText().trim();
168
                                        String email = ((TextBox) table.getWidget(i, 1)).getText().trim();
169
                                        sendInvitation(email, name);
170
                                }
171
                        }
172
                });
173
                send.addStyleName("pithos-sendInvitationButton");
174
                send.setVisible(inv.getInvitationsLeft() > 0);
175
                left.add(send);
176
                
177
                split.add(left);
178
                
179
                VerticalPanel right = new VerticalPanel();
180
                right.addStyleName("pithos-sentInvitationsPanel");
181
                
182
                HTML sentLabel = new HTML("Invitations sent");
183
                sentLabel.addStyleName("pithos-sentInvitationsTitle");
184
                right.add(sentLabel);
185
                
186
                sentInvitationsTable = new FlexTable();
187
                fillSentInvitationsTable(inv);
188
                right.add(sentInvitationsTable);
189
                
190
                pagerPanel = new HorizontalPanel();
191
                pagerPanel.setSpacing(5);
192
                Button prev = new Button("Prev");
193
                prev.addStyleName("pithos-pagerButton");
194
                prev.addClickHandler(new ClickHandler() {
195
                        
196
                        @Override
197
                        public void onClick(ClickEvent event) {
198
                                if (currentPage > 0) {
199
                                        pagerPanel.getWidget(currentPage + 1).removeStyleName("pithos-pagerButtonCurrent");
200
                                        currentPage--;
201
                                        fillSentInvitationsTable(inv);
202
                                        pagerPanel.getWidget(currentPage + 1).addStyleName("pithos-pagerButtonCurrent");
203
                                }
204
                        }
205
                });
206
                pagerPanel.add(prev);
207
                Button next = new Button("Next");
208
                next.addStyleName("pithos-pagerButton");
209
                next.addClickHandler(new ClickHandler() {
210
                        
211
                        @Override
212
                        public void onClick(ClickEvent event) {
213
                                int numOfInvs = inv.getSentInvitations().size();
214
                                int numOfPages = numOfInvs / INV_PER_PAGE + (numOfInvs % INV_PER_PAGE == 0 ? 0 : 1);
215
                                if (currentPage < numOfPages - 1) {
216
                                        pagerPanel.getWidget(currentPage + 1).removeStyleName("pithos-pagerButtonCurrent");
217
                                        currentPage++;
218
                                        pagerPanel.getWidget(currentPage + 1).addStyleName("pithos-pagerButtonCurrent");
219
                                        fillSentInvitationsTable(inv);
220
                                }
221
                        }
222
                });
223
                pagerPanel.add(next);
224
                right.add(pagerPanel);
225
                updatePagerPanel(inv);
226
                
227
                split.add(right);
228
                
229
                inner.add(split);
230
                
231
                outer.add(inner);
232
                outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
233
                setWidget(outer);
234
        }
235

    
236
        void sendInvitation(String email, final String realname) {
237
                PostRequest sendInvitation = new PostRequest("/im/", "", "invite", "uniq=" + email + "&realname=" + realname) {
238
                        
239
                        @Override
240
                        protected void onUnauthorized(Response response) {
241
                                app.sessionExpired();
242
                        }
243
                        
244
                        @Override
245
                        public void onSuccess(Resource result) {
246
                                HTML msg = new HTML("Invitation to <span class='user'>" + realname + "</span> was sent.");
247
                                msg.addStyleName("pithos-invitationResponse");
248
                                messagesPanel.add(msg);
249
                                
250
                        }
251
                        
252
                        @Override
253
                        public void onError(Throwable t) {
254
                                GWT.log("", t);
255
                        }
256
                };
257
                sendInvitation.setHeader("X-Auth-Token", app.getToken());
258
                Scheduler.get().scheduleDeferred(sendInvitation);
259
        }
260
        
261
        void fillSentInvitationsTable(Invitations inv) {
262
                sentInvitationsTable.removeAllRows();
263
                int row = 0;
264
                for (int j=currentPage * INV_PER_PAGE + 0; j<inv.getSentInvitations().size() && j<(currentPage + 1)* INV_PER_PAGE; j++) {
265
                        final Invitation i = inv.getSentInvitations().get(j);
266
                        sentInvitationsTable.setText(row, 0, i.getRealname());
267
                        sentInvitationsTable.getFlexCellFormatter().addStyleName(row, 0, "pithos-invitedEmail");
268
                        if (i.isAccepted())
269
                                sentInvitationsTable.setWidget(row, 1, new Image("images/invitation_accepted.png"));
270
                        else {
271
                                Image img = new Image("images/resend.png");
272
                                img.addStyleName("pithos-resendInvitation");
273
                                img.addClickHandler(new ClickHandler() {
274
                                        
275
                                        @Override
276
                                        public void onClick(ClickEvent event) {
277
                                                sendInvitation(i.getEmail(), i.getRealname());
278
                                        }
279
                                });
280
                                sentInvitationsTable.setWidget(row, 1, img);
281
                        }
282
                        sentInvitationsTable.getFlexCellFormatter().setRowSpan(row, 1, 2);
283
                        row++;
284
                        sentInvitationsTable.setText(row, 0, i.getEmail());
285
                        row++;
286
                }
287
        }
288
        
289
        void updatePagerPanel(final Invitations inv) {
290
                int numOfInvitations = inv.getSentInvitations().size();
291
                int numOfPages = numOfInvitations / INV_PER_PAGE + (numOfInvitations % INV_PER_PAGE == 0 ? 0 : 1);
292
                for (int i=0; i<numOfPages; i++) {
293
                        final Button page = new Button(String.valueOf(i + 1));
294
                        page.addStyleName("pithos-pagerButton");
295
                        if (i == currentPage)
296
                                page.addStyleName("pithos-pagerButtonCurrent");
297
                                
298
                        final int j = i;
299
                        page.addClickHandler(new ClickHandler() {
300
                                
301
                                @Override
302
                                public void onClick(ClickEvent event) {
303
                                        pagerPanel.getWidget(currentPage + 1).removeStyleName("pithos-pagerButtonCurrent");
304
                                        currentPage = j;
305
                                        fillSentInvitationsTable(inv);
306
                                        page.addStyleName("pithos-pagerButtonCurrent");
307
                                }
308
                        });
309
                        pagerPanel.insert(page, i + 1);
310
                }
311
        }
312
}