pack before displaying splashscreen to ensure its preferred size is correctly calculated
[jalview.git] / src / jalview / gui / SplashScreen.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3   * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
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 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.gui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22
23 import javax.swing.*;
24
25 /**
26  * DOCUMENT ME!
27  * 
28  * @author $author$
29  * @version $Revision$
30  */
31 public class SplashScreen extends JPanel implements Runnable
32 {
33   boolean visible = true;
34
35   JPanel iconimg = new JPanel(new BorderLayout());
36
37   JTextPane authlist = new JTextPane();
38
39   JInternalFrame iframe;
40
41   Image image;
42
43   int fontSize = 11;
44
45   int yoffset = 30;
46
47   /**
48    * Creates a new SplashScreen object.
49    */
50   public SplashScreen()
51   {
52     Thread t = new Thread(this);
53     t.start();
54   }
55
56   MouseAdapter closer = new MouseAdapter()
57   {
58     public void mousePressed(MouseEvent evt)
59     {
60       try
61       {
62         visible = false;
63         closeSplash();
64       } catch (Exception ex)
65       {
66       }
67     }
68   };
69
70   /**
71    * ping the jalview version page then create and display the jalview
72    * splashscreen window.
73    */
74   void initSplashScreenWindow()
75   {
76     addMouseListener(closer);
77     try
78     {
79       java.net.URL url = getClass().getResource("/images/logo.gif");
80
81       if (url != null)
82       {
83         image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
84
85         MediaTracker mt = new MediaTracker(this);
86         mt.addImage(image, 0);
87         mt.waitForID(0);
88         Desktop.instance.setIconImage(image);
89       }
90     } catch (Exception ex)
91     {
92     }
93
94     iframe = new JInternalFrame();
95     iframe.setFrameIcon(null);
96     iframe.setClosable(false);
97     this.setLayout(new BorderLayout());
98     iframe.setContentPane(this);
99     iframe.setLayer(JLayeredPane.PALETTE_LAYER);
100
101     Desktop.desktop.add(iframe);
102     SplashImage splashimg = new SplashImage(image);
103     iconimg.add(splashimg, BorderLayout.CENTER);
104     add(iconimg, BorderLayout.WEST);
105     add(authlist, BorderLayout.CENTER);
106     authlist.setEditable(false);
107     authlist.addMouseListener(closer);
108     refreshText();
109   }
110
111   long oldtext = -1;
112
113   /**
114    * update text in author text panel reflecting current version information
115    */
116   protected boolean refreshText()
117   {
118     String newtext = Desktop.instance.getAboutMessage(true).toString();
119     System.err.println("Text found: \n"+newtext+"\nEnd of newtext.");
120     if (oldtext != newtext.length())
121     {
122       iframe.setVisible(false);
123       oldtext = newtext.length();
124       authlist=new JTextPane();
125       authlist.setEditable(false);
126       authlist.addMouseListener(closer);
127       authlist.setContentType("text/html");
128       authlist.setText(newtext);
129       authlist.setVisible(true);
130       add(authlist, BorderLayout.CENTER);
131       iframe.pack();
132       iframe.setVisible(true);
133       iframe.setBounds((int) ((Desktop.instance.getWidth() - 750) / 2),
134               (int) ((Desktop.instance.getHeight() - 160) / 2), 750,
135               iframe.getPreferredSize().height);
136       System.err.println("New preferred size: "+iframe.getPreferredSize().height);
137
138       return true;
139     }
140     return false;
141   }
142
143   /**
144    * Create splash screen, display it and clear it off again.
145    */
146   public void run()
147   {
148     initSplashScreenWindow();
149
150     long startTime = System.currentTimeMillis() / 1000;
151
152     while (visible)
153     {
154       try
155       {
156         Thread.sleep(500);
157       } catch (Exception ex)
158       {
159       }
160
161       if (((System.currentTimeMillis() / 1000) - startTime) > 5)
162       {
163         visible = false;
164       }
165
166       if (visible && refreshText())
167       {
168           repaint();
169       }
170     }
171
172     closeSplash();
173   }
174
175   /**
176    * DOCUMENT ME!
177    */
178   public void closeSplash()
179   {
180     try
181     {
182
183       iframe.setClosed(true);
184     } catch (Exception ex)
185     {
186     }
187   }
188
189   public class SplashImage extends JPanel
190   {
191     Image image;
192
193     public SplashImage(Image todisplay)
194     {
195       image = todisplay;
196       setPreferredSize(new Dimension(image.getWidth(this) + 8,
197               image.getHeight(this)));
198     }
199
200     public void paintComponent(Graphics g)
201     {
202       g.setColor(Color.white);
203       g.fillRect(0, 0, getWidth(), getHeight());
204       g.setColor(Color.black);
205       g.setFont(new Font("Verdana", Font.BOLD, fontSize + 6));
206
207       if (image != null)
208       {
209         g.drawImage(image, 4, (getHeight() - image.getHeight(this)) / 2,
210                 this);
211       }
212     }
213     /*
214      * int y = yoffset;
215      * 
216      * g.drawString("Jalview " + jalview.bin.Cache.getProperty("VERSION"), 50,
217      * y);
218      * 
219      * FontMetrics fm = g.getFontMetrics(); int vwidth =
220      * fm.stringWidth("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
221      * g.setFont(new Font("Verdana", Font.BOLD, fontSize + 2)); g.drawString(
222      * "Last updated: " + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"),
223      * 50 + vwidth + 5, y); if (jalview.bin.Cache.getDefault("LATEST_VERSION",
224      * "Checking").equals( "Checking")) { // Displayed when code version and
225      * jnlp version do not match g.drawString("...Checking latest version...",
226      * 50, y += fontSize + 10); y += 5; g.setColor(Color.black); } else if
227      * (!jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
228      * .equals(jalview.bin.Cache.getProperty("VERSION"))) { if
229      * (jalview.bin.Cache.getProperty("VERSION").toLowerCase()
230      * .indexOf("automated build") == -1) { // Displayed when code version and
231      * jnlp version do not match and code // version is not a development build
232      * g.setColor(Color.red); } g.drawString( "!! Jalview version " +
233      * jalview.bin.Cache.getDefault("LATEST_VERSION", "..Checking..") +
234      * " is available for download from "
235      * +jalview.bin.Cache.getDefault("www.jalview.org"
236      * ,"http://www.jalview.org")+" !!", 50, y += fontSize + 10); y += 5;
237      * g.setColor(Color.black); }
238      * 
239      * g.setFont(new Font("Verdana", Font.BOLD, fontSize)); g.drawString(
240      * "Authors: Jim Procter, Andrew Waterhouse, Michele Clamp, James Cuff, Steve Searle,"
241      * , 50, y += fontSize + 4); g.drawString("David Martin & Geoff Barton.",
242      * 60, y += fontSize + 4); g.drawString(
243      * "Development managed by The Barton Group, University of Dundee.", 50, y
244      * += fontSize + 4); g.drawString("If  you use Jalview, please cite: ", 50,
245      * y += fontSize + 4); g.drawString(
246      * "Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)"
247      * , 50, y += fontSize + 4); g.drawString(
248      * "Jalview Version 2 - a multiple sequence alignment editor and analysis workbench"
249      * , 50, y += fontSize + 4);
250      * g.drawString("Bioinformatics doi: 10.1093/bioinformatics/btp033", 50, y
251      * += fontSize + 4); }
252      */
253   }
254 }