2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.BorderLayout;
24 import java.awt.Color;
25 import java.awt.Dimension;
27 import java.awt.Graphics;
28 import java.awt.Image;
29 import java.awt.MediaTracker;
30 import java.awt.Toolkit;
31 import java.awt.event.MouseAdapter;
32 import java.awt.event.MouseEvent;
34 import javax.swing.JInternalFrame;
35 import javax.swing.JLayeredPane;
36 import javax.swing.JPanel;
37 import javax.swing.JTextPane;
38 import javax.swing.event.HyperlinkEvent;
39 import javax.swing.event.HyperlinkListener;
41 import jalview.util.Platform;
42 import javajs.async.SwingJSUtils.StateHelper;
43 import javajs.async.SwingJSUtils.StateMachine;
51 @SuppressWarnings("serial")
52 public class SplashScreen extends JPanel
53 implements HyperlinkListener, StateMachine
56 private static final int STATE_INIT = 0;
58 private static final int STATE_LOOP = 1;
60 private static final int STATE_DONE = 2;
62 private static final int SHOW_FOR_SECS = 5;
64 private int FONT_SIZE = (Platform.isJS() ? 14 : 11);
66 private JPanel imgPanel = new JPanel(new BorderLayout());
68 private JInternalFrame iframe;
70 private Image image, logo;
72 protected boolean isStartup = false;
74 private long oldTextLength = -1;
76 private StateHelper helper;
78 * allow click in the initial splash screen to dismiss it
79 * immediately (not if opened from About menu)
81 private MouseAdapter closer = new MouseAdapter()
84 public void mousePressed(MouseEvent evt)
91 } catch (Exception ex)
99 * Constructor that displays the splash screen
102 * if true the panel removes itself on click or after a few seconds;
103 * if false it stays up until closed by the user (from Help..About menu)
105 public SplashScreen(boolean isStartup)
107 this.isStartup = isStartup;
108 // we must get the image in JavaScript BEFORE starting the helper,
109 // as it will take a 1 ms clock tick to obtain width and height information.
110 image = Toolkit.getDefaultToolkit().createImage(
111 getClass().getResource("/images/Jalview_Logo.png"));
112 helper = new StateHelper(this);
113 helper.next(STATE_INIT);
116 protected void initSplashScreenWindow()
118 addMouseListener(closer);
120 setLayout(new BorderLayout());
121 iframe = new JInternalFrame();
122 iframe.setFrameIcon(null);
123 iframe.setClosable(true);
124 iframe.setContentPane(this);
125 iframe.setLayer(JLayeredPane.PALETTE_LAYER);
126 SplashImage splashimg = new SplashImage(image);
127 imgPanel.add(splashimg, BorderLayout.CENTER);
128 add(imgPanel, BorderLayout.NORTH);
129 Desktop.getDesktopPane().add(iframe);
134 * Both Java and JavaScript have to wait for images, but this method will
135 * accomplish nothing for JavaScript. We have already taken care of image
136 * loading with our state loop in JavaScript.
139 private void waitForImages()
143 MediaTracker mt = new MediaTracker(this);
146 mt.addImage(image, 0);
147 logo = Toolkit.getDefaultToolkit().createImage(
148 getClass().getResource("/images/Jalview_Logo_small.png"));
149 } catch (Exception ex)
154 mt.addImage(logo, 1);
161 } catch (InterruptedException x)
166 System.err.println("Error when loading images!");
169 } while (!mt.checkAll());
172 Desktop.getInstance().setIconImage(logo);
177 * update text in author text panel reflecting current version information
179 protected boolean refreshText()
181 String newtext = Desktop.getInstance().getAboutMessage();
182 if (oldTextLength == newtext.length())
186 oldTextLength = newtext.length();
187 iframe.setVisible(false);
188 JTextPane jtp = new JTextPane();
189 jtp.setEditable(false);
190 jtp.setContentType("text/html");
191 jtp.setText("<html>" + newtext + "</html>");
192 jtp.addHyperlinkListener(this);
193 jtp.setFont(new Font("Verdana", Font.PLAIN, FONT_SIZE));
194 jtp.addMouseListener(closer);
195 jtp.setVisible(true);
196 jtp.setSize(new Dimension(750, 425));
197 add(jtp, BorderLayout.CENTER);
199 int h = jtp.getHeight() + imgPanel.getHeight();
200 iframe.setBounds(Math.max(0, (iframe.getParent().getWidth() - 750) / 2),
201 Math.max(0, (iframe.getParent().getHeight() - h)/2), 750, h);
203 iframe.setVisible(true);
207 protected void closeSplash()
212 iframe.setClosed(true);
213 } catch (Exception ex)
219 * A simple state machine with just three states: init, loop, and done. Ideal
220 * for a simple while/sleep loop that works in Java and JavaScript
225 public boolean stateLoop()
229 switch (helper.getState())
232 initSplashScreenWindow();
233 helper.setState(STATE_LOOP);
238 helper.setState(STATE_DONE);
246 helper.delayedState(SHOW_FOR_SECS * 1000, STATE_DONE);
252 Desktop.getInstance().startDialogQueue();
258 private class SplashImage extends JPanel
262 public SplashImage(Image todisplay)
267 setPreferredSize(new Dimension(image.getWidth(this) + 8,
268 image.getHeight(this)));
273 public Dimension getPreferredSize()
275 return new Dimension(image.getWidth(this) + 8, image.getHeight(this));
279 public void paintComponent(Graphics g)
281 g.setColor(Color.white);
282 g.fillRect(0, 0, getWidth(), getHeight());
283 g.setColor(Color.black);
287 g.drawImage(image, (getWidth() - image.getWidth(this)) / 2,
288 (getHeight() - image.getHeight(this)) / 2, this);
294 public void hyperlinkUpdate(HyperlinkEvent e)
296 Desktop.hyperlinkUpdate(e);