1. package org.jr.swing;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年2月12日
  6. * @author Cherami
  7. */
  8. import java.awt.*;
  9. import java.awt.event.*;
  10. import javax.swing.*;
  11. /**
  12. * 文本色彩选择器。
  13. * @since 0.1
  14. */
  15. public class TextColorChooser
  16. extends JColorChooser {
  17. /**
  18. * 根据初始颜色构造一个TextColorChooser。
  19. * @param initialColor 初始的文本颜色
  20. * @param reference 初始的背景颜色
  21. * @param isForgroundSelection 是否选中前景色
  22. * @since 0.1
  23. */
  24. public TextColorChooser(Color initialColor, Color reference,
  25. boolean isForgroundSelection) {
  26. super(initialColor);
  27. if (isForgroundSelection) {
  28. setPreviewPanel(new TextPreviewLabel(initialColor, reference,
  29. isForgroundSelection));
  30. }
  31. else {
  32. setPreviewPanel(new TextPreviewLabel(reference, initialColor,
  33. isForgroundSelection));
  34. }
  35. updateUI();
  36. }
  37. /**
  38. * 显示文本色彩选择对话框。
  39. * @param component 主框架的一个子组件
  40. * @param title 对话框标题
  41. * @return 用户选择的颜色。
  42. * @since 0.1
  43. */
  44. public Color showDialog(Component component, String title) {
  45. ColorChooserDialog colorDialog = new ColorChooserDialog(component, title, this);
  46. colorDialog.show();
  47. Color color = colorDialog.getColor();
  48. colorDialog.dispose();
  49. return color;
  50. }
  51. /**
  52. * 预览文本域。
  53. * @since 0.1
  54. */
  55. class TextPreviewLabel
  56. extends JLabel {
  57. private String sampleText = " Sample Text Sample Text ";
  58. boolean isForgroundSelection;
  59. /**
  60. * 构造一个缺省的TextPreviewLabel。
  61. * @since 0.1
  62. */
  63. public TextPreviewLabel() {
  64. this(Color.black, Color.white, true);
  65. }
  66. /**
  67. * 以指定颜色构造一个TextPreviewLabel。
  68. * @param fore 前景色
  69. * @param back 背景色
  70. * @param isForgroundSelection 是否使用前景色
  71. * @since 0.1
  72. */
  73. public TextPreviewLabel(Color fore, Color back,
  74. boolean isForgroundSelection) {
  75. setOpaque(true);
  76. this.isForgroundSelection = isForgroundSelection;
  77. setForeground(fore);
  78. setBackground(back);
  79. setText(sampleText);
  80. }
  81. /**
  82. * 设置前景色。
  83. * @param color 前景色
  84. * @since 0.1
  85. */
  86. public void setForeground(Color color) {
  87. if (isForgroundSelection) {
  88. super.setForeground(color);
  89. }
  90. else {
  91. super.setBackground(color);
  92. }
  93. }
  94. }
  95. /**
  96. * 颜色选择对话框。
  97. * @since 0.1
  98. */
  99. class ColorChooserDialog
  100. extends JDialog {
  101. private Color initialColor;
  102. private Color returnColor;
  103. private JColorChooser chooserPane;
  104. /**
  105. * 构造一个ColorChooserDialog。
  106. * @param c 对话框的父容器
  107. * @param title 对话框的标题
  108. * @param chooserPane 对话框的颜色选择器
  109. * @since 0.1
  110. */
  111. public ColorChooserDialog(Component c, String title,
  112. final JColorChooser chooserPane) {
  113. super(JOptionPane.getFrameForComponent(c), title, true);
  114. setResizable(false);
  115. this.chooserPane = chooserPane;
  116. String okString = UIManager.getString("ColorChooser.okText");
  117. String cancelString = UIManager.getString("ColorChooser.cancelText");
  118. String resetString = UIManager.getString("ColorChooser.resetText");
  119. Container contentPane = getContentPane();
  120. contentPane.setLayout(new BorderLayout());
  121. contentPane.add(chooserPane, BorderLayout.CENTER);
  122. JPanel buttonPane = new JPanel();
  123. buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
  124. JButton okButton = new JButton(okString);
  125. getRootPane().setDefaultButton(okButton);
  126. okButton.addActionListener(new ActionListener() {
  127. public void actionPerformed(ActionEvent e) {
  128. returnColor = chooserPane.getColor();
  129. setVisible(false);
  130. }
  131. });
  132. buttonPane.add(okButton);
  133. JButton cancelButton = new JButton(cancelString);
  134. cancelButton.addActionListener(new ActionListener() {
  135. public void actionPerformed(ActionEvent e) {
  136. returnColor = null;
  137. setVisible(false);
  138. }
  139. });
  140. buttonPane.add(cancelButton);
  141. JButton resetButton = new JButton(resetString);
  142. resetButton.addActionListener(new ActionListener() {
  143. public void actionPerformed(ActionEvent e) {
  144. chooserPane.setColor(initialColor);
  145. }
  146. });
  147. buttonPane.add(resetButton);
  148. contentPane.add(buttonPane, BorderLayout.SOUTH);
  149. pack();
  150. setLocationRelativeTo(c);
  151. addWindowListener(new WindowAdapter() {
  152. public void windowClosing(WindowEvent e) {
  153. setVisible(false);
  154. }
  155. });
  156. }
  157. /**
  158. * 得到用户选择的颜色。
  159. * @return 用户选择的颜色
  160. * @since 0.1
  161. */
  162. public Color getColor() {
  163. return returnColor;
  164. }
  165. }
  166. }