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;
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
{
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;
this(false);
}
- protected boolean interactiveDialog = false;
+ protected boolean isInteractive = false;
/**
*
* 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()
{
try
{
- if (!interactiveDialog)
+ if (!isInteractive)
{
- visible = false;
+ setVisible(false);
closeSplash();
}
} catch (Exception ex)
}
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;
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
/**
* @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;
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:
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;
}
@Override
public void actionPerformed(ActionEvent e)
{
+ System.out.println(htmlPane.getFont());
mainLoop();
}
});
+ timer.setRepeats(false);
timer.start();
return;
case STATE_DONE:
*/
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)
{
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)
{
Desktop.hyperlinkUpdate(e);
}
+
}