1. /*
  2. * @(#)DefaultPreviewPanel.java 1.7 00/02/02
  3. *
  4. * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.colorchooser;
  11. import javax.swing.*;
  12. import javax.swing.border.*;
  13. import javax.swing.event.*;
  14. import javax.swing.text.*;
  15. import java.awt.*;
  16. import java.awt.image.*;
  17. import java.awt.event.*;
  18. import java.beans.PropertyChangeEvent;
  19. import java.beans.PropertyChangeListener;
  20. import java.io.Serializable;
  21. /**
  22. * The standard preview panel for the color chooser.
  23. * <p>
  24. * <strong>Warning:</strong>
  25. * Serialized objects of this class will not be compatible with
  26. * future Swing releases. The current serialization support is appropriate
  27. * for short term storage or RMI between applications running the same
  28. * version of Swing. A future release of Swing will provide support for
  29. * long term persistence.
  30. *
  31. * @version 1.7 02/02/00
  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. public Dimension getPreferredSize() {
  45. FontMetrics fm = Toolkit.getDefaultToolkit().getFontMetrics(getFont());
  46. int ascent = fm.getAscent();
  47. int height = fm.getHeight();
  48. int width = fm.stringWidth(sampleText);
  49. int y = height*3 + textGap*3;
  50. int x = squareSize * 3 + squareGap*4 + swatchWidth + width;
  51. return new Dimension( x,y );
  52. }
  53. public void paintComponent(Graphics g) {
  54. if (oldColor == null)
  55. oldColor = getForeground();
  56. g.setColor(getBackground());
  57. g.fillRect(0,0,getWidth(),getHeight());
  58. int squareWidth = paintSquares(g);
  59. int textWidth = paintText(g, squareWidth);
  60. paintSwatch(g, squareWidth + textWidth);
  61. }
  62. private void paintSwatch(Graphics g, int offsetX) {
  63. int swatchX = offsetX + squareGap;
  64. g.setColor(oldColor);
  65. g.fillRect(swatchX, 0, swatchWidth, (squareSize) + (squareGap2));
  66. g.setColor(getForeground());
  67. g.fillRect(swatchX, (squareSize) + (squareGap2), swatchWidth, (squareSize) + (squareGap2) );
  68. }
  69. private int paintText(Graphics g, int offsetX) {
  70. g.setFont(getFont());
  71. FontMetrics fm = g.getFontMetrics();
  72. int ascent = fm.getAscent();
  73. int height = fm.getHeight();
  74. int width = fm.stringWidth(sampleText);
  75. int textXOffset = offsetX + textGap;
  76. Color color = getForeground();
  77. g.setColor(color);
  78. g.drawString(sampleText, textXOffset, ascent+2);
  79. g.fillRect(textXOffset,
  80. ( height) + textGap,
  81. width + (textGap),
  82. height +2);
  83. g.setColor(Color.black);
  84. g.drawString(sampleText,
  85. textXOffset+(textGap2),
  86. height+ascent+textGap+2);
  87. g.setColor(Color.white);
  88. g.fillRect(textXOffset,
  89. ( height + textGap) * 2,
  90. width + (textGap),
  91. height +2);
  92. g.setColor(color);
  93. g.drawString(sampleText,
  94. textXOffset+(textGap2),
  95. ((height+textGap) * 2)+ascent+2);
  96. return width + textGap + 4;
  97. }
  98. private int paintSquares(Graphics g) {
  99. Color color = getForeground();
  100. g.setColor(Color.white);
  101. g.fillRect(0,0,squareSize,squareSize);
  102. g.setColor(color);
  103. g.fillRect(innerGap,
  104. innerGap,
  105. squareSize - (innerGap*2),
  106. squareSize - (innerGap*2));
  107. g.setColor(Color.white);
  108. g.fillRect(innerGap*2,
  109. innerGap*2,
  110. squareSize - (innerGap*4),
  111. squareSize - (innerGap*4));
  112. g.setColor(color);
  113. g.fillRect(0,squareSize+squareGap,squareSize,squareSize);
  114. g.translate(squareSize+squareGap, 0);
  115. g.setColor(Color.black);
  116. g.fillRect(0,0,squareSize,squareSize);
  117. g.setColor(color);
  118. g.fillRect(innerGap,
  119. innerGap,
  120. squareSize - (innerGap*2),
  121. squareSize - (innerGap*2));
  122. g.setColor(Color.white);
  123. g.fillRect(innerGap*2,
  124. innerGap*2,
  125. squareSize - (innerGap*4),
  126. squareSize - (innerGap*4));
  127. g.translate(-(squareSize+squareGap), 0);
  128. g.translate(squareSize+squareGap, squareSize+squareGap);
  129. g.setColor(Color.white);
  130. g.fillRect(0,0,squareSize,squareSize);
  131. g.setColor(color);
  132. g.fillRect(innerGap,
  133. innerGap,
  134. squareSize - (innerGap*2),
  135. squareSize - (innerGap*2));
  136. g.translate(-(squareSize+squareGap), -(squareSize+squareGap));
  137. g.translate((squareSize+squareGap)*2, 0);
  138. g.setColor(Color.white);
  139. g.fillRect(0,0,squareSize,squareSize);
  140. g.setColor(color);
  141. g.fillRect(innerGap,
  142. innerGap,
  143. squareSize - (innerGap*2),
  144. squareSize - (innerGap*2));
  145. g.setColor(Color.black);
  146. g.fillRect(innerGap*2,
  147. innerGap*2,
  148. squareSize - (innerGap*4),
  149. squareSize - (innerGap*4));
  150. g.translate(-((squareSize+squareGap)*2), 0);
  151. g.translate((squareSize+squareGap)*2, (squareSize+squareGap));
  152. g.setColor(Color.black);
  153. g.fillRect(0,0,squareSize,squareSize);
  154. g.setColor(color);
  155. g.fillRect(innerGap,
  156. innerGap,
  157. squareSize - (innerGap*2),
  158. squareSize - (innerGap*2));
  159. g.translate(-((squareSize+squareGap)*2), -(squareSize+squareGap));
  160. return ((squareSize+squareGap) *3);
  161. }
  162. }