enabling Splash screen in JavaScript.
authorBobHanson <hansonr@stolaf.edu>
Tue, 5 Nov 2019 18:33:23 +0000 (12:33 -0600)
committerBobHanson <hansonr@stolaf.edu>
Wed, 27 Nov 2019 02:36:14 +0000 (20:36 -0600)
src/jalview/gui/Desktop.java
src/jalview/gui/SplashScreen.java
swingjs/SwingJS-site.zip
swingjs/net.sf.j2s.core.jar
swingjs/timestamp
swingjs/ver/3.2.4/SwingJS-site.zip
swingjs/ver/3.2.4/net.sf.j2s.core.jar
swingjs/ver/3.2.4/timestamp

index 9e0c89b..ae3b652 100644 (file)
@@ -473,17 +473,6 @@ public class Desktop extends GDesktop
 
         checkURLLinks();
 
-        // Spawn a thread that shows the splashscreen
-
-        SwingUtilities.invokeLater(new Runnable()
-        {
-          @Override
-          public void run()
-          {
-            new SplashScreen();
-          }
-        });
-
         // Thread off a new instance of the file chooser - this reduces the time
         // it
         // takes to open it later on.
@@ -516,6 +505,17 @@ public class Desktop extends GDesktop
 
       }
 
+      // Spawn a thread that shows the splashscreen
+
+      SwingUtilities.invokeLater(new Runnable()
+      {
+        @Override
+        public void run()
+        {
+          new SplashScreen();
+        }
+      });
+
       this.setDropTarget(new java.awt.dnd.DropTarget(desktopPane, this));
 
       this.addWindowListener(new WindowAdapter()
index f5648d2..97707be 100755 (executable)
@@ -29,7 +29,10 @@ import java.awt.Dimension;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Image;
+import java.awt.Insets;
 import java.awt.MediaTracker;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.awt.event.MouseAdapter;
 import java.awt.event.MouseEvent;
 
@@ -38,6 +41,8 @@ import javax.swing.JLabel;
 import javax.swing.JLayeredPane;
 import javax.swing.JPanel;
 import javax.swing.JTextPane;
+import javax.swing.Timer;
+import javax.swing.border.EmptyBorder;
 import javax.swing.event.HyperlinkEvent;
 import javax.swing.event.HyperlinkListener;
 
@@ -50,6 +55,12 @@ import javax.swing.event.HyperlinkListener;
 public class SplashScreen extends JPanel
         implements Runnable, HyperlinkListener
 {
+  private static final int STATE_INIT = 0;
+
+  private static final int STATE_LOOP = 1;
+
+  private static final int STATE_DONE = 2;
+
   boolean visible = true;
 
   JPanel iconimg = new JPanel(new BorderLayout());
@@ -57,7 +68,7 @@ public class SplashScreen extends JPanel
   /**
    * either text area in javascript or in java text pane
    */
-  Component authlist;
+  Component htmlPane;
 
   JInternalFrame iframe;
 
@@ -75,7 +86,7 @@ public class SplashScreen extends JPanel
     this(false);
   }
 
-  private boolean interactiveDialog = false;
+  protected boolean interactiveDialog = false;
 
   /**
    * 
@@ -89,7 +100,7 @@ public class SplashScreen extends JPanel
     // show a splashscreen that will disapper
     if (Platform.isJS()) // BH 2019
     {
-      authlist = new JLabel("");
+      htmlPane = new JLabel("");
       run();
     }
     else
@@ -100,7 +111,7 @@ public class SplashScreen extends JPanel
        * @j2sIgnore
        */
       {
-        authlist = new JTextPane();
+        htmlPane = new JTextPane();
         Thread t = new Thread(this, "SplashScreen");
         t.start();
       }
@@ -130,77 +141,68 @@ public class SplashScreen extends JPanel
    * ping the jalview version page then create and display the jalview
    * splashscreen window.
    */
-  void initSplashScreenWindow()
+  protected void initSplashScreenWindow()
   {
     addMouseListener(closer);
     try
     {
       java.net.URL url = getClass().getResource("/images/Jalview_Logo.png");
-      java.net.URL urllogo = getClass()
-              .getResource("/images/Jalview_Logo_small.png");
-
-      if (!Platform.isJS() && url != null)
+      image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
+      MediaTracker mt = new MediaTracker(this);
+      mt.addImage(image, 0);
+      Image logo = (Platform.isJS() ? null
+              : java.awt.Toolkit.getDefaultToolkit().createImage(getClass()
+                      .getResource("/images/Jalview_Logo_small.png")));
+      if (logo != null)
       {
-        image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
-        Image logo = java.awt.Toolkit.getDefaultToolkit()
-                .createImage(urllogo);
-        MediaTracker mt = new MediaTracker(this);
-        mt.addImage(image, 0);
         mt.addImage(logo, 1);
-        do
+      }
+      do
+      {
+        try
         {
-          try
-          {
-            mt.waitForAll();
-          } catch (InterruptedException x)
-          {
-          }
-          if (mt.isErrorAny())
-          {
-            System.err.println("Error when loading images!");
-          }
-        } while (!mt.checkAll());
+          mt.waitForAll();
+        } catch (InterruptedException x)
+        {
+        }
+        if (mt.isErrorAny())
+        {
+          System.err.println("Error when loading images!");
+        }
+      } while (!mt.checkAll());
+      if (url != null)
+      {
         Desktop.getInstance().setIconImage(logo);
       }
     } catch (Exception ex)
     {
     }
-
     iframe = new JInternalFrame();
     iframe.setFrameIcon(null);
     iframe.setClosable(interactiveDialog);
     this.setLayout(new BorderLayout());
     iframe.setContentPane(this);
     iframe.setLayer(JLayeredPane.PALETTE_LAYER);
-    if (Platform.isJS())
+    if (htmlPane instanceof JTextPane) 
     {
-      // ignore in JavaScript
-    }
-    else
-    /**
-     * Java only
-     * 
-     * @j2sIgnore
-     */
-    {
-      ((JTextPane) authlist).setEditable(false);
-
-      SplashImage splashimg = new SplashImage(image);
-      iconimg.add(splashimg, BorderLayout.CENTER);
-      add(iconimg, BorderLayout.NORTH);
+      ((JTextPane) htmlPane).setEditable(false);
     }
-    add(authlist, BorderLayout.CENTER);
-    authlist.addMouseListener(closer);
+    SplashImage splashimg = new SplashImage(image);
+    iconimg.add(splashimg, BorderLayout.CENTER);
+    add(iconimg, BorderLayout.NORTH);
+    add(htmlPane, BorderLayout.CENTER);
+    htmlPane.addMouseListener(closer);
     Desktop.getDesktopPane().add(iframe);
     refreshText();
   }
 
   long oldtext = -1;
 
