1. /*
  2. * @(#)BasicIconFactory.java 1.27 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.plaf.basic;
  8. import javax.swing.*;
  9. import javax.swing.plaf.UIResource;
  10. import java.awt.Graphics;
  11. import java.awt.Color;
  12. import java.awt.Component;
  13. import java.awt.Polygon;
  14. import java.io.Serializable;
  15. /**
  16. * Factory object that can vend Icons appropriate for the basic L & F.
  17. * <p>
  18. * <strong>Warning:</strong>
  19. * Serialized objects of this class will not be compatible with
  20. * future Swing releases. The current serialization support is
  21. * appropriate for short term storage or RMI between applications running
  22. * the same version of Swing. As of 1.4, support for long term storage
  23. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  24. * has been added to the <code>java.beans</code> package.
  25. * Please see {@link java.beans.XMLEncoder}.
  26. *
  27. * @version 1.27 12/19/03
  28. * @author David Kloba
  29. * @author Georges Saab
  30. */
  31. public class BasicIconFactory implements Serializable
  32. {
  33. private static Icon frame_icon;
  34. private static Icon checkBoxIcon;
  35. private static Icon radioButtonIcon;
  36. private static Icon checkBoxMenuItemIcon;
  37. private static Icon radioButtonMenuItemIcon;
  38. private static Icon menuItemCheckIcon;
  39. private static Icon menuItemArrowIcon;
  40. private static Icon menuArrowIcon;
  41. public static Icon getMenuItemCheckIcon() {
  42. if (menuItemCheckIcon == null) {
  43. menuItemCheckIcon = new MenuItemCheckIcon();
  44. }
  45. return menuItemCheckIcon;
  46. }
  47. public static Icon getMenuItemArrowIcon() {
  48. if (menuItemArrowIcon == null) {
  49. menuItemArrowIcon = new MenuItemArrowIcon();
  50. }
  51. return menuItemArrowIcon;
  52. }
  53. public static Icon getMenuArrowIcon() {
  54. if (menuArrowIcon == null) {
  55. menuArrowIcon = new MenuArrowIcon();
  56. }
  57. return menuArrowIcon;
  58. }
  59. public static Icon getCheckBoxIcon() {
  60. if (checkBoxIcon == null) {
  61. checkBoxIcon = new CheckBoxIcon();
  62. }
  63. return checkBoxIcon;
  64. }
  65. public static Icon getRadioButtonIcon() {
  66. if (radioButtonIcon == null) {
  67. radioButtonIcon = new RadioButtonIcon();
  68. }
  69. return radioButtonIcon;
  70. }
  71. public static Icon getCheckBoxMenuItemIcon() {
  72. if (checkBoxMenuItemIcon == null) {
  73. checkBoxMenuItemIcon = new CheckBoxMenuItemIcon();
  74. }
  75. return checkBoxMenuItemIcon;
  76. }
  77. public static Icon getRadioButtonMenuItemIcon() {
  78. if (radioButtonMenuItemIcon == null) {
  79. radioButtonMenuItemIcon = new RadioButtonMenuItemIcon();
  80. }
  81. return radioButtonMenuItemIcon;
  82. }
  83. public static Icon createEmptyFrameIcon() {
  84. if(frame_icon == null)
  85. frame_icon = new EmptyFrameIcon();
  86. return frame_icon;
  87. }
  88. private static class EmptyFrameIcon implements Icon, Serializable {
  89. int height = 16;
  90. int width = 14;
  91. public void paintIcon(Component c, Graphics g, int x, int y) {
  92. }
  93. public int getIconWidth() { return width; }
  94. public int getIconHeight() { return height; }
  95. };
  96. private static class CheckBoxIcon implements Icon, Serializable
  97. {
  98. final static int csize = 13;
  99. public void paintIcon(Component c, Graphics g, int x, int y) {
  100. }
  101. public int getIconWidth() {
  102. return csize;
  103. }
  104. public int getIconHeight() {
  105. return csize;
  106. }
  107. }
  108. private static class RadioButtonIcon implements Icon, UIResource, Serializable
  109. {
  110. public void paintIcon(Component c, Graphics g, int x, int y) {
  111. }
  112. public int getIconWidth() {
  113. return 13;
  114. }
  115. public int getIconHeight() {
  116. return 13;
  117. }
  118. } // end class RadioButtonIcon
  119. private static class CheckBoxMenuItemIcon implements Icon, UIResource, Serializable
  120. {
  121. public void paintIcon(Component c, Graphics g, int x, int y) {
  122. AbstractButton b = (AbstractButton) c;
  123. ButtonModel model = b.getModel();
  124. boolean isSelected = model.isSelected();
  125. if (isSelected) {
  126. g.drawLine(x+7, y+1, x+7, y+3);
  127. g.drawLine(x+6, y+2, x+6, y+4);
  128. g.drawLine(x+5, y+3, x+5, y+5);
  129. g.drawLine(x+4, y+4, x+4, y+6);
  130. g.drawLine(x+3, y+5, x+3, y+7);
  131. g.drawLine(x+2, y+4, x+2, y+6);
  132. g.drawLine(x+1, y+3, x+1, y+5);
  133. }
  134. }
  135. public int getIconWidth() { return 9; }
  136. public int getIconHeight() { return 9; }
  137. } // End class CheckBoxMenuItemIcon
  138. private static class RadioButtonMenuItemIcon implements Icon, UIResource, Serializable
  139. {
  140. public void paintIcon(Component c, Graphics g, int x, int y) {
  141. AbstractButton b = (AbstractButton) c;
  142. ButtonModel model = b.getModel();
  143. if (b.isSelected() == true) {
  144. g.fillOval(x+1, y+1, getIconWidth(), getIconHeight());
  145. }
  146. }
  147. public int getIconWidth() { return 6; }
  148. public int getIconHeight() { return 6; }
  149. } // End class RadioButtonMenuItemIcon
  150. private static class MenuItemCheckIcon implements Icon, UIResource, Serializable{
  151. public void paintIcon(Component c, Graphics g, int x, int y) {
  152. }
  153. public int getIconWidth() { return 9; }
  154. public int getIconHeight() { return 9; }
  155. } // End class MenuItemCheckIcon
  156. private static class MenuItemArrowIcon implements Icon, UIResource, Serializable {
  157. public void paintIcon(Component c, Graphics g, int x, int y) {
  158. }
  159. public int getIconWidth() { return 4; }
  160. public int getIconHeight() { return 8; }
  161. } // End class MenuItemArrowIcon
  162. private static class MenuArrowIcon implements Icon, UIResource, Serializable {
  163. public void paintIcon(Component c, Graphics g, int x, int y) {
  164. Polygon p = new Polygon();
  165. p.addPoint(x, y);
  166. p.addPoint(x+getIconWidth(), y+getIconHeight()/2);
  167. p.addPoint(x, y+getIconHeight());
  168. g.fillPolygon(p);
  169. }
  170. public int getIconWidth() { return 4; }
  171. public int getIconHeight() { return 8; }
  172. } // End class MenuArrowIcon
  173. }