Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / StatusPanel.java @ 0fc071d9

History | View | Annotate | Download (6.2 kB)

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