+  private int mainState;
+
   /**
    * update text in author text panel reflecting current version information
    */
-  @SuppressWarnings("unused")
   protected boolean refreshText()
   {
     Desktop desktop = Desktop.getInstance();
@@ -210,13 +212,16 @@ public class SplashScreen extends JPanel
     {
       iframe.setVisible(false);
       oldtext = newtext.length();
+      System.out.println(newtext);
       if (Platform.isJS()) // BH 2019
       {
-        authlist = new JLabel(
-                "<html><br/><br/><img src=\"swingjs/j2s/images/Jalview_Logo.png\"/><br/>"
-                        + newtext);
-        ((JLabel) authlist).setOpaque(true);
-        ((JLabel) authlist).setBackground(Color.white);
+        // BH TODO SwingJS does not implement HTML style. Could rethink this.
+
+        htmlPane = new JLabel(newtext);
+        ((JLabel) htmlPane)
+                .setBorder(new EmptyBorder(new Insets(5, 5, 5, 5)));
+        ((JLabel) htmlPane).setOpaque(true);
+        ((JLabel) htmlPane).setBackground(Color.white);
       }
       else
       /**
@@ -225,21 +230,21 @@ public class SplashScreen extends JPanel
        * @j2sIgnore
        */
       {
-        authlist = new JTextPane();
-        ((JTextPane) authlist).setEditable(false);
-        ((JTextPane) authlist).setContentType("text/html");
-        ((JTextPane) authlist).setText(newtext);
-        ((JTextPane) authlist).addHyperlinkListener(this);
+        htmlPane = new JTextPane();
+        ((JTextPane) htmlPane).setEditable(false);
+        ((JTextPane) htmlPane).setContentType("text/html");
+        ((JTextPane) htmlPane).setText(newtext);
+        ((JTextPane) htmlPane).addHyperlinkListener(this);
       }
-      authlist.addMouseListener(closer);
+      htmlPane.addMouseListener(closer);
 
-      authlist.setVisible(true);
-      authlist.setSize(new Dimension(750, 375));
-      add(authlist, BorderLayout.CENTER);
+      htmlPane.setVisible(true);
+      htmlPane.setSize(new Dimension(750, 375));
+      add(htmlPane, BorderLayout.CENTER);
       revalidate();
       iframe.setBounds((desktop.getWidth() - 750) / 2,
               (desktop.getHeight() - 375) / 2, 750,
-              authlist.getHeight() + iconimg.getHeight());
+              htmlPane.getHeight() + iconimg.getHeight());
       iframe.validate();
       iframe.setVisible(true);
       return true;
@@ -253,46 +258,65 @@ public class SplashScreen extends JPanel
   @Override
   public void run()
   {
-    initSplashScreenWindow();
+    mainState = STATE_INIT;
+    mainLoop();
+  }
 
-    long startTime = System.currentTimeMillis() / 1000;
+  protected long startTime;
 
-    while (visible)
+  protected void mainLoop()
+  {
+    while (true)
     {
-      iframe.repaint();
-      try
-      {
-        Thread.sleep(500);
-      } catch (Exception ex)
+      if (!visible)
       {
+        mainState = STATE_DONE;
       }
-
-      if (!interactiveDialog
-              && ((System.currentTimeMillis() / 1000) - startTime) > 5)
+      switch (mainState)
       {
-        visible = false;
-      }
+      case STATE_INIT:
+        initSplashScreenWindow();
+        startTime = System.currentTimeMillis() / 1000;
+        mainState = STATE_LOOP;
+        continue;
+      case STATE_LOOP:
+        if (!interactiveDialog
+                && ((System.currentTimeMillis() / 1000) - startTime) > 5)
+        {
+          visible = false;
+          continue;
+        }
+        if (visible && refreshText())
+        {
+          iframe.repaint();
+        }
+        if (interactiveDialog)
+        {
+          return;
+        }
+        Timer timer = new Timer(500, new ActionListener()
+        {
+          @Override
+          public void actionPerformed(ActionEvent e)
+          {
+            mainLoop();
+          }
 
-      if (visible && refreshText())
-      {
-        // if (interactiveDialog) {
-        iframe.repaint();
-        // } else {
-        // iframe.repaint();
-        // };
-      }
-      if (interactiveDialog)
-      {
+        });
+        timer.start();
+        return;
+      case STATE_DONE:
+        closeSplash();
+        Desktop.getInstance().startDialogQueue();
         return;
       }
     }
 
-    closeSplash();
-    Desktop.getInstance().startDialogQueue();
   }
 
   /**
-   * DOCUMENT ME!
+   * Close the internal frame, either from the timer expiring or from the mouse
+   * action.
    */
   public void closeSplash()
   {
index b5efc84..88b0b71 100644 (file)
Binary files a/swingjs/SwingJS-site.zip and b/swingjs/SwingJS-site.zip differ
index e2efdd4..e0cee1d 100644 (file)
Binary files a/swingjs/net.sf.j2s.core.jar and b/swingjs/net.sf.j2s.core.jar differ
index d4e34e1..7e54dca 100644 (file)
@@ -1 +1 @@
-20191030143851 
+20191105122130 
index b5efc84..88b0b71 100644 (file)
Binary files a/swingjs/ver/3.2.4/SwingJS-site.zip and b/swingjs/ver/3.2.4/SwingJS-site.zip differ
index e2efdd4..e0cee1d 100644 (file)
Binary files a/swingjs/ver/3.2.4/net.sf.j2s.core.jar and b/swingjs/ver/3.2.4/net.sf.j2s.core.jar differ
index d4e34e1..7e54dca 100644 (file)
@@ -1 +1 @@
-20191030143851 
+20191105122130