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