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