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