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