Finally displayed virtual folder hierarchy up to second level
[pithos-web-client] / src / gr / grnet / pithos / web / client / StatusPanel.java
1 /*
2  * Copyright (c) 2011 Greek Research and Technology Network
3  */
4 package gr.grnet.pithos.web.client;
5
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;
14
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;
24
25 /**
26  * The panel that displays a status bar with quota information.
27  */
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("");
37
38         /**
39          * An image bundle for this widget's images.
40          */
41         public interface Images extends ClientBundle {
42
43                 @Source("gr/grnet/pithos/resources/windowlist.png")
44                 ImageResource totalFiles();
45
46                 @Source("gr/grnet/pithos/resources/database.png")
47                 ImageResource totalSize();
48
49                 @Source("gr/grnet/pithos/resources/redled.png")
50                 ImageResource redSize();
51
52                 @Source("gr/grnet/pithos/resources/greenled.png")
53                 ImageResource greenSize();
54
55                 @Source("gr/grnet/pithos/resources/yellowled.png")
56                 ImageResource yellowSize();
57
58                 @Source("gr/grnet/pithos/resources/xclock.png")
59                 ImageResource lastLogin();              
60         }
61
62         private final Images images;
63
64         /**
65          * The constructor of the status panel.
66          *
67          * @param theImages the supplied images
68          */
69         public StatusPanel(Images theImages) {
70                 images = theImages;
71                 HorizontalPanel outer = new HorizontalPanel();
72                 outer.setWidth("100%");
73                 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
74
75                 HorizontalPanel left = new HorizontalPanel();
76                 left.setSpacing(8);
77                 HorizontalPanel middle = new HorizontalPanel();
78                 middle.setSpacing(8);
79                 HorizontalPanel right = new HorizontalPanel();
80                 right.setSpacing(8);
81                 outer.add(left);
82                 outer.add(middle);
83                 outer.add(right);
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());
90                 left.add(quotaIcon);
91                 left.add(quotaLabel);
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);
103
104                 initWidget(outer);
105
106         Scheduler.get().scheduleDeferred(new ScheduledCommand() {
107             @Override
108             public void execute() {
109                 AccountResource account = GSS.get().getAccount();
110                 displayStats(account);
111             }
112         });
113         }
114
115         /**
116          * Refresh the widget with the provided statistics.
117          */
118         private void displayStats(AccountResource account) {
119                 if (account.getNumberOfObjects() == 1)
120                         fileCountLabel.setHTML("1 object");
121                 else
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);
125                 if (pc < 10) {
126                         quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
127                         quotaLabel.setHTML(account.getQuotaLeftAsString() + " free");
128                 } else if(pc<20) {
129                         quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
130                         quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
131                 } else {
132                         quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
133                         quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
134                 }
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()));
138         }
139
140         /**
141          * Requests updated quota information from the server and refreshes
142          * the display.
143          */
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) {
148             @Override
149             public void onSuccess(AccountResource result) {
150                 displayStats(result);
151             }
152
153             @Override
154             public void onError(Throwable t) {
155                 if(t instanceof RestException)
156                     app.displayError("Unable to fetch quota:" +
157                                 ((RestException)t).getHttpStatusText());
158                 else
159                     app.displayError("System error fetching quota:" +
160                                 t.getMessage());
161                 GWT.log("ERR", t);
162             }
163         };
164
165         Scheduler.get().scheduleDeferred(getAccount);
166         }
167
168         /**
169          * Displays the statistics for the current folder.
170          *
171          * @param text the statistics to display
172          */
173         public void updateCurrentlyShowing(String text) {
174                 if (text == null)
175                         currentlyShowingLabel.setText("");
176                 else
177                         currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);
178         }
179
180 }