JAL-1130 JAL-1139 hardcode preferred height to logo image height
[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       add(authlist, BorderLayout.CENTER);
157       iframe.setBounds((int) ((Desktop.instance.getWidth() - 750) / 2),
158               (int) ((Desktop.instance.getHeight() - 160) / 2), 750,
159               iframe.getPreferredSize().height);
160       //System.err.println("New preferred size: "+iframe.getPreferredSize().height);
161       iframe.validate();
162       iframe.setVisible(true);
163
164       return true;
165     }
166     return false;
167   }
168
169   /**
170    * Create splash screen, display it and clear it off again.
171    */
172   public void run()
173   {
174     initSplashScreenWindow();
175
176     long startTime = System.currentTimeMillis() / 1000;
177
178     while (visible)
179     {
180       iframe.repaint();
181       try
182       {
183         Thread.sleep(500);
184       } catch (Exception ex)
185       {
186       }
187       
188       if (!interactiveDialog && ((System.currentTimeMillis() / 1000) - startTime) > 5)
189       {
190         visible = false;
191       }
192
193       if (visible && refreshText())
194       {
195 //          if (interactiveDialog) {
196             iframe.repaint();
197 //          } else {
198 //            iframe.repaint();
199 //          };
200       }
201       if (interactiveDialog)
202       {
203         return;
204       }
205     }
206
207     closeSplash();
208     Desktop.instance.startDialogQueue();
209   }
210
211   /**
212    * DOCUMENT ME!
213    */
214   public void closeSplash()
215   {
216     try
217     {
218
219       iframe.setClosed(true);
220     } catch (Exception ex)
221     {
222     }
223   }
224
225   public class SplashImage extends JPanel
226   {
227     Image image;
228
229     public SplashImage(Image todisplay)
230     {
231       image = todisplay;
232       setPreferredSize(new Dimension(image.getWidth(this) + 8,
233               image.getHeight(this)));
234     }
235     @Override
236     public Dimension getPreferredSize()
237     {
238      return new Dimension(image.getWidth(this) + 8,
239              image.getHeight(this));
240     }
241     public void paintComponent(Graphics g)
242     {
243       g.setColor(Color.white);
244       g.fillRect(0, 0, getWidth(), getHeight());
245       g.setColor(Color.black);
246       g.setFont(new Font("Verdana", Font.BOLD, fontSize + 6));
247
248       if (image != null)
249       {
250         g.drawImage(image, (getWidth() - image.getWidth(this)) / 2, (getHeight() - image.getHeight(this)) / 2,
251                 this);
252       }
253     }
254     /*
255      * int y = yoffset;
256      * 
257      * g.drawString("Jalview " + jalview.bin.Cache.getProperty("VERSION"), 50,
258      * y);
259      * 
260      * FontMetrics fm = g.getFontMetrics(); int vwidth =
261      * fm.stringWidth("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
262      * g.setFont(new Font("Verdana", Font.BOLD, fontSize + 2)); g.drawString(
263      * "Last updated: " + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"),
264      * 50 + vwidth + 5, y); if (jalview.bin.Cache.getDefault("LATEST_VERSION",
265      * "Checking").equals( "Checking")) { // Displayed when code version and
266      * jnlp version do not match g.drawString("...Checking latest version...",
267      * 50, y += fontSize + 10); y += 5; g.setColor(Color.black); } else if
268      * (!jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
269      * .equals(jalview.bin.Cache.getProperty("VERSION"))) { if
270      * (jalview.bin.Cache.getProperty("VERSION").toLowerCase()
271      * .indexOf("automated build") == -1) { // Displayed when code version and
272      * jnlp version do not match and code // version is not a development build
273      * g.setColor(Color.red); } g.drawString( "!! Jalview version " +
274      * jalview.bin.Cache.getDefault("LATEST_VERSION", "..Checking..") +
275      * " is available for download from "
276      * +jalview.bin.Cache.getDefault("www.jalview.org"
277      * ,"http://www.jalview.org")+" !!", 50, y += fontSize + 10); y += 5;
278      * g.setColor(Color.black); }
279      * 
280      * g.setFont(new Font("Verdana", Font.BOLD, fontSize)); g.drawString(
281      * "Authors: Jim Procter, Andrew Waterhouse, Michele Clamp, James Cuff, Steve Searle,"
282      * , 50, y += fontSize + 4); g.drawString("David Martin & Geoff Barton.",
283      * 60, y += fontSize + 4); g.drawString(
284      * "Development managed by The Barton Group, University of Dundee.", 50, y
285      * += fontSize + 4); g.drawString("If  you use Jalview, please cite: ", 50,
286      * y += fontSize + 4); g.drawString(
287      * "Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)"
288      * , 50, y += fontSize + 4); g.drawString(
289      * "Jalview Version 2 - a multiple sequence alignment editor and analysis workbench"
290      * , 50, y += fontSize + 4);
291      * g.drawString("Bioinformatics doi: 10.1093/bioinformatics/btp033", 50, y
292      * += fontSize + 4); }
293      */
294   }
295   @Override
296   public void hyperlinkUpdate(HyperlinkEvent e)
297   {
298     Desktop.hyperlinkUpdate(e);
299     
300   }
301 }