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