2 * Jexer - Java Text User Interface
4 * The MIT License (MIT)
6 * Copyright (C) 2019 Kevin Lamonte
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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.
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
31 import jexer
.bits
.CellAttributes
;
32 import jexer
.bits
.GraphicsChars
;
33 import jexer
.bits
.StringUtils
;
36 * TProgressBar implements a simple progress bar.
38 public class TProgressBar
extends TWidget
{
40 // ------------------------------------------------------------------------
41 // Variables --------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * Value that corresponds to 0% progress.
47 private int minValue
= 0;
50 * Value that corresponds to 100% progress.
52 private int maxValue
= 100;
55 * Current value of the progress.
57 private int value
= 0;
60 * The left border character.
62 private int leftBorderChar
= GraphicsChars
.CP437
[0xC3];
65 * The filled-in part of the bar.
67 private int completedChar
= GraphicsChars
.BOX
;
70 * The remaining to be filled in part of the bar.
72 private int remainingChar
= GraphicsChars
.SINGLE_BAR
;
75 * The right border character.
77 private int rightBorderChar
= GraphicsChars
.CP437
[0xB4];
79 // ------------------------------------------------------------------------
80 // Constructors -----------------------------------------------------------
81 // ------------------------------------------------------------------------
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
92 public TProgressBar(final TWidget parent
, final int x
, final int y
,
93 final int width
, final int value
) {
95 // Set parent and window
96 super(parent
, false, x
, y
, width
, 1);
101 // ------------------------------------------------------------------------
102 // Event handlers ---------------------------------------------------------
103 // ------------------------------------------------------------------------
106 // ------------------------------------------------------------------------
107 // TWidget ----------------------------------------------------------------
108 // ------------------------------------------------------------------------
111 * Override TWidget's height: we can only set height at construction
114 * @param height new widget height (ignored)
117 public void setHeight(final int height
) {
122 * Draw a static progress bar.
127 if (getWidth() <= 2) {
128 // Bail out, we are too narrow to draw anything.
132 CellAttributes completeColor
= getTheme().getColor("tprogressbar.complete");
133 CellAttributes incompleteColor
= getTheme().getColor("tprogressbar.incomplete");
135 float progress
= ((float)value
- minValue
) / ((float)maxValue
- minValue
);
136 int progressInt
= (int)(progress
* 100);
137 int progressUnit
= 100 / (getWidth() - 2);
139 putCharXY(0, 0, leftBorderChar
, incompleteColor
);
140 for (int i
= StringUtils
.width(leftBorderChar
); i
< getWidth() - 2;) {
141 float iProgress
= (float)i
/ (getWidth() - 2);
142 int iProgressInt
= (int)(iProgress
* 100);
143 if (iProgressInt
<= progressInt
- progressUnit
) {
144 putCharXY(i
, 0, completedChar
, completeColor
);
145 i
+= StringUtils
.width(completedChar
);
147 putCharXY(i
, 0, remainingChar
, incompleteColor
);
148 i
+= StringUtils
.width(remainingChar
);
151 if (value
>= maxValue
) {
152 putCharXY(getWidth() - StringUtils
.width(leftBorderChar
) -
153 StringUtils
.width(rightBorderChar
), 0, completedChar
,
156 putCharXY(getWidth() - StringUtils
.width(leftBorderChar
) -
157 StringUtils
.width(rightBorderChar
), 0, remainingChar
,
160 putCharXY(getWidth() - StringUtils
.width(rightBorderChar
), 0,
161 rightBorderChar
, incompleteColor
);
164 // ------------------------------------------------------------------------
165 // TProgressBar -----------------------------------------------------------
166 // ------------------------------------------------------------------------
169 * Get the value that corresponds to 0% progress.
171 * @return the value that corresponds to 0% progress
173 public int getMinValue() {
178 * Set the value that corresponds to 0% progress.
180 * @param minValue the value that corresponds to 0% progress
182 public void setMinValue(final int minValue
) {
183 this.minValue
= minValue
;
187 * Get the value that corresponds to 100% progress.
189 * @return the value that corresponds to 100% progress
191 public int getMaxValue() {
196 * Set the value that corresponds to 100% progress.
198 * @param maxValue the value that corresponds to 100% progress
200 public void setMaxValue(final int maxValue
) {
201 this.maxValue
= maxValue
;
205 * Get the current value of the progress.
207 * @return the current value of the progress
209 public int getValue() {
214 * Set the current value of the progress.
216 * @param value the current value of the progress
218 public void setValue(final int value
) {
223 * Set the left border character.
225 * @param ch the char to use
227 public void setLeftBorderChar(final int ch
) {
232 * Get the left border character.
236 public int getLeftBorderChar() {
237 return leftBorderChar
;
241 * Set the filled-in part of the bar.
243 * @param ch the char to use
245 public void setCompletedChar(final int ch
) {
250 * Get the filled-in part of the bar.
254 public int getCompletedChar() {
255 return completedChar
;
259 * Set the remaining to be filled in part of the bar.
261 * @param ch the char to use
263 public void setRemainingChar(final int ch
) {
268 * Get the remaining to be filled in part of the bar.
272 public int getRemainingChar() {
273 return remainingChar
;
277 * Set the right border character.
279 * @param ch the char to use
281 public void setRightBorderChar(final int ch
) {
282 rightBorderChar
= ch
;
286 * Get the right border character.
290 public int getRightBorderChar() {
291 return rightBorderChar
;