#53 progress bar style
authorKevin Lamonte <kevin.lamonte@gmail.com>
Fri, 23 Aug 2019 21:47:22 +0000 (16:47 -0500)
committerKevin Lamonte <kevin.lamonte@gmail.com>
Fri, 23 Aug 2019 21:47:22 +0000 (16:47 -0500)
src/jexer/TLabel.java
src/jexer/TProgressBar.java
src/jexer/demos/DemoMainWindow.java

index f34a533eb8ffb44c1d2aa9ebefc2a2e925a89a66..cc341cfa862964ed9097c425ea4d10b55627ac0c 100644 (file)
@@ -158,9 +158,9 @@ public class TLabel extends TWidget {
         final TAction action) {
 
         // Set parent and window
-        super(parent, false, x, y, StringUtils.width(text), 1);
+        super(parent, false, x, y, 0, 1);
 
-        mnemonic = new MnemonicString(text);
+        setLabel(text);
         this.colorKey = colorKey;
         this.useWindowBackground = useWindowBackground;
         this.action = action;
@@ -170,6 +170,16 @@ public class TLabel extends TWidget {
     // TWidget ----------------------------------------------------------------
     // ------------------------------------------------------------------------
 
+    /**
+     * Override TWidget's width: we can only set width at construction time.
+     *
+     * @param width new widget width (ignored)
+     */
+    @Override
+    public void setWidth(final int width) {
+        // Do nothing
+    }
+
     /**
      * Override TWidget's height: we can only set height at construction
      * time.
@@ -232,6 +242,7 @@ public class TLabel extends TWidget {
      */
     public void setLabel(final String label) {
         mnemonic = new MnemonicString(label);
+        super.setWidth(StringUtils.width(mnemonic.getRawLabel()));
     }
 
     /**
index 1e3b55a34e17d05c4717aea78ed9e20b8e5a3b43..6ed5ccf752dbb8553bf7308be4165c165f6a8b43 100644 (file)
@@ -30,6 +30,7 @@ package jexer;
 
 import jexer.bits.CellAttributes;
 import jexer.bits.GraphicsChars;
+import jexer.bits.StringUtils;
 
 /**
  * TProgressBar implements a simple progress bar.
@@ -55,6 +56,26 @@ public class TProgressBar extends TWidget {
      */
     private int value = 0;
 
+    /**
+     * The left border character.
+     */
+    private int leftBorderChar = GraphicsChars.CP437[0xC3];
+
+    /**
+     * The filled-in part of the bar.
+     */
+    private int completedChar = GraphicsChars.BOX;
+
+    /**
+     * The remaining to be filled in part of the bar.
+     */
+    private int remainingChar = GraphicsChars.SINGLE_BAR;
+
+    /**
+     * The right border character.
+     */
+    private int rightBorderChar = GraphicsChars.CP437[0xB4];
+
     // ------------------------------------------------------------------------
     // Constructors -----------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -109,24 +130,29 @@ public class TProgressBar extends TWidget {
         int progressInt = (int)(progress * 100);
         int progressUnit = 100 / (getWidth() - 2);
 
-        putCharXY(0, 0, GraphicsChars.CP437[0xC3], incompleteColor);
-        for (int i = 0; i < getWidth() - 2; i++) {
+        putCharXY(0, 0, leftBorderChar, incompleteColor);
+        for (int i = StringUtils.width(leftBorderChar); i < getWidth() - 2;) {
             float iProgress = (float)i / (getWidth() - 2);
             int iProgressInt = (int)(iProgress * 100);
             if (iProgressInt <= progressInt - progressUnit) {
-                putCharXY(i + 1, 0, GraphicsChars.BOX, completeColor);
+                putCharXY(i, 0, completedChar, completeColor);
+                i += StringUtils.width(completedChar);
             } else {
-                putCharXY(i + 1, 0, GraphicsChars.SINGLE_BAR, incompleteColor);
+                putCharXY(i, 0, remainingChar, incompleteColor);
+                i += StringUtils.width(remainingChar);
             }
         }
         if (value >= maxValue) {
-            putCharXY(getWidth() - 2, 0, GraphicsChars.BOX, completeColor);
+            putCharXY(getWidth() - StringUtils.width(leftBorderChar) -
+                StringUtils.width(rightBorderChar), 0, completedChar,
+                completeColor);
         } else {
-            putCharXY(getWidth() - 2, 0, GraphicsChars.SINGLE_BAR,
+            putCharXY(getWidth() - StringUtils.width(leftBorderChar) -
+                StringUtils.width(rightBorderChar), 0, remainingChar,
                 incompleteColor);
         }
-        putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0xB4],
-            incompleteColor);
+        putCharXY(getWidth() - StringUtils.width(rightBorderChar), 0,
+            rightBorderChar, incompleteColor);
     }
 
     // ------------------------------------------------------------------------
@@ -187,4 +213,76 @@ public class TProgressBar extends TWidget {
         this.value = value;
     }
 
+    /**
+     * Set the left border character.
+     *
+     * @param ch the char to use
+     */
+    public void setLeftBorderChar(final int ch) {
+        leftBorderChar = ch;
+    }
+
+    /**
+     * Get the left border character.
+     *
+     * @return the char
+     */
+    public int getLeftBorderChar() {
+        return leftBorderChar;
+    }
+
+    /**
+     * Set the filled-in part of the bar.
+     *
+     * @param ch the char to use
+     */
+    public void setCompletedChar(final int ch) {
+        completedChar = ch;
+    }
+
+    /**
+     * Get the filled-in part of the bar.
+     *
+     * @return the char
+     */
+    public int getCompletedChar() {
+        return completedChar;
+    }
+
+    /**
+     * Set the remaining to be filled in part of the bar.
+     *
+     * @param ch the char to use
+     */
+    public void setRemainingChar(final int ch) {
+        remainingChar = ch;
+    }
+
+    /**
+     * Get the remaining to be filled in part of the bar.
+     *
+     * @return the char
+     */
+    public int getRemainingChar() {
+        return remainingChar;
+    }
+
+    /**
+     * Set the right border character.
+     *
+     * @param ch the char to use
+     */
+    public void setRightBorderChar(final int ch) {
+        rightBorderChar = ch;
+    }
+
+    /**
+     * Get the right border character.
+     *
+     * @return the char
+     */
+    public int getRightBorderChar() {
+        return rightBorderChar;
+    }
+
 }
