j2sNative clean-up. Fixes problem with second try of sequence fetching
[jalview.git] / src / jalview / gui / SplashScreen.java
index 32c6aac..f7d7cbf 100755 (executable)
@@ -1,26 +1,45 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
-  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
- * 
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *  
  * Jalview 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 General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.gui;
 
-import java.awt.*;
-import java.awt.event.*;
+import jalview.util.Platform;
+
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.MediaTracker;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
 
-import javax.swing.*;
+import javax.swing.JInternalFrame;
+import javax.swing.JLabel;
+import javax.swing.JLayeredPane;
+import javax.swing.JPanel;
+import javax.swing.JTextPane;
+import javax.swing.event.HyperlinkEvent;
+import javax.swing.event.HyperlinkListener;
 
 /**
  * DOCUMENT ME!
@@ -28,13 +47,17 @@ import javax.swing.*;
  * @author $author$
  * @version $Revision$
  */
-public class SplashScreen extends JPanel implements Runnable
+public class SplashScreen extends JPanel
+        implements Runnable, HyperlinkListener
 {
   boolean visible = true;
 
   JPanel iconimg = new JPanel(new BorderLayout());
 
-  JTextPane authlist = new JTextPane();
+  /**
+   * either text area in javascript or in java text pane
+   */
+  Component authlist;
 
   JInternalFrame iframe;
 
@@ -49,18 +72,54 @@ public class SplashScreen extends JPanel implements Runnable
    */
   public SplashScreen()
   {
-    Thread t = new Thread(this);
-    t.start();
+    this(false);
+  }
+
+  private boolean interactiveDialog = false;
+
+  /**
+   * 
+   * @param interactive
+   *          if true - an internal dialog is opened rather than a free-floating
+   *          splash screen
+   */
+  public SplashScreen(boolean interactive)
+  {
+    this.interactiveDialog = interactive;
+    // show a splashscreen that will disapper
+    if (Platform.isJS()) // BH 2019
+    {
+      authlist = new JLabel("");
+      run();
+    }
+    else
+    {
+      /**
+       * Java only
+       * 
+       * @j2sNative
+       */
+      {
+        authlist = new JTextPane();
+        Thread t = new Thread(this);
+        t.start();
+      }
+    }
+
   }
 
   MouseAdapter closer = new MouseAdapter()
   {
+    @Override
     public void mousePressed(MouseEvent evt)
     {
       try
       {
-        visible = false;
-        closeSplash();
+        if (!interactiveDialog)
+        {
+          visible = false;
+          closeSplash();
+        }
       } catch (Exception ex)
       {
       }
@@ -76,16 +135,32 @@ public class SplashScreen extends JPanel implements Runnable
     addMouseListener(closer);
     try
     {
-      java.net.URL url = getClass().getResource("/images/logo.gif");
+      java.net.URL url = getClass().getResource("/images/Jalview_Logo.png");
+      java.net.URL urllogo = getClass()
+              .getResource("/images/Jalview_Logo_small.png");
 
-      if (url != null)
+      if (!Platform.isJS() && url != 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.waitForID(0);
-        Desktop.instance.setIconImage(image);
+        mt.addImage(logo, 1);
+        do
+        {
+          try
+          {
+            mt.waitForAll();
+          } catch (InterruptedException x)
+          {
+          }
+          if (mt.isErrorAny())
+          {
+            System.err.println("Error when loading images!");
+          }
+        } while (!mt.checkAll());
+        Desktop.instance.setIconImage(logo);
       }
     } catch (Exception ex)
     {
@@ -93,18 +168,30 @@ public class SplashScreen extends JPanel implements Runnable
 
     iframe = new JInternalFrame();
     iframe.setFrameIcon(null);
-    iframe.setClosable(false);
+    iframe.setClosable(interactiveDialog);
     this.setLayout(new BorderLayout());
     iframe.setContentPane(this);
     iframe.setLayer(JLayeredPane.PALETTE_LAYER);
+    if (Platform.isJS())
+    {
+      // ignore in JavaScript
+    }
+    else
+    /**
+     * Java only
+     * 
+     * @j2sNative
+     */
+    {
+      ((JTextPane) authlist).setEditable(false);
 
-    Desktop.desktop.add(iframe);
-    SplashImage splashimg = new SplashImage(image);
-    iconimg.add(splashimg, BorderLayout.CENTER);
-    add(iconimg, BorderLayout.WEST);
+      SplashImage splashimg = new SplashImage(image);
+      iconimg.add(splashimg, BorderLayout.CENTER);
+      add(iconimg, BorderLayout.NORTH);
+    }
     add(authlist, BorderLayout.CENTER);
-    authlist.setEditable(false);
     authlist.addMouseListener(closer);
+    Desktop.desktop.add(iframe);
     refreshText();
   }
 
@@ -113,28 +200,47 @@ public class SplashScreen extends JPanel implements Runnable
   /**
    * update text in author text panel reflecting current version information
    */
+  @SuppressWarnings("unused")
   protected boolean refreshText()
   {
     String newtext = Desktop.instance.getAboutMessage(true).toString();
-    System.err.println("Text found: \n"+newtext+"\nEnd of newtext.");
+    // System.err.println("Text found: \n"+newtext+"\nEnd of newtext.");
     if (oldtext != newtext.length())
     {
       iframe.setVisible(false);
       oldtext = newtext.length();
-      authlist=new JTextPane();
-      authlist.setEditable(false);
+      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);
+      }
+      else
+      /**
+       * Java only
+       * 
+       * @j2sNative
+       */
+      {
+        authlist = new JTextPane();
+        ((JTextPane) authlist).setEditable(false);
+        ((JTextPane) authlist).setContentType("text/html");
+        ((JTextPane) authlist).setText(newtext);
+        ((JTextPane) authlist).addHyperlinkListener(this);
+      }
       authlist.addMouseListener(closer);
-      authlist.setContentType("text/html");
-      authlist.setText(newtext);
+
       authlist.setVisible(true);
+      authlist.setSize(new Dimension(750, 375));
       add(authlist, BorderLayout.CENTER);
-      iframe.pack();
+      revalidate();
+      iframe.setBounds((Desktop.instance.getWidth() - 750) / 2,
+              (Desktop.instance.getHeight() - 375) / 2, 750,
+              authlist.getHeight() + iconimg.getHeight());
+      iframe.validate();
       iframe.setVisible(true);
-      iframe.setBounds((int) ((Desktop.instance.getWidth() - 750) / 2),
-              (int) ((Desktop.instance.getHeight() - 160) / 2), 750,
-              iframe.getPreferredSize().height);
-      System.err.println("New preferred size: "+iframe.getPreferredSize().height);
-
       return true;
     }
     return false;
@@ -143,6 +249,7 @@ public class SplashScreen extends JPanel implements Runnable
   /**
    * Create splash screen, display it and clear it off again.
    */
+  @Override
   public void run()
   {
     initSplashScreenWindow();
@@ -151,6 +258,7 @@ public class SplashScreen extends JPanel implements Runnable
 
     while (visible)
     {
+      iframe.repaint();
       try
       {
         Thread.sleep(500);
@@ -158,18 +266,28 @@ public class SplashScreen extends JPanel implements Runnable
       {
       }
 
-      if (((System.currentTimeMillis() / 1000) - startTime) > 5)
+      if (!interactiveDialog
+              && ((System.currentTimeMillis() / 1000) - startTime) > 5)
       {
         visible = false;
       }
 
       if (visible && refreshText())
       {
-          repaint();
+        // if (interactiveDialog) {
+        iframe.repaint();
+        // } else {
+        // iframe.repaint();
+        // };
+      }
+      if (interactiveDialog)
+      {
+        return;
       }
     }
 
     closeSplash();
+    Desktop.instance.startDialogQueue();
   }
 
   /**
@@ -193,10 +311,20 @@ public class SplashScreen extends JPanel implements Runnable
     public SplashImage(Image todisplay)
     {
       image = todisplay;
-      setPreferredSize(new Dimension(image.getWidth(this) + 8,
-              image.getHeight(this)));
+      if (image != null)
+      {
+        setPreferredSize(new Dimension(image.getWidth(this) + 8,
+                image.getHeight(this)));
+      }
     }
 
+    @Override
+    public Dimension getPreferredSize()
+    {
+      return new Dimension(image.getWidth(this) + 8, image.getHeight(this));
+    }
+
+    @Override
     public void paintComponent(Graphics g)
     {
       g.setColor(Color.white);
@@ -206,8 +334,8 @@ public class SplashScreen extends JPanel implements Runnable
 
       if (image != null)
       {
-        g.drawImage(image, 4, (getHeight() - image.getHeight(this)) / 2,
-                this);
+        g.drawImage(image, (getWidth() - image.getWidth(this)) / 2,
+                (getHeight() - image.getHeight(this)) / 2, this);
       }
     }
     /*
@@ -251,4 +379,11 @@ public class SplashScreen extends JPanel implements Runnable
      * += fontSize + 4); }
      */
   }
-}
\ No newline at end of file
+
+  @Override
+  public void hyperlinkUpdate(HyperlinkEvent e)
+  {
+    Desktop.hyperlinkUpdate(e);
+
+  }
+}