/jEdit/branches/concurrency/org/gjt/sp/jedit/gui/AboutDialog.java

# · Java · 280 lines · 214 code · 31 blank · 35 comment · 13 complexity · c6fa48acdd7331520f6c489c14ee7891 MD5 · raw file

  1. /*
  2. * AboutDialog.java - About jEdit dialog box
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2000, 2001, 2002 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.awt.event.*;
  26. import java.awt.*;
  27. import java.awt.geom.*;
  28. import java.awt.image.BufferedImage;
  29. import java.util.*;
  30. import org.gjt.sp.jedit.*;
  31. import org.gjt.sp.util.Log;
  32. //}}}
  33. public class AboutDialog extends JDialog implements ActionListener
  34. {
  35. //{{{ AboutDialog constructor
  36. public AboutDialog(View view)
  37. {
  38. super(view,jEdit.getProperty("about.title"), true);
  39. setResizable(false);
  40. JButton closeBtn = new JButton(jEdit.getProperty("common.close"));
  41. closeBtn.addActionListener(this);
  42. getRootPane().setDefaultButton(closeBtn);
  43. JPanel p = new JPanel(new BorderLayout());
  44. AboutPanel aboutPanel = new AboutPanel();
  45. JPanel flowP = new JPanel(new FlowLayout());
  46. flowP.add(closeBtn);
  47. flowP.add(Box.createRigidArea(new Dimension(40, 40)));
  48. Dimension dim = new Dimension(10, 0);
  49. p.add(BorderLayout.WEST, Box.createRigidArea(dim));
  50. p.add(BorderLayout.EAST, Box.createRigidArea(dim));
  51. p.add(BorderLayout.NORTH, Box.createRigidArea(new Dimension(10, 10)));
  52. p.add(BorderLayout.SOUTH, flowP);
  53. p.add(BorderLayout.CENTER, aboutPanel);
  54. setContentPane(p);
  55. pack();
  56. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  57. setLocation((d.width-getWidth())/2, (d.height-getHeight())/2);
  58. addWindowListener(new WindowAdapter()
  59. {
  60. @Override
  61. public void windowClosing(WindowEvent e)
  62. {
  63. closeDialog();
  64. }
  65. });
  66. setVisible(true);
  67. } //}}}
  68. //{{{ actionPerformed() method
  69. public void actionPerformed(ActionEvent e)
  70. {
  71. closeDialog();
  72. } //}}}
  73. //{{{ closeDialog() method
  74. private void closeDialog()
  75. {
  76. AboutPanel.stopThread();
  77. dispose();
  78. } //}}}
  79. //{{{ AboutPanel class
  80. private static class AboutPanel extends JComponent implements Runnable
  81. {
  82. private BufferedImage bufImage;
  83. private Graphics2D g;
  84. private static final Font defaultFont = UIManager.getFont("Label.font");
  85. private final Font bottomLineFont = defaultFont.deriveFont(9.8f);
  86. private final String sBottomLine;
  87. private final ImageIcon image;
  88. private final Vector<String> vLines;
  89. private static boolean doWork;
  90. private Thread th;
  91. private final FontMetrics fm;
  92. private int iLineHeight = 0, iListHeight, iLineCount = 0,
  93. iBottomLineXOffset = 0, iBottomLineYOffset = 0,
  94. iPipeLineCount = 0, w = 0, h = 0, y = 0;
  95. private static final int
  96. SLEEP_TIME = 30,
  97. iBottomPadding = 36,
  98. iTopPadding = 120;
  99. private static Rectangle2D.Float rectangle;
  100. private static GradientPaint gradientPaint;
  101. AboutPanel()
  102. {
  103. String[] args = { jEdit.getVersion(), System.getProperty("java.version") };
  104. sBottomLine = jEdit.getProperty("about.version",args);
  105. setFont(defaultFont);
  106. fm = getFontMetrics(defaultFont);
  107. FontMetrics fmBottom = getFontMetrics(bottomLineFont);
  108. iLineHeight = fm.getHeight();
  109. vLines = new Vector<String>(50);
  110. image = (ImageIcon)GUIUtilities.loadIcon("about.png");
  111. MediaTracker tracker = new MediaTracker(this);
  112. tracker.addImage(image.getImage(), 0);
  113. try
  114. {
  115. tracker.waitForID(0);
  116. }
  117. catch(Exception exc)
  118. {
  119. tell("AboutPanel: " + exc);
  120. }
  121. Dimension d = new Dimension(image.getIconWidth(), image.getIconHeight());
  122. setSize(d);
  123. setPreferredSize(d);
  124. w = d.width;
  125. h = d.height;
  126. iBottomLineXOffset = (w / 2) - (fmBottom.stringWidth(sBottomLine) / 2);
  127. iBottomLineYOffset = h-iLineHeight/2;
  128. StringTokenizer st = new StringTokenizer(
  129. jEdit.getProperty("about.text"),"\n");
  130. while(st.hasMoreTokens())
  131. {
  132. vLines.add(st.nextToken());
  133. }
  134. iLineCount = vLines.size();
  135. iListHeight = iLineCount * iLineHeight;
  136. startThread();
  137. updateUI();
  138. }
  139. private void drain()
  140. {
  141. if (bufImage == null)
  142. {
  143. //pre-computing all data that can be known at this time
  144. Dimension d = getSize();
  145. bufImage = new BufferedImage(d.width, d.height,
  146. BufferedImage.TYPE_INT_RGB);
  147. g = bufImage.createGraphics();
  148. rectangle = new Rectangle2D.Float(0, iTopPadding,
  149. d.width, d.height-iBottomPadding-iTopPadding);
  150. //"+1" makes sure every new line from below comes up smoothly
  151. //cause it gets pre-painted and clipped as needed
  152. iPipeLineCount = 1 + (int)Math.ceil(rectangle.height/iLineHeight);
  153. y = d.height+iBottomPadding;
  154. g.setFont(defaultFont);
  155. gradientPaint = new GradientPaint(
  156. rectangle.width/2, iTopPadding+80, new Color(80, 80, 80),
  157. rectangle.width/2, iTopPadding, new Color(205, 205, 205)
  158. );
  159. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  160. }
  161. g.drawImage(image.getImage(), 0, 0, w, h, this);
  162. g.setFont(bottomLineFont);
  163. g.setPaint(new Color(55, 55, 55));
  164. g.drawString(sBottomLine, iBottomLineXOffset, iBottomLineYOffset);
  165. // Draw a highlight effect
  166. g.setPaint(new Color(255, 255, 255, 50));
  167. g.drawString(sBottomLine, iBottomLineXOffset + 1, iBottomLineYOffset + 1);
  168. g.setFont(defaultFont);
  169. g.setPaint(Color.black);
  170. g.drawRect(0, 0, w-1, h-1);
  171. g.clip(rectangle);
  172. g.setPaint(gradientPaint);
  173. int iDrawnLinesCount = 0, yCoor = 0;
  174. for (int i=0; i<iLineCount; i++)
  175. {
  176. //check whether the text line is above the canvas, if so, the code skips it
  177. yCoor = y+ i * iLineHeight;
  178. if (yCoor < iTopPadding)
  179. {
  180. continue;
  181. }
  182. //good to go, now draw only iPipeLineCount lines and get out from loop
  183. String sLine = vLines.get(i);
  184. int x = (w - fm.stringWidth(sLine))/2;
  185. g.drawString(sLine, x, yCoor);
  186. if (++iDrawnLinesCount >= iPipeLineCount)
  187. {
  188. break;
  189. }
  190. }
  191. y--;
  192. paint(getGraphics());
  193. //check if the end of the list has been reached,
  194. //if so rewind
  195. if ((y + iListHeight) < iTopPadding)
  196. {
  197. y = h+iBottomPadding;
  198. }
  199. }
  200. @Override
  201. public void update(Graphics g)
  202. {
  203. paint(g);
  204. }
  205. @Override
  206. public void paint(Graphics panelGraphics)
  207. {
  208. if (panelGraphics != null && bufImage != null)
  209. {
  210. panelGraphics.drawImage(bufImage, 0, 0, w, h, this);
  211. }
  212. }
  213. public void run()
  214. {
  215. try
  216. {
  217. while(doWork)
  218. {
  219. drain();
  220. Thread.sleep(SLEEP_TIME);
  221. }
  222. }
  223. catch(Exception exc)
  224. {
  225. Log.log(Log.ERROR, this, exc);
  226. }
  227. doWork = false;
  228. th = null;
  229. }
  230. public void startThread()
  231. {
  232. if (th == null)
  233. {
  234. th = new Thread(this);
  235. doWork = true;
  236. th.start();
  237. }
  238. }
  239. public static void stopThread()
  240. {
  241. doWork = false;
  242. }
  243. public static void tell(Object obj)
  244. {
  245. String str = obj == null ? "NULL" : obj.toString();
  246. JOptionPane.showMessageDialog(jEdit.getActiveView(), str, "Title", 1);
  247. }
  248. } //}}}
  249. }