2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client;
6 import com.google.gwt.core.client.Scheduler;
7 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
8 import gr.grnet.pithos.web.client.foldertree.AccountResource;
9 import gr.grnet.pithos.web.client.rest.GetCommand;
10 import gr.grnet.pithos.web.client.rest.GetRequest;
11 import gr.grnet.pithos.web.client.rest.RestException;
12 import gr.grnet.pithos.web.client.rest.resource.QuotaHolder;
13 import gr.grnet.pithos.web.client.rest.resource.UserResource;
15 import com.google.gwt.core.client.GWT;
16 import com.google.gwt.i18n.client.DateTimeFormat;
17 import com.google.gwt.resources.client.ClientBundle;
18 import com.google.gwt.resources.client.ImageResource;
19 import com.google.gwt.user.client.ui.AbstractImagePrototype;
20 import com.google.gwt.user.client.ui.Composite;
21 import com.google.gwt.user.client.ui.HTML;
22 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
23 import com.google.gwt.user.client.ui.HorizontalPanel;
26 * The panel that displays a status bar with quota information.
28 public class StatusPanel extends Composite {
29 public static final boolean DONE = false;
30 private HTML fileCountLabel = new HTML("");
31 private HTML fileSizeLabel = new HTML("");
32 private HTML quotaIcon = new HTML("");
33 private HTML quotaLabel = new HTML("");
34 private HTML lastLoginLabel = new HTML("");
35 private HTML currentLoginLabel = new HTML("");
36 private HTML currentlyShowingLabel = new HTML("");
39 * An image bundle for this widget's images.
41 public interface Images extends ClientBundle {
43 @Source("gr/grnet/pithos/resources/windowlist.png")
44 ImageResource totalFiles();
46 @Source("gr/grnet/pithos/resources/database.png")
47 ImageResource totalSize();
49 @Source("gr/grnet/pithos/resources/redled.png")
50 ImageResource redSize();
52 @Source("gr/grnet/pithos/resources/greenled.png")
53 ImageResource greenSize();
55 @Source("gr/grnet/pithos/resources/yellowled.png")
56 ImageResource yellowSize();
58 @Source("gr/grnet/pithos/resources/xclock.png")
59 ImageResource lastLogin();
62 private final Images images;
65 * The constructor of the status panel.
67 * @param theImages the supplied images
69 public StatusPanel(Images theImages) {
71 HorizontalPanel outer = new HorizontalPanel();
72 outer.setWidth("100%");
73 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
75 HorizontalPanel left = new HorizontalPanel();
77 HorizontalPanel middle = new HorizontalPanel();
79 HorizontalPanel right = new HorizontalPanel();
84 left.add(new HTML("<b>Totals:</b> "));
85 left.add(AbstractImagePrototype.create(images.totalFiles()).createImage());
86 left.add(fileCountLabel);
87 left.add(AbstractImagePrototype.create(images.totalSize()).createImage());
88 left.add(fileSizeLabel);
89 quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
92 middle.add(AbstractImagePrototype.create(images.lastLogin()).createImage());
93 middle.add(new HTML("<b>Last login:</b> "));
94 middle.add(lastLoginLabel);
95 middle.add(new HTML("<b>\u0387 Current session login:</b> "));
96 middle.add(currentLoginLabel);
97 right.add(currentlyShowingLabel);
98 outer.setStyleName("statusbar-inner");
99 left.setStyleName("statusbar-inner");
100 middle.setStyleName("statusbar-inner");
101 right.setStyleName("statusbar-inner");
102 outer.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_RIGHT);
106 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
108 public void execute() {
109 AccountResource account = GSS.get().getAccount();
110 displayStats(account);
116 * Refresh the widget with the provided statistics.
118 private void displayStats(AccountResource account) {
119 if (account.getNumberOfObjects() == 1)
120 fileCountLabel.setHTML("1 object");
122 fileCountLabel.setHTML(account.getNumberOfObjects() + " objects");
123 fileSizeLabel.setHTML(account.getFileSizeAsString() + " used");
124 long pc = (long) ((double) account.getBytesRemaining()/(account.getBytesRemaining() + account.getBytesUsed()) + 0.5);
126 quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
127 quotaLabel.setHTML(account.getQuotaLeftAsString() + " free");
129 quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
130 quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
132 quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
133 quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
135 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
136 lastLoginLabel.setHTML(formatter.format(account.getLastLogin()));
137 currentLoginLabel.setHTML(formatter.format(account.getCurrentLogin()));
141 * Requests updated quota information from the server and refreshes
144 public void updateStats() {
145 final GSS app = GSS.get();
146 String path = app.getApiPath() + app.getUsername();
147 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, path) {
149 public void onSuccess(AccountResource result) {
150 displayStats(result);
154 public void onError(Throwable t) {
155 if(t instanceof RestException)
156 app.displayError("Unable to fetch quota:" +
157 ((RestException)t).getHttpStatusText());
159 app.displayError("System error fetching quota:" +
165 Scheduler.get().scheduleDeferred(getAccount);
169 * Displays the statistics for the current folder.
171 * @param text the statistics to display
173 public void updateCurrentlyShowing(String text) {
175 currentlyShowingLabel.setText("");
177 currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);