1. package org.jr.swing.table;
  2. /**
  3. * Copyright: Copyright (c) 2002-2004
  4. * Company: JavaResearch(http://www.javaresearch.org)
  5. * 最后更新日期:2003年3月21日
  6. * @author Cherami
  7. */
  8. import java.text.*;
  9. /**
  10. * 日期型数据的单元渲染器。
  11. * @since 0.5
  12. */
  13. public class DateCellRenderer
  14. extends FormatCellRenderer {
  15. public static final int DATE = 0;
  16. public static final int TIME = 1;
  17. public static final int DATETIME = 2;
  18. public static final int defaultDateStyle = DateFormat.LONG;
  19. public static final int defaultTimeStyle = DateFormat.LONG;
  20. int type;
  21. int dateStyle;
  22. int timeStyle;
  23. /**
  24. * 构造一个显示日期的DateCellRenderer。
  25. * @since 0.5
  26. */
  27. public DateCellRenderer() {
  28. this(DATETIME, defaultDateStyle, defaultTimeStyle);
  29. }
  30. /**
  31. * 构造一个显示日期的DateCellRenderer。
  32. * @param type 日期类型(日期、时间或者日期加时间)
  33. * @since 0.5
  34. */
  35. public DateCellRenderer(int type) {
  36. this(type, defaultDateStyle, defaultTimeStyle);
  37. }
  38. /**
  39. * 构造一个显示日期的DateCellRenderer。
  40. * @param type 日期类型(日期、时间或者日期加时间)
  41. * @param style 显示风格
  42. * @since 0.5
  43. */
  44. public DateCellRenderer(int type, int style) {
  45. this(type, style, defaultTimeStyle);
  46. }
  47. /**
  48. * 构造一个显示日期的DateCellRenderer。
  49. * <b>需要注意的是如果type的类型不是日期加时间,那么将使用dateStyle作为风格。
  50. * 只有在type是日期加时间的时候第二个风格才有用。</b>
  51. * @param type 日期类型(日期、时间或者日期加时间)
  52. * @param dateStyle 日期的显示风格
  53. * @param timeStyle 时间的显示风格
  54. * @since 0.5
  55. */
  56. public DateCellRenderer(int type, int dateStyle, int timeStyle) {
  57. this.type = type;
  58. this.dateStyle = dateStyle;
  59. this.timeStyle = timeStyle;
  60. switch (type) {
  61. case DATE:
  62. formatter = DateFormat.getDateInstance(dateStyle);
  63. break;
  64. case TIME:
  65. formatter = DateFormat.getTimeInstance(dateStyle);
  66. break;
  67. case DATETIME:
  68. formatter = DateFormat.getDateTimeInstance(dateStyle, timeStyle);
  69. break;
  70. default:
  71. formatter = DateFormat.getDateInstance(dateStyle);
  72. this.type = DATE;
  73. }
  74. }
  75. }