Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:38:20 GMT
  2. Package jalview.gui

File SplashScreen.java

 

Coverage histogram

../../img/srcFileCovDistChart9.png
7% of files have more coverage

Code metrics

38
122
11
2
406
288
38
0.31
11.09
5.5
3.45

Classes

Class Line # Actions
SplashScreen 52 112 33
0.805194880.5%
SplashScreen.SplashImage 364 10 5
0.8823529588.2%
 

Contributing tests

This file is covered by 75 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 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.SwingUtilities;
40    import javax.swing.event.HyperlinkEvent;
41    import javax.swing.event.HyperlinkListener;
42   
43    import jalview.util.ChannelProperties;
44    import jalview.util.Platform;
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    // could change fg, bg, font later to use ChannelProperties (these are not
64    // actually being used!)
65    private static Color bg = Color.WHITE;
66   
67    private static Color fg = Color.BLACK;
68   
69    private static Font font = new Font("SansSerif", Font.PLAIN, FONT_SIZE);
70   
71    /*
72    * as JTextPane in Java, JLabel in javascript
73    */
74    private Component splashText;
75   
76    private JInternalFrame iframe;
77   
78    private Image image;
79   
80    private boolean transientDialog = false;
81   
82    private long oldTextLength = -1;
83   
84    public static int logoSize = 32;
85   
86    /*
87    * allow click in the initial splash screen to dismiss it
88    * immediately (not if opened from About menu)
89    */
90    private MouseAdapter closer = new MouseAdapter()
91    {
 
92  0 toggle @Override
93    public void mousePressed(MouseEvent evt)
94    {
95  0 if (transientDialog)
96    {
97  0 try
98    {
99  0 visible = false;
100  0 closeSplash();
101    } catch (Exception ex)
102    {
103    }
104    }
105    }
106    };
107   
108    /**
109    * Constructor that displays the splash screen
110    *
111    * @param isTransient
112    * if true the panel removes itself on click or after a few seconds;
113    * if false it stays up until closed by the user
114    */
 
115  31 toggle public SplashScreen(boolean isTransient)
116    {
117  31 this.transientDialog = isTransient;
118  31 if (this.transientDialog)
119    {
120  31 Desktop.instance.acquireDialogQueue();
121    }
122   
123  30 if (Platform.isJS()) // BH 2019
124    {
125  0 splashText = new JLabel("");
126  0 run();
127    }
128    else
129    {
130    /**
131    * Java only
132    *
133    * @j2sIgnore
134    */
135    {
136  30 splashText = new JTextPane();
137  30 splashText.setBackground(bg);
138  30 splashText.setForeground(fg);
139  30 splashText.setFont(font);
140  30 Thread t = new Thread(this);
141  30 t.start();
142    }
143    }
144    }
145   
146    /**
147    * ping the jalview version page then create and display the jalview
148    * splashscreen window.
149    */
 
150  30 toggle void initSplashScreenWindow()
151    {
152  30 addMouseListener(closer);
153   
154  30 try
155    {
156  30 if (!Platform.isJS())
157    {
158  30 image = ChannelProperties.getImage("banner");
159  30 Image logo = ChannelProperties.getImage("logo.48");
160  30 MediaTracker mt = new MediaTracker(this);
161  30 if (image != null)
162    {
163  30 mt.addImage(image, 0);
164    }
165  30 if (logo != null)
166    {
167  30 mt.addImage(logo, 1);
168    }
169  30 do
170    {
171  30 try
172    {
173  30 mt.waitForAll();
174    } catch (InterruptedException x)
175    {
176    }
177  30 if (mt.isErrorAny())
178    {
179  0 jalview.bin.Console.errPrintln("Error when loading images!");
180    }
181  30 } while (!mt.checkAll());
182  30 Desktop.instance.setIconImages(ChannelProperties.getIconList());
183    }
184    } catch (Exception ex)
185    {
186    }
187   
188  30 this.setBackground(bg);
189  30 this.setForeground(fg);
190  30 this.setFont(font);
191   
192  30 iframe = new JInternalFrame();
193  30 iframe.setFrameIcon(null);
194  30 iframe.setClosable(true);
195  30 this.setLayout(new BorderLayout());
196  30 iframe.setContentPane(this);
197  30 iframe.setLayer(JLayeredPane.PALETTE_LAYER);
198  30 iframe.setBackground(bg);
199  30 iframe.setForeground(fg);
200  30 iframe.setFont(font);
201   
202  30 if (Platform.isJS())
203    {
204    // ignore in JavaScript
205    }
206    else
207    /**
208    * Java only
209    *
210    * @j2sIgnore
211    */
212    {
213  30 ((JTextPane) splashText).setEditable(false);
214  30 splashText.setBackground(bg);
215  30 splashText.setForeground(fg);
216  30 splashText.setFont(font);
217   
218  30 SplashImage splashimg = new SplashImage(image);
219  30 iconimg.add(splashimg, BorderLayout.LINE_START);
220  30 iconimg.setBackground(bg);
221  30 add(iconimg, BorderLayout.NORTH);
222    }
223  30 add(splashText, BorderLayout.CENTER);
224  30 splashText.addMouseListener(closer);
225  30 Desktop.desktop.add(iframe);
226  30 refreshText();
227    }
228   
229    /**
230    * update text in author text panel reflecting current version information
231    */
 
