1. /*
  2. * @(#)MotifIconFactory.java 1.18 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.java.swing.plaf.motif;
  8. import javax.swing.*;
  9. import javax.swing.plaf.UIResource;
  10. import java.awt.Color;
  11. import java.awt.Component;
  12. import java.awt.Dimension;
  13. import java.awt.Graphics;
  14. import java.awt.Polygon;
  15. import java.io.Serializable;
  16. /**
  17. * Icon factory for the CDE/Motif Look and Feel
  18. * <p>
  19. * <strong>Warning:</strong>
  20. * Serialized objects of this class will not be compatible with
  21. * future Swing releases. The current serialization support is appropriate
  22. * for short term storage or RMI between applications running the same
  23. * version of Swing. A future release of Swing will provide support for
  24. * long term persistence.
  25. *
  26. * 1.18 11/29/01
  27. * @author Georges Saab
  28. */
  29. public class MotifIconFactory implements Serializable
  30. {
  31. private static Icon checkBoxIcon;
  32. private static Icon radioButtonIcon;
  33. private static Icon menuItemCheckIcon;
  34. private static Icon menuItemArrowIcon;
  35. private static Icon menuArrowIcon;
  36. public static Icon getMenuItemCheckIcon() {
  37. if (menuItemCheckIcon == null) {
  38. menuItemCheckIcon = new MenuItemCheckIcon();
  39. }
  40. return menuItemCheckIcon;
  41. }
  42. public static Icon getMenuItemArrowIcon() {
  43. if (menuItemArrowIcon == null) {
  44. menuItemArrowIcon = new MenuItemArrowIcon();
  45. }
  46. return menuItemArrowIcon;
  47. }
  48. public static Icon getMenuArrowIcon() {
  49. if (menuArrowIcon == null) {
  50. menuArrowIcon = new MenuArrowIcon();
  51. }
  52. return menuArrowIcon;
  53. }
  54. public static Icon getCheckBoxIcon() {
  55. if (checkBoxIcon == null) {
  56. checkBoxIcon = new CheckBoxIcon();
  57. }
  58. return checkBoxIcon;
  59. }
  60. public static Icon getRadioButtonIcon() {
  61. if (radioButtonIcon == null) {
  62. radioButtonIcon = new RadioButtonIcon();
  63. }
  64. return radioButtonIcon;
  65. }
  66. private static class CheckBoxIcon implements Icon, UIResource, Serializable {
  67. final static int csize = 13;
  68. private Color control = UIManager.getColor("control");
  69. private Color foreground = UIManager.getColor("CheckBox.foreground");
  70. private Color shadow = UIManager.getColor("controlShadow");
  71. private Color highlight = UIManager.getColor("controlHighlight");
  72. private Color lightShadow = UIManager.getColor("controlLightShadow");
  73. public void paintIcon(Component c, Graphics g, int x, int y){
  74. AbstractButton b = (AbstractButton) c;
  75. ButtonModel model = b.getModel();
  76. boolean isPressed = model.isPressed();
  77. boolean isArmed = model.isArmed();
  78. boolean isEnabled = model.isEnabled();
  79. boolean isSelected = model.isSelected();
  80. // 4 -looks- to the Motif CheckBox
  81. // drawCheckBezelOut - default unchecked state
  82. // drawBezel - when we uncheck in toggled state
  83. // drawCheckBezel - when we check in toggle state
  84. // drawCheckBezelIn - selected, mouseReleased
  85. boolean checkToggleIn = ((isPressed &&
  86. !isArmed &&
  87. isSelected) ||
  88. (isPressed &&
  89. isArmed &&
  90. !isSelected));
  91. boolean uncheckToggleOut = ((isPressed &&
  92. !isArmed &&
  93. !isSelected) ||
  94. (isPressed &&
  95. isArmed &&
  96. isSelected));
  97. boolean checkIn = (!isPressed &&
  98. isArmed &&
  99. isSelected ||
  100. (!isPressed &&
  101. !isArmed &&
  102. isSelected));
  103. // Padding required to keep focus highlight from intersecting icon.
  104. x += (MotifGraphicsUtils.isLeftToRight(c)) ? 2 : -3;
  105. if (checkToggleIn)
  106. {
  107. // toggled from unchecked to checked
  108. drawCheckBezel(g,x,y,csize,true,false,false);
  109. }
  110. else if (uncheckToggleOut)
  111. {
  112. // MotifBorderFactory.drawBezel(g,x,y,csize,csize,false,false);
  113. drawCheckBezel(g,x,y,csize,true,true,false);
  114. }
  115. else if (checkIn)
  116. {
  117. // show checked, unpressed state
  118. drawCheckBezel(g,x,y,csize,false,false,true);
  119. }
  120. else
  121. { // show unchecked state
  122. drawCheckBezelOut(g,x,y,csize);
  123. }
  124. }
  125. public int getIconWidth() {
  126. return csize;
  127. }
  128. public int getIconHeight() {
  129. return csize;
  130. }
  131. public void drawCheckBezelOut(Graphics g, int x, int y, int csize){
  132. Color controlShadow = UIManager.getColor("controlShadow");
  133. int w = csize;
  134. int h = csize;
  135. Color oldColor = g.getColor();
  136. g.translate(x,y);
  137. g.setColor(highlight); // inner 3D border
  138. g.drawLine(0, 0, 0, h-1);
  139. g.drawLine(1, 0, w-1, 0);
  140. g.setColor(shadow); // black drop shadow __|
  141. g.drawLine(1, h-1, w-1, h-1);
  142. g.drawLine(w-1, h-1, w-1, 1);
  143. g.translate(-x,-y);
  144. g.setColor(oldColor);
  145. }
  146. public void drawCheckBezel(Graphics g, int x, int y, int csize,
  147. boolean shade, boolean out, boolean check)
  148. {
  149. Color oldColor = g.getColor();
  150. g.translate(x, y);
  151. if (out){
  152. g.setColor(control);
  153. g.fillRect(0,0,csize,csize);
  154. }
  155. else {
  156. g.setColor(lightShadow);
  157. g.fillRect(0,0,csize,csize);
  158. }
  159. //bottom
  160. if (out)
  161. g.setColor(shadow);
  162. else
  163. g.setColor(highlight);
  164. g.drawLine(1,csize-1,csize-2,csize-1);
  165. if (shade) {
  166. g.drawLine(2,csize-2,csize-3,csize-2);
  167. g.drawLine(csize-2,2,csize-2 ,csize-1);
  168. if (out)
  169. g.setColor(highlight);
  170. else
  171. g.setColor(shadow);
  172. g.drawLine(1,2,1,csize-2);
  173. g.drawLine(1,1,csize-3,1);
  174. if (out)
  175. g.setColor(shadow);
  176. else
  177. g.setColor(highlight);
  178. }
  179. //right
  180. g.drawLine(csize-1,1,csize-1,csize-1);
  181. //left
  182. if (out)
  183. g.setColor(highlight);
  184. else
  185. g.setColor(shadow);
  186. g.drawLine(0,1,0,csize-1);
  187. //top
  188. g.drawLine(0,0,csize-1,0);
  189. if (check){
  190. // draw check
  191. g.setColor(foreground);
  192. g.drawLine(csize-2,1,csize-2,2);
  193. g.drawLine(csize-3,2,csize-3,3);
  194. g.drawLine(csize-4,3,csize-4,4);
  195. g.drawLine(csize-5,4,csize-5,6);
  196. g.drawLine(csize-6,5,csize-6,8);
  197. g.drawLine(csize-7,6,csize-7,10);
  198. g.drawLine(csize-8,7,csize-8,10);
  199. g.drawLine(csize-9,6,csize-9,9);
  200. g.drawLine(csize-10,5,csize-10,8);
  201. g.drawLine(csize-11,5,csize-11,7);
  202. g.drawLine(csize-12,6,csize-12,6);
  203. }
  204. g.translate(-x, -y);
  205. g.setColor(oldColor);
  206. }
  207. } // end class CheckBoxIcon
  208. private static class RadioButtonIcon implements Icon, UIResource, Serializable {
  209. private Color dot = UIManager.getColor("activeCaptionBorder");
  210. private Color highlight = UIManager.getColor("controlHighlight");
  211. private Color shadow = UIManager.getColor("controlShadow");
  212. public void paintIcon(Component c, Graphics g, int x, int y) {
  213. // fill interior
  214. AbstractButton b = (AbstractButton) c;
  215. ButtonModel model = b.getModel();
  216. int w = getIconWidth();
  217. int h = getIconHeight();
  218. // add pad so focus isn't smudged on the x
  219. x += (MotifGraphicsUtils.isLeftToRight(c))? 2 : -3;
  220. boolean isPressed = model.isPressed();
  221. boolean isArmed = model.isArmed();
  222. boolean isEnabled = model.isEnabled();
  223. boolean isSelected = model.isSelected();
  224. boolean checkIn = ((isPressed &&
  225. !isArmed &&
  226. isSelected) ||
  227. (isPressed &&
  228. isArmed &&
  229. !isSelected)
  230. ||
  231. (!isPressed &&
  232. isArmed &&
  233. isSelected ||
  234. (!isPressed &&
  235. !isArmed &&
  236. isSelected)));
  237. if (checkIn){
  238. g.setColor(shadow);
  239. g.drawLine(x+5,y+0,x+8,y+0);
  240. g.drawLine(x+3,y+1,x+4,y+1);
  241. g.drawLine(x+9,y+1,x+9,y+1);
  242. g.drawLine(x+2,y+2,x+2,y+2);
  243. g.drawLine(x+1,y+3,x+1,y+3);
  244. g.drawLine(x,y+4,x,y+9);
  245. g.drawLine(x+1,y+10,x+1,y+10);
  246. g.drawLine(x+2,y+11,x+2,y+11);
  247. g.setColor(highlight);
  248. g.drawLine(x+3,y+12,x+4,y+12);
  249. g.drawLine(x+5,y+13,x+8,y+13);
  250. g.drawLine(x+9,y+12,x+10,y+12);
  251. g.drawLine(x+11,y+11,x+11,y+11);
  252. g.drawLine(x+12,y+10,x+12,y+10);
  253. g.drawLine(x+13,y+9,x+13,y+4);
  254. g.drawLine(x+12,y+3,x+12,y+3);
  255. g.drawLine(x+11,y+2,x+11,y+2);
  256. g.drawLine(x+10,y+1,x+10,y+1);
  257. g.setColor(dot);
  258. g.fillRect(x+4,y+5,6,4);
  259. g.drawLine(x+5,y+4,x+8,y+4);
  260. g.drawLine(x+5,y+9,x+8,y+9);
  261. }
  262. else {
  263. g.setColor(highlight);
  264. g.drawLine(x+5,y+0,x+8,y+0);
  265. g.drawLine(x+3,y+1,x+4,y+1);
  266. g.drawLine(x+9,y+1,x+9,y+1);
  267. g.drawLine(x+2,y+2,x+2,y+2);
  268. g.drawLine(x+1,y+3,x+1,y+3);
  269. g.drawLine(x,y+4,x,y+9);
  270. g.drawLine(x+1,y+10,x+1,y+10);
  271. g.drawLine(x+2,y+11,x+2,y+11);
  272. g.setColor(shadow);
  273. g.drawLine(x+3,y+12,x+4,y+12);
  274. g.drawLine(x+5,y+13,x+8,y+13);
  275. g.drawLine(x+9,y+12,x+10,y+12);
  276. g.drawLine(x+11,y+11,x+11,y+11);
  277. g.drawLine(x+12,y+10,x+12,y+10);
  278. g.drawLine(x+13,y+9,x+13,y+4);
  279. g.drawLine(x+12,y+3,x+12,y+3);
  280. g.drawLine(x+11,y+2,x+11,y+2);
  281. g.drawLine(x+10,y+1,x+10,y+1);
  282. }
  283. }
  284. public int getIconWidth() {
  285. return 13;
  286. }
  287. public int getIconHeight() {
  288. return 13;
  289. }
  290. } // end class RadioButtonIcon
  291. private static class MenuItemCheckIcon implements Icon, UIResource, Serializable
  292. {
  293. public void paintIcon(Component c,Graphics g, int x, int y)
  294. {
  295. }
  296. public int getIconWidth() { return 0; }
  297. public int getIconHeight() { return 0; }
  298. } // end class MenuItemCheckIcon
  299. private static class MenuItemArrowIcon implements Icon, UIResource, Serializable
  300. {
  301. public void paintIcon(Component c,Graphics g, int x, int y)
  302. {
  303. }
  304. public int getIconWidth() { return 0; }
  305. public int getIconHeight() { return 0; }
  306. } // end class MenuItemArrowIcon
  307. private static class MenuArrowIcon implements Icon, UIResource, Serializable
  308. {
  309. private Color focus = UIManager.getColor("windowBorder");
  310. private Color shadow = UIManager.getColor("controlShadow");
  311. private Color highlight = UIManager.getColor("controlHighlight");
  312. public void paintIcon(Component c, Graphics g, int x, int y) {
  313. AbstractButton b = (AbstractButton) c;
  314. ButtonModel model = b.getModel();
  315. int w = getIconWidth();
  316. int h = getIconHeight();
  317. Color oldColor = g.getColor();
  318. // REMIND: this is a mess
  319. if (model.isSelected()){
  320. if( MotifGraphicsUtils.isLeftToRight(c) ){
  321. g.setColor(shadow);
  322. g.fillRect(x+1,y+1,2,h);
  323. g.drawLine(x+4,y+2,x+4,y+2);
  324. g.drawLine(x+6,y+3,x+6,y+3);
  325. g.drawLine(x+8,y+4,x+8,y+5);
  326. g.setColor(focus);
  327. g.fillRect(x+2,y+2,2,h-2);
  328. g.fillRect(x+4,y+3,2,h-4);
  329. g.fillRect(x+6,y+4,2,h-6);
  330. g.setColor(highlight);
  331. g.drawLine(x+2,y+h,x+2,y+h);
  332. g.drawLine(x+4,y+h-1,x+4,y+h-1);
  333. g.drawLine(x+6,y+h-2,x+6,y+h-2);
  334. g.drawLine(x+8,y+h-4,x+8,y+h-3);
  335. } else {
  336. g.setColor(shadow);
  337. g.fillRect(x+h-3,y+1,2,h);
  338. g.drawLine(x+h-5,y+2,x+h-5,y+2);
  339. g.drawLine(x+h-7,y+3,x+h-7,y+3);
  340. g.drawLine(x+h-9,y+4,x+h-9,y+5);
  341. g.setColor(focus);
  342. g.fillRect(x+h-4,y+2,2,h-2);
  343. g.fillRect(x+h-6,y+3,2,h-4);
  344. g.fillRect(x+h-8,y+4,2,h-6);
  345. g.setColor(highlight);
  346. g.drawLine(x+h-3,y+h,x+h-3,y+h);
  347. g.drawLine(x+h-5,y+h-1,x+h-5,y+h-1);
  348. g.drawLine(x+h-7,y+h-2,x+h-7,y+h-2);
  349. g.drawLine(x+h-9,y+h-4,x+h-9,y+h-3);
  350. }
  351. } else {
  352. if( MotifGraphicsUtils.isLeftToRight(c) ){
  353. g.setColor(highlight);
  354. g.drawLine(x+1,y+1,x+1,y+h);
  355. g.drawLine(x+2,y+1,x+2,y+h-2);
  356. g.fillRect(x+3,y+2,2,2);
  357. g.fillRect(x+5,y+3,2,2);
  358. g.fillRect(x+7,y+4,2,2);
  359. g.setColor(shadow);
  360. g.drawLine(x+2,y+h-1,x+2,y+h);
  361. g.fillRect(x+3,y+h-2,2,2);
  362. g.fillRect(x+5,y+h-3,2,2);
  363. g.fillRect(x+7,y+h-4,2,2);
  364. g.setColor(oldColor);
  365. } else {
  366. g.setColor(highlight);
  367. g.drawLine(x+h-2,y+1,x+h-2,y+h);
  368. g.drawLine(x+h-3,y+1,x+h-3,y+h-2);
  369. g.fillRect(x+h-5,y+2,2,2);
  370. g.fillRect(x+h-7,y+3,2,2);
  371. g.fillRect(x+h-9,y+4,2,2);
  372. g.setColor(shadow);
  373. g.drawLine(x+h-3,y+h-1,x+h-3,y+h);
  374. g.fillRect(x+h-5,y+h-2,2,2);
  375. g.fillRect(x+h-7,y+h-3,2,2);
  376. g.fillRect(x+h-9,y+h-4,2,2);
  377. g.setColor(oldColor);
  378. }
  379. }
  380. }
  381. public int getIconWidth() { return 10; }
  382. public int getIconHeight() { return 10; }
  383. } // End class MenuArrowIcon
  384. }