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