9e75474408c3ae4a829cb268a8b9cedb7d5b7a87
[jalview.git] / src / jalview / gui / SplashScreen.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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 import javax.swing.event.HyperlinkEvent;
25 import javax.swing.event.HyperlinkListener;
26
27 /**
28  * DOCUMENT ME!
29  * 
30  * @author $author$
31  * @version $Revision$
32  */
33 public class SplashScreen extends JPanel implements Runnable,
34         HyperlinkListener
35 {
36   boolean visible = true;
37
38   JPanel iconimg = new JPanel(new BorderLayout());
39
40   JTextPane authlist = new JTextPane();
41
42   JInternalFrame iframe;
43
44   Image image;
45
46   int fontSize = 11;
47
48   int yoffset = 30;
49
50   /**
51    * Creates a new SplashScreen object.
52    */
53   public SplashScreen()
54   {
55     this(false);
56   }
57
58   private boolean interactiveDialog = false;
59
60   /**
61    * 
62    * @param interactive
63    *          if true - an internal dialog is opened rather than a free-floating
64    *          splash screen
65    */
66   public SplashScreen(boolean interactive)
67   {
68     this.interactiveDialog = interactive;
69     // show a splashscreen that will disapper
70     Thread t = new Thread(this);
71     t.start();
72   }
73
74   MouseAdapter closer = new MouseAdapter()
75   {
76     public void mousePressed(MouseEvent evt)
77     {
78       try
79       {
80         if (!interactiveDialog)
81         {
82           visible = false;
83           closeSplash();
84         }
85       } catch (Exception ex)
86       {
87       }
88     }
89   };
90
91   /**
92    * ping the jalview version page then create and display the jalview
93    * splashscreen window.
94    */
95   void initSplashScreenWindow()
96   {
97     addMouseListener(closer);
98     try
99     {
100       java.net.URL url = getClass().getResource("/images/Jalview_Logo.png");
101       java.net.URL urllogo = getClass().getResource(
102               "/images/Jalview_Logo_small.png");
103
104       if (url != null)
105       {
106         image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
107         Image logo = java.awt.Toolkit.getDefaultToolkit().createImage(
108                 urllogo);
109         MediaTracker mt = new MediaTracker(this);
110         mt.addImage(image, 0);
111         mt.addImage(logo, 1);
112         do
113         {
114           try
115           {
116             mt.waitForAll();
117           } catch (InterruptedException x)
118           {
119           }
120           ;
121           if (mt.isErrorAny())
122           {
123             System.err.println("Error when loading images!");
124           }
125         } while (!mt.checkAll());
126         Desktop.instance.setIconImage(logo);
127       }
128     } catch (Exception ex)
129     {
130     }
131
132     iframe = new JInternalFrame();
133     iframe.setFrameIcon(null);
134     iframe.setClosable(interactiveDialog);
135     this.setLayout(new BorderLayout());
136     iframe.setContentPane(this);
137     iframe.setLayer(JLayeredPane.PALETTE_LAYER);
138
139     SplashImage splashimg = new SplashImage(image);
140     iconimg.add(splashimg, BorderLayout.CENTER);
141     add(iconimg, BorderLayout.NORTH);
142     add(authlist, BorderLayout.CENTER);
143     authlist.setEditable(false);
144     authlist.addMouseListener(closer);
145     Desktop.desktop.add(iframe);
146     refreshText();
147   }
148
149   long oldtext = -1;
150
151   /**
152    * update text in author text panel reflecting current version information
153    */
154   protected boolean refreshText()
155   {
156     String newtext = Desktop.instance.getAboutMessage(true).toString();
157     // System.err.println("Text found: \n"+newtext+"\nEnd of newtext.");
158     if (oldtext != newtext.length())
159     {
160       iframe.setVisible(false);
161       oldtext = newtext.length();
162       authlist = new JTextPane();
163       authlist.setEditable(false);
164       authlist.addMouseListener(closer);
165       authlist.addHyperlinkListener(this);
166       authlist.setContentType("text/html");
167       authlist.setText(newtext);
168       authlist.setVisible(true);
169       authlist.setSize(new Dimension(750, 275));
170       add(authlist, BorderLayout.CENTER);
171       revalidate();
172       iframe.setBounds((int) ((Desktop.instance.getWidth() - 750) / 2),
173               (int) ((Desktop.instance.getHeight() - 140) / 2), 750,
174               authlist.getHeight() + iconimg.getHeight());
175       iframe.validate();
176       iframe.setVisible(true);
177
178       return true;
179     }
180     return false;
181   }
182
183   /**
184    * Create splash screen, display it and clear it off again.
185    */
186   public void run()
187   {
188     initSplashScreenWindow();
189
190     long startTime = System.currentTimeMillis() / 1000;
191
192     while (visible)
193     {
194       iframe.repaint();
195       try
196       {
197         Thread.sleep(500);
198       } catch (Exception ex)
199       {
200       }
201
202       if (!interactiveDialog
203               && ((System.currentTimeMillis() / 1000) - startTime) > 5)
204       {
205         visible = false;
206       }
207
208       if (visible && refreshText())
209       {
210         // if (interactiveDialog) {
211         iframe.repaint();
212         // } else {
213         // iframe.repaint();
214         // };
215       }
216       if (interactiveDialog)
217       {
218         return;
219       }
220     }
221
222     closeSplash();
223     Desktop.instance.startDialogQueue();
224   }
225
226   /**
227    * DOCUMENT ME!
228    */
229   public void closeSplash()
230   {
231     try
232     {
233
234       iframe.setClosed(true);
235     } catch (Exception ex)
236     {
237     }
238   }
239
240   public class SplashImage extends JPanel
241   {
242     Image image;
243
244     public SplashImage(Image todisplay)
245     {
246       image = todisplay;
247       setPreferredSize(new Dimension(image.getWidth(this) + 8,
248               image.getHeight(this)));
249     }
250
251     @Override
252     public Dimension getPreferredSize()
253     {
254       return new Dimension(image.getWidth(this) + 8, image.getHeight(this));
255     }
256
257     public void paintComponent(Graphics g)
258     {
259       g.setColor(Color.white);
260       g.fillRect(0, 0, getWidth(), getHeight());
261       g.setColor(Color.black);
262       g.setFont(new Font("Verdana", Font.BOLD, fontSize + 6));
263
264       if (image != null)
265       {
266         g.drawImage(image, (getWidth() - image.getWidth(this)) / 2,
267                 (getHeight() - image.getHeight(this)) / 2, this);
268       }
269     }
270     /*
271      * int y = yoffset;
272      * 
273      * g.drawString("Jalview " + jalview.bin.Cache.getProperty("VERSION"), 50,
274      * y);
275      * 
276      * FontMetrics fm = g.getFontMetrics(); int vwidth =
277      * fm.stringWidth("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
278      * g.setFont(new Font("Verdana", Font.BOLD, fontSize + 2)); g.drawString(
279      * "Last updated: " + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"),
280      * 50 + vwidth + 5, y); if (jalview.bin.Cache.getDefault("LATEST_VERSION",
281      * "Checking").equals( "Checking")) { // Displayed when code version and
282      * jnlp version do not match g.drawString("...Checking latest version...",
283      * 50, y += fontSize + 10); y += 5; g.setColor(Color.black); } else if
284      * (!jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
285      * .equals(jalview.bin.Cache.getProperty("VERSION"))) { if
286      * (jalview.bin.Cache.getProperty("VERSION").toLowerCase()
287      * .indexOf("automated build") == -1) { // Displayed when code version and
288      * jnlp version do not match and code // version is not a development build
289      * g.setColor(Color.red); } g.drawString( "!! Jalview version " +
290      * jalview.bin.Cache.getDefault("LATEST_VERSION", "..Checking..") +
291      * " is available for download from "
292      * +jalview.bin.Cache.getDefault("www.jalview.org"
293      * ,"http://www.jalview.org")+" !!", 50, y += fontSize + 10); y += 5;
294      * g.setColor(Color.black); }
295      * 
296      * g.setFont(new Font("Verdana", Font.BOLD, fontSize)); g.drawString(
297      * "Authors: Jim Procter, Andrew Waterhouse, Michele Clamp, James Cuff, Steve Searle,"
298      * , 50, y += fontSize + 4); g.drawString("David Martin & Geoff Barton.",
299      * 60, y += fontSize + 4); g.drawString(
300      * "Development managed by The Barton Group, University of Dundee.", 50, y
301      * += fontSize + 4); g.drawString("If  you use Jalview, please cite: ", 50,
302      * y += fontSize + 4); g.drawString(
303      * "Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)"
304      * , 50, y += fontSize + 4); g.drawString(
305      * "Jalview Version 2 - a multiple sequence alignment editor and analysis workbench"
306      * , 50, y += fontSize + 4);
307      * g.drawString("Bioinformatics doi: 10.1093/bioinformatics/btp033", 50, y
308      * += fontSize + 4); }
309      */
310   }
311
312   @Override
313   public void hyperlinkUpdate(HyperlinkEvent e)
314   {
315     Desktop.hyperlinkUpdate(e);
316
317   }
318 }