f7c42b3820e82913fb53110b77419c29cb2943ff
[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     iframe.setVisible(true);
110     iframe.setBounds((int) ((Desktop.instance.getWidth() - 750) / 2),
111             (int) ((Desktop.instance.getHeight() - 160) / 2), 750,
112             iframe.getPreferredSize().height);
113
114   }
115
116   long oldtext = -1;
117
118   /**
119    * update text in author text panel reflecting current version information
120    */
121   protected boolean refreshText()
122   {
123     String newtext = Desktop.instance.getAboutMessage(true).toString();
124     if (oldtext != newtext.hashCode())
125     {
126       oldtext = newtext.hashCode();
127       authlist.setContentType("text/html");
128       authlist.setText(newtext);
129       return true;
130     }
131     return false;
132   }
133
134   /**
135    * Create splash screen, display it and clear it off again.
136    */
137   public void run()
138   {
139     initSplashScreenWindow();
140
141     long startTime = System.currentTimeMillis() / 1000;
142
143     while (visible)
144     {
145       try
146       {
147         Thread.sleep(1000);
148       } catch (Exception ex)
149       {
150       }
151
152       if (((System.currentTimeMillis() / 1000) - startTime) > 5)
153       {
154         visible = false;
155       }
156       else
157       {
158         if (refreshText())
159         {
160           repaint();
161         }
162       }
163     }
164
165     closeSplash();
166   }
167
168   /**
169    * DOCUMENT ME!
170    */
171   public void closeSplash()
172   {
173     try
174     {
175
176       iframe.setClosed(true);
177     } catch (Exception ex)
178     {
179     }
180   }
181
182   public class SplashImage extends JPanel
183   {
184     Image image;
185
186     public SplashImage(Image todisplay)
187     {
188       image = todisplay;
189       setPreferredSize(new Dimension(image.getWidth(this) + 8,
190               image.getHeight(this)));
191     }
192
193     public void paintComponent(Graphics g)
194     {
195       g.setColor(Color.white);
196       g.fillRect(0, 0, getWidth(), getHeight());
197       g.setColor(Color.black);
198       g.setFont(new Font("Verdana", Font.BOLD, fontSize + 6));
199
200       if (image != null)
201       {
202         g.drawImage(image, 4, (getHeight() - image.getHeight(this)) / 2,
203                 this);
204       }
205     }
206     /*
207      * int y = yoffset;
208      * 
209      * g.drawString("Jalview " + jalview.bin.Cache.getProperty("VERSION"), 50,
210      * y);
211      * 
212      * FontMetrics fm = g.getFontMetrics(); int vwidth =
213      * fm.stringWidth("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
214      * g.setFont(new Font("Verdana", Font.BOLD, fontSize + 2)); g.drawString(
215      * "Last updated: " + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"),
216      * 50 + vwidth + 5, y); if (jalview.bin.Cache.getDefault("LATEST_VERSION",
217      * "Checking").equals( "Checking")) { // Displayed when code version and
218      * jnlp version do not match g.drawString("...Checking latest version...",
219      * 50, y += fontSize + 10); y += 5; g.setColor(Color.black); } else if
220      * (!jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
221      * .equals(jalview.bin.Cache.getProperty("VERSION"))) { if
222      * (jalview.bin.Cache.getProperty("VERSION").toLowerCase()
223      * .indexOf("automated build") == -1) { // Displayed when code version and
224      * jnlp version do not match and code // version is not a development build
225      * g.setColor(Color.red); } g.drawString( "!! Jalview version " +
226      * jalview.bin.Cache.getDefault("LATEST_VERSION", "..Checking..") +
227      * " is available for download from "
228      * +jalview.bin.Cache.getDefault("www.jalview.org"
229      * ,"http://www.jalview.org")+" !!", 50, y += fontSize + 10); y += 5;
230      * g.setColor(Color.black); }
231      * 
232      * g.setFont(new Font("Verdana", Font.BOLD, fontSize)); g.drawString(
233      * "Authors: Jim Procter, Andrew Waterhouse, Michele Clamp, James Cuff, Steve Searle,"
234      * , 50, y += fontSize + 4); g.drawString("David Martin & Geoff Barton.",
235      * 60, y += fontSize + 4); g.drawString(
236      * "Development managed by The Barton Group, University of Dundee.", 50, y
237      * += fontSize + 4); g.drawString("If  you use Jalview, please cite: ", 50,
238      * y += fontSize + 4); g.drawString(
239      * "Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)"
240      * , 50, y += fontSize + 4); g.drawString(
241      * "Jalview Version 2 - a multiple sequence alignment editor and analysis workbench"
242      * , 50, y += fontSize + 4);
243      * g.drawString("Bioinformatics doi: 10.1093/bioinformatics/btp033", 50, y
244      * += fontSize + 4); }
245      */
246   }
247 }