- Add an administration application.
[pithos] / src / gr / ebs / gss / client / StatusPanel.java
1 /*
2  * Copyright 2007, 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client;
20
21 import gr.ebs.gss.client.rest.GetCommand;
22 import gr.ebs.gss.client.rest.RestException;
23 import gr.ebs.gss.client.rest.resource.QuotaHolder;
24 import gr.ebs.gss.client.rest.resource.UserResource;
25
26 import com.google.gwt.core.client.GWT;
27 import com.google.gwt.i18n.client.DateTimeFormat;
28 import com.google.gwt.resources.client.ClientBundle;
29 import com.google.gwt.resources.client.ImageResource;
30 import com.google.gwt.user.client.DeferredCommand;
31 import com.google.gwt.user.client.IncrementalCommand;
32 import com.google.gwt.user.client.ui.AbstractImagePrototype;
33 import com.google.gwt.user.client.ui.Composite;
34 import com.google.gwt.user.client.ui.HTML;
35 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
36 import com.google.gwt.user.client.ui.HorizontalPanel;
37
38 /**
39  * The panel that displays a status bar with quota information.
40  */
41 public class StatusPanel extends Composite {
42         public static final boolean DONE = false;
43         private HTML fileCountLabel = new HTML("");
44         private HTML fileSizeLabel = new HTML("");
45         private HTML quotaIcon = new HTML("");
46         private HTML quotaLabel = new HTML("");
47         private HTML lastLoginLabel = new HTML("");
48         private HTML currentlyShowingLabel = new HTML("");
49
50         /**
51          * An image bundle for this widget's images.
52          */
53         public interface Images extends ClientBundle {
54
55                 @Source("gr/ebs/gss/resources/windowlist.png")
56                 ImageResource totalFiles();
57
58                 @Source("gr/ebs/gss/resources/database.png")
59                 ImageResource totalSize();
60
61                 @Source("gr/ebs/gss/resources/redled.png")
62                 ImageResource redSize();
63
64                 @Source("gr/ebs/gss/resources/greenled.png")
65                 ImageResource greenSize();
66
67                 @Source("gr/ebs/gss/resources/yellowled.png")
68                 ImageResource yellowSize();
69
70                 @Source("gr/ebs/gss/resources/xclock.png")
71                 ImageResource lastLogin();
72         }
73
74         private final Images images;
75
76         /**
77          * The constructor of the status panel.
78          *
79          * @param theImages the supplied images
80          */
81         public StatusPanel(Images theImages) {
82                 images = theImages;
83                 HorizontalPanel outer = new HorizontalPanel();
84                 outer.setWidth("100%");
85                 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
86
87                 HorizontalPanel left = new HorizontalPanel();
88                 left.setSpacing(8);
89                 HorizontalPanel middle = new HorizontalPanel();
90                 middle.setSpacing(8);
91                 HorizontalPanel right = new HorizontalPanel();
92                 right.setSpacing(8);
93                 outer.add(left);
94                 outer.add(middle);
95                 outer.add(right);
96                 left.add(new HTML("<b>Totals:</b> "));
97                 left.add(AbstractImagePrototype.create(images.totalFiles()).createImage());
98                 left.add(fileCountLabel);
99                 left.add(AbstractImagePrototype.create(images.totalSize()).createImage());
100                 left.add(fileSizeLabel);
101                 quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
102                 left.add(quotaIcon);
103                 left.add(quotaLabel);
104                 middle.add(AbstractImagePrototype.create(images.lastLogin()).createImage());
105                 middle.add(new HTML("<b>Last login:</b> "));
106                 middle.add(lastLoginLabel);
107                 right.add(currentlyShowingLabel);
108                 outer.setStyleName("statusbar-inner");
109                 left.setStyleName("statusbar-inner");
110                 middle.setStyleName("statusbar-inner");
111                 right.setStyleName("statusbar-inner");
112                 outer.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_RIGHT);
113
114                 initWidget(outer);
115
116                 // Initialize and display the quota information.
117                 DeferredCommand.addCommand(new IncrementalCommand() {
118                         @Override
119                         public boolean execute() {
120                                 GSS app = GSS.get();
121                                 UserResource user = app.getCurrentUserResource();
122                                 if (user == null || app.getFolders().getRootItem() == null)
123                                         return !DONE;
124                                 displayStats(user);
125                                 return DONE;
126                         }
127                 });
128         }
129
130         /**
131          * Refresh the widget with the provided statistics.
132          */
133         private void displayStats(UserResource user) {
134                 QuotaHolder stats = user.getQuota();
135                 if (stats.getFileCount() == 1)
136                         fileCountLabel.setHTML("1 file");
137                 else
138                         fileCountLabel.setHTML(stats.getFileCount() + " files");
139                 fileSizeLabel.setHTML(stats.getFileSizeAsString() + " used");
140                 long pc = stats.percentOfFreeSpace();
141                 if(pc<10) {
142                         quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
143                         quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
144                 } else if(pc<20) {
145                         quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
146                         quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
147                 } else {
148                         quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
149                         quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
150                 }
151                 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
152                 lastLoginLabel.setHTML(formatter.format(user.getLastLogin()));
153         }
154
155         /**
156          * Requests updated quota information from the server and refreshes
157          * the display.
158          */
159         public void updateStats() {
160                 final GSS app = GSS.get();
161                 UserResource user = app.getCurrentUserResource();
162                 GetCommand<UserResource> uc = new GetCommand<UserResource>(UserResource.class, user.getUri(), null){
163                         @Override
164                         public void onComplete() {
165                                 displayStats(getResult());
166                         }
167
168                         @Override
169                         public void onError(Throwable t) {
170                                 if(t instanceof RestException)
171                                         app.displayError("Unable to fetch quota:" +
172                                                                 ((RestException)t).getHttpStatusText());
173                                 else
174                                         app.displayError("System error fetching quota:" +
175                                                                 t.getMessage());
176                                 GWT.log("ERR", t);
177                         }
178                 };
179                 DeferredCommand.addCommand(uc);
180         }
181
182         /**
183          * Displays the statistics for the current folder.
184          *
185          * @param text the statistics to display
186          */
187         public void updateCurrentlyShowing(String text) {
188                 if (text == null)
189                         currentlyShowingLabel.setText("");
190                 else
191                         currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);
192         }
193
194 }