PageRenderTime 71ms CodeModel.GetById 58ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/SplashScreen.java

#
Java | 209 lines | 142 code | 29 blank | 38 comment | 10 complexity | 4a54539ba6d8fa22644487fde3c36abd MD5 | raw file
  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
 20package org.gjt.sp.jedit.gui;
 21
 22//{{{ Imports
 23import javax.swing.*;
 24import java.awt.*;
 25
 26import org.gjt.sp.jedit.jEdit;
 27import org.gjt.sp.util.Log;
 28//}}}
 29
 30/**
 31 * The splash screen displayed on startup.
 32 * @version $Id: SplashScreen.java 13029 2008-07-08 08:43:48Z kpouer $
 33 */
 34public class SplashScreen extends JComponent
 35{
 36	//{{{ SplashScreen constructor
 37	public SplashScreen()
 38	{
 39		setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
 40		setBackground(Color.white);
 41
 42		setFont(defaultFont);
 43		fm = getFontMetrics(defaultFont);
 44		image = getToolkit().getImage(
 45			getClass().getResource("/org/gjt/sp/jedit/icons/splash.png"));
 46		MediaTracker tracker = new MediaTracker(this);
 47		tracker.addImage(image,0);
 48
 49		try
 50		{
 51			tracker.waitForAll();
 52		}
 53		catch(Exception e)
 54		{
 55			Log.log(Log.ERROR,this,e);
 56		}
 57		Dimension screen = getToolkit().getScreenSize(); // sane default
 58		win = new JWindow();
 59		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 60		GraphicsDevice[] gs = ge.getScreenDevices();
 61		GraphicsDevice gd = gs[0];
 62		if (gd != null)
 63		{
 64			GraphicsConfiguration gconf = gd.getDefaultConfiguration();
 65			if (gconf != null)
 66			{
 67				Rectangle bounds = gconf.getBounds();
 68				screen = new Dimension(bounds.width, bounds.height);
 69			}
 70		}
 71		Dimension size = new Dimension(image.getWidth(this) + 2,
 72			image.getHeight(this) + 2 + PROGRESS_HEIGHT);
 73		win.setSize(size);
 74
 75		win.getContentPane().add(this, BorderLayout.CENTER);
 76
 77		win.setLocation((screen.width - size.width) / 2,
 78			(screen.height - size.height) / 2);
 79		win.validate();
 80		win.setVisible(true);
 81	} //}}}
 82
 83	//{{{ dispose() method
 84	public void dispose()
 85	{
 86		win.dispose();
 87	} //}}}
 88
 89	//{{{ advance() methods
 90	public synchronized void advance()
 91	{
 92		logAdvanceTime(null);
 93		progress++;
 94		repaint();
 95
 96		// wait for it to be painted to ensure progress is updated
 97		// continuously
 98		try
 99		{
100			wait();
101		}
102		catch(InterruptedException ie)
103		{
104			Log.log(Log.ERROR,this,ie);
105		}
106	}
107
108	public synchronized void advance(String label)
109	{
110		logAdvanceTime(label);
111		progress++;
112		this.label = label;
113		repaint();
114
115		// wait for it to be painted to ensure progress is updated
116		// continuously
117		try
118		{
119			wait();
120		}
121		catch(InterruptedException ie)
122		{
123			Log.log(Log.ERROR,this,ie);
124		}
125	} //}}}
126
127	//{{{ logAdvanceTime() method
128	private void logAdvanceTime(String label)
129	{
130		long currentTime = System.currentTimeMillis();
131		if (lastLabel != null)
132		{
133			Log.log(Log.DEBUG, SplashScreen.class,
134				lastLabel +':'+(currentTime - lastAdvanceTime) + "ms");
135		}
136		if (label != null)
137		{
138			lastLabel = label;
139			lastAdvanceTime = currentTime;
140
141		}
142	} //}}}
143
144	//{{{ paintComponent() method
145	@Override
146	public synchronized void paintComponent(Graphics g)
147	{
148		Dimension size = getSize();
149
150		g.setColor(Color.black);
151		g.drawRect(0,0,size.width - 1,size.height - 1);
152
153		g.drawImage(image,1,1,this);
154
155		// XXX: This should not be hardcoded
156		g.setColor(Color.white);
157		g.fillRect(1,image.getHeight(this) + 1,
158			((win.getWidth() - 2) * progress) / PROGRESS_COUNT, PROGRESS_HEIGHT);
159
160		g.setColor(Color.black);
161
162		if (label != null)
163		{
164			int drawOffsetX = (getWidth() - fm.stringWidth(label)) / 2;
165			int drawOffsetY = image.getHeight(this) + (PROGRESS_HEIGHT
166							      + fm.getAscent() + fm.getDescent()) / 2;
167
168			paintString(g, label, drawOffsetX, drawOffsetY);
169		}
170
171		String version = "version " + jEdit.getVersion();
172
173		int drawOffsetX = (getWidth() / 2) - (fm.stringWidth(version) / 2);
174		int drawOffsetY = image.getHeight(this) - fm.getDescent() - 2;
175
176		paintString(g, version, drawOffsetX, drawOffsetY);
177
178		notify();
179	} //}}}
180
181	//{{{ paintString() method
182	private void paintString(Graphics g, String version, int drawOffsetX,
183				 int drawOffsetY)
184	{
185		g.setFont( labelFont );
186
187		g.setColor( versionColor1 );
188		g.drawString( version, drawOffsetX, drawOffsetY );
189		// Draw a highlight effect
190		g.setColor( versionColor2 );
191		g.drawString( version, drawOffsetX + 1, drawOffsetY + 1 );
192	} //}}}
193
194	//{{{ private members
195	private final FontMetrics fm;
196	private final JWindow win;
197	private final Image image;
198	private int progress;
199	private static final int PROGRESS_HEIGHT = 20;
200	private static final int PROGRESS_COUNT = 28;
201	private String label;
202	private String lastLabel;
203	private long lastAdvanceTime = System.currentTimeMillis();
204	private Font defaultFont = new Font("Dialog",Font.PLAIN,10);
205	private Font labelFont = UIManager.getFont("Label.font").deriveFont(9.8f);
206	private Color versionColor1 = new Color(55, 55, 55);
207	private Color versionColor2 = new Color(255, 255, 255, 50);
208	//}}}
209}