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