1. package org.jr.awt.icon;
  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.util.*;
  9. import java.awt.*;
  10. import javax.swing.*;
  11. import javax.swing.plaf.metal.*;
  12. /**
  13. * 文本图标。
  14. * @since 0.1
  15. */
  16. public class TextIcon
  17. extends MetalIconFactory.TreeLeafIcon {
  18. protected String label;
  19. private static HashMap labels;
  20. /**
  21. * 构造一个空白的TextIcon。
  22. * @since 0.3
  23. */
  24. public TextIcon() {
  25. label = " ";
  26. }
  27. /**
  28. * 根据扩展名构造一个具有合适文本的TextIcon。
  29. * @param ext 扩展名
  30. * @since 0.3
  31. */
  32. public TextIcon(String ext) {
  33. label = getLabel(ext);
  34. }
  35. /**
  36. * 绘制图标
  37. * @param c 绘制组件
  38. * @param g 图形设备
  39. * @param x 绘制的x坐标的起始点
  40. * @param y 绘制的y坐标的起始点
  41. * @since 0.1
  42. */
  43. public void paintIcon(Component c, Graphics g, int x, int y) {
  44. super.paintIcon(c, g, x, y);
  45. if (label != null) {
  46. FontMetrics fm = g.getFontMetrics();
  47. int offsetX = (getIconWidth() - fm.stringWidth(label)) / 2;
  48. int offsetY = (getIconHeight() - fm.getHeight()) / 2 - 2;
  49. g.drawString(label, x + offsetX,
  50. y + offsetY + fm.getHeight());
  51. }
  52. }
  53. /**
  54. * 根据指定的扩展名得到一个文本图标。
  55. * @param ext 扩展名
  56. * @return 指定的扩展名对应的文本图标
  57. * @since 0.1
  58. */
  59. public static Icon getIcon(String ext) {
  60. TextIcon icon = new TextIcon(getLabel(ext));
  61. return icon;
  62. }
  63. /**
  64. * 设置扩展名对应的图标文本。
  65. * @param ext 扩展名
  66. * @param label 图标文本
  67. * @since 0.1
  68. */
  69. public static void setLabel(String ext, String label) {
  70. if (labels == null) {
  71. labels = new HashMap();
  72. setDefaultSet();
  73. }
  74. labels.put(ext, label);
  75. }
  76. /**
  77. * 得到扩展名对应的图标文本。
  78. * 如果在缺省的对应集里面没有则使用扩展名的第一个字符作为图标文本并加入对应集。
  79. * @param ext 扩展名
  80. * @return 扩展名对应的图标文本
  81. * @since 0.3
  82. */
  83. public static String getLabel(String ext) {
  84. if (labels == null) {
  85. labels = new HashMap();
  86. setDefaultSet();
  87. }
  88. String label = (String) labels.get(ext);
  89. if (label == null) {
  90. label = ext.substring(0, 1);
  91. setLabel(ext, label);
  92. }
  93. return label;
  94. }
  95. /**
  96. * 初始化缺省的扩展名-图标文本集。
  97. */
  98. private static void setDefaultSet() {
  99. labels.put("c", "C");
  100. labels.put("java", "C");
  101. labels.put("html", "D");
  102. labels.put("htm", "D");
  103. labels.put("txt", "T");
  104. labels.put("cc", "C");
  105. labels.put("cpp", "C");
  106. labels.put("exe", "B");
  107. labels.put("class", "B");
  108. labels.put("gif", "I");
  109. labels.put("jpg", "I");
  110. labels.put("bat", "S");
  111. labels.put("sh", "S");
  112. labels.put("pl", "S");
  113. labels.put("h", "H");
  114. labels.put("hpp", "H");
  115. labels.put("jsp", "C");
  116. labels.put("xml", "C");
  117. labels.put("doc", "T");
  118. labels.put("pdf", "D");
  119. labels.put("jar", "Z");
  120. labels.put("zip", "Z");
  121. labels.put("gz", "Z");
  122. labels.put("tar", "A");
  123. }
  124. }