1. package org.jr.swing.table;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年2月16日
  6. * @author Cherami
  7. */
  8. import java.awt.*;
  9. import javax.swing.*;
  10. import javax.swing.table.*;
  11. /**
  12. * 可以一次性设置表格的各列的对齐方式的表格单元渲染器。
  13. * @since 0.1
  14. */
  15. public class AlignmentCellRender
  16. extends DefaultTableCellRenderer {
  17. int columns[];
  18. int aligments[];
  19. int defaultAligment;
  20. /**
  21. * 构造一个缺省的AlignmentCellRender。
  22. * 缺省对齐方式为靠左对齐。
  23. * @since 0.1
  24. */
  25. public AlignmentCellRender() {
  26. defaultAligment = LEFT;
  27. columns = new int[0];
  28. aligments = new int[0];
  29. }
  30. /**
  31. * 根据指定的缺省对齐方式构造一个AlignmentCellRender。
  32. * @param defaultAligment 缺省的对齐方式
  33. * @since 0.3
  34. */
  35. public AlignmentCellRender(int defaultAligment) {
  36. this.defaultAligment = defaultAligment;
  37. columns = new int[0];
  38. aligments = new int[0];
  39. }
  40. /**
  41. * 根据指定行列的对齐方式的设置构造一个AlignmentCellRender。
  42. * 缺省对齐方式为靠左对齐。
  43. * @param columns 要设置对齐方式的列
  44. * @param aligments 对应的对齐方式
  45. * @since 0.1
  46. */
  47. public AlignmentCellRender(int columns[], int aligments[]) {
  48. this(LEFT, columns, aligments);
  49. }
  50. /**
  51. * 根据缺省对齐方式和指定行列的对齐方式的设置构造一个AlignmentCellRender。
  52. * @param defaultAligment 缺省对齐方式
  53. * @param columns 要设置对齐方式的列
  54. * @param aligments 对应的对齐方式
  55. * @since 0.3
  56. */
  57. public AlignmentCellRender(int defaultAligment, int columns[], int aligments[]) {
  58. if (columns.length != aligments.length) {
  59. throw new IllegalArgumentException(
  60. "The length of two arguments must be equal.");
  61. }
  62. this.defaultAligment = defaultAligment;
  63. copyArrayValue(columns, aligments);
  64. }
  65. /**
  66. * 对表格单元进行渲染。
  67. * @param table 要渲染的表格
  68. * @param value 表格单元的值
  69. * @param isSelected 是否选中
  70. * @param hasFocus 是否得到焦点
  71. * @param row 行数
  72. * @param column 列数
  73. * @return 经渲染后的表格单元
  74. * @since 0.1
  75. */
  76. public Component getTableCellRendererComponent(JTable table, Object value,
  77. boolean isSelected,
  78. boolean hasFocus, int row,
  79. int column) {
  80. for (int i = 0; i < columns.length; i++) {
  81. if (column == columns[i]) {
  82. setHorizontalAlignment(aligments[i]);
  83. setValue(value);
  84. return this;
  85. }
  86. }
  87. setHorizontalAlignment(defaultAligment);
  88. setValue(value);
  89. return this;
  90. }
  91. /**
  92. * 设置缺省对齐方式。
  93. * @param aligment 缺省对齐方式
  94. * @since 0.1
  95. */
  96. public void setDefaultAligment(int aligment) {
  97. defaultAligment = aligment;
  98. }
  99. /**
  100. * 重新设置各列的对齐方式。
  101. * @param columns 要设置对齐方式的列
  102. * @param aligments 对应的对齐方式
  103. * @since 0.3
  104. */
  105. public void setAligments(int columns[], int aligments[]) {
  106. if (columns.length != aligments.length) {
  107. throw new IllegalArgumentException(
  108. "The length of two arguments must be equal.");
  109. }
  110. copyArrayValue(columns, aligments);
  111. }
  112. /**
  113. * 设置某一列的对齐方式。
  114. * 如果该列已经被设置过则用新的值替换原来的值。
  115. * @param column 要设置对齐方式的列
  116. * @param aligment 对应的对齐方式
  117. * @since 0.3
  118. */
  119. public void setAligment(int column, int aligment) {
  120. boolean haveOne = false;
  121. for (int i = 0; i < columns.length; i++) {
  122. if (column == columns[i]) {
  123. aligments[i] = aligment;
  124. haveOne = true;
  125. break;
  126. }
  127. }
  128. if (!haveOne) {
  129. int[] oldColumns = columns;
  130. int[] oldAligments = aligments;
  131. copyArrayValue(oldColumns, oldAligments);
  132. columns[oldColumns.length] = column;
  133. aligments[oldColumns.length] = aligment;
  134. }
  135. }
  136. /**
  137. * 拷贝数组。
  138. * @param columnArray 列数组
  139. * @param aligmentArray 对齐方式数组
  140. */
  141. private void copyArrayValue(int[] columnArray, int[] aligmentArray) {
  142. this.columns = new int[columnArray.length];
  143. this.aligments = new int[columnArray.length];
  144. for (int i = 0; i < columnArray.length; i++) {
  145. this.columns[i] = columnArray[i];
  146. this.aligments[i] = aligmentArray[i];
  147. }
  148. }
  149. }