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