Browse Source

TimeUtils - dodano runAfterUI
LazyInvoker - użycie runAfterUI

Ranides Atterwim 13 years ago
parent
commit
314fd5599b

+ 16 - 13
src/main/java/net/ranides/assira/time/LazyInvoker.java

@@ -10,7 +10,21 @@ import java.util.TimerTask;
 import javax.swing.SwingUtilities;
 import javax.swing.SwingUtilities;
 
 
 /**
 /**
- *
+ * Użycie:
+ * <pre>
+ *   {@code LazyInvoker action = new LazyInvoker(true, 1000) { 
+ *      public void run() {
+ *        // action ...
+ *      }
+ *   };}</pre>
+ * Następnie np w "onKeyPress" kolejkujemy wykonanie:
+ * <pre>
+ *  {@code action.runLazy() }</pre>
+ * 
+ * Mimo, że {@link #runLazy} może być wywoływane wielokrotnie w odstępach 
+ * {@code t<1000}, to {@link #run} uruchomi się tylko raz, {@code 1000}ms 
+ * po ostatnim wywołaniu {@link #runLazy}.
+ * 
  * @author ranides
  * @author ranides
  */
  */
 public abstract class LazyInvoker implements Runnable {
 public abstract class LazyInvoker implements Runnable {
@@ -28,18 +42,7 @@ public abstract class LazyInvoker implements Runnable {
         if (task != null) {
         if (task != null) {
             task.cancel();
             task.cancel();
         }
         }
-        if(awt) {
-            task = TimeUtils.runAfter(delay, new Runnable(){
-
-                @Override
-                public void run() {
-                    SwingUtilities.invokeLater(LazyInvoker.this);
-                }
-                
-            });
-        } else {
-            task = TimeUtils.runAfter(delay, this);
-        }
+        task = awt ? TimeUtils.runAfterUI(delay, this) : TimeUtils.runAfter(delay, this);
     }
     }
 
 
 }
 }

+ 14 - 0
src/main/java/net/ranides/assira/time/TimeUtils.java

@@ -12,6 +12,7 @@ import java.util.Date;
 import java.util.Locale;
 import java.util.Locale;
 import java.util.Timer;
 import java.util.Timer;
 import java.util.TimerTask;
 import java.util.TimerTask;
+import javax.swing.SwingUtilities;
 import net.ranides.assira.trace.ExceptionInspector;
 import net.ranides.assira.trace.ExceptionInspector;
 
 
 /**
 /**
@@ -68,6 +69,19 @@ public class TimeUtils {
         return new TimeResult(repeats, t1, t2);
         return new TimeResult(repeats, t1, t2);
     }
     }
     
     
+    public static TimerTask runAfterUI(int ms, final TimerTask action) {
+        return runAfterUI(ms, new Runnable() {
+            @Override
+            public void run() { action.run(); }
+        });
+    }
+    
+    public static TimerTask runAfterUI(int ms, final Runnable action) {
+        return runAfter(ms, new Runnable(){
+            @Override
+            public void run() { SwingUtilities.invokeLater(action); }
+        });
+    }
     
     
     public static TimerTask runAfter(int ms, final Runnable action) {
     public static TimerTask runAfter(int ms, final Runnable action) {
         return runAfter(ms, new TimerTask() { 
         return runAfter(ms, new TimerTask() {