001: /*
002: * SplashScreen.java - Splash screen
003: * Copyright (C) 1998, 2004 Slava Pestov
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package org.gjt.sp.jedit.gui;
021:
022: import javax.swing.*;
023: import java.awt.*;
024:
025: import org.gjt.sp.jedit.jEdit;
026: import org.gjt.sp.util.Log;
027:
028: /**
029: * The splash screen displayed on startup.
030: * @version $Id: SplashScreen.java 8122 2006-11-24 11:29:49Z kpouer $
031: */
032: public class SplashScreen extends JComponent {
033: public SplashScreen() {
034: setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
035: setBackground(Color.white);
036:
037: Font font = new Font("Dialog", Font.PLAIN, 10);
038: setFont(font);
039: fm = getFontMetrics(font);
040:
041: image = getToolkit().getImage(
042: getClass().getResource(
043: "/org/gjt/sp/jedit/icons/splash.png"));
044: MediaTracker tracker = new MediaTracker(this );
045: tracker.addImage(image, 0);
046:
047: try {
048: tracker.waitForAll();
049: } catch (Exception e) {
050: Log.log(Log.ERROR, this , e);
051: }
052:
053: win = new JWindow();
054: GraphicsEnvironment ge = GraphicsEnvironment
055: .getLocalGraphicsEnvironment();
056: GraphicsDevice[] gs = ge.getScreenDevices();
057: int height = gs[0].getDisplayMode().getHeight();
058: int width = gs[0].getDisplayMode().getWidth();
059: Dimension screen = new Dimension(width, height);
060: Dimension size = new Dimension(image.getWidth(this ) + 2, image
061: .getHeight(this )
062: + 2 + PROGRESS_HEIGHT);
063: win.setSize(size);
064:
065: win.getContentPane().add(this , BorderLayout.CENTER);
066:
067: win.setLocation((screen.width - size.width) / 2,
068: (screen.height - size.height) / 2);
069: win.validate();
070: win.setVisible(true);
071: }
072:
073: public void dispose() {
074: win.dispose();
075: }
076:
077: public synchronized void advance() {
078: logAdvanceTime(null);
079: progress++;
080: repaint();
081:
082: // wait for it to be painted to ensure progress is updated
083: // continuously
084: try {
085: wait();
086: } catch (InterruptedException ie) {
087: Log.log(Log.ERROR, this , ie);
088: }
089: }
090:
091: public synchronized void advance(String label) {
092: logAdvanceTime(label);
093: progress++;
094: this .label = label;
095: repaint();
096:
097: // wait for it to be painted to ensure progress is updated
098: // continuously
099: try {
100: wait();
101: } catch (InterruptedException ie) {
102: Log.log(Log.ERROR, this , ie);
103: }
104: }
105:
106: private void logAdvanceTime(String label) {
107: long currentTime = System.currentTimeMillis();
108: if (lastLabel != null) {
109: Log.log(Log.DEBUG, SplashScreen.class, lastLabel + ':'
110: + (currentTime - lastAdvanceTime) + "ms");
111: }
112: if (label != null) {
113: lastLabel = label;
114: lastAdvanceTime = currentTime;
115:
116: }
117:
118: }
119:
120: public synchronized void paintComponent(Graphics g) {
121: Dimension size = getSize();
122:
123: g.setColor(Color.black);
124: g.drawRect(0, 0, size.width - 1, size.height - 1);
125:
126: g.drawImage(image, 1, 1, this );
127:
128: // XXX: This should not be hardcoded
129: g.setColor(Color.white);
130: g.fillRect(1, image.getHeight(this ) + 1,
131: ((win.getWidth() - 2) * progress) / PROGRESS_COUNT,
132: PROGRESS_HEIGHT);
133:
134: g.setColor(Color.black);
135:
136: if (label != null) {
137: g.drawString(label,
138: (getWidth() - fm.stringWidth(label)) / 2, image
139: .getHeight(this )
140: + (PROGRESS_HEIGHT + fm.getAscent() + fm
141: .getDescent()) / 2);
142: }
143:
144: String version = jEdit.getVersion();
145: g.drawString(version, getWidth() - fm.stringWidth(version) - 2,
146: image.getHeight(this ) - fm.getDescent());
147: notify();
148: }
149:
150: // private members
151: private final FontMetrics fm;
152: private final JWindow win;
153: private final Image image;
154: private int progress;
155: private static final int PROGRESS_HEIGHT = 20;
156: private static final int PROGRESS_COUNT = 28;
157: private String label;
158: private String lastLabel;
159: private long lastAdvanceTime = System.currentTimeMillis();
160: }
|