| 1 |
/* |
| 2 |
* @(#)MenuCheckIcon.java 4/29/2009 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2009 JIDE Software Inc. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.jidesoft.icons; |
| 9 |
|
| 10 |
|
| 11 |
import javax.swing.*; |
| 12 |
import javax.swing.plaf.UIResource; |
| 13 |
import java.io.Serializable; |
| 14 |
import java.awt.*; |
| 15 |
|
| 16 |
/** |
| 17 |
* Icon wrapper class for check icons. The only thing this class does is to check the selection before it really paints |
| 18 |
* its wrapped icon. The reason we provide this class is that similar mechanism resides in Swing and we cannot override it. |
| 19 |
* So if we just update the UI with normal icons, the icon could be displayed unexpectedly. With this icon wrapper class, |
| 20 |
* you can get exactly the same behavior with Swing default icons. |
| 21 |
*/ |
| 22 |
public class MenuCheckIcon implements Icon, UIResource, Serializable { |
| 23 |
|
| 24 |
private ImageIcon _icon; |
| 25 |
private static final long serialVersionUID = -6303936713472505092L; |
| 26 |
|
| 27 |
public MenuCheckIcon(ImageIcon icon) { |
| 28 |
if (icon == null) { |
| 29 |
throw new IllegalArgumentException("The icon should not be null."); |
| 30 |
} |
| 31 |
_icon = icon; |
| 32 |
} |
| 33 |
|
| 34 |
public int getIconHeight() { |
| 35 |
return getIcon().getIconHeight(); |
| 36 |
} |
| 37 |
|
| 38 |
public int getIconWidth() { |
| 39 |
return getIcon().getIconWidth(); |
| 40 |
} |
| 41 |
|
| 42 |
public void paintIcon(Component c, Graphics g, int x, int y) { |
| 43 |
Icon icon = getIcon(); |
| 44 |
if (c instanceof AbstractButton) { |
| 45 |
AbstractButton b = (AbstractButton) c; |
| 46 |
if (b.isSelected()) { |
| 47 |
icon.paintIcon(c, g, x, y); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
private Icon getIcon() { |
| 53 |
return _icon; |
| 54 |
} |
| 55 |
} |