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.
126 CellAttributes completeColor
= getTheme().getColor("tprogressbar.complete");
127 CellAttributes incompleteColor
= getTheme().getColor("tprogressbar.incomplete");
129 float progress
= ((float)value
- minValue
) / ((float)maxValue
- minValue
);
130 int progressInt
= (int)(progress
* 100);
131 int progressUnit
= 100 / (getWidth() - 2);
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
);
141 putCharXY(i
, 0, remainingChar
, incompleteColor
);
142 i
+= StringUtils
.width(remainingChar
);
145 if (value
>= maxValue
) {
146 putCharXY(getWidth() - StringUtils
.width(leftBorderChar
) -
147 StringUtils
.width(rightBorderChar
), 0, completedChar
,
150 putCharXY(getWidth() - StringUtils
.width(leftBorderChar
) -
151 StringUtils
.width(rightBorderChar
), 0, remainingChar
,
154 putCharXY(getWidth() - StringUtils
.width(rightBorderChar
), 0,
155 rightBorderChar
, incompleteColor
);
158 // ------------------------------------------------------------------------
159 // TProgressBar -----------------------------------------------------------
160 // ------------------------------------------------------------------------
163 * Get the value that corresponds to 0% progress.
165 * @return the value that corresponds to 0% progress
167 public int getMinValue() {
172 * Set the value that corresponds to 0% progress.
174 * @param minValue the value that corresponds to 0% progress
176 public void setMinValue(final int minValue
) {
177 this.minValue
= minValue
;
181 * Get the value that corresponds to 100% progress.
183 * @return the value that corresponds to 100% progress
185 public int getMaxValue() {
190 * Set the value that corresponds to 100% progress.
192 * @param maxValue the value that corresponds to 100% progress
194 public void setMaxValue(final int maxValue
) {
195 this.maxValue
= maxValue
;
199 * Get the current value of the progress.
201 * @return the current value of the progress
203 public int getValue() {
208 * Set the current value of the progress.
210 * @param value the current value of the progress
212 public void setValue(final int value
) {
217 * Set the left border character.
219 * @param ch the char to use
221 public void setLeftBorderChar(final int ch
) {
226 * Get the left border character.
230 public int getLeftBorderChar() {
231 return leftBorderChar
;
235 * Set the filled-in part of the bar.
237 * @param ch the char to use
239 public void setCompletedChar(final int ch
) {
244 * Get the filled-in part of the bar.
248 public int getCompletedChar() {
249 return completedChar
;
253 * Set the remaining to be filled in part of the bar.
255 * @param ch the char to use
257 public void setRemainingChar(final int ch
) {
262 * Get the remaining to be filled in part of the bar.
266 public int getRemainingChar() {
267 return remainingChar
;
271 * Set the right border character.
273 * @param ch the char to use
275 public void setRightBorderChar(final int ch
) {
276 rightBorderChar
= ch
;
280 * Get the right border character.
284 public int getRightBorderChar() {
285 return rightBorderChar
;