1. package org.jr.swing.table;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年3月14日
  6. * @author Cherami
  7. */
  8. import org.jr.text.*;
  9. /**
  10. * 可以自动的以可读的方式显示大小的表格单元渲染器。
  11. * 注意,这个渲染器只能对Number类型的值有效并且是显示其long值的。
  12. * @since 0.5
  13. */
  14. public class ReadableSizeCellRenderer
  15. extends FormatCellRenderer {
  16. boolean autoReadable = true;
  17. int unit = -1;
  18. /**
  19. * 构造一个自动显示可读大小并且显示单位的ReadableSizeCellRenderer。
  20. * @since 0.5
  21. */
  22. public ReadableSizeCellRenderer() {
  23. formatter = new FileSizeFormat(true);
  24. }
  25. /**
  26. * 根据指定的参数构造一个ReadableSizeCellRenderer。
  27. * 显示尺寸单位。
  28. * @param autoReadable 是否自动设置大小的单位
  29. * @param unit 单位,此单位只有在autoReadable设置为false时才有效
  30. * @since 0.5
  31. */
  32. public ReadableSizeCellRenderer(boolean autoReadable, int unit) {
  33. this.autoReadable = autoReadable;
  34. this.unit = unit;
  35. formatter = new FileSizeFormat(true);
  36. }
  37. /**
  38. * 根据指定的参数构造一个ReadableSizeCellRenderer。
  39. * @param autoReadable 是否自动设置大小的单位
  40. * @param showUnit 是否显示单位
  41. * @since 0.5
  42. */
  43. public ReadableSizeCellRenderer(boolean autoReadable, boolean showUnit) {
  44. this.autoReadable = autoReadable;
  45. formatter = new FileSizeFormat(showUnit);
  46. }
  47. /**
  48. * 根据指定的参数构造一个ReadableSizeCellRenderer。
  49. * @param autoReadable 是否自动设置大小的单位
  50. * @param showUnit 是否显示单位
  51. * @param unit 单位,此单位只有在autoReadable设置为false时才有效
  52. * @since 0.5
  53. */
  54. public ReadableSizeCellRenderer(boolean autoReadable, boolean showUnit,
  55. int unit) {
  56. this.autoReadable = autoReadable;
  57. formatter = new FileSizeFormat(showUnit);
  58. this.unit = unit;
  59. }
  60. /**
  61. * 将表格单元的值格式化。
  62. * @param value 原始值
  63. * @return 格式化以后的值
  64. * @since 0.5
  65. */
  66. protected String format(Object value) {
  67. String resultValue;
  68. if ( ( (Number) value).longValue() > 0) {
  69. if (autoReadable == true) {
  70. resultValue = formatter.format( (Number) value);
  71. }
  72. else {
  73. resultValue = ( (FileSizeFormat) formatter).format( (Number) value,
  74. unit);
  75. }
  76. }
  77. else {
  78. resultValue = "";
  79. }
  80. return resultValue;
  81. }
  82. /**
  83. * 设置是否自动判断大小。
  84. * @param autoReadable 是否自动判断大小
  85. * @since 0.5
  86. */
  87. public void setAutoReadable(boolean autoReadable) {
  88. this.autoReadable = autoReadable;
  89. if (autoReadable == true && unit == -1) {
  90. unit = FileSizeFormat.BYTE;
  91. }
  92. }
  93. /**
  94. * 是否自动判断大小
  95. * @return 自动判断时返回true,否则返回false
  96. * @since 0.5
  97. */
  98. public boolean isAutoReadable() {
  99. return autoReadable;
  100. }
  101. /**
  102. * 设置显示的单位。
  103. * 调用此方法会将自动判断模式设置为false并使用指定的单位进行显示。
  104. * @param unit 单位
  105. * @since 0.5
  106. */
  107. public void setUnit(int unit) {
  108. autoReadable = false;
  109. this.unit = unit;
  110. }
  111. /**
  112. * 得到所使用的单位。
  113. * <b>注意:</b>在自动模式下此方法的返回值是没有意义的,因此返回的是-1。
  114. * @return 自动模式下返回-1,否则返回指定的单位
  115. * @since 0.5
  116. */
  117. public int getUnit() {
  118. if (autoReadable == true) {
  119. return unit;
  120. }
  121. else {
  122. return -1;
  123. }
  124. }
  125. /**
  126. * 设置是否显示单位。
  127. * @param unitNameVisible 是否显示单位
  128. * @since 0.5
  129. */
  130. public void setUnitNameVisible(boolean unitNameVisible) {
  131. ( (FileSizeFormat) formatter).setUnitNameVisible(unitNameVisible);
  132. }
  133. /**
  134. * 是否显示单位
  135. * @return 显示时返回true,否则返回false
  136. * @since 0.5
  137. */
  138. public boolean isUnitNameVisible() {
  139. return ( (FileSizeFormat) formatter).isUnitNameVisible();
  140. }
  141. }