Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / StatusPanel.java @ 749068ba

History | View | Annotate | Download (7.4 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import com.google.gwt.core.client.Scheduler;
38
import gr.grnet.pithos.web.client.foldertree.AccountResource;
39
import gr.grnet.pithos.web.client.rest.GetRequest;
40
import gr.grnet.pithos.web.client.rest.RestException;
41

    
42
import com.google.gwt.core.client.GWT;
43
import com.google.gwt.i18n.client.DateTimeFormat;
44
import com.google.gwt.resources.client.ClientBundle;
45
import com.google.gwt.resources.client.ImageResource;
46
import com.google.gwt.user.client.ui.AbstractImagePrototype;
47
import com.google.gwt.user.client.ui.Composite;
48
import com.google.gwt.user.client.ui.HTML;
49
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
50
import com.google.gwt.user.client.ui.HorizontalPanel;
51
import java.util.Date;
52

    
53
/**
54
 * The panel that displays a status bar with quota information.
55
 */
56
public class StatusPanel extends Composite {
57
        public static final boolean DONE = false;
58
        private HTML fileCountLabel = new HTML("");
59
        private HTML fileSizeLabel = new HTML("");
60
        private HTML quotaIcon = new HTML("");
61
        private HTML quotaLabel = new HTML("");
62
        private HTML lastLoginLabel = new HTML("");
63
        private HTML currentLoginLabel = new HTML("");
64
        private HTML currentlyShowingLabel = new HTML("");
65

    
66
        /**
67
         * An image bundle for this widget's images.
68
         */
69
        public interface Images extends ClientBundle {
70

    
71
                @Source("gr/grnet/pithos/resources/windowlist.png")
72
                ImageResource totalFiles();
73

    
74
                @Source("gr/grnet/pithos/resources/database.png")
75
                ImageResource totalSize();
76

    
77
                @Source("gr/grnet/pithos/resources/redled.png")
78
                ImageResource redSize();
79

    
80
                @Source("gr/grnet/pithos/resources/greenled.png")
81
                ImageResource greenSize();
82

    
83
                @Source("gr/grnet/pithos/resources/yellowled.png")
84
                ImageResource yellowSize();
85

    
86
                @Source("gr/grnet/pithos/resources/xclock.png")
87
                ImageResource lastLogin();                
88
        }
89

    
90
        private final Images images;
91

    
92
        /**
93
         * The constructor of the status panel.
94
         *
95
         * @param theImages the supplied images
96
         */
97
        public StatusPanel(Images theImages) {
98
                images = theImages;
99
                HorizontalPanel outer = new HorizontalPanel();
100
                outer.setWidth("100%");
101
                outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
102

    
103
                HorizontalPanel left = new HorizontalPanel();
104
                left.setSpacing(8);
105
                HorizontalPanel middle = new HorizontalPanel();
106
                middle.setSpacing(8);
107
                HorizontalPanel right = new HorizontalPanel();
108
                right.setSpacing(8);
109
                outer.add(left);
110
                outer.add(middle);
111
                outer.add(right);
112
                left.add(new HTML("<b>Totals:</b> "));
113
                left.add(AbstractImagePrototype.create(images.totalFiles()).createImage());
114
                left.add(fileCountLabel);
115
                left.add(AbstractImagePrototype.create(images.totalSize()).createImage());
116
                left.add(fileSizeLabel);
117
                quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
118
                left.add(quotaIcon);
119
                left.add(quotaLabel);
120
                middle.add(AbstractImagePrototype.create(images.lastLogin()).createImage());
121
                middle.add(new HTML("<b>Last login:</b> "));
122
                middle.add(lastLoginLabel);
123
                middle.add(new HTML("<b>\u0387 Current session login:</b> "));
124
                middle.add(currentLoginLabel);
125
                right.add(currentlyShowingLabel);
126
                outer.setStyleName("statusbar-inner");
127
                left.setStyleName("statusbar-inner");
128
                middle.setStyleName("statusbar-inner");
129
                right.setStyleName("statusbar-inner");
130
                outer.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_RIGHT);
131

    
132
                initWidget(outer);
133
        }
134

    
135
        /**
136
         * Refresh the widget with the provided statistics.
137
         */
138
        public void displayStats(AccountResource account) {
139
                if (account.getNumberOfObjects() == 1)
140
                        fileCountLabel.setHTML("1 object");
141
                else
142
                        fileCountLabel.setHTML(account.getNumberOfObjects() + " objects");
143
                fileSizeLabel.setHTML(account.getFileSizeAsString() + " used");
144
                long pc = (long) ((double) account.getBytesRemaining()/(account.getBytesRemaining() + account.getBytesUsed()) + 0.5);
145
                if (pc < 10) {
146
                        quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
147
                        quotaLabel.setHTML(account.getQuotaLeftAsString() + " free");
148
                } else if(pc<20) {
149
                        quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
150
                        quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
151
                } else {
152
                        quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
153
                        quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
154
                }
155
                final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
156
        Date login = account.getLastLogin();
157
                lastLoginLabel.setHTML(login == null ? "" : formatter.format(login));
158

    
159
        login = account.getCurrentLogin();
160
                currentLoginLabel.setHTML(login == null ? "" : formatter.format(login));
161
        }
162

    
163
        /**
164
         * Requests updated quota information from the server and refreshes
165
         * the display.
166
         */
167
    //TODO: This should not be done here
168
        public void updateStats() {
169
                final Pithos app = Pithos.get();
170
        String path = app.getApiPath() + app.getUsername();
171
        GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, path) {
172
            @Override
173
            public void onSuccess(AccountResource result) {
174
                displayStats(result);
175
            }
176

    
177
            @Override
178
            public void onError(Throwable t) {
179
                if(t instanceof RestException)
180
                    app.displayError("Unable to fetch quota:" +
181
                                ((RestException)t).getHttpStatusText());
182
                else
183
                    app.displayError("System error fetching quota:" +
184
                                t.getMessage());
185
                GWT.log("ERR", t);
186
            }
187
        };
188
        getAccount.setHeader("X-Auth-Token", app.getToken());
189
        Scheduler.get().scheduleDeferred(getAccount);
190
        }
191

    
192
        /**
193
         * Displays the statistics for the current folder.
194
         *
195
         * @param text the statistics to display
196
         */
197
        public void updateCurrentlyShowing(String text) {
198
                if (text == null)
199
                        currentlyShowingLabel.setText("");
200
                else
201
                        currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);
202
        }
203

    
204
}