2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client;
6 import gr.grnet.pithos.web.client.rest.GetCommand;
7 import gr.grnet.pithos.web.client.rest.RestException;
8 import gr.grnet.pithos.web.client.rest.resource.QuotaHolder;
9 import gr.grnet.pithos.web.client.rest.resource.UserResource;
11 import com.google.gwt.core.client.GWT;
12 import com.google.gwt.i18n.client.DateTimeFormat;
13 import com.google.gwt.resources.client.ClientBundle;
14 import com.google.gwt.resources.client.ImageResource;
15 import com.google.gwt.user.client.DeferredCommand;
16 import com.google.gwt.user.client.IncrementalCommand;
17 import com.google.gwt.user.client.ui.AbstractImagePrototype;
18 import com.google.gwt.user.client.ui.Composite;
19 import com.google.gwt.user.client.ui.HTML;
20 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
21 import com.google.gwt.user.client.ui.HorizontalPanel;
24 * The panel that displays a status bar with quota information.
26 public class StatusPanel extends Composite {
27 public static final boolean DONE = false;
28 private HTML fileCountLabel = new HTML("");
29 private HTML fileSizeLabel = new HTML("");
30 private HTML quotaIcon = new HTML("");
31 private HTML quotaLabel = new HTML("");
32 private HTML lastLoginLabel = new HTML("");
33 private HTML currentLoginLabel = new HTML("");
34 private HTML currentlyShowingLabel = new HTML("");
37 * An image bundle for this widget's images.
39 public interface Images extends ClientBundle {
41 @Source("gr/grnet/pithos/resources/windowlist.png")
42 ImageResource totalFiles();
44 @Source("gr/grnet/pithos/resources/database.png")
45 ImageResource totalSize();
47 @Source("gr/grnet/pithos/resources/redled.png")
48 ImageResource redSize();
50 @Source("gr/grnet/pithos/resources/greenled.png")
51 ImageResource greenSize();
53 @Source("gr/grnet/pithos/resources/yellowled.png")
54 ImageResource yellowSize();
56 @Source("gr/grnet/pithos/resources/xclock.png")
57 ImageResource lastLogin();
60 private final Images images;
63 * The constructor of the status panel.
65 * @param theImages the supplied images
67 public StatusPanel(Images theImages) {
69 HorizontalPanel outer = new HorizontalPanel();
70 outer.setWidth("100%");
71 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
73 HorizontalPanel left = new HorizontalPanel();
75 HorizontalPanel middle = new HorizontalPanel();
77 HorizontalPanel right = new HorizontalPanel();
82 left.add(new HTML("<b>Totals:</b> "));
83 left.add(AbstractImagePrototype.create(images.totalFiles()).createImage());
84 left.add(fileCountLabel);
85 left.add(AbstractImagePrototype.create(images.totalSize()).createImage());
86 left.add(fileSizeLabel);
87 quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
90 middle.add(AbstractImagePrototype.create(images.lastLogin()).createImage());
91 middle.add(new HTML("<b>Last login:</b> "));
92 middle.add(lastLoginLabel);
93 middle.add(new HTML("<b>\u0387 Current session login:</b> "));
94 middle.add(currentLoginLabel);
95 right.add(currentlyShowingLabel);
96 outer.setStyleName("statusbar-inner");
97 left.setStyleName("statusbar-inner");
98 middle.setStyleName("statusbar-inner");
99 right.setStyleName("statusbar-inner");
100 outer.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_RIGHT);
104 // Initialize and display the quota information.
105 DeferredCommand.addCommand(new IncrementalCommand() {
107 public boolean execute() {
109 UserResource user = app.getCurrentUserResource();
110 if (user == null || app.getTreeView().getMyFolders() == null)
119 * Refresh the widget with the provided statistics.
121 private void displayStats(UserResource user) {
122 QuotaHolder stats = user.getQuota();
123 if (stats.getFileCount() == 1)
124 fileCountLabel.setHTML("1 file");
126 fileCountLabel.setHTML(stats.getFileCount() + " files");
127 fileSizeLabel.setHTML(stats.getFileSizeAsString() + " used");
128 long pc = stats.percentOfFreeSpace();
130 quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
131 quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
133 quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
134 quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
136 quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
137 quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
139 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
140 lastLoginLabel.setHTML(formatter.format(user.getLastLogin()));
141 currentLoginLabel.setHTML(formatter.format(user.getCurrentLogin()));
145 * Requests updated quota information from the server and refreshes
148 public void updateStats() {
149 final GSS app = GSS.get();
150 UserResource user = app.getCurrentUserResource();
151 GetCommand<UserResource> uc = new GetCommand<UserResource>(UserResource.class, user.getUri(), null){
153 public void onComplete() {
154 displayStats(getResult());
158 public void onError(Throwable t) {
159 if(t instanceof RestException)
160 app.displayError("Unable to fetch quota:" +
161 ((RestException)t).getHttpStatusText());
163 app.displayError("System error fetching quota:" +
168 DeferredCommand.addCommand(uc);
172 * Displays the statistics for the current folder.
174 * @param text the statistics to display
176 public void updateCurrentlyShowing(String text) {
178 currentlyShowingLabel.setText("");
180 currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);