Merge branch 'develop' into spike/JAL-4047/JAL-4048_columns_in_sequenceID
[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     Desktop.instance.acquireDialogQueue();
117     this.transientDialog = isTransient;
118
119     if (Platform.isJS()) // BH 2019
120     {
121       splashText = new JLabel("");
122       run();
123     }
124     else
125     {
126       /**
127        * Java only
128        *
129        * @j2sIgnore
130        */
131       {
132         splashText = new JTextPane();
133         splashText.setBackground(bg);
134         splashText.setForeground(fg);
135         splashText.setFont(font);
136         Thread t = new Thread(this);
137         t.start();
138       }
139     }
140   }
141
142   /**
143    * ping the jalview version page then create and display the jalview
144    * splashscreen window.
145    */
146   void initSplashScreenWindow()
147   {
148     addMouseListener(closer);
149
150     try
151     {
152       if (!Platform.isJS())
153       {
154         image = ChannelProperties.getImage("banner");
155         Image logo = ChannelProperties.getImage("logo.48");
156         MediaTracker mt = new MediaTracker(this);
157         if (image != null)
158         {
159           mt.addImage(image, 0);
160         }
161         if (logo != null)
162         {
163           mt.addImage(logo, 1);
164         }
165         do
166         {
167           try
168           {
169             mt.waitForAll();
170           } catch (InterruptedException x)
171           {
172           }
173           if (mt.isErrorAny())
174           {
175             jalview.bin.Console.errPrintln("Error when loading images!");
176           }
177         } while (!mt.checkAll());
178         Desktop.instance.setIconImages(ChannelProperties.getIconList());
179       }
180     } catch (Exception ex)
181     {
182     }
183
184     this.setBackground(bg);
185     this.setForeground(fg);
186     this.setFont(font);
187
188     iframe = new JInternalFrame();
189     iframe.setFrameIcon(null);
190     iframe.setClosable(true);
191     this.setLayout(new BorderLayout());
192     iframe.setContentPane(this);
193     iframe.setLayer(JLayeredPane.PALETTE_LAYER);
194     iframe.setBackground(bg);
195     iframe.setForeground(fg);
196     iframe.setFont(font);
197
198     if (Platform.isJS())
199     {
200       // ignore in JavaScript
201     }
202     else
203     /**
204      * Java only
205      * 
206      * @j2sIgnore
207      */
208     {
209       ((JTextPane) splashText).setEditable(false);
210       splashText.setBackground(bg);
211       splashText.setForeground(fg);
212       splashText.setFont(font);
213
214       SplashImage splashimg = new SplashImage(image);
215       iconimg.add(splashimg, BorderLayout.LINE_START);
216       iconimg.setBackground(bg);
217       add(iconimg, BorderLayout.NORTH);
218     }
219     add(splashText, BorderLayout.CENTER);
220     splashText.addMouseListener(closer);
221     Desktop.desktop.add(iframe);
222     refreshText();
223   }
224
225   /**
226    * update text in author text panel reflecting current version information
227    */
228   protected boolean refreshText()
229   {
230     String newtext = Desktop.instance.getAboutMessage();
231     // jalview.bin.Console.errPrintln("Text found: \n"+newtext+"\nEnd of newtext.");
232     if (oldTextLength != newtext.length())
233     {
234       iframe.setVisible(false);
235       oldTextLength = newtext.length();
236       if (Platform.isJS()) // BH 2019
237       {
238         /*
239          * SwingJS doesn't have HTMLEditorKit, required for a JTextPane
240          * to display formatted html, so we use a simple alternative
241          */
242         String text = "<html><br><img src=\""
243                 + ChannelProperties.getImageURL("banner") + "\"/>" + newtext
244                 + "<br></html>";
245         JLabel ta = new JLabel(text);
246         ta.setOpaque(true);
247         ta.setBackground(Color.white);
248         splashText = ta;
249       }
250       else
251       /**
252        * Java only
253        *
254        * @j2sIgnore
255        */
256       {
257         JTextPane jtp = new JTextPane();
258         jtp.setEditable(false);
259         jtp.setBackground(bg);
260         jtp.setForeground(fg);
261         jtp.setFont(font);
262         jtp.setContentType("text/html");
263         jtp.setText("<html>" + newtext + "</html>");
264         jtp.addHyperlinkListener(this);
265         splashText = jtp;
266       }
267       splashText.addMouseListener(closer);
268
269       splashText.setVisible(true);
270       splashText.setSize(new Dimension(750,
271               425 + logoSize + (Platform.isJS() ? 40 : 0)));
272       splashText.setBackground(bg);
273       splashText.setForeground(fg);
274       splashText.setFont(font);
275       add(splashText, BorderLayout.CENTER);
276       revalidate();
277       int width = Math.max(splashText.getWidth(), iconimg.getWidth());
278       int height = splashText.getHeight() + iconimg.getHeight();
279       iframe.setBounds(
280               Math.max(0, (Desktop.instance.getWidth() - width) / 2),
281               Math.max(0, (Desktop.instance.getHeight() - height) / 2),
282               width, height);
283       iframe.validate();
284       iframe.setVisible(true);
285       return true;
286     }
287     return false;
288   }
289
290   /**
291    * Create splash screen, display it and clear it off again.
292    */
293   @Override
294   public void run()
295   {
296     initSplashScreenWindow();
297
298     long startTime = System.currentTimeMillis() / 1000;
299
300     while (visible)
301     {
302       iframe.repaint();
303       try
304       {
305         Thread.sleep(500);
306       } catch (Exception ex)
307       {
308       }
309
310       if (transientDialog && ((System.currentTimeMillis() / 1000)
311               - startTime) > SHOW_FOR_SECS)
312       {
313         visible = false;
314       }
315
316       if (visible && refreshText())
317       {
318         iframe.repaint();
319       }
320       if (!transientDialog)
321       {
322         return;
323       }
324     }
325
326     closeSplash();
327     Desktop.instance.releaseDialogQueue();
328   }
329
330   /**
331    * DOCUMENT ME!
332    */
333   public void closeSplash()
334   {
335     try
336     {
337
338       iframe.setClosed(true);
339     } catch (Exception ex)
340     {
341     }
342   }
343
344   public class SplashImage extends JPanel
345   {
346     Image image;
347
348     public SplashImage(Image todisplay)
349     {
350       image = todisplay;
351       if (image != null)
352       {
353         setPreferredSize(new Dimension(image.getWidth(this) + 8,
354                 image.getHeight(this)));
355       }
356     }
357
358     @Override
359     public Dimension getPreferredSize()
360     {
361       return new Dimension(image.getWidth(this) + 8, image.getHeight(this));
362     }
363
364     @Override
365     public void paintComponent(Graphics g)
366     {
367       g.setColor(bg);
368       g.fillRect(0, 0, getWidth(), getHeight());
369       g.setColor(fg);
370       g.setFont(new Font(font.getFontName(), Font.BOLD, FONT_SIZE + 6));
371
372       if (image != null)
373       {
374         g.drawImage(image, (getWidth() - image.getWidth(this)) / 2,
375                 (getHeight() - image.getHeight(this)) / 2, this);
376       }
377     }
378   }
379
380   @Override
381   public void hyperlinkUpdate(HyperlinkEvent e)
382   {
383     Desktop.hyperlinkUpdate(e);
384
385   }
386 }