52c1d18d0d45af39fe4f48cf4acee83b4f4a149c
[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.Component;
26 import java.awt.Dimension;
27 import java.awt.Font;
28 import java.awt.Graphics;
29 import java.awt.Image;
30 import java.awt.MediaTracker;
31 import java.awt.event.MouseAdapter;
32 import java.awt.event.MouseEvent;
33
34 import javax.swing.JInternalFrame;
35 import javax.swing.JLabel;
36 import javax.swing.JLayeredPane;
37 import javax.swing.JPanel;
38 import javax.swing.JTextPane;
39 import javax.swing.event.HyperlinkEvent;
40 import javax.swing.event.HyperlinkListener;
41
42 import jalview.util.ChannelProperties;
43 import jalview.util.Platform;
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   private static final int SHOW_FOR_SECS = 5;
55
56   private static final int FONT_SIZE = 11;
57
58   private boolean visible = true;
59
60   private JPanel iconimg = new JPanel(new BorderLayout());
61
62   // could change fg, bg, font later to use ChannelProperties (these are not
63   // actually being used!)
64   private static Color bg = Color.WHITE;
65
66   private static Color fg = Color.BLACK;
67
68   private static Font font = new Font("SansSerif", Font.PLAIN, FONT_SIZE);
69
70   /*
71    * as JTextPane in Java, JLabel in javascript
72    */
73   private Component splashText;
74
75   private JInternalFrame iframe;
76
77   private Image image;
78
79   private boolean transientDialog = false;
80
81   private long oldTextLength = -1;
82
83   public static int logoSize = 32;
84
85   /*
86    * allow click in the initial splash screen to dismiss it
87    * immediately (not if opened from About menu)
88    */
89   private MouseAdapter closer = new MouseAdapter()
90   {
91     @Override
92     public void mousePressed(MouseEvent evt)
93     {
94       if (transientDialog)
95       {
96         try
97         {
98           visible = false;
99           closeSplash();
100         } catch (Exception ex)
101         {
102         }
103       }
104     }
105   };
106
107   /**
108    * Constructor that displays the splash screen
109    * 
110    * @param isTransient
111    *          if true the panel removes itself on click or after a few seconds;
112    *          if false it stays up until closed by the user
113    */
114   public SplashScreen(boolean isTransient)
115   {
116     this.transientDialog = isTransient;
117     if (this.transientDialog)
118     {
119       Desktop.instance.acquireDialogQueue();
120     }
121
122     if (Platform.isJS()) // BH 2019
123     {
124       splashText = new JLabel("");
125       run();
126     }
127     else
128     {
129       /**
130        * Java only
131        *
132        * @j2sIgnore
133        */
134       {
135         splashText = new JTextPane();
136         splashText.setBackground(bg);
137         splashText.setForeground(fg);
138         splashText.setFont(font);
139         Thread t = new Thread(this);
140         t.start();
141       }
142     }
143   }
144
145   /**
146    * ping the jalview version page then create and display the jalview
147    * splashscreen window.
148    */
149   void initSplashScreenWindow()
150   {
151     addMouseListener(closer);
152
153     try
154     {
155       if (!Platform.isJS())
156       {
157         image = ChannelProperties.getImage("banner");
158         Image logo = ChannelProperties.getImage("logo.48");
159         MediaTracker mt = new MediaTracker(this);
160         if (image != null)
161         {
162           mt.addImage(image, 0);
163         }
164         if (logo != null)
165         {
166           mt.addImage(logo, 1);
167         }
168         do
169         {
170           try
171           {
172             mt.waitForAll();
173           } catch (InterruptedException x)
174           {
175           }
176           if (mt.isErrorAny())
177           {
178             jalview.bin.Console.errPrintln("Error when loading images!");
179           }
180         } while (!mt.checkAll());
181         Desktop.instance.setIconImages(ChannelProperties.getIconList());
182       }
183     } catch (Exception ex)
184     {
185     }
186
187     this.setBackground(bg);
188     this.setForeground(fg);
189     this.setFont(font);
190
191     iframe = new JInternalFrame();
192     iframe.setFrameIcon(null);
193     iframe.setClosable(true);
194     this.setLayout(new BorderLayout());
195     iframe.setContentPane(this);
196     iframe.setLayer(JLayeredPane.PALETTE_LAYER);
197     iframe.setBackground(bg);
198     iframe.setForeground(fg);
199     iframe.setFont(font);
200
201     if (Platform.isJS())
202     {
203       // ignore in JavaScript
204     }
205     else
206     /**
207      * Java only
208      * 
209      * @j2sIgnore
210      */
211     {
212       ((JTextPane) splashText).setEditable(false);
213       splashText.setBackground(bg);
214       splashText.setForeground(fg);
215       splashText.setFont(font);
216
217       SplashImage splashimg = new SplashImage(image);
218       iconimg.add(splashimg, BorderLayout.LINE_START);
219       iconimg.setBackground(bg);
220       add(iconimg, BorderLayout.NORTH);
221     }
222     add(splashText, BorderLayout.CENTER);
223     splashText.addMouseListener(closer);
224     Desktop.desktop.add(iframe);
225     refreshText();
226   }
227
228   /**
229    * update text in author text panel reflecting current version information
230    */
231   protected boolean refreshText()
232   {
233     String newtext = Desktop.instance.getAboutMessage();
234     // jalview.bin.Console.errPrintln("Text found: \n"+newtext+"\nEnd of
235     // newtext.");
236     if (oldTextLength != newtext.length())
237     {
238       iframe.setVisible(false);
239       oldTextLength = newtext.length();
240       if (Platform.isJS()) // BH 2019
241       {
242         /*
243          * SwingJS doesn't have HTMLEditorKit, required for a JTextPane
244          * to display formatted html, so we use a simple alternative
245          */
246         String text = "<html><br><img src=\""
247                 + ChannelProperties.getImageURL("banner") + "\"/>" + newtext
248                 + "<br></html>";
249         JLabel ta = new JLabel(text);
250         ta.setOpaque(true);
251         ta.setBackground(Color.white);
252         splashText = ta;
253       }
254       else
255       /**
256        * Java only
257        *
258        * @j2sIgnore
259        */
260       {
261         JTextPane jtp = new JTextPane();
262         jtp.setEditable(false);
263         jtp.setBackground(bg);
264         jtp.setForeground(fg);
265         jtp.setFont(font);
266         jtp.setContentType("text/html");
267         jtp.setText("<html>" + newtext + "</html>");
268         jtp.addHyperlinkListener(this);
269         splashText = jtp;
270       }
271       splashText.addMouseListener(closer);
272
273       splashText.setVisible(true);
274       splashText.setSize(new Dimension(750,
275               425 + logoSize + (Platform.isJS() ? 40 : 0)));
276       splashText.setBackground(bg);
277       splashText.setForeground(fg);
278       splashText.setFont(font);
279       add(splashText, BorderLayout.CENTER);
280       revalidate();
281       int width = Math.max(splashText.getWidth(), iconimg.getWidth());
282       int height = splashText.getHeight() + iconimg.getHeight();
283       iframe.setBounds(
284               Math.max(0, (Desktop.instance.getWidth() - width) / 2),
285               Math.max(0, (Desktop.instance.getHeight() - height) / 2),
286               width, height);
287       iframe.validate();
288       iframe.setVisible(true);
289       return true;
290     }
291     return false;
292   }
293
294   /**
295    * Create splash screen, display it and clear it off again.
296    */
297   @Override
298   public void run()
299   {
300     initSplashScreenWindow();
301
302     long startTime = System.currentTimeMillis() / 1000;
303
304     while (visible)
305     {
306       iframe.repaint();
307       try
308       {
309         Thread.sleep(500);
310       } catch (Exception ex)
311       {
312       }
313
314       if (transientDialog && ((System.currentTimeMillis() / 1000)
315               - startTime) > SHOW_FOR_SECS)
316       {
317         visible = false;
318       }
319
320       if (visible && refreshText())
321       {
322         iframe.repaint();
323       }
324       if (!transientDialog)
325       {
326         return;
327       }
328     }
329
330     closeSplash();
331   }
332
333   /**
334    * DOCUMENT ME!
335    */
336   public void closeSplash()
337   {
338     if (this.transientDialog)
339     {
340       Desktop.instance.releaseDialogQueue();
341     }
342     try
343     {
344
345       iframe.setClosed(true);
346     } catch (Exception ex)
347     {
348     }
349   }
350
351   public class SplashImage extends JPanel
352   {
353     Image image;
354
355     public SplashImage(Image todisplay)
356     {
357       image = todisplay;
358       if (image != null)
359       {
360         setPreferredSize(new Dimension(image.getWidth(this) + 8,
361                 image.getHeight(this)));
362       }
363     }
364
365     @Override
366     public Dimension getPreferredSize()
367     {
368       return new Dimension(image.getWidth(this) + 8, image.getHeight(this));
369     }
370
371     @Override
372     public void paintComponent(Graphics g)
373     {
374       g.setColor(bg);
375       g.fillRect(0, 0, getWidth(), getHeight());
376       g.setColor(fg);
377       g.setFont(new Font(font.getFontName(), Font.BOLD, FONT_SIZE + 6));
378
379       if (image != null)
380       {
381         g.drawImage(image, (getWidth() - image.getWidth(this)) / 2,
382                 (getHeight() - image.getHeight(this)) / 2, this);
383       }
384     }
385   }
386
387   @Override
388   public void hyperlinkUpdate(HyperlinkEvent e)
389   {
390     Desktop.hyperlinkUpdate(e);
391
392   }
393 }