[Schmitzm-commits] r2374 - in trunk/schmitzm-core/src/main/java/de/schmitzm/swing: . event

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Jul 26 19:12:22 CEST 2013


Author: mojays
Date: 2013-07-26 19:12:22 +0200 (Fri, 26 Jul 2013)
New Revision: 2374

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/SmoothProgressBarUpdater.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ProgressBarWithText.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
Log:
new SmoothProgressBarUpdater
ProgressBarWithText: configures to update smooth

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ProgressBarWithText.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ProgressBarWithText.java	2013-07-26 15:12:46 UTC (rev 2373)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ProgressBarWithText.java	2013-07-26 17:12:22 UTC (rev 2374)
@@ -15,7 +15,9 @@
 
 /**
  * Extends the {@link JProgressBar} with a static text
- * label which is drawn over the bar. 
+ * label which is drawn over the bar. Further more (because most times
+ * this progress bar is large) the bar is configure to update smooth.
+ * @see SwingUtil#setProgressBarUpdateSmooth(JProgressBar) 
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  *
  */
@@ -27,6 +29,7 @@
    */
   public ProgressBarWithText() {
     super();
+    init();
   }
 
   /**
@@ -34,6 +37,7 @@
    */
   public ProgressBarWithText(BoundedRangeModel newModel) {
     super(newModel);
+    init();
   }
 
   /**
@@ -41,6 +45,7 @@
    */
   public ProgressBarWithText(int orient, int min, int max) {
     super(orient, min, max);
+    init();
   }
 
   /**
@@ -48,6 +53,7 @@
    */
   public ProgressBarWithText(int min, int max) {
     super(min, max);
+    init();
   }
 
   /**
@@ -55,7 +61,15 @@
    */
   public ProgressBarWithText(int orient) {
     super(orient);
+    init();
   }
+  
+  /**
+   * Called by every constructor to do further initialization.
+   */
+  protected void init() {
+    SwingUtil.setProgressBarUpdateSmooth(this);
+  }
 
   /**
    * Returns the text shown on top of the bar.

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2013-07-26 15:12:46 UTC (rev 2373)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2013-07-26 17:12:22 UTC (rev 2374)
@@ -123,7 +123,9 @@
 import javax.swing.SwingUtilities;
 import javax.swing.UIDefaults;
 import javax.swing.UIManager;
+import javax.swing.event.ChangeListener;
 import javax.swing.plaf.FontUIResource;
+import javax.swing.plaf.basic.BasicProgressBarUI;
 import javax.swing.table.DefaultTableCellRenderer;
 import javax.swing.table.TableCellEditor;
 import javax.swing.table.TableCellRenderer;
@@ -146,6 +148,7 @@
 import de.schmitzm.io.IOUtil;
 import de.schmitzm.lang.LangUtil;
 import de.schmitzm.lang.ResourceProvider;
+import de.schmitzm.swing.event.SmoothProgressBarUpdater;
 import de.schmitzm.swing.input.BooleanInputOption;
 import de.schmitzm.swing.input.InputOption;
 import de.schmitzm.swing.input.ManualInputOption;
@@ -2618,6 +2621,22 @@
       else
         EventQueue.invokeLater(runnable);
     }
+    
+    /**
+     * Adds a listener to the progress bar which updates the bar every time the
+     * bar should growth at least 1 pixel.<br>
+     * Note: The behavior of {@link BasicProgressBarUI} is to update the progress bar
+     * only on full percentages. For large progress bars (e.g. in application frame
+     * status bar) and many small progress steps this means that there is a delay of
+     * more than a second between updates and the progress bar increases by several pixels
+     * each time
+     * @see SmoothProgressBarUpdater
+     */
+    public static ChangeListener setProgressBarUpdateSmooth(JProgressBar progressBar) {
+      final ChangeListener repaintRequiredListener = new SmoothProgressBarUpdater(progressBar);
+      progressBar.addChangeListener(repaintRequiredListener);
+      return repaintRequiredListener;
+    }
 
     /**
      * Sets a {@link JProgressBar} to (in)determinate with the given maximum value.

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/SmoothProgressBarUpdater.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/SmoothProgressBarUpdater.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/event/SmoothProgressBarUpdater.java	2013-07-26 17:12:22 UTC (rev 2374)
@@ -0,0 +1,73 @@
+package de.schmitzm.swing.event;
+
+import java.awt.Insets;
+
+import javax.swing.BoundedRangeModel;
+import javax.swing.JProgressBar;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import javax.swing.plaf.basic.BasicProgressBarUI;
+
+/**
+ * The behavior of {@link BasicProgressBarUI} is to update the progress bar
+ * only on full percentages.
+ * For large progress bars (e.g. in application frame status bar) and
+ * many small progress steps this means that there is a delay of more than a
+ * second between updates and the progress bar increases by several pixels.<br>
+ * This listener does not check the percentage, but the necessary growth (in pixels)
+ * on each value change and forces a {@link JProgressBar#repaint()} every time there
+ * is a growth of at least 1 pixel. 
+ * @author Martin O.J. Schmitz
+ */
+public class SmoothProgressBarUpdater implements ChangeListener {
+  protected JProgressBar progressBar;
+  /** Holds the progress bar width (amount of pixels) on last update */
+  protected int oldAmountFull = -1;
+  
+  /**
+   * Creates new listener.
+   * @param progressBar
+   */
+  public SmoothProgressBarUpdater(JProgressBar progressBar) {
+    this.progressBar = progressBar;
+  }
+
+  /**
+   * Determines the amount of the progress bar that should be filled
+   * based on the percent done gathered from the model.<br>
+   * Mainly a copy of {@link BasicProgressBarUI#getAmountFull(..)}.
+   */
+  protected int getAmountFull() {
+    Insets b = progressBar.getInsets(); // area for border
+    int width = progressBar.getWidth() - (b.right + b.left);
+    int height = progressBar.getHeight() - (b.top + b.bottom);
+    
+    int amountFull = 0;
+    BoundedRangeModel model = progressBar.getModel();
+
+    if ( (model.getMaximum() - model.getMinimum()) != 0) {
+        if (progressBar.getOrientation() == JProgressBar.HORIZONTAL) {
+            amountFull = (int)Math.round(width *
+                                         progressBar.getPercentComplete());
+        } else {
+            amountFull = (int)Math.round(height *
+                                         progressBar.getPercentComplete());
+        }
+    }
+    return amountFull;
+  }
+  
+  /**
+   * Calls {@link JProgressBar#repaint()} each time the progress
+   * increases at least 1 pixel.
+   */
+  @Override
+  public void stateChanged(ChangeEvent e) {
+    int amountFull = getAmountFull();
+    if (amountFull != oldAmountFull) {
+      oldAmountFull = amountFull;
+      progressBar.repaint();
+    }
+  }
+
+}



More information about the Schmitzm-commits mailing list