Decorative changes related to Issue 64
[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 currentLoginLabel = new HTML("");
49         private HTML currentlyShowingLabel = new HTML("");
50
51         /**
52          * An image bundle for this widget's images.
53          */
54         public interface Images extends ClientBundle {
55
56                 @Source("gr/ebs/gss/resources/windowlist.png")
57                 ImageResource totalFiles();
58
59                 @Source("gr/ebs/gss/resources/database.png")
60                 ImageResource totalSize();
61
62                 @Source("gr/ebs/gss/resources/redled.png")
63                 ImageResource redSize();
64
65                 @Source("gr/ebs/gss/resources/greenled.png")
66                 ImageResource greenSize();
67
68                 @Source("gr/ebs/gss/resources/yellowled.png")
69                 ImageResource yellowSize();
70
71                 @Source("gr/ebs/gss/resources/xclock.png")
72                 ImageResource lastLogin();              
73         }
74
75         private final Images images;
76
77         /**
78          * The constructor of the status panel.
79          *
80          * @param theImages the supplied images
81          */
82         public StatusPanel(Images theImages) {
83                 images = theImages;
84                 HorizontalPanel outer = new HorizontalPanel();
85                 outer.setWidth("100%");
86                 outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
87
88                 HorizontalPanel left = new HorizontalPanel();
89                 left.setSpacing(8);
90                 HorizontalPanel middle = new HorizontalPanel();
91                 middle.setSpacing(8);
92                 HorizontalPanel right = new HorizontalPanel();
93                 right.setSpacing(8);
94                 outer.add(left);
95                 outer.add(middle);
96                 outer.add(right);
97                 left.add(new HTML("<b>Totals:</b> "));
98                 left.add(AbstractImagePrototype.create(images.totalFiles()).createImage());
99                 left.add(fileCountLabel);
100                 left.add(AbstractImagePrototype.create(images.totalSize()).createImage());
101                 left.add(fileSizeLabel);
102                 quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
103                 left.add(quotaIcon);
104                 left.add(quotaLabel);
105                 middle.add(AbstractImagePrototype.create(images.lastLogin()).createImage());
106                 middle.add(new HTML("<b>Last login:</b> "));
107                 middle.add(lastLoginLabel);
108                 middle.add(new HTML("<b>\u0387 Current session login:</b> "));
109                 middle.add(currentLoginLabel);
110                 right.add(currentlyShowingLabel);
111                 outer.setStyleName("statusbar-inner");
112                 left.setStyleName("statusbar-inner");
113                 middle.setStyleName("statusbar-inner");
114                 right.setStyleName("statusbar-inner");
115                 outer.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_RIGHT);
116
117                 initWidget(outer);
118
119                 // Initialize and display the quota information.
120                 DeferredCommand.addCommand(new IncrementalCommand() {
121                         @Override
122                         public boolean execute() {
123                                 GSS app = GSS.get();
124                                 UserResource user = app.getCurrentUserResource();
125                                 if (user == null || app.getFolders().getRootItem() == null)
126                                         return !DONE;
127                                 displayStats(user);
128                                 return DONE;
129                         }
130                 });
131         }
132
133         /**
134          * Refresh the widget with the provided statistics.
135          */
136         private void displayStats(UserResource user) {
137                 QuotaHolder stats = user.getQuota();
138                 if (stats.getFileCount() == 1)
139                         fileCountLabel.setHTML("1 file");
140                 else
141                         fileCountLabel.setHTML(stats.getFileCount() + " files");
142                 fileSizeLabel.setHTML(stats.getFileSizeAsString() + " used");
143                 long pc = stats.percentOfFreeSpace();
144                 if(pc<10) {
145                         quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
146                         quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
147                 } else if(pc<20) {
148                         quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
149                         quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
150                 } else {
151                         quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
152                         quotaLabel.setHTML(stats.getQuotaLeftAsString() +" free");
153                 }
154                 final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
155                 lastLoginLabel.setHTML(formatter.format(user.getLastLogin()));
156                 currentLoginLabel.setHTML(formatter.format(user.getCurrentLogin()));
157         }
158
159         /**
160          * Requests updated quota information from the server and refreshes
161          * the display.
162          */
163         public void updateStats() {
164                 final GSS app = GSS.get();
165                 UserResource user = app.getCurrentUserResource();
166                 GetCommand<UserResource> uc = new GetCommand<UserResource>(UserResource.class, user.getUri(), null){
167                         @Override
168                         public void onComplete() {
169                                 displayStats(getResult());
170                         }
171
172                         @Override
173                         public void onError(Throwable t) {
174                                 if(t instanceof RestException)
175                                         app.displayError("Unable to fetch quota:" +
176                                                                 ((RestException)t).getHttpStatusText());
177                                 else
178                                         app.displayError("System error fetching quota:" +
179                                                                 t.getMessage());
180                                 GWT.log("ERR", t);
181                         }
182                 };
183                 DeferredCommand.addCommand(uc);
184         }
185
186         /**
187          * Displays the statistics for the current folder.
188          *
189          * @param text the statistics to display
190          */
191         public void updateCurrentlyShowing(String text) {
192                 if (text == null)
193                         currentlyShowingLabel.setText("");
194                 else
195                         currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);
196         }
197
198 }