Implemented ability to send error data along with user feedback in case of error
[pithos-web-client] / src / gr / grnet / pithos / web / client / TopPanel.java
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.GetRequest;
39 import gr.grnet.pithos.web.client.rest.RestException;
40
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.core.client.Scheduler;
43 import com.google.gwt.http.client.Response;
44 import com.google.gwt.resources.client.ImageResource;
45 import com.google.gwt.safehtml.shared.SafeHtml;
46 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
47 import com.google.gwt.safehtml.shared.SafeHtmlUtils;
48 import com.google.gwt.user.client.Command;
49 import com.google.gwt.user.client.Cookies;
50 import com.google.gwt.user.client.ui.AbstractImagePrototype;
51 import com.google.gwt.user.client.ui.Composite;
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.HasVerticalAlignment;
55 import com.google.gwt.user.client.ui.HorizontalPanel;
56 import com.google.gwt.user.client.ui.Image;
57 import com.google.gwt.user.client.ui.MenuBar;
58 import com.google.gwt.user.client.ui.MenuItem;
59
60 /**
61  * The top panel, which contains the menu bar icons and the user name.
62  */
63 public class TopPanel extends Composite {
64
65         /**
66          * A constant that denotes the completion of an IncrementalCommand.
67          */
68         public static final boolean DONE = false;
69
70     Pithos app;
71
72         /**
73          * An image bundle for this widgets images.
74          */
75         public interface Images extends FilePropertiesDialog.Images {
76
77                 @Source("gr/grnet/pithos/resources/pithos2-logo.png")
78                 ImageResource pithosLogo();
79                 
80                 @Source("gr/grnet/pithos/resources/desc.png")
81                 ImageResource downArrow();
82         }
83
84         /**
85          * The constructor for the top panel.
86          *
87          * @param images the supplied images
88          */
89         public TopPanel(Pithos _app, final Images images) {
90         this.app = _app;
91                 HorizontalPanel outer = new HorizontalPanel();
92                 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
93
94                 outer.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
95                 outer.setStyleName("pithos-topPanel");
96
97                 HorizontalPanel inner = new HorizontalPanel();
98                 inner.setWidth("75%");
99                 inner.setVerticalAlignment(HasVerticalAlignment.ALIGN_BOTTOM);
100                 
101                 HTML logos = new HTML("<table><tr><td><a href='/'>" + AbstractImagePrototype.create(images.pithosLogo()).getHTML() + "</a></td></tr></table>");
102                 logos.addStyleName("pithos-logo");
103                 inner.add(logos);
104
105         MenuBar username = new MenuBar();
106         username.setStyleName("pithos-usernameMenu");
107         
108         MenuBar userItemMenu = new MenuBar(true);
109         userItemMenu.addStyleName("pithos-userItemMenu");
110         userItemMenu.addItem(new MenuItem("invite friends...", new Command() {
111                         
112                         @Override
113                         public void execute() {
114                                 GetRequest<Invitations> getInvitations = new GetRequest<Invitations> (Invitations.class, "/im/", "", "invite?format=json") {
115
116                                         @Override
117                                         public void onSuccess(Invitations _result) {
118                                                 new InvitationsDialog(app, _result).center();
119                                         }
120
121                                         @Override
122                                         public void onError(Throwable t) {
123                                                 GWT.log("", t);
124                                                 app.setError(t);
125                             if (t instanceof RestException)
126                                 app.displayError("Error getting invitations: " + ((RestException) t).getHttpStatusText());
127                             else
128                                 app.displayError("System error getting invitations: " + t.getMessage());
129                                         }
130
131                                         @Override
132                                         protected void onUnauthorized(Response response) {
133                                                 app.sessionExpired();
134                                         }
135                                 };
136                                 getInvitations.setHeader("X-Auth-Token", app.getToken());
137                                 Scheduler.get().scheduleDeferred(getInvitations);
138                         }
139                 }));
140         userItemMenu.addItem(new MenuItem("send feedback...", new Command() {
141                         
142                         @Override
143                         public void execute() {
144                                 new FeedbackDialog(app, "").center();
145                         }
146                 }));
147         userItemMenu.addItem(new MenuItem("API access", new Command() {
148                         
149                         @Override
150                         public void execute() {
151                                 new CredentialsDialog(app, images).center();
152                         }
153                 }));
154         userItemMenu.addItem(new MenuItem("logout", new Command() {
155                         
156                         @Override
157                         public void execute() {
158                                 app.logoff();
159                         }
160                 }));
161
162         MenuItem userItem = new MenuItem(_app.getUsername(), userItemMenu);
163         userItem.addStyleName("pithos-usernameMenuItem");
164         username.addItem(userItem);
165         username.addSeparator();
166         
167         MenuItem langItem = new MenuItem("en", (Command) null);
168         langItem.addStyleName("pithos-langMenuItem");
169         username.addItem(langItem);
170         
171         inner.add(username);
172         inner.setCellHorizontalAlignment(username, HasHorizontalAlignment.ALIGN_RIGHT);
173         
174         outer.add(inner);
175         outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
176         outer.setCellVerticalAlignment(inner, HasVerticalAlignment.ALIGN_BOTTOM);
177                 initWidget(outer);
178         }
179 }