1d667b58620d72026bfbbb2027dbac512b0dcfbf
[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>" + AbstractImagePrototype.create(images.pithosLogo()).getHTML() + "</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                             if (t instanceof RestException)
125                                 app.displayError("Error getting invitations: " + ((RestException) t).getHttpStatusText());
126                             else
127                                 app.displayError("System error getting invitations: " + t.getMessage());
128                                         }
129
130                                         @Override
131                                         protected void onUnauthorized(Response response) {
132                                                 app.sessionExpired();
133                                         }
134                                 };
135                                 getInvitations.setHeader("X-Auth-Token", app.getToken());
136                                 Scheduler.get().scheduleDeferred(getInvitations);
137                         }
138                 }));
139         userItemMenu.addItem(new MenuItem("send feedback...", new Command() {
140                         
141                         @Override
142                         public void execute() {
143                                 new FeedbackDialog().center();
144                         }
145                 }));
146         userItemMenu.addItem(new MenuItem("API access", new Command() {
147                         
148                         @Override
149                         public void execute() {
150                                 new CredentialsDialog(app, images).center();
151                         }
152                 }));
153         userItemMenu.addItem(new MenuItem("logout", new Command() {
154                         
155                         @Override
156                         public void execute() {
157                                 app.logoff();
158                         }
159                 }));
160
161         MenuItem userItem = new MenuItem(_app.getUsername(), userItemMenu);
162         userItem.addStyleName("pithos-usernameMenuItem");
163         username.addItem(userItem);
164         username.addSeparator();
165         
166         MenuItem langItem = new MenuItem("en", (Command) null);
167         langItem.addStyleName("pithos-langMenuItem");
168         username.addItem(langItem);
169         
170         inner.add(username);
171         inner.setCellHorizontalAlignment(username, HasHorizontalAlignment.ALIGN_RIGHT);
172         
173         outer.add(inner);
174         outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
175         outer.setCellVerticalAlignment(inner, HasVerticalAlignment.ALIGN_BOTTOM);
176                 initWidget(outer);
177         }
178 }