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