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