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