Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / StatusPanel.java @ 783db80b

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