[Schmitzm-commits] r2316 - in trunk/schmitzm-core/src/main/java/de/schmitzm: io swing

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Mon May 27 17:00:12 CEST 2013


Author: mojays
Date: 2013-05-27 17:00:12 +0200 (Mon, 27 May 2013)
New Revision: 2316

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ProgressBarUpdater.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/ConsoleProgressBar.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/ProgressUpdater.java
Log:
ProgressUpdater improved

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java	2013-05-24 14:42:12 UTC (rev 2315)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java	2013-05-27 15:00:12 UTC (rev 2316)
@@ -102,10 +102,35 @@
    * @throws UnsupportedOperationException if {@link #initProgress(int, int)} was not yet called
    */
   public void updateProgress(int value) {
+    double perc = calculatePercentage(value);
+    updateProgress(perc);
+  }
+
+  /**
+   * Calculates the percentage for a given value.
+   */
+  protected double calculatePercentage(int value) {
+    checkInitialized();
+    double perc = (value - minValue) * 100.0 / (maxValue - minValue);
+    return perc;
+  }
+
+  /**
+   * Calculates the absolute value from percentage.
+   */
+  protected int calculateValue(double perc) {
+    checkInitialized();
+    int value = (int)Math.round(perc/100.0 * (maxValue - minValue) + minValue);
+    return value;
+  }
+
+  /**
+   * Checks whether the updater is already initialized. If not and
+   * {@link UnsupportedOperationException} is thrown.
+   */
+  protected void checkInitialized() {
     if ( !isInitialized() )
       throw new UnsupportedOperationException("ProgressUpdater not yet initialized!");
-    double perc = (value - minValue) * 100.0 / (maxValue - minValue);
-    updateProgress(perc);
   }
 
   /**

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/ConsoleProgressBar.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/ConsoleProgressBar.java	2013-05-24 14:42:12 UTC (rev 2315)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/ConsoleProgressBar.java	2013-05-27 15:00:12 UTC (rev 2316)
@@ -59,6 +59,7 @@
    */
   @Override
   public void updateProgress(double perc) {
+    checkInitialized();
     if ( lastPerc == 100 )
       return;
 //    int perc = (int)Math.round(cellsProcessed*100.0/cellsTotal);

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/ProgressUpdater.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/ProgressUpdater.java	2013-05-24 14:42:12 UTC (rev 2315)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/ProgressUpdater.java	2013-05-27 15:00:12 UTC (rev 2316)
@@ -33,7 +33,8 @@
 
 /**
  * Interface to update process during time-consuming processes.
- * May implement a {@link JProgressBar} update or some progress bar on {@link System#out}.
+ * May implement a {@link JProgressBar} update or some progress bar on {@link System#out}.<br>
+ * <b>Note:</b> Process must support the use of {@link ProgressUpdater}!
  * @author Martin O.J. Schmitz
  *
  */

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ProgressBarUpdater.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ProgressBarUpdater.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ProgressBarUpdater.java	2013-05-27 15:00:12 UTC (rev 2316)
@@ -0,0 +1,106 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.swing;
+
+import javax.swing.JProgressBar;
+
+import de.schmitzm.io.AbstractProgressUpdater;
+import de.schmitzm.io.ProgressUpdater;
+
+/**
+ * Implementation of {@link ProgressUpdater} to updates a {@link JProgressBar}
+ * on EDT during time-consuming processes.<br>
+ * Note: Process must support the use of {@link ProgressUpdater}!
+ * @see SwingUtil#setProgressBarDeterminateOnEDT(JProgressBar, int, int)
+ * @see SwingUtil#updateProgressBarValueOnEDT(JProgressBar, int)
+ * @author Martin O.J. Schmitz
+ */
+public class ProgressBarUpdater extends AbstractProgressUpdater {
+
+  /** Holds the {@link JProgressBar} to be updated. */
+  protected JProgressBar progressBar = null;
+
+  /**
+   * Creates a new updater.
+   * @param progressBar progress bar to update
+   */
+  public ProgressBarUpdater(JProgressBar progressBar) {
+    super();
+    if ( progressBar == null )
+      throw new IllegalArgumentException("JProgressBar not allowed to be NULL!");
+    this.progressBar = progressBar;
+  }
+  
+  
+  
+  /**
+   * Returns the {@link JProgressBar} which is updated.
+   */
+  public JProgressBar getProgressBar() {
+    return progressBar;
+  }
+  
+  /**
+   * Initializes the progress and prints a percentage bar to
+   * console.
+   * @see SwingUtil#setProgressBarDeterminateOnEDT(JProgressBar, int, int)
+   */
+  @Override
+  public void initProgress(int minValue, int maxValue) {
+    super.initProgress(minValue, maxValue);
+    SwingUtil.setProgressBarDeterminateOnEDT(progressBar, minValue, maxValue);
+  }
+  
+  /**
+   * Updates the progress status with the given value.
+   * Calculates the percentage and calls {@link #updateProgress(double)}.
+   * @param value value between {@link #getMinimumValue()} and {@link #getMaximumValue()}
+   * @throws UnsupportedOperationException if {@link #initProgress(int, int)} was not yet called
+   * @see SwingUtil#updateProgressBarValueOnEDT(JProgressBar, int)
+   */
+  @Override
+  public void updateProgress(int value) {
+    checkInitialized();
+    SwingUtil.updateProgressBarValueOnEDT(progressBar, value);
+  }
+
+  /**
+   * Updates the progress status with the given value.
+   * Calculates the absolute value and calls {@link #updateProgress(int)}.
+   * @param perc value between 0 and 100
+   * @throws UnsupportedOperationException if {@link #initProgress(int, int)} was not yet called
+   * @see SwingUtil#updateProgressBarValueOnEDT(JProgressBar, int)
+   */
+  @Override
+  public void updateProgress(double perc) {
+    int value = calculateValue(perc);
+    updateProgress(value);
+  }
+}



More information about the Schmitzm-commits mailing list