Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / client / ProgressBar.java @ a60ea262

History | View | Annotate | Download (10.6 kB)

1 14ad7326 pastith
/*
2 1348bfda Panagiotis Astithas
 * Copyright 2006 Robert Hanson <iamroberthanson AT gmail.com>
3 14ad7326 pastith
 *
4 1348bfda Panagiotis Astithas
 * Licensed under the Apache License, Version 2.0 (the "License");
5 1348bfda Panagiotis Astithas
 * you may not use this file except in compliance with the License.
6 1348bfda Panagiotis Astithas
 * You may obtain a copy of the License at
7 14ad7326 pastith
 *
8 1348bfda Panagiotis Astithas
 *    http://www.apache.org/licenses/LICENSE-2.0
9 14ad7326 pastith
 *
10 1348bfda Panagiotis Astithas
 * Unless required by applicable law or agreed to in writing, software
11 1348bfda Panagiotis Astithas
 * distributed under the License is distributed on an "AS IS" BASIS,
12 1348bfda Panagiotis Astithas
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 1348bfda Panagiotis Astithas
 * See the License for the specific language governing permissions and
14 1348bfda Panagiotis Astithas
 * limitations under the License.
15 14ad7326 pastith
 */
16 1348bfda Panagiotis Astithas
17 14ad7326 pastith
package gr.ebs.gss.client;
18 14ad7326 pastith
19 14ad7326 pastith
import com.google.gwt.user.client.ui.FlexTable;
20 14ad7326 pastith
import com.google.gwt.user.client.ui.Grid;
21 14ad7326 pastith
import com.google.gwt.user.client.ui.Label;
22 14ad7326 pastith
import com.google.gwt.user.client.ui.VerticalPanel;
23 14ad7326 pastith
24 14ad7326 pastith
/**
25 14ad7326 pastith
 * <P>
26 14ad7326 pastith
 * A simple progress bar that uses table elements to show progress and with a
27 14ad7326 pastith
 * basic time remaining calculation built in.
28 14ad7326 pastith
 * <P>
29 14ad7326 pastith
 * You can optionally display some text above the progress bar and/or display
30 14ad7326 pastith
 * time remaining underneath the progress bar. To control the display of those
31 14ad7326 pastith
 * features, set the options in the constructor as shown in the following usage
32 14ad7326 pastith
 * example:
33 14ad7326 pastith
 *
34 14ad7326 pastith
 * <PRE>
35 14ad7326 pastith
 * final ProgressBar progressBar = new ProgressBar(20, ProgressBar.SHOW_TIME_REMAINING + ProgressBar.SHOW_TEXT);
36 14ad7326 pastith
 * progressBar.setText(&quot;Doing something...&quot;);
37 14ad7326 pastith
 * RootPanel.get().add(progressBar);
38 14ad7326 pastith
 * Timer t = new Timer() {
39 14ad7326 pastith
 *
40 14ad7326 pastith
 *         public void run() {
41 14ad7326 pastith
 *                 int progress = progressBar.getProgress() + 4;
42 14ad7326 pastith
 *                 if (progress &gt; 100)
43 14ad7326 pastith
 *                         cancel();
44 14ad7326 pastith
 *                 progressBar.setProgress(progress);
45 14ad7326 pastith
 *         }
46 14ad7326 pastith
 * };
47 14ad7326 pastith
 * t.scheduleRepeating(1000);
48 14ad7326 pastith
 * </PRE>
49 14ad7326 pastith
 * <P>
50 14ad7326 pastith
 * How the time remaining is displayed can be controlled by setting the relevant
51 14ad7326 pastith
 * messages using the language of your choice.
52 14ad7326 pastith
 * <P>
53 14ad7326 pastith
 * The default setting for the messages are as follows:
54 14ad7326 pastith
 *
55 14ad7326 pastith
 * <PRE>
56 14ad7326 pastith
 * setSecondsMessage(&quot;Time remaining: {0} Seconds&quot;);
57 14ad7326 pastith
 * setMinutesMessage(&quot;Time remaining: {0} Minutes&quot;);
58 14ad7326 pastith
 * setHoursMessage(&quot;Time remaining: {0} Hours&quot;);
59 14ad7326 pastith
 * </PRE>
60 14ad7326 pastith
 * <P>
61 14ad7326 pastith
 * To reset the time remaining/set the start time, simply set the progress to
62 14ad7326 pastith
 * zero.
63 14ad7326 pastith
 * <P>
64 14ad7326 pastith
 * Some basic CSS styling is available to control the text, border around the
65 14ad7326 pastith
 * progress bar itself and the colour of the progress bar elements.
66 14ad7326 pastith
 *
67 14ad7326 pastith
 * <PRE>
68 14ad7326 pastith
 * .progressbar-text {
69 14ad7326 pastith
 *     font-weight: bold;
70 14ad7326 pastith
 * }
71 14ad7326 pastith
 * .progressbar-remaining {
72 14ad7326 pastith
 *     font-size: 12px;
73 14ad7326 pastith
 *     font-style: italic;
74 14ad7326 pastith
 * }
75 14ad7326 pastith
 * .progressbar-outer {
76 14ad7326 pastith
 *     border: 1px solid black;
77 14ad7326 pastith
 * }
78 14ad7326 pastith
 * .progressbar-inner {
79 14ad7326 pastith
 *     border: 1px solid black;
80 14ad7326 pastith
 *     margin: 1px;
81 14ad7326 pastith
 * }
82 14ad7326 pastith
 * .progressbar-bar {
83 14ad7326 pastith
 *     width: 5px;
84 14ad7326 pastith
 *     height: 15px;
85 14ad7326 pastith
 *     margin: 1px;
86 14ad7326 pastith
 * }
87 14ad7326 pastith
 * .progressbar-fullbar {
88 14ad7326 pastith
 *     background: blue;
89 14ad7326 pastith
 * }
90 14ad7326 pastith
 * .progressbar-blankbar {
91 14ad7326 pastith
 *     background: #eee;
92 14ad7326 pastith
 * }
93 14ad7326 pastith
 *</PRE>
94 14ad7326 pastith
 * <P>
95 14ad7326 pastith
 * You can take advantage of the default style by adding the following to the
96 14ad7326 pastith
 * head of your HTML page.
97 14ad7326 pastith
 * <P>
98 14ad7326 pastith
 * &lt;link rel="stylesheet" type="text/css" href="style/gwl-progressBar.css">
99 14ad7326 pastith
 * <P>
100 14ad7326 pastith
 * This style sheet also has two additional styles which you can use by adding
101 14ad7326 pastith
 * the stye name to the widget. You can use either one of these, or use both
102 14ad7326 pastith
 * combined.
103 14ad7326 pastith
 *
104 14ad7326 pastith
 * <PRE>
105 14ad7326 pastith
 * ProgressBar progressBar = new ProgressBar(20);
106 14ad7326 pastith
 * progressBar.addStyleName(&quot;progressbar-solid&quot;);
107 14ad7326 pastith
 * progressBar.addStyleName(&quot;progressbar-noborder&quot;);
108 14ad7326 pastith
 * </PRE>
109 14ad7326 pastith
 *
110 14ad7326 pastith
 * @author Bjarne Matzen - Bjarne[dot]Matzen[at]gmail[dot]com
111 14ad7326 pastith
 */
