1f23531aeae06d514f878caf6cbf6d4928a8a6d8
[nikiroo-utils.git] / src / jexer / TProgressBar.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer;
32
33 import jexer.bits.CellAttributes;
34 import jexer.bits.GraphicsChars;
35
36 /**
37 * TProgressBar implements a simple progress bar.
38 */
39 public final class TProgressBar extends TWidget {
40
41 /**
42 * Value that corresponds to 0% progress.
43 */
44 private int minValue = 0;
45
46 /**
47 * Get the value that corresponds to 0% progress.
48 *
49 * @return the value that corresponds to 0% progress
50 */
51 public int getMinValue() {
52 return minValue;
53 }
54
55 /**
56 * Set the value that corresponds to 0% progress.
57 *
58 * @param minValue the value that corresponds to 0% progress
59 */
60 public void setMinValue(final int minValue) {
61 this.minValue = minValue;
62 }
63
64 /**
65 * Value that corresponds to 100% progress.
66 */
67 private int maxValue = 100;
68
69 /**
70 * Get the value that corresponds to 100% progress.
71 *
72 * @return the value that corresponds to 100% progress
73 */
74 public int getMaxValue() {
75 return maxValue;
76 }
77
78 /**
79 * Set the value that corresponds to 100% progress.
80 *
81 * @param maxValue the value that corresponds to 100% progress
82 */
83 public void setMaxValue(final int maxValue) {
84 this.maxValue = maxValue;
85 }
86
87 /**
88 * Current value of the progress.
89 */
90 private int value = 0;
91
92 /**
93 * Get the current value of the progress.
94 *
95 * @return the current value of the progress
96 */
97 public int getValue() {
98 return value;
99 }
100
101 /**
102 * Set the current value of the progress.
103 *
104 * @param value the current value of the progress
105 */
106 public void setValue(final int value) {
107 this.value = value;
108 }
109
110 /**
111 * Public constructor.
112 *
113 * @param parent parent widget
114 * @param x column relative to parent
115 * @param y row relative to parent
116 * @param width width of progress bar
117 * @param value initial value of percent complete
118 */
119 public TProgressBar(final TWidget parent, final int x, final int y,
120 final int width, final int value) {
121
122 // Set parent and window
123 super(parent, false, x, y, width, 1);
124
125 this.value = value;
126 }
127
128 /**
129 * Draw a static progress bar.
130 */
131 @Override
132 public void draw() {
133 CellAttributes completeColor = getTheme().getColor("tprogressbar.complete");
134 CellAttributes incompleteColor = getTheme().getColor("tprogressbar.incomplete");
135
136 float progress = ((float)value - minValue) / ((float)maxValue - minValue);
137 int progressInt = (int)(progress * 100);
138 int progressUnit = 100 / (getWidth() - 2);
139
140 getScreen().putCharXY(0, 0, GraphicsChars.CP437[0xC3], incompleteColor);
141 for (int i = 0; i < getWidth() - 2; i++) {
142 float iProgress = (float)i / (getWidth() - 2);
143 int iProgressInt = (int)(iProgress * 100);
144 if (iProgressInt <= progressInt - progressUnit) {
145 getScreen().putCharXY(i + 1, 0, GraphicsChars.BOX,
146 completeColor);
147 } else {
148 getScreen().putCharXY(i + 1, 0, GraphicsChars.SINGLE_BAR,
149 incompleteColor);
150 }
151 }
152 if (value >= maxValue) {
153 getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars.BOX,
154 completeColor);
155 } else {
156 getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars.SINGLE_BAR,
157 incompleteColor);
158 }
159 getScreen().putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0xB4],
160 incompleteColor);
161 }
162
163 }