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