2003/11/05 10:25:00
authormegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:41:05 +0000 (07:41 +0000)
committermegacz <megacz@xwt.org>
Fri, 30 Jan 2004 07:41:05 +0000 (07:41 +0000)
darcs-hash:20040130074105-2ba56-18be0ee684ad4cf15f0443224826bd7b6a40f4df.gz

src/org/xwt/Main.java
src/org/xwt/Scheduler.java
src/org/xwt/Surface.java
src/org/xwt/js/JS.java

index bc26f96..1268fdc 100644 (file)
@@ -97,7 +97,7 @@ public class Main {
                     return null;
                 } });
 
-        new Thread() { public void run() { Scheduler.singleton.run(); } }.start();
+        new Thread() { public void run() { Scheduler.init(); } }.start();
         Platform.running();
     }
 
index 2cb7892..a46c75c 100644 (file)
@@ -8,21 +8,37 @@ import org.xwt.util.*;
 /** Implements cooperative multitasking */
 public class Scheduler {
 
-    public static final Scheduler singleton = Platform.getScheduler();
-    protected Scheduler() { }
+    // Public API Exposed to org.xwt /////////////////////////////////////////////////
 
     public static abstract class Task { public abstract void perform(); }
+    public static void add(Task t) { singleton.runnable.append(t); }
+    public static void init() { singleton.run(); }
+
+
+    // API which must be supported by subclasses /////////////////////////////////////
+
+    protected Scheduler() { }
+
+    /**
+     *  INVARIANT: all scheduler implementations MUST invoke
+     *  Surface.renderAll() after performing a Task if no tasks remain
+     *  in the queue.  A scheduler may choose to invoke
+     *  Surface.renderAll() more often than that if it so chooses.
+     */
+    public void run() { defaultRun(); }
+
+
+    // Default Implementation /////////////////////////////////////
 
     protected static Queue runnable = new Queue(50);
-    public static void add(Task t) { singleton.runnable.append(t); }
-    public void run() {
+    private static final Scheduler singleton = Platform.getScheduler();
+    public void defaultRun() {
         while(true) {
             Task t = (Task)runnable.remove(true);
             try {
                 t.perform();
                 // FIXME: be smarter about this
-                for(int i=0; i<Surface.allSurfaces.size(); i++)
-                    ((Surface)Surface.allSurfaces.elementAt(i)).render();
+                Surface.renderAll();
             } catch (Exception e) {
                 Log.log(Scheduler.class, "Task threw an exception: " + e);
                 Log.log(Scheduler.class, e);
index 887534b..89906b5 100644 (file)
@@ -256,10 +256,14 @@ public abstract class Surface extends PixelBuffer {
     protected final void Focused(boolean b) { new SimpleMessage("Focused", b ? Boolean.TRUE : Boolean.FALSE, root); }
     public static void Refresh() {
         Scheduler.add(new Scheduler.Task() { public void perform() {
-                for(int i=0; i<allSurfaces.size(); i++)
-                    ((Surface)allSurfaces.elementAt(i)).render();
+            renderAll();
         }}); }
 
+    public static void renderAll() {
+        for(int i=0; i<allSurfaces.size(); i++)
+            ((Surface)allSurfaces.elementAt(i)).render();
+    }
+
     public final void setMaximized(boolean b) { if (b != maximized) _setMaximized(maximized = b); }
     public final void setMinimized(boolean b) { if (b != minimized) _setMinimized(minimized = b); }
 
index 79800fe..82370a6 100644 (file)
@@ -103,9 +103,19 @@ public abstract class JS {
 
     /** A sensible implementation of the abstract methods in the JS class */
     public static class Obj extends JS {
+
+        // FIXME: move these to an interface so they're optional
         // this gets around a wierd fluke in the Java type checking rules for ?..:
         public static final Object T = Boolean.TRUE;
         public static final Object F = Boolean.FALSE;
+
+        // FIXME: be smart here; perhaps intern
+        public static final Number N(int i) { return new Integer(i); }
+        public static final Number N(long l) { return new Long(l); }
+        public static final Number N(double d) { return new Double(d); }
+
+        public static final Boolean B(boolean b) { return b ? Boolean.TRUE : Boolean.FALSE; }
+
         private Hash entries = null;
         private boolean sealed = false;
         public Obj() { this(false); }