Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package jalview.gui

File SplashScreen.java

 

Coverage histogram

../../img/srcFileCovDistChart8.png
20% of files have more coverage

Code metrics

28
95
10
2
348
237
32
0.34
9.5
5
3.2

Classes

Class Line # Actions
SplashScreen 52 85 27
0.7672413676.7%
SplashScreen.SplashImage 306 10 5
0.8823529588.2%
 

Contributing tests

This file is covered by 63 tests. .

Source view

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.Toolkit;
34    import java.awt.event.MouseAdapter;
35    import java.awt.event.MouseEvent;
36    import java.net.URL;
37   
38    import javax.swing.JInternalFrame;
39    import javax.swing.JLabel;
40    import javax.swing.JLayeredPane;
41    import javax.swing.JPanel;
42    import javax.swing.JTextPane;
43    import javax.swing.event.HyperlinkEvent;
44    import javax.swing.event.HyperlinkListener;
45   
46    /**
47    * DOCUMENT ME!
48    *
49    * @author $author$
50    * @version $Revision$
51    */
 
52    public class SplashScreen extends JPanel
53    implements Runnable, HyperlinkListener
54    {
55    private static final int SHOW_FOR_SECS = 5;
56   
57    private static final int FONT_SIZE = 11;
58   
59    private boolean visible = true;
60   
61    private JPanel iconimg = new JPanel(new BorderLayout());
62   
63    /*
64    * as JTextPane in Java, JLabel in javascript
65    */
66    private Component splashText;
67   
68    private JInternalFrame iframe;
69   
70    private Image image;
71   
72    private boolean transientDialog = false;
73   
74    private long oldTextLength = -1;
75   
76    /*
77    * allow click in the initial splash screen to dismiss it
78    * immediately (not if opened from About menu)
79    */
80    private MouseAdapter closer = new MouseAdapter()
81    {
 
82  0 toggle @Override
83    public void mousePressed(MouseEvent evt)
84    {
85  0 if (transientDialog)
86    {
87  0 try
88    {
89  0 visible = false;
90  0 closeSplash();
91    } catch (Exception ex)
92    {
93    }
94    }
95    }
96    };
97   
98    /**
99    * Constructor that displays the splash screen
100    *
101    * @param isTransient
102    * if true the panel removes itself on click or after a few seconds;
103    * if false it stays up until closed by the user
104    */
 
105  18 toggle public SplashScreen(boolean isTransient)
106    {
107  18 this.transientDialog = isTransient;
108   
109  18 if (Platform.isJS()) // BH 2019
110    {
111  0 splashText = new JLabel("");
112  0 run();
113    }
114    else
115    {
116    /**
117    * Java only
118    *
119    * @j2sIgnore
120    */
121    {
122  18 splashText = new JTextPane();
123  18 Thread t = new Thread(this);
124  18 t.start();
125    }
126    }
127    }
128   
129    /**
130    * ping the jalview version page then create and display the jalview
131    * splashscreen window.
132    */
 
133  18 toggle void initSplashScreenWindow()
134    {
135  18 addMouseListener(closer);
136   
137  18 try
138    {
139  18 URL url = getClass().getResource("/images/Jalview_Logo.png");
140  18 URL urllogo = getClass()
141    .getResource("/images/Jalview_Logo_small.png");
142   
143  18 if (!Platform.isJS() && url != null)
144    {
145  18 image = Toolkit.getDefaultToolkit().createImage(url);
146  18 Image logo = Toolkit.getDefaultToolkit().createImage(urllogo);
147  18 MediaTracker mt = new MediaTracker(this);
148  18 mt.addImage(image, 0);
149  18 mt.addImage(logo, 1);
150  18 do
151    {
152  18 try
153    {
154  18 mt.waitForAll();
155    } catch (InterruptedException x)
156    {
157    }
158  18 if (mt.isErrorAny())
159    {
160  0 System.err.println("Error when loading images!");
161    }
162  18 } while (!mt.checkAll());
163  18 Desktop.instance.setIconImage(logo);
164    }
165    } catch (Exception ex)
166    {
167    }
168   
169  18 iframe = new JInternalFrame();
170  18 iframe.setFrameIcon(null);
171  18 iframe.setClosable(true);
172  18 this.setLayout(new BorderLayout());
173  18 iframe.setContentPane(this);
174  18 iframe.setLayer(JLayeredPane.PALETTE_LAYER);
175  18 if (Platform.isJS())
176    {
177    // ignore in JavaScript
178    }
179    else
180    /**
181    * Java only
182    *
183    * @j2sIgnore
184    */
185    {
186  18 ((JTextPane) splashText).setEditable(false);
187   
188  18 SplashImage splashimg = new SplashImage(image);
189  18 iconimg.add(splashimg, BorderLayout.CENTER);
190  18 add(iconimg, BorderLayout.NORTH);
191    }
192  18 add(splashText, BorderLayout.CENTER);
193  18 splashText.addMouseListener(closer);
194  18 Desktop.desktop.add(iframe);
195  18 refreshText();
196    }
197   
198    /**
199    * update text in author text panel reflecting current version information
200    */
 
201  188 toggle protected boolean refreshText()
202    {
203  188 String newtext = Desktop.instance.getAboutMessage();
204    // System.err.println("Text found: \n"+newtext+"\nEnd of newtext.");
205  188 if (oldTextLength != newtext.length())
206    {
207  18 iframe.setVisible(false);
208  18 oldTextLength = newtext.length();
209  18 if (Platform.isJS()) // BH 2019
210    {
211    /*
212    * SwingJS doesn't have HTMLEditorKit, required for a JTextPane
213    * to display formatted html, so we use a simple alternative
214    */
215  0 String text = "<html><br><br><img src=\"swingjs/j2s/images/Jalview_Logo.png\"/><br>"
216    + newtext + "</html>";
217  0 JLabel ta = new JLabel(text);
218  0 ta.setOpaque(true);
219  0 ta.setBackground(Color.white);
220  0 splashText = ta;
221    }
222    else
223    /**
224    * Java only
225    *
226    * @j2sIgnore
227    */
228    {
229  18 JTextPane jtp = new JTextPane();
230  18 jtp.setEditable(false);
231  18 jtp.setContentType("text/html");
232  18 jtp.setText("<html>" + newtext + "</html>");
233  18 jtp.addHyperlinkListener(this);
234  18 splashText = jtp;
235    }
236  18 splashText.addMouseListener(closer);
237   
238  18 splashText.setVisible(true);
239  18 splashText.setSize(new Dimension(750, 375));
240  18 add(splashText, BorderLayout.CENTER);
241  18 revalidate();
242  18 iframe.setBounds((Desktop.instance.getWidth() - 750) / 2,
243    (Desktop.instance.getHeight() - 375) / 2, 750,
244    splashText.getHeight() + iconimg.getHeight());
245  18 iframe.validate();
246  18 iframe.setVisible(true);
247  18 return true;
248    }
249  170 return false;
250    }
251   
252    /**
253    * Create splash screen, display it and clear it off again.
254    */
 
255  18 toggle @Override
256    public void run()
257    {
258  18 initSplashScreenWindow();
259   
260  18 long startTime = System.currentTimeMillis() / 1000;
261   
262  204 while (visible)
263    {
264  188 iframe.repaint();
265  188 try
266    {
267  188 Thread.sleep(500);
268    } catch (Exception ex)
269    {
270    }
271   
272  186 if (transientDialog
273    && ((System.currentTimeMillis() / 1000) - startTime) > SHOW_FOR_SECS)
274    {
275  16 visible = false;
276    }
277   
278  186 if (visible && refreshText())
279    {
280  0 iframe.repaint();
281    }
282  186 if (!transientDialog)
283    {
284  0 return;
285    }
286    }
287   
288  16 closeSplash();
289  16 Desktop.instance.startDialogQueue();
290    }
291   
292    /**
293    * DOCUMENT ME!
294    */
 
295  16 toggle public void closeSplash()
296    {
297  16 try
298    {
299   
300  16 iframe.setClosed(true);
301    } catch (Exception ex)
302    {
303    }
304    }
305   
 
306    public class SplashImage extends JPanel
307    {
308    Image image;
309   
 
310  18 toggle public SplashImage(Image todisplay)
311    {
312  18 image = todisplay;
313  18 if (image != null)
314    {
315  18 setPreferredSize(new Dimension(image.getWidth(this) + 8,
316    image.getHeight(this)));
317    }
318    }
319   
 
320  37 toggle @Override
321    public Dimension getPreferredSize()
322    {
323  37 return new Dimension(image.getWidth(this) + 8, image.getHeight(this));
324    }
325   
 
326  305 toggle @Override
327    public void paintComponent(Graphics g)
328    {
329  305 g.setColor(Color.white);
330  305 g.fillRect(0, 0, getWidth(), getHeight());
331  305 g.setColor(Color.black);
332  305 g.setFont(new Font("Verdana", Font.BOLD, FONT_SIZE + 6));
333   
334  305 if (image != null)
335    {
336  305 g.drawImage(image, (getWidth() - image.getWidth(this)) / 2,
337    (getHeight() - image.getHeight(this)) / 2, this);
338    }
339    }
340    }
341   
 
342  0 toggle @Override
343    public void hyperlinkUpdate(HyperlinkEvent e)
344    {
345  0 Desktop.hyperlinkUpdate(e);
346   
347    }
348    }