1. /*
  2. * @(#)DefaultPreviewPanel.java 1.13 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.colorchooser;
  8. import javax.swing.*;
  9. import javax.swing.border.*;
  10. import javax.swing.event.*;
  11. import javax.swing.text.*;
  12. import java.awt.*;
  13. import java.awt.image.*;
  14. import java.awt.event.*;
  15. import java.beans.PropertyChangeEvent;
  16. import java.beans.PropertyChangeListener;
  17. import java.io.Serializable;
  18. import com.sun.java.swing.SwingUtilities2;
  19. /**
  20. * The standard preview panel for the color chooser.
  21. * <p>
  22. * <strong>Warning:</strong>
  23. * Serialized objects of this class will not be compatible with
  24. * future Swing releases. The current serialization support is
  25. * appropriate for short term storage or RMI between applications running
  26. * the same version of Swing. As of 1.4, support for long term storage
  27. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  28. * has been added to the <code>java.beans</code> package.
  29. * Please see {@link java.beans.XMLEncoder}.
  30. *
  31. * @version 1.13 12/19/03
  32. * @author Steve Wilson
  33. * @see JColorChooser
  34. */
  35. class DefaultPreviewPanel extends JPanel {
  36. private int squareSize = 25;
  37. private int squareGap = 5;
  38. private int innerGap = 5;
  39. private int textGap = 5;
  40. private Font font = new Font("Dialog", Font.PLAIN, 12);
  41. private String sampleText = UIManager.getString("ColorChooser.sampleText");
  42. private int swatchWidth = 50;
  43. private Color oldColor = null;
  44. private JColorChooser getColorChooser() {
  45. return (JColorChooser)SwingUtilities.getAncestorOfClass(
  46. JColorChooser.class, this);
  47. }
  48. public Dimension getPreferredSize() {
  49. JComponent host = getColorChooser();
  50. if (host == null) {
  51. host = this;
  52. }
  53. FontMetrics fm = host.getFontMetrics(getFont());
  54. int ascent = fm.getAscent();
  55. int height = fm.getHeight();
  56. int width = SwingUtilities2.stringWidth(host, fm, sampleText);
  57. int y = height*3 + textGap*3;
  58. int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3;
  59. return new Dimension( x,y );
  60. }
  61. public void paintComponent(Graphics g) {
  62. if (oldColor == null)
  63. oldColor = getForeground();
  64. g.setColor(getBackground());
  65. g.fillRect(0,0,getWidth(),getHeight());
  66. if (this.getComponentOrientation().isLeftToRight()) {
  67. int squareWidth = paintSquares(g, 0);
  68. int textWidth = paintText(g, squareWidth);
  69. paintSwatch(g, squareWidth + textWidth);
  70. } else {
  71. int swatchWidth = paintSwatch(g, 0);
  72. int textWidth = paintText(g, swatchWidth);
  73. paintSquares(g , swatchWidth + textWidth);
  74. }
  75. }
  76. private int paintSwatch(Graphics g, int offsetX) {
  77. int swatchX = offsetX;
  78. g.setColor(oldColor);
  79. g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap2));
  80. g.setColor(getForeground());
  81. g.fillRect(swatchX, (squareSize) + (squareGap2), swatchWidth, (squareSize) + (squareGap2) );
  82. return (swatchX+swatchWidth);
  83. }
  84. private int paintText(Graphics g, int offsetX) {
  85. g.setFont(getFont());
  86. JComponent host = getColorChooser();
  87. if (host == null) {
  88. host = this;
  89. }
  90. FontMetrics fm = SwingUtilities2.getFontMetrics(host, g);
  91. int ascent = fm.getAscent();
  92. int height = fm.getHeight();
  93. int width = SwingUtilities2.stringWidth(host, fm, sampleText);
  94. int textXOffset = offsetX + textGap;
  95. Color color = getForeground();
  96. g.setColor(color);
  97. SwingUtilities2.drawString(host, g, sampleText,textXOffset+(textGap2),
  98. ascent+2);
  99. g.fillRect(textXOffset,
  100. ( height) + textGap,
  101. width + (textGap),
  102. height +2);
  103. g.setColor(Color.black);
  104. SwingUtilities2.drawString(host, g, sampleText,
  105. textXOffset+(textGap2),
  106. height+ascent+textGap+2);
  107. g.setColor(Color.white);
  108. g.fillRect(textXOffset,
  109. ( height + textGap) * 2,
  110. width + (textGap),
  111. height +2);
  112. g.setColor(color);
  113. SwingUtilities2.drawString(host, g, sampleText,
  114. textXOffset+(textGap2),
  115. ((height+textGap) * 2)+ascent+2);
  116. return width + textGap*3;
  117. }
  118. private int paintSquares(Graphics g, int offsetX) {
  119. int squareXOffset = offsetX;
  120. Color color = getForeground();
  121. g.setColor(Color.white);
  122. g.fillRect(squareXOffset,0,squareSize,squareSize);
  123. g.setColor(color);
  124. g.fillRect(squareXOffset+innerGap,
  125. innerGap,
  126. squareSize - (innerGap*2),
  127. squareSize - (innerGap*2));
  128. g.setColor(Color.white);
  129. g.fillRect(squareXOffset+innerGap*2,
  130. innerGap*2,
  131. squareSize - (innerGap*4),
  132. squareSize - (innerGap*4));
  133. g.setColor(color);
  134. g.fillRect(squareXOffset,squareSize+squareGap,squareSize,squareSize);
  135. g.translate(squareSize+squareGap, 0);
  136. g.setColor(Color.black);
  137. g.fillRect(squareXOffset,0,squareSize,squareSize);
  138. g.setColor(color);
  139. g.fillRect(squareXOffset+innerGap,
  140. innerGap,
  141. squareSize - (innerGap*2),
  142. squareSize - (innerGap*2));
  143. g.setColor(Color.white);
  144. g.fillRect(squareXOffset+innerGap*2,
  145. innerGap*2,
  146. squareSize - (innerGap*4),
  147. squareSize - (innerGap*4));
  148. g.translate(-(squareSize+squareGap), 0);
  149. g.translate(squareSize+squareGap, squareSize+squareGap);
  150. g.setColor(Color.white);
  151. g.fillRect(squareXOffset,0,squareSize,squareSize);
  152. g.setColor(color);
  153. g.fillRect(squareXOffset+innerGap,
  154. innerGap,
  155. squareSize - (innerGap*2),
  156. squareSize - (innerGap*2));
  157. g.translate(-(squareSize+squareGap), -(squareSize+squareGap));
  158. g.translate((squareSize+squareGap)*2, 0);
  159. g.setColor(Color.white);
  160. g.fillRect(squareXOffset,0,squareSize,squareSize);
  161. g.setColor(color);
  162. g.fillRect(squareXOffset+innerGap,
  163. innerGap,
  164. squareSize - (innerGap*2),
  165. squareSize - (innerGap*2));
  166. g.setColor(Color.black);
  167. g.fillRect(squareXOffset+innerGap*2,
  168. innerGap*2,
  169. squareSize - (innerGap*4),
  170. squareSize - (innerGap*4));
  171. g.translate(-((squareSize+squareGap)*2), 0);
  172. g.translate((squareSize+squareGap)*2, (squareSize+squareGap));
  173. g.setColor(Color.black);
  174. g.fillRect(squareXOffset,0,squareSize,squareSize);
  175. g.setColor(color);
  176. g.fillRect(squareXOffset+innerGap,
  177. innerGap,
  178. squareSize - (innerGap*2),
  179. squareSize - (innerGap*2));
  180. g.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap));
  181. return (squareSize*3+squareGap*2);
  182. }
  183. }