1. /*
  2. * @(#)DefaultPreviewPanel.java 1.11 03/01/23
  3. *
  4. * Copyright 2003 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. /**
  19. * The standard preview panel for the color chooser.
  20. * <p>
  21. * <strong>Warning:</strong>
  22. * Serialized objects of this class will not be compatible with
  23. * future Swing releases. The current serialization support is
  24. * appropriate for short term storage or RMI between applications running
  25. * the same version of Swing. As of 1.4, support for long term storage
  26. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  27. * has been added to the <code>java.beans</code> package.
  28. * Please see {@link java.beans.XMLEncoder}.
  29. *
  30. * @version 1.11 01/23/03
  31. * @author Steve Wilson
  32. * @see JColorChooser
  33. */
  34. class DefaultPreviewPanel extends JPanel {
  35. private int squareSize = 25;
  36. private int squareGap = 5;
  37. private int innerGap = 5;
  38. private int textGap = 5;
  39. private Font font = new Font("Dialog", Font.PLAIN, 12);
  40. private String sampleText = UIManager.getString("ColorChooser.sampleText");
  41. private int swatchWidth = 50;
  42. private Color oldColor = null;
  43. public Dimension getPreferredSize() {
  44. FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(getFont());
  45. int ascent = fm.getAscent();
  46. int height = fm.getHeight();
  47. int width = fm.stringWidth(sampleText);
  48. int y = height*3 + textGap*3;
  49. int x = squareSize * 3 + squareGap*2 + swatchWidth + width + textGap*3;
  50. return new Dimension( x,y );
  51. }
  52. public void paintComponent(Graphics g) {
  53. if (oldColor == null)
  54. oldColor = getForeground();
  55. g.setColor(getBackground());
  56. g.fillRect(0,0,getWidth(),getHeight());
  57. if (this.getComponentOrientation().isLeftToRight()) {
  58. int squareWidth = paintSquares(g, 0);
  59. int textWidth = paintText(g, squareWidth);
  60. paintSwatch(g, squareWidth + textWidth);
  61. } else {
  62. int swatchWidth = paintSwatch(g, 0);
  63. int textWidth = paintText(g, swatchWidth);
  64. paintSquares(g , swatchWidth + textWidth);
  65. }
  66. }
  67. private int paintSwatch(Graphics g, int offsetX) {
  68. int swatchX = offsetX;
  69. g.setColor(oldColor);
  70. g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap2));
  71. g.setColor(getForeground());
  72. g.fillRect(swatchX, (squareSize) + (squareGap2), swatchWidth, (squareSize) + (squareGap2) );
  73. return (swatchX+swatchWidth);
  74. }
  75. private int paintText(Graphics g, int offsetX) {
  76. g.setFont(getFont());
  77. FontMetrics fm = g.getFontMetrics();
  78. int ascent = fm.getAscent();
  79. int height = fm.getHeight();
  80. int width = fm.stringWidth(sampleText);
  81. int textXOffset = offsetX + textGap;
  82. Color color = getForeground();
  83. g.setColor(color);
  84. g.drawString(sampleText, textXOffset+(textGap2), ascent+2);
  85. g.fillRect(textXOffset,
  86. ( height) + textGap,
  87. width + (textGap),
  88. height +2);
  89. g.setColor(Color.black);
  90. g.drawString(sampleText,
  91. textXOffset+(textGap2),
  92. height+ascent+textGap+2);
  93. g.setColor(Color.white);
  94. g.fillRect(textXOffset,
  95. ( height + textGap) * 2,
  96. width + (textGap),
  97. height +2);
  98. g.setColor(color);
  99. g.drawString(sampleText,
  100. textXOffset+(textGap2),
  101. ((height+textGap) * 2)+ascent+2);
  102. return width + textGap*3;
  103. }
  104. private int paintSquares(Graphics g, int offsetX) {
  105. int squareXOffset = offsetX;
  106. Color color = getForeground();
  107. g.setColor(Color.white);
  108. g.fillRect(squareXOffset,0,squareSize,squareSize);
  109. g.setColor(color);
  110. g.fillRect(squareXOffset+innerGap,
  111. innerGap,
  112. squareSize - (innerGap*2),
  113. squareSize - (innerGap*2));
  114. g.setColor(Color.white);
  115. g.fillRect(squareXOffset+innerGap*2,
  116. innerGap*2,
  117. squareSize - (innerGap*4),
  118. squareSize - (innerGap*4));
  119. g.setColor(color);
  120. g.fillRect(squareXOffset,squareSize+squareGap,squareSize,squareSize);
  121. g.translate(squareSize+squareGap, 0);
  122. g.setColor(Color.black);
  123. g.fillRect(squareXOffset,0,squareSize,squareSize);
  124. g.setColor(color);
  125. g.fillRect(squareXOffset+innerGap,
  126. innerGap,
  127. squareSize - (innerGap*2),
  128. squareSize - (innerGap*2));
  129. g.setColor(Color.white);
  130. g.fillRect(squareXOffset+innerGap*2,
  131. innerGap*2,
  132. squareSize - (innerGap*4),
  133. squareSize - (innerGap*4));
  134. g.translate(-(squareSize+squareGap), 0);
  135. g.translate(squareSize+squareGap, squareSize+squareGap);
  136. g.setColor(Color.white);
  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.translate(-(squareSize+squareGap), -(squareSize+squareGap));
  144. g.translate((squareSize+squareGap)*2, 0);
  145. g.setColor(Color.white);
  146. g.fillRect(squareXOffset,0,squareSize,squareSize);
  147. g.setColor(color);
  148. g.fillRect(squareXOffset+innerGap,
  149. innerGap,
  150. squareSize - (innerGap*2),
  151. squareSize - (innerGap*2));
  152. g.setColor(Color.black);
  153. g.fillRect(squareXOffset+innerGap*2,
  154. innerGap*2,
  155. squareSize - (innerGap*4),
  156. squareSize - (innerGap*4));
  157. g.translate(-((squareSize+squareGap)*2), 0);
  158. g.translate((squareSize+squareGap)*2, (squareSize+squareGap));
  159. g.setColor(Color.black);
  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.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap));
  167. return (squareSize*3+squareGap*2);
  168. }
  169. }