index 0be7088556e959b2c64c9d725e316ebbfb1b7321..8f77448b29f45c65cd67e52c38268e0564ad1e0d 100644 (file)
@@ -66,7 +66,12 @@ public class DemoMainWindow extends TWindow {
     /**
      * Timer that increments a number.
      */
-    private TTimer timer;
+    private TTimer timer1;
+
+    /**
+     * Timer that increments a number.
+     */
+    private TTimer timer2;
 
     /**
      * Timer label is updated with timer ticks.
@@ -77,13 +82,25 @@ public class DemoMainWindow extends TWindow {
      * Timer increment used by the timer loop.  Has to be at class scope so
      * that it can be accessed by the anonymous TAction class.
      */
-    int timerI = 0;
+    int timer1I = 0;
+
+    /**
+     * Timer increment used by the timer loop.  Has to be at class scope so
+     * that it can be accessed by the anonymous TAction class.
+     */
+    int timer2I = 0;
 
     /**
      * Progress bar used by the timer loop.  Has to be at class scope so that
      * it can be accessed by the anonymous TAction class.
      */
-    TProgressBar progressBar;
+    TProgressBar progressBar1;
+
+    /**
+     * Progress bar used by the timer loop.  Has to be at class scope so that
+     * it can be accessed by the anonymous TAction class.
+     */
+    TProgressBar progressBar2;
 
     // ------------------------------------------------------------------------
     // Constructors -----------------------------------------------------------
@@ -235,22 +252,43 @@ public class DemoMainWindow extends TWindow {
         );
 
         row = 15;
-        progressBar = addProgressBar(48, row, 12, 0);
+        progressBar1 = addProgressBar(48, row, 12, 0);
         row++;
         timerLabel = addLabel(i18n.getString("timerLabel"), 48, row);
-        timer = getApplication().addTimer(250, true,
+        timer1 = getApplication().addTimer(250, true,
             new TAction() {
 
                 public void DO() {
                     timerLabel.setLabel(String.format(i18n.
-                            getString("timerText"), timerI));
+                            getString("timerText"), timer1I));
                     timerLabel.setWidth(timerLabel.getLabel().length());
-                    if (timerI < 100) {
-                        timerI++;
+                    if (timer1I < 100) {
+                        timer1I++;
+                    } else {
+                        timer1.setRecurring(false);
+                    }
+                    progressBar1.setValue(timer1I);
+                }
+            }
+        );
+
+        row += 2;
+        progressBar2 = addProgressBar(48, row, 12, 0);
+        progressBar2.setLeftBorderChar('\u255e');
+        progressBar2.setRightBorderChar('\u2561');
+        progressBar2.setCompletedChar('\u2592');
+        progressBar2.setRemainingChar('\u2550');
+        row++;
+        timer2 = getApplication().addTimer(125, true,
+            new TAction() {
+
+                public void DO() {
+                    if (timer2I < 100) {
+                        timer2I++;
                     } else {
-                        timer.setRecurring(false);
+                        timer2.setRecurring(false);
                     }
-                    progressBar.setValue(timerI);
+                    progressBar2.setValue(timer2I);
                 }
             }
         );
@@ -293,7 +331,8 @@ public class DemoMainWindow extends TWindow {
      */
     @Override
     public void onClose() {
-        getApplication().removeTimer(timer);
+        getApplication().removeTimer(timer1);
+        getApplication().removeTimer(timer2);
     }
 
     /**