76168ba768cdce2c7d5d2adce97a916fe7fc0c30
[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   /*
63    * as JTextPane in Java, JLabel in javascript
64    */
65   private Component splashText;
66
67   private JInternalFrame iframe;
68
69   private Image image;
70
71   private boolean transientDialog = false;
72
73   private long oldTextLength = -1;
74
75   /*
76    * allow click in the initial splash screen to dismiss it
77    * immediately (not if opened from About menu)
78    */
79   private MouseAdapter closer = new MouseAdapter()
80   {
81     @Override
82     public void mousePressed(MouseEvent evt)
83     {
84       if (transientDialog)
85       {
86         try
87         {
88           visible = false;
89           closeSplash();
90         } catch (Exception ex)
91         {
92         }
93       }
94     }
95   };
96
97   /**
98    * Constructor that displays the splash screen
99    * 
100    * @param isTransient
101    *          if true the panel removes itself on click or after a few seconds;
102    *          if false it stays up until closed by the user
103    */
104   public SplashScreen(boolean isTransient)
105   {
106     this.transientDialog = isTransient;
107
108     if (Platform.isJS()) // BH 2019
109     {
110       splashText = new JLabel("");
111       run();
112     }
113     else
114     {
115       /**
116        * Java only
117        *
118        * @j2sIgnore
119        */
120       {
121         splashText = new JTextPane();
122         Thread t = new Thread(this);
123         t.start();
124       }
125     }
126   }
127
128   /**
129    * ping the jalview version page then create and display the jalview
130    * splashscreen window.
131    */
132   void initSplashScreenWindow()
133   {
134     addMouseListener(closer);
135
136     try
137     {
138       if (!Platform.isJS())
139       {
140         image = ChannelProperties.getImage("banner");
141         Image logo = ChannelProperties.getImage("logo.48");
142         MediaTracker mt = new MediaTracker(this);
143         if (image != null)
144         {
145           mt.addImage(image, 0);
146         }
147         if (logo != null)
148         {
149           mt.addImage(logo, 1);
150         }
151         do
152         {
153           try
154           {
155             mt.waitForAll();
156           } catch (InterruptedException x)
157           {
158           }
159           if (mt.isErrorAny())
160           {
161             System.err.println("Error when loading images!");
162           }
163         } while (!mt.checkAll());
164         Desktop.instance.setIconImage(logo);
165       }
166     } catch (Exception ex)
167     {
168     }
169
170     iframe = new JInternalFrame();
171     iframe.setFrameIcon(null);
172     iframe.setClosable(true);
173     this.setLayout(new BorderLayout());
174     iframe.setContentPane(this);
175     iframe.setLayer(JLayeredPane.PALETTE_LAYER);
176     if (Platform.isJS())
177     {
178       // ignore in JavaScript
179     }
180     else
181     /**
182      * Java only
183      * 
184      * @j2sIgnore
185      */
186     {
187       ((JTextPane) splashText).setEditable(false);
188
189       SplashImage splashimg = new SplashImage(image);
190       iconimg.add(splashimg, BorderLayout.CENTER);
191       add(iconimg, BorderLayout.NORTH);
192     }
193     add(splashText, BorderLayout.CENTER);
194     splashText.addMouseListener(closer);
195     Desktop.desktop.add(iframe);
196     refreshText();
197   }
198
199   /**
200    * update text in author text panel reflecting current version information
201    */
202   protected boolean refreshText()
203   {
204     String newtext = Desktop.instance.getAboutMessage();
205     // System.err.println("Text found: \n"+newtext+"\nEnd of newtext.");
206     if (oldTextLength != newtext.length())
207     {
208       iframe.setVisible(false);
209       oldTextLength = newtext.length();
210       if (Platform.isJS()) // BH 2019
211       {
212         /*
213          * SwingJS doesn't have HTMLEditorKit, required for a JTextPane
214          * to display formatted html, so we use a simple alternative
215          */
216         String text = "<html><br><br><img src=\"swingjs/j2s/images/jalview_logo-48.png\"/><br>"
217                 + newtext + "</html>";
218         JLabel ta = new JLabel(text);
219         ta.setOpaque(true);
220         ta.setBackground(Color.white);
221         splashText = ta;
222       }
223       else
224       /**
225        * Java only
226        *
227        * @j2sIgnore
228        */
229       {
230         JTextPane jtp = new JTextPane();
231         jtp.setEditable(false);
232         jtp.setContentType("text/html");
233         jtp.setText("<html>" + newtext + "</html>");
234         jtp.addHyperlinkListener(this);
235         splashText = jtp;
236       }
237       splashText.addMouseListener(closer);
238
239       splashText.setVisible(true);
240       splashText.setSize(new Dimension(750, 375));
241       add(splashText, BorderLayout.CENTER);
242       revalidate();
243       iframe.setBounds((Desktop.instance.getWidth() - 750) / 2,
244               (Desktop.instance.getHeight() - 375) / 2, 750,
245               splashText.getHeight() + iconimg.getHeight());
246       iframe.validate();
247       iframe.setVisible(true);
248       return true;
249     }
250     return false;
251   }
252
253   /**
254    * Create splash screen, display it and clear it off again.
255    */
256   @Override
257   public void run()
258   {
259     initSplashScreenWindow();
260
261     long startTime = System.currentTimeMillis() / 1000;
262
263     while (visible)
264     {
265       iframe.repaint();
266       try
267       {
268         Thread.sleep(500);
269       } catch (Exception ex)
270       {
271       }
272
273       if (transientDialog && ((System.currentTimeMillis() / 1000)
274               - startTime) > SHOW_FOR_SECS)
275       {
276         visible = false;
277       }
278
279       if (visible && refreshText())
280       {
281         iframe.repaint();
282       }
283       if (!transientDialog)
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, FONT_SIZE + 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
343   @Override
344   public void hyperlinkUpdate(HyperlinkEvent e)
345   {
346     Desktop.hyperlinkUpdate(e);
347
348   }
349 }