| 1 |
/* |
| 2 |
* @(#)CheckBoxIcon.java 3/6/2012 |
| 3 |
* |
| 4 |
* Copyright 2002 - 2012 JIDE Software Inc. All rights reserved. |
| 5 |
*/ |
| 6 |
|
| 7 |
package com.jidesoft.icons; |
| 8 |
|
| 9 |
import com.jidesoft.swing.TristateCheckBox; |
| 10 |
|
| 11 |
import javax.swing.*; |
| 12 |
import java.awt.*; |
| 13 |
import java.awt.image.BufferedImage; |
| 14 |
|
| 15 |
/** |
| 16 |
* The icon to paint CheckBox icon safely with any L&F. |
| 17 |
* |
| 18 |
* @since 3.3.7 |
| 19 |
*/ |
| 20 |
public class CheckBoxIcon implements Icon { |
| 21 |
private TristateCheckBox _checkBox; |
| 22 |
private Icon _checkBoxIcon; |
| 23 |
private BufferedImage _uncheckImage; |
| 24 |
private BufferedImage _checkedImage; |
| 25 |
private BufferedImage _mixedImage; |
| 26 |
private int _state; |
| 27 |
|
| 28 |
/** |
| 29 |
* The constructor. |
| 30 |
*/ |
| 31 |
public CheckBoxIcon() { |
| 32 |
} |
| 33 |
|
| 34 |
private void validateCheckBox() { |
| 35 |
if (_checkBox == null || _checkBoxIcon != UIManager.getDefaults().getIcon("CheckBox.icon")) { |
| 36 |
_checkBox = new TristateCheckBox(); |
| 37 |
_checkBox.setOpaque(false); |
| 38 |
_checkBoxIcon = UIManager.getDefaults().getIcon("CheckBox.icon"); |
| 39 |
_uncheckImage = null; |
| 40 |
_checkedImage = null; |
| 41 |
_mixedImage = null; |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public void paintIcon(Component c, Graphics g, int x, int y) { |
| 47 |
validateCheckBox(); |
| 48 |
if (_uncheckImage == null) { |
| 49 |
GraphicsConfiguration graphicsConfiguration = ((Graphics2D) g).getDeviceConfiguration(); |
| 50 |
_uncheckImage = graphicsConfiguration.createCompatibleImage(getIconWidth(), getIconHeight(), Transparency.TRANSLUCENT); |
| 51 |
_checkBox.setState(TristateCheckBox.STATE_UNSELECTED); |
| 52 |
_checkBox.setBounds(0, 0, getIconWidth(), getIconHeight()); |
| 53 |
_checkBox.paint(_uncheckImage.getGraphics()); |
| 54 |
|
| 55 |
_checkedImage = graphicsConfiguration.createCompatibleImage(getIconWidth(), getIconHeight(), Transparency.TRANSLUCENT); |
| 56 |
_checkBox.setState(TristateCheckBox.STATE_SELECTED); |
| 57 |
_checkBox.setBounds(0, 0, getIconWidth(), getIconHeight()); |
| 58 |
_checkBox.paint(_checkedImage.getGraphics()); |
| 59 |
|
| 60 |
_mixedImage = graphicsConfiguration.createCompatibleImage(getIconWidth(), getIconHeight(), Transparency.TRANSLUCENT); |
| 61 |
_checkBox.setState(TristateCheckBox.STATE_MIXED); |
| 62 |
_checkBox.setBounds(0, 0, getIconWidth(), getIconHeight()); |
| 63 |
_checkBox.paint(_mixedImage.getGraphics()); |
| 64 |
} |
| 65 |
if (getState() == TristateCheckBox.STATE_SELECTED) { |
| 66 |
g.drawImage(_checkedImage, x, y, null); |
| 67 |
} |
| 68 |
else if (getState() == TristateCheckBox.STATE_UNSELECTED) { |
| 69 |
g.drawImage(_uncheckImage, x, y, null); |
| 70 |
} |
| 71 |
else if (getState() == TristateCheckBox.STATE_MIXED) { |
| 72 |
g.drawImage(_mixedImage, x, y, null); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public int getIconWidth() { |
| 78 |
validateCheckBox(); |
| 79 |
return _checkBox.getPreferredSize().width; |
| 80 |
} |
| 81 |
|
| 82 |
@Override |
| 83 |
public int getIconHeight() { |
| 84 |
validateCheckBox(); |
| 85 |
return _checkBox.getPreferredSize().height; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Sets the state of the icon to paint. |
| 90 |
* <p/> |
| 91 |
* The state could be {@link TristateCheckBox#STATE_SELECTED}, {@link TristateCheckBox#STATE_UNSELECTED} or |
| 92 |
* {@link TristateCheckBox#STATE_MIXED}. |
| 93 |
* |
| 94 |
* @param state the state |
| 95 |
*/ |
| 96 |
public void setState(int state) { |
| 97 |
_state = state; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Gets the state of the icon to paint. |
| 102 |
* |
| 103 |
* @return the state. |
| 104 |
* @see #setState(int) |
| 105 |
*/ |
| 106 |
public int getState() { |
| 107 |
return _state; |
| 108 |
} |
| 109 |
} |