Use an exponential backoff strategy for retrying rolled back transactions.
[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.user.client.DeferredCommand;\r
28 import com.google.gwt.user.client.IncrementalCommand;\r
29 import com.google.gwt.user.client.ui.AbstractImagePrototype;\r
30 import com.google.gwt.user.client.ui.Composite;\r
31 import com.google.gwt.user.client.ui.HTML;\r
32 import com.google.gwt.user.client.ui.HasHorizontalAlignment;\r
33 import com.google.gwt.user.client.ui.HorizontalPanel;\r
34 import com.google.gwt.user.client.ui.ImageBundle;\r
35 \r
36 /**\r
37  * The panel that displays a status bar with quota information.\r
38  */\r
39 public class StatusPanel extends Composite {\r
40         public static final boolean DONE = false;\r
41         private HTML fileCountLabel;\r
42         private HTML fileSizeLabel;\r
43         private HTML quotaLabel;\r
44         private HTML currentlyShowingLabel;\r
45 \r
46         /**\r
47          * An image bundle for this widget's images.\r
48          */\r
49         public interface Images extends ImageBundle {\r
50 \r
51                 @Resource("gr/ebs/gss/resources/windowlist.png")\r
52                 AbstractImagePrototype totalFiles();\r
53 \r
54                 @Resource("gr/ebs/gss/resources/database.png")\r
55                 AbstractImagePrototype totalSize();\r
56 \r
57                 @Resource("gr/ebs/gss/resources/redled.png")\r
58                 AbstractImagePrototype freeSize();\r
59 \r
60                 @Resource("gr/ebs/gss/resources/greenled.png")\r
61                 AbstractImagePrototype greenSize();\r
62 \r
63                 @Resource("gr/ebs/gss/resources/yellowled.png")\r
64                 AbstractImagePrototype yellowSize();\r
65         }\r
66 \r
67         private final Images images;\r
68 \r
69         /**\r
70          * The constructor of the status panel.\r
71          *\r
72          * @param theImages the supplied images\r
73          */\r
74         public StatusPanel(Images theImages) {\r
75                 images = theImages;\r
76                 HorizontalPanel outer = new HorizontalPanel();\r
77                 outer.setWidth("100%");\r
78                 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);\r
79 \r
80                 HorizontalPanel left = new HorizontalPanel();\r
81                 left.setSpacing(8);\r
82                 HorizontalPanel right = new HorizontalPanel();\r
83                 right.setSpacing(8);\r
84                 outer.add(left);\r
85                 outer.add(right);\r
86                 left.add(new HTML("<b>Totals:</b> "));\r
87                 left.add(images.totalFiles().createImage());\r
88                 left.add(fileCountLabel = new HTML(""));\r
89                 left.add(images.totalSize().createImage());\r
90                 left.add(fileSizeLabel = new HTML(""));\r
91                 left.add(quotaLabel = new HTML(""));\r
92                 right.add(currentlyShowingLabel = new HTML(""));\r
93                 outer.setStyleName("statusbar-inner");\r
94                 left.setStyleName("statusbar-inner");\r
95                 right.setStyleName("statusbar-inner");\r
96                 outer.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_RIGHT);\r
97 \r
98                 initWidget(outer);\r
99 \r
100                 // Initialize and display the quota information.\r
101                 DeferredCommand.addCommand(new IncrementalCommand() {\r
102                         public boolean execute() {\r
103                                 GSS app = GSS.get();\r
104                                 UserResource user = app.getCurrentUserResource();\r
105                                 if (user == null || app.getFolders().getRootItem() == null)\r
106                                         return !DONE;\r
107                                 displayStats(user.getQuota());\r
108                                 return DONE;\r
109                         }\r
110                 });\r
111         }\r
112 \r
113         /**\r
114          * Refresh the widget with the provided statistics.\r
115          */\r
116         private void displayStats(QuotaHolder stats) {\r
117                 if (stats.getFileCount() == 1)\r
118                         fileCountLabel.setHTML("1 file");\r
119                 else\r
120                         fileCountLabel.setHTML(stats.getFileCount() + " files");\r
121                 fileSizeLabel.setHTML(stats.getFileSizeAsString() + " used");\r
122                 long pc = stats.percentOfFreeSpace();\r
123                 if(pc<10)\r
124                         quotaLabel.setHTML(images.freeSize().getHTML()+"&nbsp;"+stats.getQuotaLeftAsString() +" free");\r
125                 else if(pc<20)\r
126                         quotaLabel.setHTML(images.yellowSize().getHTML()+"&nbsp;"+stats.getQuotaLeftAsString() +" free");\r
127                 else\r
128                         quotaLabel.setHTML(images.greenSize().getHTML()+"&nbsp;"+stats.getQuotaLeftAsString() +" free");\r
129         }\r
130 \r
131         /**\r
132          * Requests updated quota information from the server and refreshes\r
133          * the display.\r
134          */\r
135         public void updateStats() {\r
136                 final GSS app = GSS.get();\r
137                 UserResource user = app.getCurrentUserResource();\r
138                 GetCommand<UserResource> uc = new GetCommand<UserResource>(UserResource.class, user.getUri()){\r
139                         @Override\r
140                         public void onComplete() {\r
141                                 displayStats(getResult().getQuota());\r
142                         }\r
143 \r
144                         @Override\r
145                         public void onError(Throwable t) {\r
146                                 if(t instanceof RestException)\r
147                                         app.displayError("Unable to fetch quota:"+((RestException)t).getHttpStatusText());\r
148                                 else\r
149                                         app.displayError("System error fetching quota:"+t.getMessage());\r
150                                 GWT.log("ERR", t);\r
151                         }\r
152                 };\r
153                 DeferredCommand.addCommand(uc);\r
154         }\r
155 \r
156         /**\r
157          * Displays the statistics for the current folder.\r
158          *\r
159          * @param text the statistics to display\r
160          */\r
161         public void updateCurrentlyShowing(String text) {\r
162                 if (text == null)\r
163                         currentlyShowingLabel.setText("");\r
164                 else\r
165                         currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);\r
166         }\r
167 \r
168 }\r