2 * BasicIconCheckBoxUI.java
\r
4 * Created on 02 November 2001, 01:42
\r
5 * $Header: /VisTA/CID/Components/BasicIconCheckBoxUI.java 2 2/11/01 2:18 Tj $
\r
7 * $History: BasicIconCheckBoxUI.java $
\r
9 * ***************** Version 2 *****************
\r
10 * User: Tj Date: 2/11/01 Time: 2:18
\r
11 * Updated in $/VisTA/CID/Components
\r
12 * Created paintIcon() to override same method in BasicIconToggleButtonUI,
\r
13 * to deal with some L&F's casting the component to a JCheckBox
\r
15 * ***************** Version 1 *****************
\r
16 * User: Tj Date: 2/11/01 Time: 1:47
\r
17 * Created in $/VisTA/CID/Components
\r
18 * Created from BasicRadioButtonUI template
\r
21 package org.tjworld.components;
\r
23 import java.awt.Graphics;
\r
24 import java.awt.Rectangle;
\r
25 import javax.swing.ButtonModel;
\r
26 import javax.swing.Icon;
\r
27 import javax.swing.JComponent;
\r
28 import javax.swing.JCheckBox;
\r
29 import javax.swing.AbstractButton;
\r
30 import javax.swing.DebugGraphics;
\r
31 import javax.swing.plaf.ComponentUI;
\r
32 import javax.swing.AbstractButton;
\r
33 import javax.swing.UIManager;
\r
36 * Draws a Check Box that incorporates a State Icon and a User Icon to visually indicate functionality
\r
37 * in the same way that BasicCheckBoxMenuItemUI does for JCheckBoxMenuItem components.
\r
39 * Except for overriding and enhancing the BasicButtonUI.installDefaults() method to get the L&F's
\r
40 * CheckBox-specific settings, this class is a wrapper for all the functionality of
\r
41 * BasicIconToggleButtonUI.
\r
43 * @see JIconCheckBox
\r
44 * @see JCheckBoxMenuItem
\r
45 * @see BasicCheckBoxMenuItemUI
\r
46 * @see BasicIconToggleButtonUI
\r
47 * @see BasicIconButtonListener
\r
52 public class BasicIconCheckBoxUI extends BasicIconToggleButtonUI {
\r
54 /** Tracks status of UI Manager Look & Feel registration */
\r
55 private boolean defaults_initialized = false;
\r
57 /** One global instance of the UI Delegate */
\r
58 private final static BasicIconCheckBoxUI iconCheckBoxUI = new BasicIconCheckBoxUI();
\r
60 /** Masquarades as a CheckBox so it can fit in with standard Look & Feels */
\r
61 private final static String propertyPrefix = "CheckBox.";
\r
63 /** Creates new BasicIconCheckBoxUI */
\r
64 public BasicIconCheckBoxUI() {
\r
67 /** Creates a new BasicIconCheckBox
\r
68 * @returns The single static instance of a BasicIconCheckBox
\r
70 public static ComponentUI createUI(JComponent b) {
\r
71 return iconCheckBoxUI;
\r
74 /** Uses the CheckBox properties */
\r
75 protected String getPropertyPrefix() {
\r
76 return propertyPrefix;
\r
79 /** Install into L&F
\r
80 * gets a reference to the L&F's (usually) dynamic factory-Icon that paints itself according to
\r
81 * the state of the (JIcon)CheckBox it is associated with.
\r
82 * @param b The Check Box
\r
84 protected void installDefaults(AbstractButton b){
\r
85 super.installDefaults(b);
\r
86 if(!defaults_initialized) {
\r
87 stateIcon = UIManager.getIcon(getPropertyPrefix() + "icon");
\r
88 defaults_initialized = true;
\r
92 /** Uninstall from L&F
\r
93 * @param b The CheckBox
\r
95 protected void uninstallDefaults(JIconCheckBox b){
\r
96 super.uninstallDefaults(b);
\r
97 defaults_initialized = false;
\r
101 * Creates a standard Java component that is an analog of the specialist JICon... components,
\r
102 * such that the paint() method can then implement the correct L&F paintButtonPressed() method
\r
103 * Should be overridden in subclasses to return the correct analog
\r
105 * @returns A JComponent subclass being the analog of the called class
\r
107 public JComponent createCompatibleLandFComponent(JIconToggleButton iconCB) {
\r
108 JCheckBox cb = new JCheckBox(); // create the analog
\r
110 ButtonModel my = iconCB.getModel();
\r
111 ButtonModel cbBM = cb.getModel(); // synchronize it's model
\r
113 cbBM.setArmed(my.isArmed());
\r
114 cbBM.setPressed(my.isPressed());
\r
115 cbBM.setSelected(my.isSelected());
\r
116 cbBM.setRollover(my.isRollover());
\r
118 cb.setBorder(iconCB.getBorder());
\r
119 cb.setMargin(iconCB.getMargin());
\r
120 cb.setSize(iconCB.getSize());
\r
122 return cb; // analog of JIconRadioButton
\r
126 * Paint the State (selected) Icon
\r
128 * @param g The Graphics context to draw into
\r
129 * @param b The button that requires the Icon to be drawn
\r
130 * @param iconRect The bounds within in which to paint the Icon
\r
132 protected synchronized void paintStateIcon(Graphics g, JIconToggleButton b, Icon state, Rectangle iconRect) {
\r
133 Icon icon = state; // the icon to paint
\r
135 /* remember that L&Fs might be drawing this rather than BITBLTing pixel arrays (GIF/JPGs etc.)
\r
136 * also beware that the button is converted to a type that the L&F's can recast to other types in their Icon paint
\r
138 * Metal L&F (Java 1.4 beta 2) will throw an exception because it casts to a JCheckBox when the JIconToggleButton
\r
139 * is passed without conversion.
\r
140 * It could get away with casting to an AbstractButton and save my woes!
\r
143 // need to fool some L&F's into thinking the Component it's painting for is a descendent of java.awt.Component
\r
144 AbstractButton ab = null;
\r
146 if(b instanceof JIconCheckBox) { // Metal L&F casts the component to a JCheckBox
\r
147 JCheckBox c = new JCheckBox(b.getText(), icon, b.isSelected()); // create a stand-in to mimic this button
\r
148 ab = (AbstractButton) c;
\r
151 if(ab != null) // only paint it if a component is available
\r
152 icon.paintIcon(ab, g, iconRect.x, iconRect.y);
\r
154 // graphics debugging info
\r
155 if((b.getDebugGraphicsOptions() & DebugGraphics.LOG_OPTION) == DebugGraphics.LOG_OPTION)
\r
156 System.out.println("Painting State Icon at " + iconRect);
\r