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