KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > SplashScreen


1 /*
2  * SplashScreen.java - Splash screen
3  * Copyright (C) 1998, 2004 Slava Pestov
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */

19
20 package org.gjt.sp.jedit.gui;
21
22 import javax.swing.*;
23 import java.awt.*;
24
25 import org.gjt.sp.jedit.jEdit;
26 import org.gjt.sp.util.Log;
27
28 /**
29  * The splash screen displayed on startup.
30  * @version $Id: SplashScreen.java 8122 2006-11-24 11:29:49Z kpouer $
31  */

32 public class SplashScreen extends JComponent
33 {
34     public SplashScreen()
35     {
36         setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
37         setBackground(Color.white);
38
39         Font font = new Font("Dialog",Font.PLAIN,10);
40         setFont(font);
41         fm = getFontMetrics(font);
42
43         image = getToolkit().getImage(
44             getClass().getResource("/org/gjt/sp/jedit/icons/splash.png"));
45         MediaTracker tracker = new MediaTracker(this);
46         tracker.addImage(image,0);
47
48         try
49         {
50             tracker.waitForAll();
51         }
52         catch(Exception JavaDoc e)
53         {
54             Log.log(Log.ERROR,this,e);
55         }
56
57         win = new JWindow();
58         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
59         GraphicsDevice[] gs = ge.getScreenDevices();
60         int height = gs[0].getDisplayMode().getHeight();
61         int width = gs[0].getDisplayMode().getWidth();
62         Dimension screen = new Dimension(width, height);
63         Dimension size = new Dimension(image.getWidth(this) + 2,
64             image.getHeight(this) + 2 + PROGRESS_HEIGHT);
65         win.setSize(size);
66
67         win.getContentPane().add(this, BorderLayout.CENTER);
68
69         win.setLocation((screen.width - size.width) / 2,
70             (screen.height - size.height) / 2);
71         win.validate();
72         win.setVisible(true);
73     }
74
75     public void dispose()
76     {
77         win.dispose();
78     }
79
80     public synchronized void advance()
81     {
82         logAdvanceTime(null);
83         progress++;
84         repaint();
85
86         // wait for it to be painted to ensure progress is updated
87
// continuously
88
try
89         {
90             wait();
91         }
92         catch(InterruptedException JavaDoc ie)
93         {
94             Log.log(Log.ERROR,this,ie);
95         }
96     }
97
98     public synchronized void advance(String JavaDoc label)
99     {
100         logAdvanceTime(label);
101         progress++;
102         this.label = label;
103         repaint();
104
105         // wait for it to be painted to ensure progress is updated
106
// continuously
107
try
108         {
109             wait();
110         }
111         catch(InterruptedException JavaDoc ie)
112         {
113             Log.log(Log.ERROR,this,ie);
114         }
115     }
116
117     private void logAdvanceTime(String JavaDoc label)
118     {
119         long currentTime = System.currentTimeMillis();
120         if (lastLabel != null)
121         {
122             Log.log(Log.DEBUG, SplashScreen.class, lastLabel +':'+(currentTime - lastAdvanceTime) + "ms");
123         }
124         if (label != null)
125         {
126             lastLabel = label;
127             lastAdvanceTime = currentTime;
128
129         }
130
131     }
132
133     public synchronized void paintComponent(Graphics g)
134     {
135         Dimension size = getSize();
136
137         g.setColor(Color.black);
138         g.drawRect(0,0,size.width - 1,size.height - 1);
139
140         g.drawImage(image,1,1,this);
141
142         // XXX: This should not be hardcoded
143
g.setColor(Color.white);
144         g.fillRect(1,image.getHeight(this) + 1,
145             ((win.getWidth() - 2) * progress) / PROGRESS_COUNT, PROGRESS_HEIGHT);
146
147         g.setColor(Color.black);
148
149         if (label != null)
150         {
151             g.drawString(label,
152                      (getWidth() - fm.stringWidth(label)) / 2,
153                      image.getHeight(this) + (PROGRESS_HEIGHT
154                                   + fm.getAscent() + fm.getDescent()) / 2);
155         }
156
157
158         String JavaDoc version = jEdit.getVersion();
159         g.drawString(version,
160             getWidth() - fm.stringWidth(version) - 2,
161             image.getHeight(this) - fm.getDescent());
162         notify();
163     }
164
165     // private members
166
private final FontMetrics fm;
167     private final JWindow win;
168     private final Image image;
169     private int progress;
170     private static final int PROGRESS_HEIGHT = 20;
171     private static final int PROGRESS_COUNT = 28;
172     private String JavaDoc label;
173     private String JavaDoc lastLabel;
174     private long lastAdvanceTime = System.currentTimeMillis();
175 }
176
Popular Tags