1. /*
  2. * @(#)DefaultPreviewPanel.java 1.5 01/11/29
  3. *
  4. * Copyright 2002 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 appropriate
  24. * for short term storage or RMI between applications running the same
  25. * version of Swing. A future release of Swing will provide support for
  26. * long term persistence.
  27. *
  28. * @version 1.5 11/29/01
  29. * @author Steve Wilson
  30. * @see JColorChooser
  31. */
  32. class DefaultPreviewPanel extends JPanel {
  33. private int squareSize = 25;
  34. private int squareGap = 5;
  35. private int innerGap = 5;
  36. private int textGap = 5;
  37. private Font font = new Font("Dialog", Font.PLAIN, 12);
  38. private String sampleText = "Sample Text Sample Text";
  39. private int swatchWidth = 50;
  40. private Color oldColor = null;
  41. public Dimension getPreferredSize() {
  42. FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(getFont());
  43. int ascent = fm.getAscent();
  44. int height = fm.getHeight();
  45. int width = fm.stringWidth(sampleText);
  46. int y = height*3 + textGap*3;
  47. int x = squareSize * 3 + squareGap*4 + swatchWidth + width;
  48. return new Dimension( x,y );
  49. }
  50. public void paintComponent(Graphics g) {
  51. if (oldColor == null)
  52. oldColor = getForeground();
  53. g.setColor(getBackground());
  54. g.fillRect(0,0,getWidth(),getHeight());
  55. int squareWidth = paintSquares(g);
  56. int textWidth = paintText(g, squareWidth);
  57. paintSwatch(g, squareWidth + textWidth);
  58. }
  59. private void paintSwatch(Graphics g, int offsetX) {
  60. int swatchX = offsetX + squareGap;
  61. g.setColor(oldColor);
  62. g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap2));
  63. g.setColor(getForeground());
  64. g.fillRect(swatchX, (squareSize) + (squareGap2), swatchWidth, (squareSize) + (squareGap2) );
  65. }
  66. private int paintText(Graphics g, int offsetX) {
  67. g.setFont(getFont());
  68. FontMetrics fm = g.getFontMetrics();
  69. int ascent = fm.getAscent();
  70. int height = fm.getHeight();
  71. int width = fm.stringWidth(sampleText);
  72. int textXOffset = offsetX + textGap;
  73. Color color = getForeground();
  74. g.setColor(color);
  75. g.drawString(sampleText, textXOffset, ascent+2);
  76. g.fillRect(textXOffset,
  77. ( height) + textGap,
  78. width + (textGap),
  79. height +2);
  80. g.setColor(Color.black);
  81. g.drawString(sampleText,
  82. textXOffset+(textGap2),
  83. height+ascent+textGap+2);
  84. g.setColor(Color.white);
  85. g.fillRect(textXOffset,
  86. ( height + textGap) * 2,
  87. width + (textGap),
  88. height +2);
  89. g.setColor(color);
  90. g.drawString(sampleText,
  91. textXOffset+(textGap2),
  92. ((height+textGap) * 2)+ascent+2);
  93. return width + textGap + 4;
  94. }
  95. private int paintSquares(Graphics g) {
  96. Color color = getForeground();
  97. g.setColor(Color.white);
  98. g.fillRect(0,0,squareSize,squareSize);
  99. g.setColor(color);
  100. g.fillRect(innerGap,
  101. innerGap,
  102. squareSize - (innerGap*2),
  103. squareSize - (innerGap*2));
  104. g.setColor(Color.white);
  105. g.fillRect(innerGap*2,
  106. innerGap*2,
  107. squareSize - (innerGap*4),
  108. squareSize - (innerGap*4));
  109. g.setColor(color);
  110. g.fillRect(0,squareSize+squareGap,squareSize,squareSize);
  111. g.translate(squareSize+squareGap, 0);
  112. g.setColor(Color.black);
  113. g.fillRect(0,0,squareSize,squareSize);
  114. g.setColor(color);
  115. g.fillRect(innerGap,
  116. innerGap,
  117. squareSize - (innerGap*2),
  118. squareSize - (innerGap*2));
  119. g.setColor(Color.white);
  120. g.fillRect(innerGap*2,
  121. innerGap*2,
  122. squareSize - (innerGap*4),
  123. squareSize - (innerGap*4));
  124. g.translate(-(squareSize+squareGap), 0);
  125. g.translate(squareSize+squareGap, squareSize+squareGap);
  126. g.setColor(Color.white);
  127. g.fillRect(0,0,squareSize,squareSize);
  128. g.setColor(color);
  129. g.fillRect(innerGap,
  130. innerGap,
  131. squareSize - (innerGap*2),
  132. squareSize - (innerGap*2));
  133. g.translate(-(squareSize+squareGap), -(squareSize+squareGap));
  134. g.translate((squareSize+squareGap)*2, 0);
  135. g.setColor(Color.white);
  136. g.fillRect(0,0,squareSize,squareSize);
  137. g.setColor(color);
  138. g.fillRect(innerGap,
  139. innerGap,
  140. squareSize - (innerGap*2),
  141. squareSize - (innerGap*2));
  142. g.setColor(Color.black);
  143. g.fillRect(innerGap*2,
  144. innerGap*2,
  145. squareSize - (innerGap*4),
  146. squareSize - (innerGap*4));
  147. g.translate(-((squareSize+squareGap)*2), 0);
  148. g.translate((squareSize+squareGap)*2, (squareSize+squareGap));
  149. g.setColor(Color.black);
  150. g.fillRect(0,0,squareSize,squareSize);
  151. g.setColor(color);
  152. g.fillRect(innerGap,
  153. innerGap,
  154. squareSize - (innerGap*2),
  155. squareSize - (innerGap*2));
  156. g.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap));
  157. return ((squareSize+squareGap) *3);
  158. }
  159. }