1. /*
  2. * @(#)WindowsIconFactory.java 1.19 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.windows;
  8. import javax.swing.*;
  9. import javax.swing.plaf.UIResource;
  10. import java.awt.*;
  11. import java.io.Serializable;
  12. /**
  13. * Factory object that can vend Icons appropriate for the Windows L & F.
  14. * <p>
  15. * <strong>Warning:</strong>
  16. * Serialized objects of this class will not be compatible with
  17. * future Swing releases. The current serialization support is appropriate
  18. * for short term storage or RMI between applications running the same
  19. * version of Swing. A future release of Swing will provide support for
  20. * long term persistence.
  21. *
  22. * @version 1.19 01/23/03
  23. * @author David Kloba
  24. * @author Georges Saab
  25. * @author Rich Schiavi
  26. */
  27. public class WindowsIconFactory implements Serializable
  28. {
  29. private static Icon frame_closeIcon;
  30. private static Icon frame_iconifyIcon;
  31. private static Icon frame_maxIcon;
  32. private static Icon frame_minIcon;
  33. private static Icon frame_resizeIcon;
  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 createFrameCloseIcon() {
  84. if (frame_closeIcon == null) {
  85. frame_closeIcon = new FrameButtonIcon("window.closebutton");
  86. }
  87. return frame_closeIcon;
  88. }
  89. public static Icon createFrameIconifyIcon() {
  90. if (frame_iconifyIcon == null) {
  91. frame_iconifyIcon = new FrameButtonIcon("window.minbutton");
  92. }
  93. return frame_iconifyIcon;
  94. }
  95. public static Icon createFrameMaximizeIcon() {
  96. if (frame_maxIcon == null) {
  97. frame_maxIcon = new FrameButtonIcon("window.maxbutton");
  98. }
  99. return frame_maxIcon;
  100. }
  101. public static Icon createFrameMinimizeIcon() {
  102. if (frame_minIcon == null) {
  103. frame_minIcon = new FrameButtonIcon("window.restorebutton");
  104. }
  105. return frame_minIcon;
  106. }
  107. public static Icon createFrameResizeIcon() {
  108. if(frame_resizeIcon == null)
  109. frame_resizeIcon = new ResizeIcon();
  110. return frame_resizeIcon;
  111. }
  112. private static class FrameButtonIcon implements Icon, Serializable {
  113. private int classicHeight = 16;
  114. private int classicWidth = 14;
  115. private String category;
  116. private FrameButtonIcon(String category) {
  117. this.category = category;
  118. }
  119. public void paintIcon(Component c, Graphics g, int x, int y) {
  120. XPStyle xp = XPStyle.getXP();
  121. if (xp != null) {
  122. XPStyle.Skin skin = xp.getSkin(category);
  123. JButton b = (JButton)c;
  124. ButtonModel model = b.getModel();
  125. int index = 0;
  126. if (!model.isEnabled()) {
  127. index = 3;
  128. } else if (model.isArmed() && model.isPressed()) {
  129. index = 2;
  130. } else if (model.isRollover()) {
  131. index = 1;
  132. }
  133. // Find out if frame is inactive
  134. JInternalFrame jif = (JInternalFrame)SwingUtilities.
  135. getAncestorOfClass(JInternalFrame.class, b);
  136. if (jif != null && !jif.isSelected()) {
  137. index += 4;
  138. }
  139. skin.paintSkin(g, 0, 0, index);
  140. } else if (category.equals("window.closebutton")) {
  141. g.setColor(Color.black);
  142. g.drawLine(4, 3, 10, 9);
  143. g.drawLine(5, 3, 11, 9);
  144. g.drawLine(10, 3, 4, 9);
  145. g.drawLine(11, 3, 5, 9);
  146. } else if (category.equals("window.minbutton")) {
  147. g.setColor(Color.black);
  148. g.drawRect(4, 9, 6, 1);
  149. } else if (category.equals("window.maxbutton")) {
  150. g.setColor(Color.black);
  151. g.drawRect(3, 2, 8, 8);
  152. g.drawLine(3, 3, 11, 3);
  153. } else if (category.equals("window.restorebutton")) {
  154. g.setColor(Color.black);
  155. g.drawRect(5, 2, 5, 5);
  156. g.drawLine(5, 3, 10, 3);
  157. g.drawRect(3, 5, 5, 5);
  158. g.drawLine(3, 6, 7, 6);
  159. g.setColor(UIManager.getColor("InternalFrame.minimizeIconBackground"));
  160. g.fillRect(4, 7, 4, 3);
  161. }
  162. }
  163. public int getIconWidth() {
  164. XPStyle xp = XPStyle.getXP();
  165. if (xp != null) {
  166. return xp.getSkin(category).getWidth();
  167. } else {
  168. return classicWidth;
  169. }
  170. }
  171. public int getIconHeight() {
  172. XPStyle xp = XPStyle.getXP();
  173. if (xp != null) {
  174. return xp.getSkin(category).getHeight();
  175. } else {
  176. return classicHeight;
  177. }
  178. }
  179. }
  180. private static class ResizeIcon implements Icon, Serializable {
  181. public void paintIcon(Component c, Graphics g, int x, int y) {
  182. g.setColor(UIManager.getColor("InternalFrame.resizeIconHighlight"));
  183. g.drawLine(0, 11, 11, 0);
  184. g.drawLine(4, 11, 11, 4);
  185. g.drawLine(8, 11, 11, 8);
  186. g.setColor(UIManager.getColor("InternalFrame.resizeIconShadow"));
  187. g.drawLine(1, 11, 11, 1);
  188. g.drawLine(2, 11, 11, 2);
  189. g.drawLine(5, 11, 11, 5);
  190. g.drawLine(6, 11, 11, 6);
  191. g.drawLine(9, 11, 11, 9);
  192. g.drawLine(10, 11, 11, 10);
  193. }
  194. public int getIconWidth() { return 13; }
  195. public int getIconHeight() { return 13; }
  196. };
  197. private static class CheckBoxIcon implements Icon, Serializable
  198. {
  199. final static int csize = 13;
  200. public void paintIcon(Component c, Graphics g, int x, int y) {
  201. JCheckBox cb = (JCheckBox) c;
  202. ButtonModel model = cb.getModel();
  203. XPStyle xp = XPStyle.getXP();
  204. if (xp != null) {
  205. int index = 0;
  206. if (!model.isEnabled()) {
  207. index = 3; // disabled
  208. } else if (model.isPressed() && model.isArmed()) {
  209. index = 2; // pressed
  210. } else if (model.isRollover()) {
  211. index = 1; // rollover
  212. }
  213. if (model.isSelected()) {
  214. index += 4;
  215. }
  216. xp.getSkin("button.checkbox").paintSkin(g, x, y, index);
  217. } else {
  218. // outer bevel
  219. if(!cb.isBorderPaintedFlat()) {
  220. // Outer top/left
  221. g.setColor(UIManager.getColor("CheckBox.shadow"));
  222. g.drawLine(x, y, x+11, y);
  223. g.drawLine(x, y+1, x, y+11);
  224. // Outer bottom/right
  225. g.setColor(UIManager.getColor("CheckBox.highlight"));
  226. g.drawLine(x+12, y, x+12, y+12);
  227. g.drawLine(x, y+12, x+11, y+12);
  228. // Inner top.left
  229. g.setColor(UIManager.getColor("CheckBox.darkShadow"));
  230. g.drawLine(x+1, y+1, x+10, y+1);
  231. g.drawLine(x+1, y+2, x+1, y+10);
  232. // Inner bottom/right
  233. g.setColor(UIManager.getColor("CheckBox.light"));
  234. g.drawLine(x+1, y+11, x+11, y+11);
  235. g.drawLine(x+11, y+1, x+11, y+10);
  236. // inside box
  237. if((model.isPressed() && model.isArmed()) || !model.isEnabled()) {
  238. g.setColor(UIManager.getColor("CheckBox.background"));
  239. } else {
  240. g.setColor(UIManager.getColor("CheckBox.interiorBackground"));
  241. }
  242. g.fillRect(x+2, y+2, csize-4, csize-4);
  243. } else {
  244. g.setColor(UIManager.getColor("CheckBox.shadow"));
  245. g.drawRect(x+1, y+1, csize-3, csize-3);
  246. if((model.isPressed() && model.isArmed()) || !model.isEnabled()) {
  247. g.setColor(UIManager.getColor("CheckBox.background"));
  248. } else {
  249. g.setColor(UIManager.getColor("CheckBox.interiorBackground"));
  250. }
  251. g.fillRect(x+2, y+2, csize-4, csize-4);
  252. }
  253. if(model.isEnabled()) {
  254. g.setColor(UIManager.getColor("CheckBox.darkShadow"));
  255. } else {
  256. g.setColor(UIManager.getColor("CheckBox.shadow"));
  257. }
  258. // paint check
  259. if (model.isSelected()) {
  260. g.drawLine(x+9, y+3, x+9, y+3);
  261. g.drawLine(x+8, y+4, x+9, y+4);
  262. g.drawLine(x+7, y+5, x+9, y+5);
  263. g.drawLine(x+6, y+6, x+8, y+6);
  264. g.drawLine(x+3, y+7, x+7, y+7);
  265. g.drawLine(x+4, y+8, x+6, y+8);
  266. g.drawLine(x+5, y+9, x+5, y+9);
  267. g.drawLine(x+3, y+5, x+3, y+5);
  268. g.drawLine(x+3, y+6, x+4, y+6);
  269. }
  270. }
  271. }
  272. public int getIconWidth() {
  273. XPStyle xp = XPStyle.getXP();
  274. if (xp != null) {
  275. return xp.getSkin("button.checkbox").getWidth();
  276. } else {
  277. return csize;
  278. }
  279. }
  280. public int getIconHeight() {
  281. XPStyle xp = XPStyle.getXP();
  282. if (xp != null) {
  283. return xp.getSkin("button.checkbox").getHeight();
  284. } else {
  285. return csize;
  286. }
  287. }
  288. }
  289. private static class RadioButtonIcon implements Icon, UIResource, Serializable
  290. {
  291. public void paintIcon(Component c, Graphics g, int x, int y) {
  292. AbstractButton b = (AbstractButton) c;
  293. ButtonModel model = b.getModel();
  294. XPStyle xp = XPStyle.getXP();
  295. if (xp != null) {
  296. XPStyle.Skin skin = xp.getSkin("button.radiobutton");
  297. int index = 0;
  298. if (!model.isEnabled()) {
  299. index = 3; // disabled
  300. } else if (model.isPressed() && model.isArmed()) {
  301. index = 2; // pressed
  302. } else if (model.isRollover()) {
  303. index = 1; // rollover
  304. }
  305. if (model.isSelected()) {
  306. index += 4;
  307. }
  308. skin.paintSkin(g, x, y, index);
  309. } else {
  310. // fill interior
  311. if((model.isPressed() && model.isArmed()) || !model.isEnabled()) {
  312. g.setColor(UIManager.getColor("RadioButton.background"));
  313. } else {
  314. g.setColor(UIManager.getColor("RadioButton.interiorBackground"));
  315. }
  316. g.fillRect(x+2, y+2, 8, 8);
  317. // outter left arc
  318. g.setColor(UIManager.getColor("RadioButton.shadow"));
  319. g.drawLine(x+4, y+0, x+7, y+0);
  320. g.drawLine(x+2, y+1, x+3, y+1);
  321. g.drawLine(x+8, y+1, x+9, y+1);
  322. g.drawLine(x+1, y+2, x+1, y+3);
  323. g.drawLine(x+0, y+4, x+0, y+7);
  324. g.drawLine(x+1, y+8, x+1, y+9);
  325. // outter right arc
  326. g.setColor(UIManager.getColor("RadioButton.highlight"));
  327. g.drawLine(x+2, y+10, x+3, y+10);
  328. g.drawLine(x+4, y+11, x+7, y+11);
  329. g.drawLine(x+8, y+10, x+9, y+10);
  330. g.drawLine(x+10, y+9, x+10, y+8);
  331. g.drawLine(x+11, y+7, x+11, y+4);
  332. g.drawLine(x+10, y+3, x+10, y+2);
  333. // inner left arc
  334. g.setColor(UIManager.getColor("RadioButton.darkShadow"));
  335. g.drawLine(x+4, y+1, x+7, y+1);
  336. g.drawLine(x+2, y+2, x+3, y+2);
  337. g.drawLine(x+8, y+2, x+9, y+2);
  338. g.drawLine(x+2, y+3, x+2, y+3);
  339. g.drawLine(x+1, y+4, x+1, y+7);
  340. g.drawLine(x+2, y+8, x+2, y+8);
  341. // inner right arc
  342. g.setColor(UIManager.getColor("RadioButton.light"));
  343. g.drawLine(x+2, y+9, x+3, y+9);
  344. g.drawLine(x+4, y+10, x+7, y+10);
  345. g.drawLine(x+8, y+9, x+9, y+9);
  346. g.drawLine(x+9, y+8, x+9, y+8);
  347. g.drawLine(x+10, y+7, x+10, y+4);
  348. g.drawLine(x+9, y+3, x+9, y+3);
  349. // indicate whether selected or not
  350. if(model.isSelected()) {
  351. g.setColor(UIManager.getColor("RadioButton.darkShadow"));
  352. g.fillRect(x+4, y+5, 4, 2);
  353. g.fillRect(x+5, y+4, 2, 4);
  354. }
  355. }
  356. }
  357. public int getIconWidth() {
  358. XPStyle xp = XPStyle.getXP();
  359. if (xp != null) {
  360. return xp.getSkin("button.radiobutton").getWidth();
  361. } else {
  362. return 13;
  363. }
  364. }
  365. public int getIconHeight() {
  366. XPStyle xp = XPStyle.getXP();
  367. if (xp != null) {
  368. return xp.getSkin("button.radiobutton").getHeight();
  369. } else {
  370. return 13;
  371. }
  372. }
  373. } // end class RadioButtonIcon
  374. private static class CheckBoxMenuItemIcon implements Icon, UIResource, Serializable
  375. {
  376. public void paintIcon(Component c, Graphics g, int x, int y) {
  377. AbstractButton b = (AbstractButton) c;
  378. ButtonModel model = b.getModel();
  379. boolean isSelected = model.isSelected();
  380. if (isSelected) {
  381. y = y - getIconHeight() / 2;
  382. g.drawLine(x+9, y+3, x+9, y+3);
  383. g.drawLine(x+8, y+4, x+9, y+4);
  384. g.drawLine(x+7, y+5, x+9, y+5);
  385. g.drawLine(x+6, y+6, x+8, y+6);
  386. g.drawLine(x+3, y+7, x+7, y+7);
  387. g.drawLine(x+4, y+8, x+6, y+8);
  388. g.drawLine(x+5, y+9, x+5, y+9);
  389. g.drawLine(x+3, y+5, x+3, y+5);
  390. g.drawLine(x+3, y+6, x+4, y+6);
  391. }
  392. }
  393. public int getIconWidth() { return 9; }
  394. public int getIconHeight() { return 9; }
  395. } // End class CheckBoxMenuItemIcon
  396. private static class RadioButtonMenuItemIcon implements Icon, UIResource, Serializable
  397. {
  398. public void paintIcon(Component c, Graphics g, int x, int y) {
  399. AbstractButton b = (AbstractButton) c;
  400. ButtonModel model = b.getModel();
  401. if (b.isSelected() == true) {
  402. g.fillArc(0,0,getIconWidth()-2, getIconHeight()-2, 0, 360);
  403. }
  404. }
  405. public int getIconWidth() { return 12; }
  406. public int getIconHeight() { return 12; }
  407. } // End class RadioButtonMenuItemIcon
  408. private static class MenuItemCheckIcon implements Icon, UIResource, Serializable{
  409. public void paintIcon(Component c, Graphics g, int x, int y) {
  410. /* For debugging:
  411. Color oldColor = g.getColor();
  412. g.setColor(Color.orange);
  413. g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true);
  414. g.setColor(oldColor);
  415. */
  416. }
  417. public int getIconWidth() { return 9; }
  418. public int getIconHeight() { return 9; }
  419. } // End class MenuItemCheckIcon
  420. private static class MenuItemArrowIcon implements Icon, UIResource, Serializable {
  421. public void paintIcon(Component c, Graphics g, int x, int y) {
  422. /* For debugging:
  423. Color oldColor = g.getColor();
  424. g.setColor(Color.green);
  425. g.fill3DRect(x,y,getIconWidth(), getIconHeight(), true);
  426. g.setColor(oldColor);
  427. */
  428. }
  429. public int getIconWidth() { return 4; }
  430. public int getIconHeight() { return 8; }
  431. } // End class MenuItemArrowIcon
  432. private static class MenuArrowIcon implements Icon, UIResource, Serializable {
  433. public void paintIcon(Component c, Graphics g, int x, int y) {
  434. g.translate(x,y);
  435. if( WindowsUtils.isLeftToRight(c) ) {
  436. g.drawLine( 0, 0, 0, 7 );
  437. g.drawLine( 1, 1, 1, 6 );
  438. g.drawLine( 2, 2, 2, 5 );
  439. g.drawLine( 3, 3, 3, 4 );
  440. } else {
  441. g.drawLine( 4, 0, 4, 7 );
  442. g.drawLine( 3, 1, 3, 6 );
  443. g.drawLine( 2, 2, 2, 5 );
  444. g.drawLine( 1, 3, 1, 4 );
  445. }
  446. g.translate(-x,-y);
  447. }
  448. public int getIconWidth() { return 4; }
  449. public int getIconHeight() { return 8; }
  450. } // End class MenuArrowIcon
  451. }