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