| 1 | /* |
| 2 | * Jexer - Java Text User Interface |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (C) 2017 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 | |
| 34 | /** |
| 35 | * TProgressBar implements a simple progress bar. |
| 36 | */ |
| 37 | public final class TProgressBar extends TWidget { |
| 38 | |
| 39 | /** |
| 40 | * Value that corresponds to 0% progress. |
| 41 | */ |
| 42 | private int minValue = 0; |
| 43 | |
| 44 | /** |
| 45 | * Get the value that corresponds to 0% progress. |
| 46 | * |
| 47 | * @return the value that corresponds to 0% progress |
| 48 | */ |
| 49 | public int getMinValue() { |
| 50 | return minValue; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Set the value that corresponds to 0% progress. |
| 55 | * |
| 56 | * @param minValue the value that corresponds to 0% progress |
| 57 | */ |
| 58 | public void setMinValue(final int minValue) { |
| 59 | this.minValue = minValue; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Value that corresponds to 100% progress. |
| 64 | */ |
| 65 | private int maxValue = 100; |
| 66 | |
| 67 | /** |
| 68 | * Get the value that corresponds to 100% progress. |
| 69 | * |
| 70 | * @return the value that corresponds to 100% progress |
| 71 | */ |
| 72 | public int getMaxValue() { |
| 73 | return maxValue; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Set the value that corresponds to 100% progress. |
| 78 | * |
| 79 | * @param maxValue the value that corresponds to 100% progress |
| 80 | */ |
| 81 | public void setMaxValue(final int maxValue) { |
| 82 | this.maxValue = maxValue; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Current value of the progress. |
| 87 | */ |
| 88 | private int value = 0; |
| 89 | |
| 90 | /** |
| 91 | * Get the current value of the progress. |
| 92 | * |
| 93 | * @return the current value of the progress |
| 94 | */ |
| 95 | public int getValue() { |
| 96 | return value; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Set the current value of the progress. |
| 101 | * |
| 102 | * @param value the current value of the progress |
| 103 | */ |
| 104 | public void setValue(final int value) { |
| 105 | this.value = value; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Public constructor. |
| 110 | * |
| 111 | * @param parent parent widget |
| 112 | * @param x column relative to parent |
| 113 | * @param y row relative to parent |
| 114 | * @param width width of progress bar |
| 115 | * @param value initial value of percent complete |
| 116 | */ |
| 117 | public TProgressBar(final TWidget parent, final int x, final int y, |
| 118 | final int width, final int value) { |
| 119 | |
| 120 | // Set parent and window |
| 121 | super(parent, false, x, y, width, 1); |
| 122 | |
| 123 | this.value = value; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Draw a static progress bar. |
| 128 | */ |
| 129 | @Override |
| 130 | public void draw() { |
| 131 | CellAttributes completeColor = getTheme().getColor("tprogressbar.complete"); |
| 132 | CellAttributes incompleteColor = getTheme().getColor("tprogressbar.incomplete"); |
| 133 | |
| 134 | float progress = ((float)value - minValue) / ((float)maxValue - minValue); |
| 135 | int progressInt = (int)(progress * 100); |
| 136 | int progressUnit = 100 / (getWidth() - 2); |
| 137 | |
| 138 | getScreen().putCharXY(0, 0, GraphicsChars.CP437[0xC3], incompleteColor); |
| 139 | for (int i = 0; i < getWidth() - 2; i++) { |
| 140 | float iProgress = (float)i / (getWidth() - 2); |
| 141 | int iProgressInt = (int)(iProgress * 100); |
| 142 | if (iProgressInt <= progressInt - progressUnit) { |
| 143 | getScreen().putCharXY(i + 1, 0, GraphicsChars.BOX, |
| 144 | completeColor); |
| 145 | } else { |
| 146 | getScreen().putCharXY(i + 1, 0, GraphicsChars.SINGLE_BAR, |
| 147 | incompleteColor); |
| 148 | } |
| 149 | } |
| 150 | if (value >= maxValue) { |
| 151 | getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars.BOX, |
| 152 | completeColor); |
| 153 | } else { |
| 154 | getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars.SINGLE_BAR, |
| 155 | incompleteColor); |
| 156 | } |
| 157 | getScreen().putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0xB4], |
| 158 | incompleteColor); |
| 159 | } |
| 160 | |
| 161 | } |