232  332 toggle protected boolean refreshText()
233    {
234  332 String newtext = Desktop.instance.getAboutMessage();
235    // jalview.bin.Console.errPrintln("Text found: \n"+newtext+"\nEnd of
236    // newtext.");
237  331 if (oldTextLength != newtext.length())
238    {
239  51 iframe.setVisible(false);
240  51 oldTextLength = newtext.length();
241  51 if (Platform.isJS()) // BH 2019
242    {
243    /*
244    * SwingJS doesn't have HTMLEditorKit, required for a JTextPane
245    * to display formatted html, so we use a simple alternative
246    */
247  0 String text = "<html><br><img src=\""
248    + ChannelProperties.getImageURL("banner") + "\"/>" + newtext
249    + "<br></html>";
250  0 JLabel ta = new JLabel(text);
251  0 ta.setOpaque(true);
252  0 ta.setBackground(Color.white);
253  0 splashText = ta;
254    }
255    else
256    /**
257    * Java only
258    *
259    * @j2sIgnore
260    */
261    {
262  51 JTextPane jtp = new JTextPane();
263  51 jtp.setEditable(false);
264  51 jtp.setBackground(bg);
265  51 jtp.setForeground(fg);
266  51 jtp.setFont(font);
267  51 jtp.setContentType("text/html");
268  51 jtp.setText("<html>" + newtext + "</html>");
269  51 jtp.addHyperlinkListener(this);
270  51 splashText = jtp;
271    }
272  51 splashText.addMouseListener(closer);
273   
274  51 splashText.setVisible(true);
275  51 splashText.setSize(new Dimension(750,
276  51 425 + logoSize + (Platform.isJS() ? 40 : 0)));
277  51 splashText.setBackground(bg);
278  51 splashText.setForeground(fg);
279  51 splashText.setFont(font);
280  51 add(splashText, BorderLayout.CENTER);
281  51 revalidate();
282  51 int width = Math.max(splashText.getWidth(), iconimg.getWidth());
283  51 int height = splashText.getHeight() + iconimg.getHeight();
284  51 iframe.setBounds(
285    Math.max(0, (Desktop.instance.getWidth() - width) / 2),
286    Math.max(0, (Desktop.instance.getHeight() - height) / 2),
287    width, height);
288  51 iframe.validate();
289  51 iframe.setVisible(true);
290  51 return true;
291    }
292  280 return false;
293    }
294   
295    /**
296    * Create splash screen, display it and clear it off again.
297    */
 
298  30 toggle @Override
299    public void run()
300    {
301  30 initSplashScreenWindow();
302   
303  30 long startTime = System.currentTimeMillis() / 1000;
304   
305  362 while (visible)
306    {
307  331 iframe.repaint();
308  332 try
309    {
310  331 Thread.sleep(500);
311    } catch (Exception ex)
312    {
313    }
314   
315  332 if (transientDialog && ((System.currentTimeMillis() / 1000)
316    - startTime) > SHOW_FOR_SECS)
317    {
318  30 visible = false;
319    }
320   
321  331 if (visible && refreshText())
322    {
323  21 iframe.repaint();
324    }
325  332 if (!transientDialog)
326    {
327  0 return;
328    }
329    }
330   
331  30 closeSplash();
332    }
333   
334    /**
335    * DOCUMENT ME!
336    */
 
337  30 toggle public void closeSplash()
338    {
339  30 if (this.transientDialog)
340    {
341  30 Desktop.instance.releaseDialogQueue();
342    }
343  30 try
344    {
345  30 final JInternalFrame frme = iframe;
346  30 SwingUtilities.invokeLater(new Runnable()
347    {
 
348  25 toggle @Override
349    public void run()
350    {
351  25 try
352    {
353  25 frme.setClosed(true);
354    } catch (Exception ex)
355    {
356    }
357    }
358    });
359    } catch (Exception ex)
360    {
361    }
362    }
363   
 
364    public class SplashImage extends JPanel
365    {
366    Image image;
367   
 
368  30 toggle public SplashImage(Image todisplay)
369    {
370  30 image = todisplay;
371  30 if (image != null)
372    {
373  30 setPreferredSize(new Dimension(image.getWidth(this) + 8,
374    image.getHeight(this)));
375    }
376    }
377   
 
378  120 toggle @Override
379    public Dimension getPreferredSize()
380    {
381  120 return new Dimension(image.getWidth(this) + 8, image.getHeight(this));
382    }
383   
 
384  600 toggle @Override
385    public void paintComponent(Graphics g)
386    {
387  600 g.setColor(bg);
388  600 g.fillRect(0, 0, getWidth(), getHeight());
389  600 g.setColor(fg);
390  600 g.setFont(new Font(font.getFontName(), Font.BOLD, FONT_SIZE + 6));
391   
392  600 if (image != null)
393    {
394  600 g.drawImage(image, (getWidth() - image.getWidth(this)) / 2,
395    (getHeight() - image.getHeight(this)) / 2, this);
396    }
397    }
398    }
399   
 
400  0 toggle @Override
401    public void hyperlinkUpdate(HyperlinkEvent e)
402    {
403  0 Desktop.hyperlinkUpdate(e);
404   
405    }
406    }