JAL-3032 small formatting and isJS() tidy
[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.MediaTracker;
33 import java.awt.event.MouseAdapter;
34 import java.awt.event.MouseEvent;
35
36 import javax.swing.JInternalFrame;
37 import javax.swing.JLabel;
38 import javax.swing.JLayeredPane;
39 import javax.swing.JPanel;
40 import javax.swing.JTextPane;
41 import javax.swing.event.HyperlinkEvent;
42 import javax.swing.event.HyperlinkListener;
43
44 /**
45  * DOCUMENT ME!
46  * 
47  * @author $author$
48  * @version $Revision$
49  */
50 public class SplashScreen extends JPanel
51         implements Runnable, HyperlinkListener
52 {
53   boolean visible = true;
54
55   JPanel iconimg = new JPanel(new BorderLayout());
56
57   /**
58    * either text area in javascript or in java text pane
59    */
60   Component authlist;
61
62   JInternalFrame iframe;
63
64   Image image;
65
66   int fontSize = 11;
67
68   int yoffset = 30;
69
70   /**
71    * Creates a new SplashScreen object.
72    */
73   public SplashScreen()
74   {
75     this(false);
76   }
77
78   private boolean interactiveDialog = false;
79
80   /**
81    * 
82    * @param interactive
83    *          if true - an internal dialog is opened rather than a free-floating
84    *          splash screen
85    */
86   public SplashScreen(boolean interactive)
87   {
88     this.interactiveDialog = interactive;
89     // show a splashscreen that will disapper
90     if (Platform.isJS()) // BH 2019
91     // if (/** @j2sNative true || */ false)
92     {
93       authlist = new JLabel("");
94       run();
95     }
96     else
97     {
98       /**
99        * Java only
100        * 
101        * @j2sNative
102        */
103       {
104         authlist = new JTextPane();
105         Thread t = new Thread(this);
106         t.start();
107       }
108     }
109
110   }
111
112   MouseAdapter closer = new MouseAdapter()
113   {
114     @Override
115     public void mousePressed(MouseEvent evt)
116     {
117       try
118       {
119         if (!interactiveDialog)
120         {
121           visible = false;
122           closeSplash();
123         }
124       } catch (Exception ex)
125       {
126       }
127     }
128   };
129
130   /**
131    * ping the jalview version page then create and display the jalview
132    * splashscreen window.
133    */
134   void initSplashScreenWindow()
135   {
136     addMouseListener(closer);
137     try
138     {
139       java.net.URL url = getClass().getResource("/images/Jalview_Logo.png");
140       java.net.URL urllogo = getClass()
141               .getResource("/images/Jalview_Logo_small.png");
142
143       if (!Platform.isJS() && url != null)
144       {
145         image = java.awt.Toolkit.getDefaultToolkit().createImage(url);
146         Image logo = java.awt.Toolkit.getDefaultToolkit()
147                 .createImage(urllogo);
148         MediaTracker mt = new MediaTracker(this);
149         mt.addImage(image, 0);
150         mt.addImage(logo, 1);
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(interactiveDialog);
173     this.setLayout(new BorderLayout());
174     iframe.setContentPane(this);
175     iframe.setLayer(JLayeredPane.PALETTE_LAYER);
176     /**
177      * we add image directly in html for javascript ?
178      * 
179      * @j2sNative
180      */
181     {
182       ((JTextPane) authlist).setEditable(false);
183
184       SplashImage splashimg = new SplashImage(image);
185       iconimg.add(splashimg, BorderLayout.CENTER);
186       add(iconimg, BorderLayout.NORTH);
187     }
188     add(authlist, BorderLayout.CENTER);
189     authlist.addMouseListener(closer);
190     Desktop.desktop.add(iframe);
191     refreshText();
192   }
193
194   long oldtext = -1;
195
196   /**
197    * update text in author text panel reflecting current version information
198    */
199   @SuppressWarnings("unused")
200   protected boolean refreshText()
201   {
202     String newtext = Desktop.instance.getAboutMessage(true).toString();
203     // System.err.println("Text found: \n"+newtext+"\nEnd of newtext.");
204     if (oldtext != newtext.length())
205     {
206       iframe.setVisible(false);
207       oldtext = newtext.length();
208       if (Platform.isJS()) // BH 2019
209       // if (/** @j2sNative true || */ false)
210       {
211         authlist = new JLabel(
212                 "<html><br/><br/><img src=\"swingjs/j2s/images/Jalview_Logo.png\"/><br/>"
213                         + newtext);
214         ((JLabel) authlist).setOpaque(true);
215         ((JLabel) authlist).setBackground(Color.white);
216       }
217       else
218       {
219         /**
220          * Java only
221          * 
222          * @j2sNative
223          */
224         {
225           authlist = new JTextPane();
226           ((JTextPane) authlist).setEditable(false);
227           ((JTextPane) authlist).setContentType("text/html");
228           ((JTextPane) authlist).setText(newtext);
229           ((JTextPane) authlist).addHyperlinkListener(this);
230         }
231       }
232       authlist.addMouseListener(closer);
233
234       authlist.setVisible(true);
235       authlist.setSize(new Dimension(750, 375));
236       add(authlist, BorderLayout.CENTER);
237       revalidate();
238       iframe.setBounds((Desktop.instance.getWidth() - 750) / 2,
239               (Desktop.instance.getHeight() - 375) / 2, 750,
240               authlist.getHeight() + iconimg.getHeight());
241       iframe.validate();
242       iframe.setVisible(true);
243       return true;
244     }
245     return false;
246   }
247
248   /**
249    * Create splash screen, display it and clear it off again.
250    */
251   @Override
252   public void run()
253   {
254     initSplashScreenWindow();
255
256     long startTime = System.currentTimeMillis() / 1000;
257
258     while (visible)
259     {
260       iframe.repaint();
261       try
262       {
263         Thread.sleep(500);
264       } catch (Exception ex)
265       {
266       }
267
268       if (!interactiveDialog
269               && ((System.currentTimeMillis() / 1000) - startTime) > 5)
270       {
271         visible = false;
272       }
273
274       if (visible && refreshText())
275       {
276         // if (interactiveDialog) {
277         iframe.repaint();
278         // } else {
279         // iframe.repaint();
280         // };
281       }
282       if (interactiveDialog)
283       {
284         return;
285       }
286     }
287
288     closeSplash();
289     Desktop.instance.startDialogQueue();
290   }
291
292   /**
293    * DOCUMENT ME!
294    */
295   public void closeSplash()
296   {
297     try
298     {
299
300       iframe.setClosed(true);
301     } catch (Exception ex)
302     {
303     }
304   }
305
306   public class SplashImage extends JPanel
307   {
308     Image image;
309
310     public SplashImage(Image todisplay)
311     {
312       image = todisplay;
313       if (image != null)
314       {
315         setPreferredSize(new Dimension(image.getWidth(this) + 8,
316                 image.getHeight(this)));
317       }
318     }
319
320     @Override
321     public Dimension getPreferredSize()
322     {
323       return new Dimension(image.getWidth(this) + 8, image.getHeight(this));
324     }
325
326     @Override
327     public void paintComponent(Graphics g)
328     {
329       g.setColor(Color.white);
330       g.fillRect(0, 0, getWidth(), getHeight());
331       g.setColor(Color.black);
332       g.setFont(new Font("Verdana", Font.BOLD, fontSize + 6));
333
334       if (image != null)
335       {
336         g.drawImage(image, (getWidth() - image.getWidth(this)) / 2,
337                 (getHeight() - image.getHeight(this)) / 2, this);
338       }
339     }
340     /*
341      * int y = yoffset;
342      * 
343      * g.drawString("Jalview " + jalview.bin.Cache.getProperty("VERSION"), 50,
344      * y);
345      * 
346      * FontMetrics fm = g.getFontMetrics(); int vwidth =
347      * fm.stringWidth("Jalview " + jalview.bin.Cache.getProperty("VERSION"));
348      * g.setFont(new Font("Verdana", Font.BOLD, fontSize + 2)); g.drawString(
349      * "Last updated: " + jalview.bin.Cache.getDefault("BUILD_DATE", "unknown"),
350      * 50 + vwidth + 5, y); if (jalview.bin.Cache.getDefault("LATEST_VERSION",
351      * "Checking").equals( "Checking")) { // Displayed when code version and
352      * jnlp version do not match g.drawString("...Checking latest version...",
353      * 50, y += fontSize + 10); y += 5; g.setColor(Color.black); } else if
354      * (!jalview.bin.Cache.getDefault("LATEST_VERSION", "Checking")
355      * .equals(jalview.bin.Cache.getProperty("VERSION"))) { if
356      * (jalview.bin.Cache.getProperty("VERSION").toLowerCase()
357      * .indexOf("automated build") == -1) { // Displayed when code version and
358      * jnlp version do not match and code // version is not a development build
359      * g.setColor(Color.red); } g.drawString( "!! Jalview version " +
360      * jalview.bin.Cache.getDefault("LATEST_VERSION", "..Checking..") +
361      * " is available for download from "
362      * +jalview.bin.Cache.getDefault("www.jalview.org"
363      * ,"http://www.jalview.org")+" !!", 50, y += fontSize + 10); y += 5;
364      * g.setColor(Color.black); }
365      * 
366      * g.setFont(new Font("Verdana", Font.BOLD, fontSize)); g.drawString(
367      * "Authors: Jim Procter, Andrew Waterhouse, Michele Clamp, James Cuff, Steve Searle,"
368      * , 50, y += fontSize + 4); g.drawString("David Martin & Geoff Barton.",
369      * 60, y += fontSize + 4); g.drawString(
370      * "Development managed by The Barton Group, University of Dundee.", 50, y
371      * += fontSize + 4); g.drawString("If  you use Jalview, please cite: ", 50,
372      * y += fontSize + 4); g.drawString(
373      * "Waterhouse, A.M., Procter, J.B., Martin, D.M.A, Clamp, M. and Barton, G. J. (2009)"
374      * , 50, y += fontSize + 4); g.drawString(
375      * "Jalview Version 2 - a multiple sequence alignment editor and analysis workbench"
376      * , 50, y += fontSize + 4);
377      * g.drawString("Bioinformatics doi: 10.1093/bioinformatics/btp033", 50, y
378      * += fontSize + 4); }
379      */
380   }
381
382   @Override
383   public void hyperlinkUpdate(HyperlinkEvent e)
384   {
385     Desktop.hyperlinkUpdate(e);
386
387   }
388 }