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