root / web_client / src / gr / grnet / pithos / web / client / ProgressBar.java @ ba0078a6
History | View | Annotate | Download (11.5 kB)
1 | ab1eb3f8 | Christos Stathis | /*
|
---|---|---|---|
2 | 58777026 | Christos Stathis | * Copyright 2011 GRNET S.A. All rights reserved.
|
3 | ab1eb3f8 | Christos Stathis | *
|
4 | 58777026 | Christos Stathis | * Redistribution and use in source and binary forms, with or
|
5 | 58777026 | Christos Stathis | * without modification, are permitted provided that the following
|
6 | 58777026 | Christos Stathis | * conditions are met:
|
7 | ab1eb3f8 | Christos Stathis | *
|
8 | 58777026 | Christos Stathis | * 1. Redistributions of source code must retain the above
|
9 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
10 | 58777026 | Christos Stathis | * disclaimer.
|
11 | ab1eb3f8 | Christos Stathis | *
|
12 | 58777026 | Christos Stathis | * 2. Redistributions in binary form must reproduce the above
|
13 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
14 | 58777026 | Christos Stathis | * disclaimer in the documentation and/or other materials
|
15 | 58777026 | Christos Stathis | * provided with the distribution.
|
16 | 58777026 | Christos Stathis | *
|
17 | 58777026 | Christos Stathis | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 | 58777026 | Christos Stathis | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 | 58777026 | Christos Stathis | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 | 58777026 | Christos Stathis | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 | 58777026 | Christos Stathis | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 | 58777026 | Christos Stathis | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 | 58777026 | Christos Stathis | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 | 58777026 | Christos Stathis | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 | 58777026 | Christos Stathis | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 | 58777026 | Christos Stathis | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 | 58777026 | Christos Stathis | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 | 58777026 | Christos Stathis | * POSSIBILITY OF SUCH DAMAGE.
|
29 | 58777026 | Christos Stathis | *
|
30 | 58777026 | Christos Stathis | * The views and conclusions contained in the software and
|
31 | 58777026 | Christos Stathis | * documentation are those of the authors and should not be
|
32 | 58777026 | Christos Stathis | * interpreted as representing official policies, either expressed
|
33 | 58777026 | Christos Stathis | * or implied, of GRNET S.A.
|
34 | ab1eb3f8 | Christos Stathis | */
|
35 | ab1eb3f8 | Christos Stathis | |
36 | ab1eb3f8 | Christos Stathis | package gr.grnet.pithos.web.client; |
37 | ab1eb3f8 | Christos Stathis | |
38 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.FlexTable; |
39 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.Grid; |
40 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.Label; |
41 | ab1eb3f8 | Christos Stathis | import com.google.gwt.user.client.ui.VerticalPanel; |
42 | ab1eb3f8 | Christos Stathis | |
43 | ab1eb3f8 | Christos Stathis | /**
|
44 | ab1eb3f8 | Christos Stathis | * <P>
|
45 | ab1eb3f8 | Christos Stathis | * A simple progress bar that uses table elements to show progress and with a
|
46 | ab1eb3f8 | Christos Stathis | * basic time remaining calculation built in.
|
47 | ab1eb3f8 | Christos Stathis | * <P>
|
48 | ab1eb3f8 | Christos Stathis | * You can optionally display some text above the progress bar and/or display
|
49 | ab1eb3f8 | Christos Stathis | * time remaining underneath the progress bar. To control the display of those
|
50 | ab1eb3f8 | Christos Stathis | * features, set the options in the constructor as shown in the following usage
|
51 | ab1eb3f8 | Christos Stathis | * example:
|
52 | ab1eb3f8 | Christos Stathis | *
|
53 | ab1eb3f8 | Christos Stathis | * <PRE>
|
54 | ab1eb3f8 | Christos Stathis | * final ProgressBar progressBar = new ProgressBar(20, ProgressBar.SHOW_TIME_REMAINING + ProgressBar.SHOW_TEXT);
|
55 | ab1eb3f8 | Christos Stathis | * progressBar.setText("Doing something...");
|
56 | ab1eb3f8 | Christos Stathis | * RootPanel.get().add(progressBar);
|
57 | ab1eb3f8 | Christos Stathis | * Timer t = new Timer() {
|
58 | ab1eb3f8 | Christos Stathis | *
|
59 | ab1eb3f8 | Christos Stathis | * public void run() {
|
60 | ab1eb3f8 | Christos Stathis | * int progress = progressBar.getProgress() + 4;
|
61 | ab1eb3f8 | Christos Stathis | * if (progress > 100)
|
62 | ab1eb3f8 | Christos Stathis | * cancel();
|
63 | ab1eb3f8 | Christos Stathis | * progressBar.setProgress(progress);
|
64 | ab1eb3f8 | Christos Stathis | * }
|
65 | ab1eb3f8 | Christos Stathis | * };
|
66 | ab1eb3f8 | Christos Stathis | * t.scheduleRepeating(1000);
|
67 | ab1eb3f8 | Christos Stathis | * </PRE>
|
68 | ab1eb3f8 | Christos Stathis | * <P>
|
69 | ab1eb3f8 | Christos Stathis | * How the time remaining is displayed can be controlled by setting the relevant
|
70 | ab1eb3f8 | Christos Stathis | * messages using the language of your choice.
|
71 | ab1eb3f8 | Christos Stathis | * <P>
|
72 | ab1eb3f8 | Christos Stathis | * The default setting for the messages are as follows:
|
73 | ab1eb3f8 | Christos Stathis | *
|
74 | ab1eb3f8 | Christos Stathis | * <PRE>
|
75 | ab1eb3f8 | Christos Stathis | * setSecondsMessage("Time remaining: {0} Seconds");
|
76 | ab1eb3f8 | Christos Stathis | * setMinutesMessage("Time remaining: {0} Minutes");
|
77 | ab1eb3f8 | Christos Stathis | * setHoursMessage("Time remaining: {0} Hours");
|
78 | ab1eb3f8 | Christos Stathis | * </PRE>
|
79 | ab1eb3f8 | Christos Stathis | * <P>
|
80 | ab1eb3f8 | Christos Stathis | * To reset the time remaining/set the start time, simply set the progress to
|
81 | ab1eb3f8 | Christos Stathis | * zero.
|
82 | ab1eb3f8 | Christos Stathis | * <P>
|
83 | ab1eb3f8 | Christos Stathis | * Some basic CSS styling is available to control the text, border around the
|
84 | ab1eb3f8 | Christos Stathis | * progress bar itself and the colour of the progress bar elements.
|
85 | ab1eb3f8 | Christos Stathis | *
|
86 | ab1eb3f8 | Christos Stathis | * <PRE>
|
87 | ab1eb3f8 | Christos Stathis | * .progressbar-text {
|
88 | ab1eb3f8 | Christos Stathis | * font-weight: bold;
|
89 | ab1eb3f8 | Christos Stathis | * }
|
90 | ab1eb3f8 | Christos Stathis | * .progressbar-remaining {
|
91 | ab1eb3f8 | Christos Stathis | * font-size: 12px;
|
92 | ab1eb3f8 | Christos Stathis | * font-style: italic;
|
93 | ab1eb3f8 | Christos Stathis | * }
|
94 | ab1eb3f8 | Christos Stathis | * .progressbar-outer {
|
95 | ab1eb3f8 | Christos Stathis | * border: 1px solid black;
|
96 | ab1eb3f8 | Christos Stathis | * }
|
97 | ab1eb3f8 | Christos Stathis | * .progressbar-inner {
|
98 | ab1eb3f8 | Christos Stathis | * border: 1px solid black;
|
99 | ab1eb3f8 | Christos Stathis | * margin: 1px;
|
100 | ab1eb3f8 | Christos Stathis | * }
|
101 | ab1eb3f8 | Christos Stathis | * .progressbar-bar {
|
102 | ab1eb3f8 | Christos Stathis | * width: 5px;
|
103 | ab1eb3f8 | Christos Stathis | * height: 15px;
|
104 | ab1eb3f8 | Christos Stathis | * margin: 1px;
|
105 | ab1eb3f8 | Christos Stathis | * }
|
106 | ab1eb3f8 | Christos Stathis | * .progressbar-fullbar {
|
107 | ab1eb3f8 | Christos Stathis | * background: blue;
|
108 | ab1eb3f8 | Christos Stathis | * }
|
109 | ab1eb3f8 | Christos Stathis | * .progressbar-blankbar {
|
110 | ab1eb3f8 | Christos Stathis | * background: #eee;
|
111 | ab1eb3f8 | Christos Stathis | * }
|
112 | ab1eb3f8 | Christos Stathis | *</PRE>
|
113 | ab1eb3f8 | Christos Stathis | * <P>
|
114 | ab1eb3f8 | Christos Stathis | * You can take advantage of the default style by adding the following to the
|
115 | ab1eb3f8 | Christos Stathis | * head of your HTML page.
|
116 | ab1eb3f8 | Christos Stathis | * <P>
|
117 | ab1eb3f8 | Christos Stathis | * <link rel="stylesheet" type="text/css" href="style/gwl-progressBar.css">
|
118 | ab1eb3f8 | Christos Stathis | * <P>
|
119 | ab1eb3f8 | Christos Stathis | * This style sheet also has two additional styles which you can use by adding
|
120 | ab1eb3f8 | Christos Stathis | * the stye name to the widget. You can use either one of these, or use both
|
121 | ab1eb3f8 | Christos Stathis | * combined.
|
122 | ab1eb3f8 | Christos Stathis | *
|
123 | ab1eb3f8 | Christos Stathis | * <PRE>
|
124 | ab1eb3f8 | Christos Stathis | * ProgressBar progressBar = new ProgressBar(20);
|
125 | ab1eb3f8 | Christos Stathis | * progressBar.addStyleName("progressbar-solid");
|
126 | ab1eb3f8 | Christos Stathis | * progressBar.addStyleName("progressbar-noborder");
|
127 | ab1eb3f8 | Christos Stathis | * </PRE>
|
128 | ab1eb3f8 | Christos Stathis | *
|
129 | ab1eb3f8 | Christos Stathis | * @author Bjarne Matzen - Bjarne[dot]Matzen[at]gmail[dot]com
|
130 | ab1eb3f8 | Christos Stathis | */
|
131 | ab1eb3f8 | Christos Stathis | |
132 | ab1eb3f8 | Christos Stathis | public class ProgressBar extends VerticalPanel { |
133 | ab1eb3f8 | Christos Stathis | |
134 | ab1eb3f8 | Christos Stathis | /**
|
135 | ab1eb3f8 | Christos Stathis | * Option to show text label above progress bar
|
136 | ab1eb3f8 | Christos Stathis | */
|
137 | ab1eb3f8 | Christos Stathis | public static final int SHOW_TEXT = 2; |
138 | ab1eb3f8 | Christos Stathis | |
139 | ab1eb3f8 | Christos Stathis | /**
|
140 | ab1eb3f8 | Christos Stathis | * Option to show time remaining
|
141 | ab1eb3f8 | Christos Stathis | */
|
142 | ab1eb3f8 | Christos Stathis | public static final int SHOW_TIME_REMAINING = 1; |
143 | ab1eb3f8 | Christos Stathis | |
144 | ab1eb3f8 | Christos Stathis | /**
|
145 | ab1eb3f8 | Christos Stathis | * The time the progress bar was started
|
146 | ab1eb3f8 | Christos Stathis | */
|
147 | ab1eb3f8 | Christos Stathis | private long startTime = System.currentTimeMillis(); |
148 | ab1eb3f8 | Christos Stathis | |
149 | ab1eb3f8 | Christos Stathis | /**
|
150 | ab1eb3f8 | Christos Stathis | * The number of bar elements to show
|
151 | ab1eb3f8 | Christos Stathis | */
|
152 | ab1eb3f8 | Christos Stathis | private int elements = 20; |
153 | ab1eb3f8 | Christos Stathis | |
154 | ab1eb3f8 | Christos Stathis | /**
|
155 | ab1eb3f8 | Christos Stathis | * Time element text
|
156 | ab1eb3f8 | Christos Stathis | */
|
157 | ab1eb3f8 | Christos Stathis | private String secondsMessage = "Estimated time remaining: {0} Seconds"; |
158 | ab1eb3f8 | Christos Stathis | |
159 | ab1eb3f8 | Christos Stathis | private String minutesMessage = "Estimated time remaining: {0} Minutes"; |
160 | ab1eb3f8 | Christos Stathis | |
161 | ab1eb3f8 | Christos Stathis | private String hoursMessage = "Estimated time remaining: {0} Hours"; |
162 | ab1eb3f8 | Christos Stathis | |
163 | ab1eb3f8 | Christos Stathis | /**
|
164 | ab1eb3f8 | Christos Stathis | * Current progress (as a percentage)
|
165 | ab1eb3f8 | Christos Stathis | */
|
166 | ab1eb3f8 | Christos Stathis | private int progress = 0; |
167 | ab1eb3f8 | Christos Stathis | |
168 | ab1eb3f8 | Christos Stathis | /**
|
169 | ab1eb3f8 | Christos Stathis | * This is the frame around the progress bar
|
170 | ab1eb3f8 | Christos Stathis | */
|
171 | ab1eb3f8 | Christos Stathis | private FlexTable barFrame = new FlexTable(); |
172 | ab1eb3f8 | Christos Stathis | |
173 | ab1eb3f8 | Christos Stathis | /**
|
174 | ab1eb3f8 | Christos Stathis | * This is the grid used to show the elements
|
175 | ab1eb3f8 | Christos Stathis | */
|
176 | ab1eb3f8 | Christos Stathis | private Grid elementGrid;
|
177 | ab1eb3f8 | Christos Stathis | |
178 | ab1eb3f8 | Christos Stathis | /**
|
179 | ab1eb3f8 | Christos Stathis | * This is the current text label below the progress bar
|
180 | ab1eb3f8 | Christos Stathis | */
|
181 | ab1eb3f8 | Christos Stathis | private Label remainLabel = new Label(); |
182 | ab1eb3f8 | Christos Stathis | |
183 | ab1eb3f8 | Christos Stathis | /**
|
184 | ab1eb3f8 | Christos Stathis | * This is the current text label above the progress bar
|
185 | ab1eb3f8 | Christos Stathis | */
|
186 | ab1eb3f8 | Christos Stathis | private Label textLabel = new Label(); |
187 | ab1eb3f8 | Christos Stathis | |
188 | ab1eb3f8 | Christos Stathis | /**
|
189 | ab1eb3f8 | Christos Stathis | * internal flags for options
|
190 | ab1eb3f8 | Christos Stathis | */
|
191 | ab1eb3f8 | Christos Stathis | private boolean showRemaining = false; |
192 | ab1eb3f8 | Christos Stathis | |
193 | ab1eb3f8 | Christos Stathis | private boolean showText = false; |
194 | ab1eb3f8 | Christos Stathis | |
195 | ab1eb3f8 | Christos Stathis | /**
|
196 | ab1eb3f8 | Christos Stathis | * Base constructor for this widget
|
197 | ab1eb3f8 | Christos Stathis | *
|
198 | ab1eb3f8 | Christos Stathis | * @param elementNo The number of elements (bars) to show on the progress bar
|
199 | ab1eb3f8 | Christos Stathis | * @param options The display options for the progress bar
|
200 | ab1eb3f8 | Christos Stathis | */
|
201 | ab1eb3f8 | Christos Stathis | public ProgressBar(int elementNo, int options) { |
202 | ab1eb3f8 | Christos Stathis | // Read the options and set convenience variables
|
203 | ab1eb3f8 | Christos Stathis | if ((options & SHOW_TIME_REMAINING) == SHOW_TIME_REMAINING)
|
204 | ab1eb3f8 | Christos Stathis | showRemaining = true;
|
205 | ab1eb3f8 | Christos Stathis | if ((options & SHOW_TEXT) == SHOW_TEXT)
|
206 | ab1eb3f8 | Christos Stathis | showText = true;
|
207 | ab1eb3f8 | Christos Stathis | |
208 | ab1eb3f8 | Christos Stathis | // Set element count
|
209 | ab1eb3f8 | Christos Stathis | elements = elementNo; |
210 | ab1eb3f8 | Christos Stathis | |
211 | ab1eb3f8 | Christos Stathis | // Styling
|
212 | ab1eb3f8 | Christos Stathis | remainLabel.setStyleName("progressbar-remaining");
|
213 | ab1eb3f8 | Christos Stathis | textLabel.setStyleName("progressbar-text");
|
214 | ab1eb3f8 | Christos Stathis | |
215 | ab1eb3f8 | Christos Stathis | // Initialize the progress elements
|
216 | ab1eb3f8 | Christos Stathis | elementGrid = new Grid(1, elementNo); |
217 | ab1eb3f8 | Christos Stathis | elementGrid.setStyleName("progressbar-inner");
|
218 | ab1eb3f8 | Christos Stathis | elementGrid.setCellPadding(0);
|
219 | ab1eb3f8 | Christos Stathis | elementGrid.setCellSpacing(0);
|
220 | ab1eb3f8 | Christos Stathis | |
221 | ab1eb3f8 | Christos Stathis | for (int loop = 0; loop < elementNo; loop++) { |
222 | ab1eb3f8 | Christos Stathis | Grid elm = new Grid(1, 1); |
223 | ab1eb3f8 | Christos Stathis | // elm.setHTML(0, 0, " ");
|
224 | ab1eb3f8 | Christos Stathis | elm.setHTML(0, 0, ""); |
225 | ab1eb3f8 | Christos Stathis | elm.setStyleName("progressbar-blankbar");
|
226 | ab1eb3f8 | Christos Stathis | elm.addStyleName("progressbar-bar");
|
227 | ab1eb3f8 | Christos Stathis | elementGrid.setWidget(0, loop, elm);
|
228 | ab1eb3f8 | Christos Stathis | } |
229 | ab1eb3f8 | Christos Stathis | |
230 | ab1eb3f8 | Christos Stathis | // Create the container around the elements
|
231 | ab1eb3f8 | Christos Stathis | Grid containerGrid = new Grid(1, 1); |
232 | ab1eb3f8 | Christos Stathis | containerGrid.setCellPadding(0);
|
233 | ab1eb3f8 | Christos Stathis | containerGrid.setCellSpacing(0);
|
234 | ab1eb3f8 | Christos Stathis | containerGrid.setWidget(0, 0, elementGrid); |
235 | ab1eb3f8 | Christos Stathis | containerGrid.setStyleName("progressbar-outer");
|
236 | ab1eb3f8 | Christos Stathis | // containerGrid.setBorderWidth(1);
|
237 | ab1eb3f8 | Christos Stathis | |
238 | ab1eb3f8 | Christos Stathis | // Set up the surrounding flex table based on the options
|
239 | ab1eb3f8 | Christos Stathis | int row = 0; |
240 | ab1eb3f8 | Christos Stathis | if (showText)
|
241 | ab1eb3f8 | Christos Stathis | barFrame.setWidget(row++, 0, textLabel);
|
242 | ab1eb3f8 | Christos Stathis | barFrame.setWidget(row++, 0, containerGrid);
|
243 | ab1eb3f8 | Christos Stathis | if (showRemaining)
|
244 | ab1eb3f8 | Christos Stathis | barFrame.setWidget(row++, 0, remainLabel);
|
245 | ab1eb3f8 | Christos Stathis | |
246 | ab1eb3f8 | Christos Stathis | barFrame.setWidth("100%");
|
247 | ab1eb3f8 | Christos Stathis | |
248 | ab1eb3f8 | Christos Stathis | // Add the frame to the panel
|
249 | ab1eb3f8 | Christos Stathis | this.add(barFrame);
|
250 | ab1eb3f8 | Christos Stathis | |
251 | ab1eb3f8 | Christos Stathis | // Initialize progress bar
|
252 | ab1eb3f8 | Christos Stathis | setProgress(0);
|
253 | ab1eb3f8 | Christos Stathis | } |
254 | ab1eb3f8 | Christos Stathis | |
255 | ab1eb3f8 | Christos Stathis | /**
|
256 | ab1eb3f8 | Christos Stathis | * Constructor without options
|
257 | ab1eb3f8 | Christos Stathis | *
|
258 | ab1eb3f8 | Christos Stathis | * @param elementNo The number of elements (bars) to show on the progress bar
|
259 | ab1eb3f8 | Christos Stathis | */
|
260 | ab1eb3f8 | Christos Stathis | public ProgressBar(int elementNo) { |
261 | ab1eb3f8 | Christos Stathis | this(elementNo, 0); |
262 | ab1eb3f8 | Christos Stathis | } |
263 | ab1eb3f8 | Christos Stathis | |
264 | ab1eb3f8 | Christos Stathis | /**
|
265 | ab1eb3f8 | Christos Stathis | * Set the current progress as a percentage
|
266 | ab1eb3f8 | Christos Stathis | *
|
267 | ab1eb3f8 | Christos Stathis | * @param percentage Set current percentage for the progress bar
|
268 | ab1eb3f8 | Christos Stathis | */
|
269 | ab1eb3f8 | Christos Stathis | public void setProgress(int percentage) { |
270 | ab1eb3f8 | Christos Stathis | // Make sure we are error-tolerant
|
271 | ab1eb3f8 | Christos Stathis | if (percentage > 100) |
272 | ab1eb3f8 | Christos Stathis | percentage = 100;
|
273 | ab1eb3f8 | Christos Stathis | if (percentage < 0) |
274 | ab1eb3f8 | Christos Stathis | percentage = 0;
|
275 | ab1eb3f8 | Christos Stathis | |
276 | ab1eb3f8 | Christos Stathis | // Set the internal variable
|
277 | ab1eb3f8 | Christos Stathis | progress = percentage; |
278 | ab1eb3f8 | Christos Stathis | |
279 | ab1eb3f8 | Christos Stathis | // Update the elements in the progress grid to
|
280 | ab1eb3f8 | Christos Stathis | // reflect the status
|
281 | ab1eb3f8 | Christos Stathis | int completed = elements * percentage / 100; |
282 | ab1eb3f8 | Christos Stathis | for (int loop = 0; loop < elements; loop++) { |
283 | ab1eb3f8 | Christos Stathis | Grid elm = (Grid) elementGrid.getWidget(0, loop);
|
284 | ab1eb3f8 | Christos Stathis | if (loop < completed) {
|
285 | ab1eb3f8 | Christos Stathis | elm.setStyleName("progressbar-fullbar");
|
286 | ab1eb3f8 | Christos Stathis | elm.addStyleName("progressbar-bar");
|
287 | ab1eb3f8 | Christos Stathis | } else {
|
288 | ab1eb3f8 | Christos Stathis | elm.setStyleName("progressbar-blankbar");
|
289 | ab1eb3f8 | Christos Stathis | elm.addStyleName("progressbar-bar");
|
290 | ab1eb3f8 | Christos Stathis | } |
291 | ab1eb3f8 | Christos Stathis | } |
292 | ab1eb3f8 | Christos Stathis | |
293 | ab1eb3f8 | Christos Stathis | if (percentage > 0) { |
294 | ab1eb3f8 | Christos Stathis | // Calculate the new time remaining
|
295 | ab1eb3f8 | Christos Stathis | long soFar = (System.currentTimeMillis() - startTime) / 1000; |
296 | ab1eb3f8 | Christos Stathis | long remaining = soFar * (100 - percentage) / percentage; |
297 | ab1eb3f8 | Christos Stathis | // Select the best UOM
|
298 | ab1eb3f8 | Christos Stathis | String remainText = secondsMessage;
|
299 | ab1eb3f8 | Christos Stathis | if (remaining > 120) { |
300 | ab1eb3f8 | Christos Stathis | remaining = remaining / 60;
|
301 | ab1eb3f8 | Christos Stathis | remainText = minutesMessage; |
302 | ab1eb3f8 | Christos Stathis | if (remaining > 120) { |
303 | ab1eb3f8 | Christos Stathis | remaining = remaining / 60;
|
304 | ab1eb3f8 | Christos Stathis | remainText = hoursMessage; |
305 | ab1eb3f8 | Christos Stathis | } |
306 | ab1eb3f8 | Christos Stathis | } |
307 | ab1eb3f8 | Christos Stathis | // Locate the position to insert out time remaining
|
308 | ab1eb3f8 | Christos Stathis | int pos = remainText.indexOf("{0}"); |
309 | ab1eb3f8 | Christos Stathis | if (pos >= 0) { |
310 | ab1eb3f8 | Christos Stathis | String trail = ""; |
311 | ab1eb3f8 | Christos Stathis | if (pos + 3 < remainText.length()) |
312 | ab1eb3f8 | Christos Stathis | trail = remainText.substring(pos + 3);
|
313 | ab1eb3f8 | Christos Stathis | remainText = remainText.substring(0, pos) + remaining + trail;
|
314 | ab1eb3f8 | Christos Stathis | } |
315 | ab1eb3f8 | Christos Stathis | // Set the label
|
316 | ab1eb3f8 | Christos Stathis | remainLabel.setText(remainText); |
317 | ab1eb3f8 | Christos Stathis | } else
|
318 | ab1eb3f8 | Christos Stathis | // If progress is 0, reset the start time
|
319 | ab1eb3f8 | Christos Stathis | startTime = System.currentTimeMillis();
|
320 | ab1eb3f8 | Christos Stathis | } |
321 | ab1eb3f8 | Christos Stathis | |
322 | ab1eb3f8 | Christos Stathis | /**
|
323 | ab1eb3f8 | Christos Stathis | * Get the current progress as a percentage
|
324 | ab1eb3f8 | Christos Stathis | *
|
325 | ab1eb3f8 | Christos Stathis | * @return Current percentage for the progress bar
|
326 | ab1eb3f8 | Christos Stathis | */
|
327 | ab1eb3f8 | Christos Stathis | public int getProgress() { |
328 | ab1eb3f8 | Christos Stathis | return progress;
|
329 | ab1eb3f8 | Christos Stathis | } |
330 | ab1eb3f8 | Christos Stathis | |
331 | ab1eb3f8 | Christos Stathis | /**
|
332 | ab1eb3f8 | Christos Stathis | * Get the text displayed above the progress bar
|
333 | ab1eb3f8 | Christos Stathis | *
|
334 | ab1eb3f8 | Christos Stathis | * @return the text
|
335 | ab1eb3f8 | Christos Stathis | */
|
336 | ab1eb3f8 | Christos Stathis | public String getText() { |
337 | ab1eb3f8 | Christos Stathis | return textLabel.getText();
|
338 | ab1eb3f8 | Christos Stathis | } |
339 | ab1eb3f8 | Christos Stathis | |
340 | ab1eb3f8 | Christos Stathis | /**
|
341 | ab1eb3f8 | Christos Stathis | * Set the text displayed above the progress bar
|
342 | ab1eb3f8 | Christos Stathis | *
|
343 | ab1eb3f8 | Christos Stathis | * @param text the text to set
|
344 | ab1eb3f8 | Christos Stathis | */
|
345 | ab1eb3f8 | Christos Stathis | public void setText(String text) { |
346 | ab1eb3f8 | Christos Stathis | textLabel.setText(text); |
347 | ab1eb3f8 | Christos Stathis | } |
348 | ab1eb3f8 | Christos Stathis | |
349 | ab1eb3f8 | Christos Stathis | /**
|
350 | ab1eb3f8 | Christos Stathis | * Get the message used to format the time remaining text for hours
|
351 | ab1eb3f8 | Christos Stathis | *
|
352 | ab1eb3f8 | Christos Stathis | * @return the hours message
|
353 | ab1eb3f8 | Christos Stathis | */
|
354 | ab1eb3f8 | Christos Stathis | public String getHoursMessage() { |
355 | ab1eb3f8 | Christos Stathis | return hoursMessage;
|
356 | ab1eb3f8 | Christos Stathis | } |
357 | ab1eb3f8 | Christos Stathis | |
358 | ab1eb3f8 | Christos Stathis | /**
|
359 | ab1eb3f8 | Christos Stathis | * Set the message used to format the time remaining text below the progress
|
360 | ab1eb3f8 | Christos Stathis | * bar. There are 3 messages used for hours, minutes and seconds
|
361 | ab1eb3f8 | Christos Stathis | * respectively. The message must contain a placeholder for the value. The
|
362 | ab1eb3f8 | Christos Stathis | * placeholder must be {0}. For example, the following is a valid message:
|
363 | ab1eb3f8 | Christos Stathis | * "Hours remaining: {0}"
|
364 | ab1eb3f8 | Christos Stathis | *
|
365 | ab1eb3f8 | Christos Stathis | * @param anHoursMessage the hours message to set
|
366 | ab1eb3f8 | Christos Stathis | */
|
367 | ab1eb3f8 | Christos Stathis | public void setHoursMessage(String anHoursMessage) { |
368 | ab1eb3f8 | Christos Stathis | hoursMessage = anHoursMessage; |
369 | ab1eb3f8 | Christos Stathis | } |
370 | ab1eb3f8 | Christos Stathis | |
371 | ab1eb3f8 | Christos Stathis | /**
|
372 | ab1eb3f8 | Christos Stathis | * Get the message used to format the time remaining text for minutes
|
373 | ab1eb3f8 | Christos Stathis | *
|
374 | ab1eb3f8 | Christos Stathis | * @return the minutesMessage
|
375 | ab1eb3f8 | Christos Stathis | */
|
376 | ab1eb3f8 | Christos Stathis | public String getMinutesMessage() { |
377 | ab1eb3f8 | Christos Stathis | return minutesMessage;
|
378 | ab1eb3f8 | Christos Stathis | } |
379 | ab1eb3f8 | Christos Stathis | |
380 | ab1eb3f8 | Christos Stathis | /**
|
381 | ab1eb3f8 | Christos Stathis | * Set the message used to format the time remaining text below the progress
|
382 | ab1eb3f8 | Christos Stathis | * bar. There are 3 messages used for hours, minutes and seconds
|
383 | ab1eb3f8 | Christos Stathis | * respectively. The message must contain a placeholder for the value. The
|
384 | ab1eb3f8 | Christos Stathis | * placeholder must be {0}. For example, the following is a valid message:
|
385 | ab1eb3f8 | Christos Stathis | * "Minutes remaining: {0}"
|
386 | ab1eb3f8 | Christos Stathis | *
|
387 | ab1eb3f8 | Christos Stathis | * @param aMinutesMessage the minutes message to set
|
388 | ab1eb3f8 | Christos Stathis | */
|
389 | ab1eb3f8 | Christos Stathis | public void setMinutesMessage(String aMinutesMessage) { |
390 | ab1eb3f8 | Christos Stathis | minutesMessage = aMinutesMessage; |
391 | ab1eb3f8 | Christos Stathis | } |
392 | ab1eb3f8 | Christos Stathis | |
393 | ab1eb3f8 | Christos Stathis | /**
|
394 | ab1eb3f8 | Christos Stathis | * Get the message used to format the time remaining text for seconds
|
395 | ab1eb3f8 | Christos Stathis | *
|
396 | ab1eb3f8 | Christos Stathis | * @return the secondsMessage
|
397 | ab1eb3f8 | Christos Stathis | */
|
398 | ab1eb3f8 | Christos Stathis | public String getSecondsMessage() { |
399 | ab1eb3f8 | Christos Stathis | return secondsMessage;
|
400 | ab1eb3f8 | Christos Stathis | } |
401 | ab1eb3f8 | Christos Stathis | |
402 | ab1eb3f8 | Christos Stathis | /**
|
403 | ab1eb3f8 | Christos Stathis | * Set the message used to format the time remaining text below the progress
|
404 | ab1eb3f8 | Christos Stathis | * bar. There are 3 messages used for hours, minutes and seconds
|
405 | ab1eb3f8 | Christos Stathis | * respectively. The message must contain a placeholder for the value. The
|
406 | ab1eb3f8 | Christos Stathis | * placeholder must be {0}. For example, the following is a valid message:
|
407 | ab1eb3f8 | Christos Stathis | * "Seconds remaining: {0}"
|
408 | ab1eb3f8 | Christos Stathis | *
|
409 | ab1eb3f8 | Christos Stathis | * @param aSecondsMessage the secondsMessage to set
|
410 | ab1eb3f8 | Christos Stathis | */
|
411 | ab1eb3f8 | Christos Stathis | public void setSecondsMessage(String aSecondsMessage) { |
412 | ab1eb3f8 | Christos Stathis | secondsMessage = aSecondsMessage; |
413 | ab1eb3f8 | Christos Stathis | } |
414 | ab1eb3f8 | Christos Stathis | |
415 | ab1eb3f8 | Christos Stathis | } |