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

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Apr 25 16:07:58 CEST 2012


Author: mojays
Date: 2012-04-25 16:07:58 +0200 (Wed, 25 Apr 2012)
New Revision: 1965

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingWorker.java
Log:
IOUtil: method to create temporary file
SwingUtil.setProgressBarDeterminateOnEDT(.) extended by minimum value
SwingWorker: extended by methods to easier access/update progress bar (on EDT)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2012-04-23 21:52:56 UTC (rev 1964)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2012-04-25 14:07:58 UTC (rev 1965)
@@ -542,6 +542,20 @@
         output.flush();
         output.close();
     }
+
+    /**
+     * Creates a file a local temporary directory.
+     * @param prefix prefix of filename (automatically extended by unique key)
+     * @param suffix file extension for temporary file including dot "." (if {@code null}
+     *               ".tmp" will be used)
+     * @param deleteOnExit indicates whether file is automatically deleted after JVM is finished 
+     */
+    public static File createTemporaryFile(String prefix, String suffix, boolean deleteOnExit) throws IOException {
+      File file = File.createTempFile(prefix,suffix);
+      if ( deleteOnExit )
+        file.deleteOnExit();
+      return file;
+    }
     
     /**
      * Downloads an {@link URL} to local file.
@@ -563,10 +577,8 @@
      * @param timeout connection timeout in ms
      */
     public static File downloadUrlToFile(URL url, Proxy proxy, File file, int timeout) throws IOException {
-      if ( file == null ) {
-        file = File.createTempFile("java-schmitzm-",null);
-        file.deleteOnExit();
-      }
+      if ( file == null )
+        file = createTemporaryFile("java-schmitzm-",null,true);
       if ( proxy == null )
         proxy = Proxy.NO_PROXY;
       

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-04-23 21:52:56 UTC (rev 1964)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-04-25 14:07:58 UTC (rev 1965)
@@ -2239,9 +2239,21 @@
      * @param maxValue maximum value (if <=0 the bar is set to indeterminate!)
      */
     public static void setProgressBarDeterminateOnEDT(final JProgressBar bar, final int maxValue) {
+      setProgressBarDeterminateOnEDT(bar, 0, maxValue);
+    }
+    
+    /**
+     * Sets a {@link JProgressBar} to (in)determinate with the given maximum value.
+     * This is done using {@link SwingUtilities#invokeLater(Runnable)} if
+     * the method is not called on EDT.
+     * The progress bar is set to indeterminate if {@code minValue > maxValue}
+     * @param minValue minimum value 
+     * @param maxValue maximum value
+     */
+    public static void setProgressBarDeterminateOnEDT(final JProgressBar bar, final int minValue, final int maxValue) {
       if ( bar == null )
         return;
-      final boolean indeterminate = (maxValue <= 0);
+      final boolean indeterminate = (maxValue < minValue);
       // define work
       Runnable work = new Runnable() {
         @Override
@@ -2249,7 +2261,7 @@
           bar.setIndeterminate(indeterminate);
           bar.setStringPainted(!indeterminate);
           if ( !indeterminate ) {
-            bar.setMinimum(0);
+            bar.setMinimum(minValue);
             bar.setMaximum(maxValue);
           }
         }
@@ -2260,7 +2272,7 @@
       else
         SwingUtilities.invokeLater(work);
     }
-    
+
     /**
      * Updates the value of a {@link JProgressBar}.
      * This is done using {@link SwingUtilities#invokeLater(Runnable)} if

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingWorker.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingWorker.java	2012-04-23 21:52:56 UTC (rev 1964)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingWorker.java	2012-04-25 14:07:58 UTC (rev 1965)
@@ -31,8 +31,10 @@
 
 import java.awt.Frame;
 
+import javax.swing.JProgressBar;
 import javax.swing.SwingUtilities;
 
+import de.schmitzm.lang.LangUtil;
 import de.schmitzm.lang.WorkingThread;
 
 /**
@@ -281,5 +283,47 @@
     public SwingWorker getSwingWorker() {
       return worker;
     }
+
+    /**
+     * Returns the progress bar of the executing {@link SwingWorker}.
+     * @return {@code null} if there is no executing {@link SwingWorker} or
+     *         no dialog with progress bar
+     */
+    public JProgressBar getProgressBar() {
+      if ( getSwingWorker() == null || 
+          getSwingWorker().getDialog() == null )
+        return null;
+      return getSwingWorker().getDialog().getProgressBar();
+    }
+    
+    /**
+     * Sets the minimum and maximum value for the progress bar of the executing
+     * {@link SwingWorker}.
+     * Does nothing if there is no executing {@link SwingWorker} or no
+     * progress bar.
+     */
+    public void setProgressRange(int minimum, int maximum) {
+      SwingUtil.setProgressBarDeterminateOnEDT(getProgressBar(), minimum, maximum);
+    }
+
+    /**
+     * Sets the value for the progress bar of the executing
+     * {@link SwingWorker}.
+     * Does nothing if there is no executing {@link SwingWorker} or no
+     * progress bar.
+     */
+    public void setProgress(int value) {
+      SwingUtil.updateProgressBarValueOnEDT(getProgressBar(), value);
+    }
+
+    /**
+     * Sets the progress bar of the executing {@link SwingWorker}
+     * to indeterminate.
+     * Does nothing if there is no executing {@link SwingWorker} or no
+     * progress bar.
+     */
+    public void setProgressIndeterminate(boolean indeterminate) {
+      SwingUtil.setProgressBarDeterminateOnEDT(getProgressBar(), 0, -1);
+    }
   }
 }



More information about the Schmitzm-commits mailing list