Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / StatusPanel.java @ 1ac430a1

History | View | Annotate | Download (6.1 kB)

1
/*
2
 * Copyright (c) 2011 Greek Research and Technology Network
3
 */
4
package gr.grnet.pithos.web.client;
5

    
6
import com.google.gwt.core.client.Scheduler;
7
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
8
import gr.grnet.pithos.web.client.foldertree.AccountResource;
9
import gr.grnet.pithos.web.client.rest.GetCommand;
10
import gr.grnet.pithos.web.client.rest.GetRequest;
11
import gr.grnet.pithos.web.client.rest.RestException;
12
import gr.grnet.pithos.web.client.rest.resource.QuotaHolder;
13
import gr.grnet.pithos.web.client.rest.resource.UserResource;
14

    
15
import com.google.gwt.core.client.GWT;
16
import com.google.gwt.i18n.client.DateTimeFormat;
17
import com.google.gwt.resources.client.ClientBundle;
18
import com.google.gwt.resources.client.ImageResource;
19
import com.google.gwt.user.client.ui.AbstractImagePrototype;
20
import com.google.gwt.user.client.ui.Composite;
21
import com.google.gwt.user.client.ui.HTML;
22
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
23
import com.google.gwt.user.client.ui.HorizontalPanel;
24
import java.util.Date;
25

    
26
/**
27
 * The panel that displays a status bar with quota information.
28
 */
29
public class StatusPanel extends Composite {
30
        public static final boolean DONE = false;
31
        private HTML fileCountLabel = new HTML("");
32
        private HTML fileSizeLabel = new HTML("");
33
        private HTML quotaIcon = new HTML("");
34
        private HTML quotaLabel = new HTML("");
35
        private HTML lastLoginLabel = new HTML("");
36
        private HTML currentLoginLabel = new HTML("");
37
        private HTML currentlyShowingLabel = new HTML("");
38

    
39
        /**
40
         * An image bundle for this widget's images.
41
         */
42
        public interface Images extends ClientBundle {
43

    
44
                @Source("gr/grnet/pithos/resources/windowlist.png")
45
                ImageResource totalFiles();
46

    
47
                @Source("gr/grnet/pithos/resources/database.png")
48
                ImageResource totalSize();
49

    
50
                @Source("gr/grnet/pithos/resources/redled.png")
51
                ImageResource redSize();
52

    
53
                @Source("gr/grnet/pithos/resources/greenled.png")
54
                ImageResource greenSize();
55

    
56
                @Source("gr/grnet/pithos/resources/yellowled.png")
57
                ImageResource yellowSize();
58

    
59
                @Source("gr/grnet/pithos/resources/xclock.png")
60
                ImageResource lastLogin();                
61
        }
62

    
63
        private final Images images;
64

    
65
        /**
66
         * The constructor of the status panel.
67
         *
68
         * @param theImages the supplied images
69
         */
70
        public StatusPanel(Images theImages) {
71
                images = theImages;
72
                HorizontalPanel outer = new HorizontalPanel();
73
                outer.setWidth("100%");
74
                outer.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
75

    
76
                HorizontalPanel left = new HorizontalPanel();
77
                left.setSpacing(8);
78
                HorizontalPanel middle = new HorizontalPanel();
79
                middle.setSpacing(8);
80
                HorizontalPanel right = new HorizontalPanel();
81
                right.setSpacing(8);
82
                outer.add(left);
83
                outer.add(middle);
84
                outer.add(right);
85
                left.add(new HTML("<b>Totals:</b> "));
86
                left.add(AbstractImagePrototype.create(images.totalFiles()).createImage());
87
                left.add(fileCountLabel);
88
                left.add(AbstractImagePrototype.create(images.totalSize()).createImage());
89
                left.add(fileSizeLabel);
90
                quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
91
                left.add(quotaIcon);
92
                left.add(quotaLabel);
93
                middle.add(AbstractImagePrototype.create(images.lastLogin()).createImage());
94
                middle.add(new HTML("<b>Last login:</b> "));
95
                middle.add(lastLoginLabel);
96
                middle.add(new HTML("<b>\u0387 Current session login:</b> "));
97
                middle.add(currentLoginLabel);
98
                right.add(currentlyShowingLabel);
99
                outer.setStyleName("statusbar-inner");
100
                left.setStyleName("statusbar-inner");
101
                middle.setStyleName("statusbar-inner");
102
                right.setStyleName("statusbar-inner");
103
                outer.setCellHorizontalAlignment(right, HasHorizontalAlignment.ALIGN_RIGHT);
104

    
105
                initWidget(outer);
106
        }
107

    
108
        /**
109
         * Refresh the widget with the provided statistics.
110
         */
111
        public void displayStats(AccountResource account) {
112
                if (account.getNumberOfObjects() == 1)
113
                        fileCountLabel.setHTML("1 object");
114
                else
115
                        fileCountLabel.setHTML(account.getNumberOfObjects() + " objects");
116
                fileSizeLabel.setHTML(account.getFileSizeAsString() + " used");
117
                long pc = (long) ((double) account.getBytesRemaining()/(account.getBytesRemaining() + account.getBytesUsed()) + 0.5);
118
                if (pc < 10) {
119
                        quotaIcon.setHTML(AbstractImagePrototype.create(images.redSize()).getHTML());
120
                        quotaLabel.setHTML(account.getQuotaLeftAsString() + " free");
121
                } else if(pc<20) {
122
                        quotaIcon.setHTML(AbstractImagePrototype.create(images.yellowSize()).getHTML());
123
                        quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
124
                } else {
125
                        quotaIcon.setHTML(AbstractImagePrototype.create(images.greenSize()).getHTML());
126
                        quotaLabel.setHTML(account.getQuotaLeftAsString() +" free");
127
                }
128
                final DateTimeFormat formatter = DateTimeFormat.getFormat("d/M/yyyy h:mm a");
129
        Date login = account.getLastLogin();
130
                lastLoginLabel.setHTML(login == null ? "" : formatter.format(login));
131

    
132
        login = account.getCurrentLogin();
133
                currentLoginLabel.setHTML(login == null ? "" : formatter.format(login));
134
        }
135

    
136
        /**
137
         * Requests updated quota information from the server and refreshes
138
         * the display.
139
         */
140
    //TODO: This should not be done here
141
        public void updateStats() {
142
                final GSS app = GSS.get();
143
        String path = app.getApiPath() + app.getUsername();
144
        GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, path) {
145
            @Override
146
            public void onSuccess(AccountResource result) {
147
                displayStats(result);
148
            }
149

    
150
            @Override
151
            public void onError(Throwable t) {
152
                if(t instanceof RestException)
153
                    app.displayError("Unable to fetch quota:" +
154
                                ((RestException)t).getHttpStatusText());
155
                else
156
                    app.displayError("System error fetching quota:" +
157
                                t.getMessage());
158
                GWT.log("ERR", t);
159
            }
160
        };
161

    
162
        Scheduler.get().scheduleDeferred(getAccount);
163
        }
164

    
165
        /**
166
         * Displays the statistics for the current folder.
167
         *
168
         * @param text the statistics to display
169
         */
170
        public void updateCurrentlyShowing(String text) {
171
                if (text == null)
172
                        currentlyShowingLabel.setText("");
173
                else
174
                        currentlyShowingLabel.setHTML(" <b>Showing:</b> " + text);
175
        }
176

    
177
}