#53 progress bar style
[fanfix.git] / src / jexer / TProgressBar.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29 package jexer;
30
31 import jexer.bits.CellAttributes;
32 import jexer.bits.GraphicsChars;
33 import jexer.bits.StringUtils;
34
35 /**
36 * TProgressBar implements a simple progress bar.
37 */
38 public class TProgressBar extends TWidget {
39
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
43
44 /**
45 * Value that corresponds to 0% progress.
46 */
47 private int minValue = 0;
48
49 /**
50 * Value that corresponds to 100% progress.
51 */
52 private int maxValue = 100;
53
54 /**
55 * Current value of the progress.
56 */
57 private int value = 0;
58
59 /**
60 * The left border character.
61 */
62 private int leftBorderChar = GraphicsChars.CP437[0xC3];
63
64 /**
65 * The filled-in part of the bar.
66 */
67 private int completedChar = GraphicsChars.BOX;
68
69 /**
70 * The remaining to be filled in part of the bar.
71 */
72 private int remainingChar = GraphicsChars.SINGLE_BAR;
73
74 /**
75 * The right border character.
76 */
77 private int rightBorderChar = GraphicsChars.CP437[0xB4];
78
79 // ------------------------------------------------------------------------
80 // Constructors -----------------------------------------------------------
81 // ------------------------------------------------------------------------
82
83 /**
84 * Public constructor.
85 *
86 * @param parent parent widget
87 * @param x column relative to parent
88 * @param y row relative to parent
89 * @param width width of progress bar
90 * @param value initial value of percent complete
91 */
92 public TProgressBar(final TWidget parent, final int x, final int y,
93 final int width, final int value) {
94
95 // Set parent and window
96 super(parent, false, x, y, width, 1);
97
98 this.value = value;
99 }
100
101 // ------------------------------------------------------------------------
102 // Event handlers ---------------------------------------------------------
103 // ------------------------------------------------------------------------
104
105
106 // ------------------------------------------------------------------------
107 // TWidget ----------------------------------------------------------------
108 // ------------------------------------------------------------------------
109
110 /**
111 * Override TWidget's height: we can only set height at construction
112 * time.
113 *
114 * @param height new widget height (ignored)
115 */
116 @Override
117 public void setHeight(final int height) {
118 // Do nothing
119 }
120
121 /**
122 * Draw a static progress bar.
123 */
124 @Override
125 public void draw() {
126 CellAttributes completeColor = getTheme().getColor("tprogressbar.complete");
127 CellAttributes incompleteColor = getTheme().getColor("tprogressbar.incomplete");
128
129 float progress = ((float)value - minValue) / ((float)maxValue - minValue);
130 int progressInt = (int)(progress * 100);
131 int progressUnit = 100 / (getWidth() - 2);
132
133 putCharXY(0, 0, leftBorderChar, incompleteColor);
134 for (int i = StringUtils.width(leftBorderChar); i < getWidth() - 2;) {
135 float iProgress = (float)i / (getWidth() - 2);
136 int iProgressInt = (int)(iProgress * 100);
137 if (iProgressInt <= progressInt - progressUnit) {
138 putCharXY(i, 0, completedChar, completeColor);
139 i += StringUtils.width(completedChar);
140 } else {
141 putCharXY(i, 0, remainingChar, incompleteColor);
142 i += StringUtils.width(remainingChar);
143 }
144 }
145 if (value >= maxValue) {
146 putCharXY(getWidth() - StringUtils.width(leftBorderChar) -
147 StringUtils.width(rightBorderChar), 0, completedChar,
148 completeColor);
149 } else {
150 putCharXY(getWidth() - StringUtils.width(leftBorderChar) -
151 StringUtils.width(rightBorderChar), 0, remainingChar,
152 incompleteColor);
153 }
154 putCharXY(getWidth() - StringUtils.width(rightBorderChar), 0,
155 rightBorderChar, incompleteColor);
156 }
157
158 // ------------------------------------------------------------------------
159 // TProgressBar -----------------------------------------------------------
160 // ------------------------------------------------------------------------
161
162 /**
163 * Get the value that corresponds to 0% progress.
164 *
165 * @return the value that corresponds to 0% progress
166 */
167 public int getMinValue() {
168 return minValue;
169 }
170
171 /**
172 * Set the value that corresponds to 0% progress.
173 *
174 * @param minValue the value that corresponds to 0% progress
175 */
176 public void setMinValue(final int minValue) {
177 this.minValue = minValue;
178 }
179
180 /**
181 * Get the value that corresponds to 100% progress.
182 *
183 * @return the value that corresponds to 100% progress
184 */
185 public int getMaxValue() {
186 return maxValue;
187 }
188
189 /**
190 * Set the value that corresponds to 100% progress.
191 *
192 * @param maxValue the value that corresponds to 100% progress
193 */
194 public void setMaxValue(final int maxValue) {
195 this.maxValue = maxValue;
196 }
197
198 /**
199 * Get the current value of the progress.
200 *
201 * @return the current value of the progress
202 */
203 public int getValue() {
204 return value;
205 }
206
207 /**
208 * Set the current value of the progress.
209 *
210 * @param value the current value of the progress
211 */
212 public void setValue(final int value) {
213 this.value = value;
214 }
215
216 /**
217 * Set the left border character.
218 *
219 * @param ch the char to use
220 */
221 public void setLeftBorderChar(final int ch) {
222 leftBorderChar = ch;
223 }
224
225 /**
226 * Get the left border character.
227 *
228 * @return the char
229 */
230 public int getLeftBorderChar() {
231 return leftBorderChar;
232 }
233
234 /**
235 * Set the filled-in part of the bar.
236 *
237 * @param ch the char to use
238 */
239 public void setCompletedChar(final int ch) {
240 completedChar = ch;
241 }
242
243 /**
244 * Get the filled-in part of the bar.
245 *
246 * @return the char
247 */
248 public int getCompletedChar() {
249 return completedChar;
250 }
251
252 /**
253 * Set the remaining to be filled in part of the bar.
254 *
255 * @param ch the char to use
256 */
257 public void setRemainingChar(final int ch) {
258 remainingChar = ch;
259 }
260
261 /**
262 * Get the remaining to be filled in part of the bar.
263 *
264 * @return the char
265 */
266 public int getRemainingChar() {
267 return remainingChar;
268 }
269
270 /**
271 * Set the right border character.
272 *
273 * @param ch the char to use
274 */
275 public void setRightBorderChar(final int ch) {
276 rightBorderChar = ch;
277 }
278
279 /**
280 * Get the right border character.
281 *
282 * @return the char
283 */
284 public int getRightBorderChar() {
285 return rightBorderChar;
286 }
287
288 }