From: BobHanson Date: Wed, 27 Nov 2019 04:23:29 +0000 (-0600) Subject: JAL-3487 simplified, streamlined Splash screen; just two isJS(): X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;ds=sidebyside;h=7891791fe9491633fac20718d2ca6d4cc47bde18;p=jalview.git JAL-3487 simplified, streamlined Splash screen; just two isJS(): - no need for minimization logo on Help frame in JavaScript - still using JLabel, not JTextPane (for now) --- diff --git a/src/jalview/gui/SplashScreen.java b/src/jalview/gui/SplashScreen.java index 97707be..48852d4 100755 --- a/src/jalview/gui/SplashScreen.java +++ b/src/jalview/gui/SplashScreen.java @@ -35,6 +35,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; +import java.beans.PropertyVetoException; import javax.swing.JInternalFrame; import javax.swing.JLabel; @@ -47,11 +48,18 @@ import javax.swing.event.HyperlinkEvent; import javax.swing.event.HyperlinkListener; /** - * DOCUMENT ME! + * A class that serves both as an initial 5-second splash screen (interactive + * false) as well as for the Help menu item action (interactive true). + * + * As a splash screen, the frame closes if clicked by the user. + * + * Closure loop converted from a while/sleep loop to a JavaScript-compatible + * state machine by Bob Hanson 2019.11.26. + * + * TODO: get JTextPane working for read-only HTML. * - * @author $author$ - * @version $Revision$ */ +@SuppressWarnings("serial") public class SplashScreen extends JPanel implements Runnable, HyperlinkListener { @@ -61,20 +69,24 @@ public class SplashScreen extends JPanel private static final int STATE_DONE = 2; - boolean visible = true; + // boolean visible = true; - JPanel iconimg = new JPanel(new BorderLayout()); + private JPanel iconimg = new JPanel(new BorderLayout()); /** - * either text area in javascript or in java text pane + * Temporary SwingJS Hack: Either a JLabel in JavaScript or a JTextPane in + * Java */ - Component htmlPane; + protected Component htmlPane; - JInternalFrame iframe; + private JInternalFrame iframe; - Image image; + private Image image; - int fontSize = 11; + private final static int fontSize = 11; + + protected final static Font largeFont = new Font("Verdana", Font.BOLD, + fontSize + 6); int yoffset = 30; @@ -86,7 +98,7 @@ public class SplashScreen extends JPanel this(false); } - protected boolean interactiveDialog = false; + protected boolean isInteractive = false; /** * @@ -94,29 +106,11 @@ public class SplashScreen extends JPanel * if true - an internal dialog is opened rather than a free-floating * splash screen */ - public SplashScreen(boolean interactive) + public SplashScreen(boolean isInteractive) { - this.interactiveDialog = interactive; - // show a splashscreen that will disapper - if (Platform.isJS()) // BH 2019 - { - htmlPane = new JLabel(""); - run(); - } - else - { - /** - * Java only - * - * @j2sIgnore - */ - { - htmlPane = new JTextPane(); - Thread t = new Thread(this, "SplashScreen"); - t.start(); - } - } - + this.isInteractive = isInteractive; + Thread t = new Thread(this, "SplashScreen"); + t.start(); } MouseAdapter closer = new MouseAdapter() @@ -126,9 +120,9 @@ public class SplashScreen extends JPanel { try { - if (!interactiveDialog) + if (!isInteractive) { - visible = false; + setVisible(false); closeSplash(); } } catch (Exception ex) @@ -179,24 +173,18 @@ public class SplashScreen extends JPanel } iframe = new JInternalFrame(); iframe.setFrameIcon(null); - iframe.setClosable(interactiveDialog); + iframe.setClosable(isInteractive); this.setLayout(new BorderLayout()); iframe.setContentPane(this); iframe.setLayer(JLayeredPane.PALETTE_LAYER); - if (htmlPane instanceof JTextPane) - { - ((JTextPane) htmlPane).setEditable(false); - } 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; + String oldtext; private int mainState; @@ -208,20 +196,27 @@ public class SplashScreen extends JPanel Desktop desktop = Desktop.getInstance(); String newtext = desktop.getAboutMessage(true).toString(); // System.err.println("Text found: \n"+newtext+"\nEnd of newtext."); - if (oldtext != newtext.length()) + boolean isNew = (htmlPane == null); + if (!newtext.equals(oldtext)) { iframe.setVisible(false); - oldtext = newtext.length(); - System.out.println(newtext); + oldtext = newtext; if (Platform.isJS()) // BH 2019 { // 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); + if (isNew) + { + htmlPane = new JLabel(); + } + JLabel l = (JLabel) htmlPane; + l.setText(newtext); + Font f = htmlPane.getFont(); + l.setFont(new Font(f.getFamily(), Font.PLAIN, f.getSize())); + l.setBorder(new EmptyBorder(new Insets(5, 5, 5, 5))); + l.setOpaque(true); + l.setBackground(Color.white); + htmlPane = l; } else /** @@ -230,23 +225,27 @@ public class SplashScreen extends JPanel * @j2sIgnore */ { - htmlPane = new JTextPane(); - ((JTextPane) htmlPane).setEditable(false); - ((JTextPane) htmlPane).setContentType("text/html"); - ((JTextPane) htmlPane).setText(newtext); - ((JTextPane) htmlPane).addHyperlinkListener(this); + if (isNew) + { + htmlPane = new JTextPane(); + } + JTextPane pane = (JTextPane) htmlPane; + pane.setEditable(false); + pane.setContentType("text/html"); + pane.setText(newtext); + pane.addHyperlinkListener(this); + htmlPane = pane; + } + if (isNew) + { + htmlPane.addMouseListener(closer); + htmlPane.setSize(new Dimension(750, 375)); + add(htmlPane, BorderLayout.CENTER); + int h = htmlPane.getHeight() + iconimg.getHeight(); + iframe.setBounds(Math.max(0, (desktop.getWidth() - 750) / 2), + Math.max(0, (desktop.getHeight() - h) / 2), 750, h); + iframe.setVisible(true); } - htmlPane.addMouseListener(closer); - - 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, - htmlPane.getHeight() + iconimg.getHeight()); - iframe.validate(); - iframe.setVisible(true); return true; } return false; @@ -264,14 +263,16 @@ public class SplashScreen extends JPanel protected long startTime; + /** + * A simple state machine with just three states: init, loop, and done. Ideal + * for a simple while/sleep loop that works in Java and JavaScript + * identically. + * + */ protected void mainLoop() { while (true) { - if (!visible) - { - mainState = STATE_DONE; - } switch (mainState) { case STATE_INIT: @@ -280,17 +281,22 @@ public class SplashScreen extends JPanel mainState = STATE_LOOP; continue; case STATE_LOOP: - if (!interactiveDialog + if (!isVisible()) + { + mainState = STATE_DONE; + continue; + } + if (!isInteractive && ((System.currentTimeMillis() / 1000) - startTime) > 5) { - visible = false; + setVisible(false); continue; } - if (visible && refreshText()) + if (isVisible() && refreshText()) { iframe.repaint(); } - if (interactiveDialog) + if (isInteractive) { return; } @@ -299,10 +305,12 @@ public class SplashScreen extends JPanel @Override public void actionPerformed(ActionEvent e) { + System.out.println(htmlPane.getFont()); mainLoop(); } }); + timer.setRepeats(false); timer.start(); return; case STATE_DONE: @@ -320,18 +328,18 @@ public class SplashScreen extends JPanel */ public void closeSplash() { + setVisible(false); try { - iframe.setClosed(true); - } catch (Exception ex) + } catch (PropertyVetoException e) { } } - public class SplashImage extends JPanel + private class SplashImage extends JPanel { - Image image; + private Image image; public SplashImage(Image todisplay) { @@ -355,7 +363,7 @@ public class SplashScreen extends JPanel g.setColor(Color.white); g.fillRect(0, 0, getWidth(), getHeight()); g.setColor(Color.black); - g.setFont(new Font("Verdana", Font.BOLD, fontSize + 6)); + g.setFont(largeFont); if (image != null) { @@ -411,4 +419,5 @@ public class SplashScreen extends JPanel Desktop.hyperlinkUpdate(e); } + }