Merge branch 'upstream-sep2019-sfix' into subtree
[fanfix.git] / TProgressBar.java
CommitLineData
daa4106c 1/*
d502a0e9
KL
2 * Jexer - Java Text User Interface
3 *
e16dda65 4 * The MIT License (MIT)
d502a0e9 5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
d502a0e9 7 *
e16dda65
KL
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:
d502a0e9 14 *
e16dda65
KL
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
d502a0e9 17 *
e16dda65
KL
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.
d502a0e9
KL
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer;
30
31import jexer.bits.CellAttributes;
32import jexer.bits.GraphicsChars;
e307b724 33import jexer.bits.StringUtils;
d502a0e9
KL
34
35/**
36 * TProgressBar implements a simple progress bar.
37 */
051e2913 38public class TProgressBar extends TWidget {
d502a0e9 39
d36057df
KL
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
43
d502a0e9
KL
44 /**
45 * Value that corresponds to 0% progress.
46 */
47 private int minValue = 0;
48
d502a0e9
KL
49 /**
50 * Value that corresponds to 100% progress.
51 */
52 private int maxValue = 100;
53
d502a0e9
KL
54 /**
55 * Current value of the progress.
56 */
57 private int value = 0;
58
e307b724
KL
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
d36057df
KL
79 // ------------------------------------------------------------------------
80 // Constructors -----------------------------------------------------------
81 // ------------------------------------------------------------------------
d502a0e9
KL
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
a83fea2b 96 super(parent, false, x, y, width, 1);
d502a0e9 97
d502a0e9
KL
98 this.value = value;
99 }
100
d36057df
KL
101 // ------------------------------------------------------------------------
102 // Event handlers ---------------------------------------------------------
103 // ------------------------------------------------------------------------
104
105
106 // ------------------------------------------------------------------------
107 // TWidget ----------------------------------------------------------------
108 // ------------------------------------------------------------------------
109
d8dc8aea
KL
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
d502a0e9
KL
121 /**
122 * Draw a static progress bar.
123 */
124 @Override
125 public void draw() {
d49a676f
KL
126
127 if (getWidth() <= 2) {
128 // Bail out, we are too narrow to draw anything.
129 return;
130 }
131
d502a0e9
KL
132 CellAttributes completeColor = getTheme().getColor("tprogressbar.complete");
133 CellAttributes incompleteColor = getTheme().getColor("tprogressbar.incomplete");
134
135 float progress = ((float)value - minValue) / ((float)maxValue - minValue);
136 int progressInt = (int)(progress * 100);
137 int progressUnit = 100 / (getWidth() - 2);
138
e307b724
KL
139 putCharXY(0, 0, leftBorderChar, incompleteColor);
140 for (int i = StringUtils.width(leftBorderChar); i < getWidth() - 2;) {
d502a0e9
KL
141 float iProgress = (float)i / (getWidth() - 2);
142 int iProgressInt = (int)(iProgress * 100);
143 if (iProgressInt <= progressInt - progressUnit) {
e307b724
KL
144 putCharXY(i, 0, completedChar, completeColor);
145 i += StringUtils.width(completedChar);
d502a0e9 146 } else {
e307b724
KL
147 putCharXY(i, 0, remainingChar, incompleteColor);
148 i += StringUtils.width(remainingChar);
d502a0e9
KL
149 }
150 }
151 if (value >= maxValue) {
e307b724
KL
152 putCharXY(getWidth() - StringUtils.width(leftBorderChar) -
153 StringUtils.width(rightBorderChar), 0, completedChar,
154 completeColor);
d502a0e9 155 } else {
e307b724
KL
156 putCharXY(getWidth() - StringUtils.width(leftBorderChar) -
157 StringUtils.width(rightBorderChar), 0, remainingChar,
d502a0e9
KL
158 incompleteColor);
159 }
e307b724
KL
160 putCharXY(getWidth() - StringUtils.width(rightBorderChar), 0,
161 rightBorderChar, incompleteColor);
d502a0e9
KL
162 }
163
d36057df
KL
164 // ------------------------------------------------------------------------
165 // TProgressBar -----------------------------------------------------------
166 // ------------------------------------------------------------------------
167
168 /**
169 * Get the value that corresponds to 0% progress.
170 *
171 * @return the value that corresponds to 0% progress
172 */
173 public int getMinValue() {
174 return minValue;
175 }
176
177 /**
178 * Set the value that corresponds to 0% progress.
179 *
180 * @param minValue the value that corresponds to 0% progress
181 */
182 public void setMinValue(final int minValue) {
183 this.minValue = minValue;
184 }
185
186 /**
187 * Get the value that corresponds to 100% progress.
188 *
189 * @return the value that corresponds to 100% progress
190 */
191 public int getMaxValue() {
192 return maxValue;
193 }
194
195 /**
196 * Set the value that corresponds to 100% progress.
197 *
198 * @param maxValue the value that corresponds to 100% progress
199 */
200 public void setMaxValue(final int maxValue) {
201 this.maxValue = maxValue;
202 }
203
204 /**
205 * Get the current value of the progress.
206 *
207 * @return the current value of the progress
208 */
209 public int getValue() {
210 return value;
211 }
212
213 /**
214 * Set the current value of the progress.
215 *
216 * @param value the current value of the progress
217 */
218 public void setValue(final int value) {
219 this.value = value;
220 }
221
e307b724
KL
222 /**
223 * Set the left border character.
224 *
225 * @param ch the char to use
226 */
227 public void setLeftBorderChar(final int ch) {
228 leftBorderChar = ch;
229 }
230
231 /**
232 * Get the left border character.
233 *
234 * @return the char
235 */
236 public int getLeftBorderChar() {
237 return leftBorderChar;
238 }
239
240 /**
241 * Set the filled-in part of the bar.
242 *
243 * @param ch the char to use
244 */
245 public void setCompletedChar(final int ch) {
246 completedChar = ch;
247 }
248
249 /**
250 * Get the filled-in part of the bar.
251 *
252 * @return the char
253 */
254 public int getCompletedChar() {
255 return completedChar;
256 }
257
258 /**
259 * Set the remaining to be filled in part of the bar.
260 *
261 * @param ch the char to use
262 */
263 public void setRemainingChar(final int ch) {
264 remainingChar = ch;
265 }
266
267 /**
268 * Get the remaining to be filled in part of the bar.
269 *
270 * @return the char
271 */
272 public int getRemainingChar() {
273 return remainingChar;
274 }
275
276 /**
277 * Set the right border character.
278 *
279 * @param ch the char to use
280 */
281 public void setRightBorderChar(final int ch) {
282 rightBorderChar = ch;
283 }
284
285 /**
286 * Get the right border character.
287 *
288 * @return the char
289 */
290 public int getRightBorderChar() {
291 return rightBorderChar;
292 }
293
d502a0e9 294}