112 14ad7326 pastith
113 14ad7326 pastith
public class ProgressBar extends VerticalPanel {
114 14ad7326 pastith
115 14ad7326 pastith
        /**
116 14ad7326 pastith
         * Option to show text label above progress bar
117 14ad7326 pastith
         */
118 14ad7326 pastith
        public static final int SHOW_TEXT = 2;
119 14ad7326 pastith
120 14ad7326 pastith
        /**
121 14ad7326 pastith
         * Option to show time remaining
122 14ad7326 pastith
         */
123 14ad7326 pastith
        public static final int SHOW_TIME_REMAINING = 1;
124 14ad7326 pastith
125 14ad7326 pastith
        /**
126 14ad7326 pastith
         * The time the progress bar was started
127 14ad7326 pastith
         */
128 14ad7326 pastith
        private long startTime = System.currentTimeMillis();
129 14ad7326 pastith
130 14ad7326 pastith
        /**
131 14ad7326 pastith
         * The number of bar elements to show
132 14ad7326 pastith
         */
133 14ad7326 pastith
        private int elements = 20;
134 14ad7326 pastith
135 14ad7326 pastith
        /**
136 14ad7326 pastith
         * Time element text
137 14ad7326 pastith
         */
138 9bd245e3 fstamatelopoulos
        private String secondsMessage = "Estimated time remaining: {0} Seconds";
139 14ad7326 pastith
140 9bd245e3 fstamatelopoulos
        private String minutesMessage = "Estimated time remaining: {0} Minutes";
141 14ad7326 pastith
142 9bd245e3 fstamatelopoulos
        private String hoursMessage = "Estimated time remaining: {0} Hours";
143 14ad7326 pastith
144 14ad7326 pastith
        /**
145 14ad7326 pastith
         * Current progress (as a percentage)
146 14ad7326 pastith
         */
147 14ad7326 pastith
        private int progress = 0;
148 14ad7326 pastith
149 14ad7326 pastith
        /**
150 14ad7326 pastith
         * This is the frame around the progress bar
151 14ad7326 pastith
         */
152 14ad7326 pastith
        private FlexTable barFrame = new FlexTable();
153 14ad7326 pastith
154 14ad7326 pastith
        /**
155 14ad7326 pastith
         * This is the grid used to show the elements
156 14ad7326 pastith
         */
157 14ad7326 pastith
        private Grid elementGrid;
158 14ad7326 pastith
159 14ad7326 pastith
        /**
160 14ad7326 pastith
         * This is the current text label below the progress bar
161 14ad7326 pastith
         */
162 14ad7326 pastith
        private Label remainLabel = new Label();
163 14ad7326 pastith
164 14ad7326 pastith
        /**
165 14ad7326 pastith
         * This is the current text label above the progress bar
166 14ad7326 pastith
         */
167 14ad7326 pastith
        private Label textLabel = new Label();
168 14ad7326 pastith
169 14ad7326 pastith
        /**
170 14ad7326 pastith
         * internal flags for options
171 14ad7326 pastith
         */
172 14ad7326 pastith
        private boolean showRemaining = false;
173 14ad7326 pastith
174 14ad7326 pastith
        private boolean showText = false;
175 14ad7326 pastith
176 14ad7326 pastith
        /**
177 14ad7326 pastith
         * Base constructor for this widget
178 14ad7326 pastith
         *
179 aa07a34b Panagiotis Astithas
         * @param elementNo The number of elements (bars) to show on the progress bar
180 14ad7326 pastith
         * @param options The display options for the progress bar
181 14ad7326 pastith
         */
182 aa07a34b Panagiotis Astithas
        public ProgressBar(int elementNo, int options) {
183 14ad7326 pastith
                // Read the options and set convenience variables
184 14ad7326 pastith
                if ((options & SHOW_TIME_REMAINING) == SHOW_TIME_REMAINING)
185 14ad7326 pastith
                        showRemaining = true;
186 14ad7326 pastith
                if ((options & SHOW_TEXT) == SHOW_TEXT)
187 14ad7326 pastith
                        showText = true;
188 14ad7326 pastith
189 14ad7326 pastith
                // Set element count
190 aa07a34b Panagiotis Astithas
                elements = elementNo;
191 14ad7326 pastith
192 14ad7326 pastith
                // Styling
193 14ad7326 pastith
                remainLabel.setStyleName("progressbar-remaining");
194 14ad7326 pastith
                textLabel.setStyleName("progressbar-text");
195 14ad7326 pastith
196 14ad7326 pastith
                // Initialize the progress elements
197 aa07a34b Panagiotis Astithas
                elementGrid = new Grid(1, elementNo);
198 14ad7326 pastith
                elementGrid.setStyleName("progressbar-inner");
199 14ad7326 pastith
                elementGrid.setCellPadding(0);
200 14ad7326 pastith
                elementGrid.setCellSpacing(0);
201 14ad7326 pastith
202 aa07a34b Panagiotis Astithas
                for (int loop = 0; loop < elementNo; loop++) {
203 14ad7326 pastith
                        Grid elm = new Grid(1, 1);
204 14ad7326 pastith
                        // elm.setHTML(0, 0, "&nbsp;");
205 14ad7326 pastith
                        elm.setHTML(0, 0, "");
206 14ad7326 pastith
                        elm.setStyleName("progressbar-blankbar");
207 14ad7326 pastith
                        elm.addStyleName("progressbar-bar");
208 14ad7326 pastith
                        elementGrid.setWidget(0, loop, elm);
209 14ad7326 pastith
                }
210 14ad7326 pastith
211 14ad7326 pastith
                // Create the container around the elements
212 14ad7326 pastith
                Grid containerGrid = new Grid(1, 1);
213 14ad7326 pastith
                containerGrid.setCellPadding(0);
214 14ad7326 pastith
                containerGrid.setCellSpacing(0);
215 14ad7326 pastith
                containerGrid.setWidget(0, 0, elementGrid);
216 14ad7326 pastith
                containerGrid.setStyleName("progressbar-outer");
217 14ad7326 pastith
                // containerGrid.setBorderWidth(1);
218 14ad7326 pastith
219 14ad7326 pastith
                // Set up the surrounding flex table based on the options
220 14ad7326 pastith
                int row = 0;
221 14ad7326 pastith
                if (showText)
222 14ad7326 pastith
                        barFrame.setWidget(row++, 0, textLabel);
223 14ad7326 pastith
                barFrame.setWidget(row++, 0, containerGrid);
224 14ad7326 pastith
                if (showRemaining)
225 14ad7326 pastith
                        barFrame.setWidget(row++, 0, remainLabel);
226 14ad7326 pastith
227 14ad7326 pastith
                barFrame.setWidth("100%");
228 14ad7326 pastith
229 14ad7326 pastith
                // Add the frame to the panel
230 14ad7326 pastith
                this.add(barFrame);
231 14ad7326 pastith
232 14ad7326 pastith
                // Initialize progress bar
233 14ad7326 pastith
                setProgress(0);
234 14ad7326 pastith
        }
235 14ad7326 pastith
236 14ad7326 pastith
        /**
237 14ad7326 pastith
         * Constructor without options
238 14ad7326 pastith
         *
239 aa07a34b Panagiotis Astithas
         * @param elementNo The number of elements (bars) to show on the progress bar
240 14ad7326 pastith
         */
241 aa07a34b Panagiotis Astithas
        public ProgressBar(int elementNo) {
242 aa07a34b Panagiotis Astithas
                this(elementNo, 0);
243 14ad7326 pastith
        }
244 14ad7326 pastith
245 14ad7326 pastith
        /**
246 14ad7326 pastith
         * Set the current progress as a percentage
247 14ad7326 pastith
         *
248 14ad7326 pastith
         * @param percentage Set current percentage for the progress bar
249 14ad7326 pastith
         */
250 14ad7326 pastith
        public void setProgress(int percentage) {
251 14ad7326 pastith
                // Make sure we are error-tolerant
252 14ad7326 pastith
                if (percentage > 100)
253 14ad7326 pastith
                        percentage = 100;
254 14ad7326 pastith
                if (percentage < 0)
255 14ad7326 pastith
                        percentage = 0;
256 14ad7326 pastith
257 14ad7326 pastith
                // Set the internal variable
258 14ad7326 pastith
                progress = percentage;
259 14ad7326 pastith
260 14ad7326 pastith
                // Update the elements in the progress grid to
261 14ad7326 pastith
                // reflect the status
262 14ad7326 pastith
                int completed = elements * percentage / 100;
263 14ad7326 pastith
                for (int loop = 0; loop < elements; loop++) {
264 14ad7326 pastith
                        Grid elm = (Grid) elementGrid.getWidget(0, loop);
265 14ad7326 pastith
                        if (loop < completed) {
266 14ad7326 pastith
                                elm.setStyleName("progressbar-fullbar");
267 14ad7326 pastith
                                elm.addStyleName("progressbar-bar");
268 14ad7326 pastith
                        } else {
269 14ad7326 pastith
                                elm.setStyleName("progressbar-blankbar");
270 14ad7326 pastith
                                elm.addStyleName("progressbar-bar");
271 14ad7326 pastith
                        }
272 14ad7326 pastith
                }
273 14ad7326 pastith
274 14ad7326 pastith
                if (percentage > 0) {
275 14ad7326 pastith
                        // Calculate the new time remaining
276 14ad7326 pastith
                        long soFar = (System.currentTimeMillis() - startTime) / 1000;
277 14ad7326 pastith
                        long remaining = soFar * (100 - percentage) / percentage;
278 14ad7326 pastith
                        // Select the best UOM
279 14ad7326 pastith
                        String remainText = secondsMessage;
280 14ad7326 pastith
                        if (remaining > 120) {
281 14ad7326 pastith
                                remaining = remaining / 60;
282 14ad7326 pastith
                                remainText = minutesMessage;
283 14ad7326 pastith
                                if (remaining > 120) {
284 14ad7326 pastith
                                        remaining = remaining / 60;
285 14ad7326 pastith
                                        remainText = hoursMessage;
286 14ad7326 pastith
                                }
287 14ad7326 pastith
                        }
288 14ad7326 pastith
                        // Locate the position to insert out time remaining
289 14ad7326 pastith
                        int pos = remainText.indexOf("{0}");
290 14ad7326 pastith
                        if (pos >= 0) {
291 14ad7326 pastith
                                String trail = "";
292 14ad7326 pastith
                                if (pos + 3 < remainText.length())
293 14ad7326 pastith
                                        trail = remainText.substring(pos + 3);
294 14ad7326 pastith
                                remainText = remainText.substring(0, pos) + remaining + trail;
295 14ad7326 pastith
                        }
296 14ad7326 pastith
                        // Set the label
297 14ad7326 pastith
                        remainLabel.setText(remainText);
298 14ad7326 pastith
                } else
299 14ad7326 pastith
                        // If progress is 0, reset the start time
300 14ad7326 pastith
                        startTime = System.currentTimeMillis();
301 14ad7326 pastith
        }
302 14ad7326 pastith
303 14ad7326 pastith
        /**
304 14ad7326 pastith
         * Get the current progress as a percentage
305 14ad7326 pastith
         *
306 14ad7326 pastith
         * @return Current percentage for the progress bar
307 14ad7326 pastith
         */
308 14ad7326 pastith
        public int getProgress() {
309 14ad7326 pastith
                return progress;
310 14ad7326 pastith
        }
311 14ad7326 pastith
312 14ad7326 pastith
        /**
313 14ad7326 pastith
         * Get the text displayed above the progress bar
314 14ad7326 pastith
         *
315 14ad7326 pastith
         * @return the text
316 14ad7326 pastith
         */
317 14ad7326 pastith
        public String getText() {
318 14ad7326 pastith
                return textLabel.getText();
319 14ad7326 pastith
        }
320 14ad7326 pastith
321 14ad7326 pastith
        /**
322 14ad7326 pastith
         * Set the text displayed above the progress bar
323 14ad7326 pastith
         *
324 14ad7326 pastith
         * @param text the text to set
325 14ad7326 pastith
         */
326 14ad7326 pastith
        public void setText(String text) {
327 14ad7326 pastith
                textLabel.setText(text);
328 14ad7326 pastith
        }
329 14ad7326 pastith
330 14ad7326 pastith
        /**
331 14ad7326 pastith
         * Get the message used to format the time remaining text for hours
332 14ad7326 pastith
         *
333 14ad7326 pastith
         * @return the hours message
334 14ad7326 pastith
         */
335 14ad7326 pastith
        public String getHoursMessage() {
336 14ad7326 pastith
                return hoursMessage;
337 14ad7326 pastith
        }
338 14ad7326 pastith
339 14ad7326 pastith
        /**
340 14ad7326 pastith
         * Set the message used to format the time remaining text below the progress
341 14ad7326 pastith
         * bar. There are 3 messages used for hours, minutes and seconds
342 14ad7326 pastith
         * respectively. The message must contain a placeholder for the value. The
343 14ad7326 pastith
         * placeholder must be {0}. For example, the following is a valid message:
344 14ad7326 pastith
         * "Hours remaining: {0}"
345 14ad7326 pastith
         *
346 aa07a34b Panagiotis Astithas
         * @param anHoursMessage the hours message to set
347 14ad7326 pastith
         */
348 aa07a34b Panagiotis Astithas
        public void setHoursMessage(String anHoursMessage) {
349 aa07a34b Panagiotis Astithas
                hoursMessage = anHoursMessage;
350 14ad7326 pastith
        }
351 14ad7326 pastith
352 14ad7326 pastith
        /**
353 14ad7326 pastith
         * Get the message used to format the time remaining text for minutes
354 14ad7326 pastith
         *
355 14ad7326 pastith
         * @return the minutesMessage
356 14ad7326 pastith
         */
357 14ad7326 pastith
        public String getMinutesMessage() {
358 14ad7326 pastith
                return minutesMessage;
359 14ad7326 pastith
        }
360 14ad7326 pastith
361 14ad7326 pastith
        /**
362 14ad7326 pastith
         * Set the message used to format the time remaining text below the progress
363 14ad7326 pastith
         * bar. There are 3 messages used for hours, minutes and seconds
364 14ad7326 pastith
         * respectively. The message must contain a placeholder for the value. The
365 14ad7326 pastith
         * placeholder must be {0}. For example, the following is a valid message:
366 14ad7326 pastith
         * "Minutes remaining: {0}"
367 14ad7326 pastith
         *
368 aa07a34b Panagiotis Astithas
         * @param aMinutesMessage the minutes message to set
369 14ad7326 pastith
         */
370 aa07a34b Panagiotis Astithas
        public void setMinutesMessage(String aMinutesMessage) {
371 aa07a34b Panagiotis Astithas
                minutesMessage = aMinutesMessage;
372 14ad7326 pastith
        }
373 14ad7326 pastith
374 14ad7326 pastith
        /**
375 14ad7326 pastith
         * Get the message used to format the time remaining text for seconds
376 14ad7326 pastith
         *
377 14ad7326 pastith
         * @return the secondsMessage
378 14ad7326 pastith
         */
379 14ad7326 pastith
        public String getSecondsMessage() {
380 14ad7326 pastith
                return secondsMessage;
381 14ad7326 pastith
        }
382 14ad7326 pastith
383 14ad7326 pastith
        /**
384 14ad7326 pastith
         * Set the message used to format the time remaining text below the progress
385 14ad7326 pastith
         * bar. There are 3 messages used for hours, minutes and seconds
386 14ad7326 pastith
         * respectively. The message must contain a placeholder for the value. The
387 14ad7326 pastith
         * placeholder must be {0}. For example, the following is a valid message:
388 14ad7326 pastith
         * "Seconds remaining: {0}"
389 14ad7326 pastith
         *
390 aa07a34b Panagiotis Astithas
         * @param aSecondsMessage the secondsMessage to set
391 14ad7326 pastith
         */
392 aa07a34b Panagiotis Astithas
        public void setSecondsMessage(String aSecondsMessage) {
393 aa07a34b Panagiotis Astithas
                secondsMessage = aSecondsMessage;
394 14ad7326 pastith
        }
395 14ad7326 pastith
396 14ad7326 pastith
}