1. package org.jr.swing.text;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年2月12日
  6. * @author Cherami
  7. */
  8. import javax.swing.text.*;
  9. /**
  10. * 自动将小写转换为大写的Swing文档模型。
  11. * <p><b>注意:</b>本方法和0.12版相比功能发生了变化。
  12. * 现在可以控制暂时取消将小写转换为大写的功能。
  13. * @since 0.1
  14. */
  15. public class UppercaseDocument
  16. extends PlainDocument {
  17. /**
  18. * 是否将小写转换为大写。
  19. */
  20. protected boolean toUppercase = true;
  21. /**
  22. * 构造一个将小写转换为大写的UppercaseDocument。
  23. * @since 0.1
  24. */
  25. public UppercaseDocument() {
  26. super();
  27. }
  28. /**
  29. * 根据指定的转换状态构造一个UppercaseDocument。
  30. * @param toUppercase 是否将小写转换为大写,true的时候进行转换,否则不转换。
  31. * @since 0.3
  32. */
  33. public UppercaseDocument(boolean toUppercase) {
  34. this.toUppercase = toUppercase;
  35. }
  36. /**
  37. * 设置是否将小写转换为大写。
  38. * @param toUppercase 是否将小写转换为大写,true的时候进行转换,否则不转换。
  39. * @since 0.3
  40. */
  41. public void setToUppercase(boolean toUppercase) {
  42. this.toUppercase = toUppercase;
  43. }
  44. /**
  45. * 返回是否将小写转换为大写。
  46. * @return 将小写转换为大写时返回true,否则返回false。
  47. * @since 0.3
  48. */
  49. public boolean isToUppercase() {
  50. return toUppercase;
  51. }
  52. /**
  53. * 插入某些内容到文档中。
  54. * 自动将小写输入转换为大写输入。
  55. * 此方法是线程安全的。
  56. * @param offs 插入位置的偏移量
  57. * @param str 插入内容
  58. * @param a 属性集合
  59. * @throws BadLocationException 给出的插入位置不是文档中的有效位置
  60. * @since 0.1
  61. */
  62. public void insertString(int offs, String str, AttributeSet a) throws
  63. BadLocationException {
  64. if (str == null) {
  65. return;
  66. }
  67. if (toUppercase) {
  68. str = str.toUpperCase();
  69. }
  70. super.insertString(offs, str, a);
  71. }
  72. }