2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
35 package gr.grnet.pithos.web.client;
37 import com.google.gwt.core.client.Scheduler;
38 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
39 import gr.grnet.pithos.web.client.foldertree.AccountResource;
40 import gr.grnet.pithos.web.client.rest.GetCommand;
41 import gr.grnet.pithos.web.client.rest.GetRequest;
42 import gr.grnet.pithos.web.client.rest.RestException;
43 import gr.grnet.pithos.web.client.rest.resource.QuotaHolder;
44 import gr.grnet.pithos.web.client.rest.resource.UserResource;
46 import com.google.gwt.core.client.GWT;
47 import com.google.gwt.i18n.client.DateTimeFormat;
48 import com.google.gwt.resources.client.ClientBundle;
49 import com.google.gwt.resources.client.ImageResource;
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.HorizontalPanel;
55 import java.util.Date;
58 * The panel that displays a status bar with quota information.
60 public class StatusPanel extends Composite {
61 public static final boolean DONE = false;
62 private HTML fileCountLabel = new HTML("");
63 private HTML fileSizeLabel = new HTML("");
64 private HTML quotaIcon = new HTML("");
65 private HTML quotaLabel = new HTML("");
66 private HTML lastLoginLabel = new HTML("");
67 private HTML currentLoginLabel = new HTML("");
68 private HTML currentlyShowingLabel = new HTML("");
71 * An image bundle for this widget's images.
73 public interface Images extends ClientBundle {
75 @Source("gr/grnet/pithos/resources/windowlist.png")
76 ImageResource totalFiles();
78 @Source("gr/grnet/pithos/resources/database.png")
79 ImageResource totalSize();
81 @Source("gr/grnet/pithos/resources/redled.png")
82 ImageResource redSize();
84 @Source("gr/grnet/pithos/resources/greenled.png")
85 ImageResource greenSize();
87 @Source("gr/grnet/pithos/resources/yellowled.png")
88 ImageResource yellowSize();
90 @Source("gr/grnet/pithos/resources/xclock.png")
91 ImageResource lastLogin();
94 private final Images images;
97 * The constructor of the status panel.
99 * @param theImages the supplied images
101 public StatusPanel(Images theImages) {
103 HorizontalPanel outer = new HorizontalPanel();
104 outer.setWidth("100%");
105 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
107 HorizontalPanel left = new HorizontalPanel();
109 HorizontalPanel middle = new HorizontalPanel();
110 middle.setSpacing(8);
111 HorizontalPanel right = new HorizontalPanel();
116 left.add(new HTML("<b>Totals:</b> "));
117 left.add(AbstractImagePrototype.create(images.totalFiles()).createImage());
118 left.add(fileCountLabel);
119 left.add(AbstractImagePrototype.create(images.totalSize()).createImage());
120 left.add(fileSizeLabel);
121 quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
123 left.add(quotaLabel);
124 middle.add(AbstractImagePrototype.create(images.lastLogin()).createImage());
125 middle.add(new HTML("<b>Last login:</b> "));
126 middle.add(lastLoginLabel);
127 middle.add(new HTML("<b>\u0387 Current session login:</b> "));
128 middle.add(currentLoginLabel);
129 right.add(currentlyShowingLabel);
130 outer.setStyleName("statusbar-inner");
131 left.setStyleName("statusbar-inner");
132 middle.setStyleName("statusbar-inner");
133 right.setStyleName("statusbar-inner");
134 outer.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_RIGHT);
140 * Refresh the widget with the provided statistics.
142 public void displayStats(AccountResource account) {
143 if (account.getNumberOfObjects() == 1)
144 fileCountLabel.setHTML("1 object");
146 fileCountLabel.setHTML(account.getNumberOfObjects() + " objects");
147 fileSizeLabel.setHTML(account.getFileSizeAsString() + " used");
148 long pc = (long) ((double) account.getBytesRemaining()/(account.getBytesRemaining() + account.getBytesUsed()) + 0.5);
150 quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
151 quotaLabel.setHTML(account.getQuotaLeftAsString() + " free");
153 quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
154 quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
156 quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
157 quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
159 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
160 Date login = account.getLastLogin();
161 lastLoginLabel.setHTML(login == null ? "" : formatter.format(login));
163 login = account.getCurrentLogin();
164 currentLoginLabel.setHTML(login == null ? "" : formatter.format(login));
168 * Requests updated quota information from the server and refreshes
171 //TODO: This should not be done here
172 public void updateStats() {
173 final GSS app = GSS.get();
174 String path = app.getApiPath() + app.getUsername();
175 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, path) {
177 public void onSuccess(AccountResource result) {
178 displayStats(result);
182 public void onError(Throwable t) {
183 if(t instanceof RestException)
184 app.displayError("Unable to fetch quota:" +
185 ((RestException)t).getHttpStatusText());
187 app.displayError("System error fetching quota:" +
192 getAccount.setHeader("X-Auth-Token", app.getToken());
193 Scheduler.get().scheduleDeferred(getAccount);
197 * Displays the statistics for the current folder.
199 * @param text the statistics to display
201 public void updateCurrentlyShowing(String text) {
203 currentlyShowingLabel.setText("");
205 currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);