Hardcoded pithos site url
[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 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;
10
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;
22
23 /**
24  * The panel that displays a status bar with quota information.
25  */
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("");
35
36         /**
37          * An image bundle for this widget's images.
38          */
39         public interface Images extends ClientBundle {
40
41                 @Source("gr/grnet/pithos/resources/windowlist.png")
42                 ImageResource totalFiles();
43
44                 @Source("gr/grnet/pithos/resources/database.png")
45                 ImageResource totalSize();
46
47                 @Source("gr/grnet/pithos/resources/redled.png")
48                 ImageResource redSize();
49
50                 @Source("gr/grnet/pithos/resources/greenled.png")
51                 ImageResource greenSize();
52
53                 @Source("gr/grnet/pithos/resources/yellowled.png")
54                 ImageResource yellowSize();
55
56                 @Source("gr/grnet/pithos/resources/xclock.png")
57                 ImageResource lastLogin();              
58         }
59
60         private final Images images;
61
62         /**
63          * The constructor of the status panel.
64          *
65          * @param theImages the supplied images
66          */
67         public StatusPanel(Images theImages) {
68                 images = theImages;
69                 HorizontalPanel outer = new HorizontalPanel();
70                 outer.setWidth("100%");
71                 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
72
73                 HorizontalPanel left = new HorizontalPanel();
74                 left.setSpacing(8);
75                 HorizontalPanel middle = new HorizontalPanel();
76                 middle.setSpacing(8);
77                 HorizontalPanel right = new HorizontalPanel();
78                 right.setSpacing(8);
79                 outer.add(left);
80                 outer.add(middle);
81                 outer.add(right);
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());
88                 left.add(quotaIcon);
89                 left.add(quotaLabel);
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);
101
102                 initWidget(outer);
103
104                 // Initialize and display the quota information.
105                 DeferredCommand.addCommand(new IncrementalCommand() {
106                         @Override
107                         public boolean execute() {
108                                 GSS app = GSS.get();
109                                 UserResource user = app.getCurrentUserResource();
110                                 if (user == null || app.getTreeView().getMyFolders() == null)
111                                         return !DONE;
112                                 displayStats(user);
113                                 return DONE;
114                         }
115                 });
116         }
117
118         /**
119          * Refresh the widget with the provided statistics.
120          */
121         private void displayStats(UserResource user) {
122                 QuotaHolder stats = user.getQuota();
123                 if (stats.getFileCount() == 1)
124                         fileCountLabel.setHTML("1 file");
125                 else
126                         fileCountLabel.setHTML(stats.getFileCount() + " files");
127                 fileSizeLabel.setHTML(stats.getFileSizeAsString() + " used");
128                 long pc = stats.percentOfFreeSpace();
129                 if(pc<10) {
130                         quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
131                         quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
132                 } else if(pc<20) {
133                         quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
134                         quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
135                 } else {
136                         quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
137                         quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
138                 }
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()));
142         }
143
144         /**
145          * Requests updated quota information from the server and refreshes
146          * the display.
147          */
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){
152                         @Override
153                         public void onComplete() {
154                                 displayStats(getResult());
155                         }
156
157                         @Override
158                         public void onError(Throwable t) {
159                                 if(t instanceof RestException)
160                                         app.displayError("Unable to fetch quota:" +
161                                                                 ((RestException)t).getHttpStatusText());
162                                 else
163                                         app.displayError("System error fetching quota:" +
164                                                                 t.getMessage());
165                                 GWT.log("ERR", t);
166                         }
167                 };
168                 DeferredCommand.addCommand(uc);
169         }
170
171         /**
172          * Displays the statistics for the current folder.
173          *
174          * @param text the statistics to display
175          */
176         public void updateCurrentlyShowing(String text) {
177                 if (text == null)
178                         currentlyShowingLabel.setText("");
179                 else
180                         currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);
181         }
182
183 }