[Schmitzm-commits] r2315 - trunk/schmitzm-core/src/main/java/de/schmitzm/io

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri May 24 16:42:12 CEST 2013


Author: mojays
Date: 2013-05-24 16:42:12 +0200 (Fri, 24 May 2013)
New Revision: 2315

Added:
   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:
new ProgressUpdater interface for generically update a progress bar during time consuming processes (e.g. JProgressBar or Console progress bar)

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/AbstractProgressUpdater.java	2013-05-24 14:42:12 UTC (rev 2315)
@@ -0,0 +1,119 @@
+/**
+ * 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.io;
+
+
+/**
+ * Abstract implementation of {@link ProgressUpdater}.
+ * @author Martin O.J. Schmitz
+ *
+ */
+public abstract class AbstractProgressUpdater implements ProgressUpdater {
+  /** Holds the minimum value for the progress which represents 0%. */
+  protected int minValue = 0;
+  /** Holds the maximum value for the progress which represents 100%. */
+  protected int maxValue = 0;
+  /** Indicates whether {@link #initProgress()} or {@link #initProgress(int, int)} was
+   *  already called. */
+  protected boolean initalized = false;
+  
+  /**
+   * Returns whether {@link #initProgress()} or {@link #initProgress(int, int)} was
+   * already called.
+   */
+  public boolean isInitialized() {
+    return initalized;
+  }
+  
+  /**
+   * Returns the minimum value which represents 0%.
+   * @throws UnsupportedOperationException if {@link #initProgress(int, int)} was not yet called
+   */
+  public int getMinimumValue() {
+    if ( !isInitialized() )
+      throw new UnsupportedOperationException("ProgressUpdater not yet initialized!");
+    return minValue;
+  }
+  
+  /**
+   * Returns the maximum value which represents 100%.
+   * @throws UnsupportedOperationException if {@link #initProgress(int, int)} was not yet called
+   */
+  public int getMaximumValue() {
+    if ( !isInitialized() )
+      throw new UnsupportedOperationException("ProgressUpdater not yet initialized!");
+    return maxValue;
+  }
+
+  /**
+   * Initializes the progress for (integer) values between 0 and 100.
+   * Simply calls {@link #initProgress(int, int) initProgress(0, 100)}
+   */
+  public void initProgress() {
+    initProgress(0,100);
+  }
+
+  /**
+   * Initializes the progress. For further initializations, sub-classes must
+   * override this method and implement extensions after calling super-method. 
+   * @param minValue value which represents 0%
+   * @param maxValue value which represents 100%
+   * @throws UnsupportedOperationException if {@code minValue >= maxValue}
+   */
+  public void initProgress(int minValue, int maxValue) {
+    if ( minValue >= maxValue )
+      throw new UnsupportedOperationException("Progress minimum value has to be lesser than maximum.");
+    this.minValue = minValue;
+    this.maxValue = maxValue;
+    this.initalized = true;
+  }
+
+  /**
+   * 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
+   */
+  public void updateProgress(int value) {
+    if ( !isInitialized() )
+      throw new UnsupportedOperationException("ProgressUpdater not yet initialized!");
+    double perc = (value - minValue) * 100.0 / (maxValue - minValue);
+    updateProgress(perc);
+  }
+
+  /**
+   * Terminates the progress. Should especially called in case of error
+   * during process.
+   */
+  public void dispose() {
+    this.initalized = false;
+  }
+
+}

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/io/ConsoleProgressBar.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/ConsoleProgressBar.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/ConsoleProgressBar.java	2013-05-24 14:42:12 UTC (rev 2315)
@@ -0,0 +1,82 @@
+/**
+ * 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.io;
+
+/**
+ * Shows and updates a progress bar (0 to 100%) on {@link System#out}.<br>
+ * IMPORTANT: No other console outputs should be printed during progress update!
+ * @author Martin O.J. Schmitz
+ *
+ */
+public class ConsoleProgressBar extends AbstractProgressUpdater {
+
+  /** Holds the last printed percentage value. */
+  protected int lastPerc = 0;
+  
+  /**
+   * Initializes the progress and prints a percentage bar to
+   * console.
+   */
+  @Override
+  public void initProgress(int minValue, int maxValue) {
+    super.initProgress(minValue, maxValue);
+    this.lastPerc = 0;
+    System.out.println("  0%|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|100%");
+    System.out.print(  "    |");
+  }
+  
+  /**
+   * Updates the progress status with the given value.<br>
+   * IMPORTANT: No other console outputs should be printed during progress update!
+   * @param perc percentage value
+   */
+  @Override
+  public void updateProgress(double perc) {
+    if ( lastPerc == 100 )
+      return;
+//    int perc = (int)Math.round(cellsProcessed*100.0/cellsTotal);
+    int percRounded = (int)Math.round(perc);
+    if ( percRounded != lastPerc ) {
+      for (int i=lastPerc; i<percRounded; i++)
+        System.out.print(percRounded < 100 ? ">" : "|");
+      lastPerc = percRounded;
+    }
+  }
+
+  /**
+   * Terminates the progress. Should especially called in case of error
+   * during process.
+   */
+  public void dispose() {
+    super.dispose();
+    System.out.println();
+  }
+
+}

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/io/ProgressUpdater.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/ProgressUpdater.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/ProgressUpdater.java	2013-05-24 14:42:12 UTC (rev 2315)
@@ -0,0 +1,84 @@
+/**
+ * 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.io;
+
+import javax.swing.JProgressBar;
+
+/**
+ * Interface to update process during time-consuming processes.
+ * May implement a {@link JProgressBar} update or some progress bar on {@link System#out}.
+ * @author Martin O.J. Schmitz
+ *
+ */
+public interface ProgressUpdater {
+
+  /**
+   * Returns the maximum value which represents 100%.
+   * @throws UnsupportedOperationException if {@link #initProgress(int, int)} was not yet called
+   */
+  public int getMaximumValue();
+
+
+  /**
+   * Returns the minimum value which represents 0%.
+   * @throws UnsupportedOperationException if {@link #initProgress(int, int)} was not yet called
+   */
+  public int getMinimumValue();
+
+  /**
+   * Initializes the progress for (integer) values between 0 and 100.
+   */
+  public void initProgress();
+  
+  /**
+   * Initializes the progress.
+   * @param minValue value which represents 0%
+   * @param maxValue value which represents 100%
+   */
+  public void initProgress(int minValue, int maxValue);
+
+  /**
+   * Updates the progress status with the given value.
+   * @param perc percentage value
+   */
+  public void updateProgress(double perc);
+
+  /**
+   * Updates the progress status with the given value.
+   * @param value value between {@link #getMinimumValue()} and {@link #getMaximumValue()}
+   */
+  public void updateProgress(int value);
+
+  /**
+   * Terminates the progress. Should especially called in case of error
+   * during process.
+   */
+  public void dispose();
+}



More information about the Schmitzm-commits mailing list