Visual Color Imbalance Detector: Reconstructed project directories and files
[VistaCID.git] / org / tjworld / components / BasicIconCheckBoxUI.java
1 /*\r
2         * BasicIconCheckBoxUI.java\r
3  *\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
6         *\r
7         * $History: BasicIconCheckBoxUI.java $\r
8  * \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
14  * \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
19  */\r
20 \r
21 package org.tjworld.components;\r
22 \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
34 \r
35 /**\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
38         *\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
42         *\r
43         * @see JIconCheckBox\r
44         * @see JCheckBoxMenuItem\r
45         * @see BasicCheckBoxMenuItemUI\r
46         * @see BasicIconToggleButtonUI\r
47         * @see BasicIconButtonListener\r
48         *\r
49         * @author  TJ\r
50  * @version 1.0\r
51  */\r
52 public class BasicIconCheckBoxUI extends BasicIconToggleButtonUI {\r
53         \r
54         /** Tracks status of UI Manager Look & Feel registration */\r
55  private boolean defaults_initialized = false;\r
56 \r
57         /** One global instance of the UI Delegate */\r
58         private final static BasicIconCheckBoxUI iconCheckBoxUI = new BasicIconCheckBoxUI();\r
59 \r
60  /** Masquarades as a CheckBox so it can fit in with standard Look & Feels */\r
61         private final static String propertyPrefix = "CheckBox.";\r
62 \r
63         /** Creates new BasicIconCheckBoxUI */\r
64  public BasicIconCheckBoxUI() {\r
65  }\r
66 \r
67  /** Creates a new BasicIconCheckBox \r
68                 * @returns The single static instance of a BasicIconCheckBox\r
69                 */\r
70  public static ComponentUI createUI(JComponent b) {\r
71   return iconCheckBoxUI;\r
72  }\r
73 \r
74  /** Uses the CheckBox properties */\r
75  protected String getPropertyPrefix() {\r
76   return propertyPrefix;\r
77  }\r
78         \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
83                 */\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
89   }\r
90  }\r
91 \r
92         /** Uninstall from L&F\r
93                 * @param b The CheckBox\r
94                 */\r
95  protected void uninstallDefaults(JIconCheckBox b){\r
96   super.uninstallDefaults(b);\r
97   defaults_initialized = false;\r
98  }\r
99 \r
100         /**\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
104                 *\r
105                 * @returns A JComponent subclass being the analog of the called class\r
106                 */\r
107  public JComponent createCompatibleLandFComponent(JIconToggleButton iconCB) {\r
108                 JCheckBox cb = new JCheckBox(); // create the analog\r
109 \r
110                 ButtonModel my = iconCB.getModel();\r
111                 ButtonModel cbBM = cb.getModel(); // synchronize it's model\r
112 \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
117                 \r
118                 cb.setBorder(iconCB.getBorder());\r
119                 cb.setMargin(iconCB.getMargin());\r
120                 cb.setSize(iconCB.getSize());\r
121                 \r
122                 return cb; // analog of JIconRadioButton\r
123         }\r
124                 \r
125         /**\r
126                 * Paint the State (selected) Icon\r
127                 *\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
131                 */\r
132  protected synchronized void paintStateIcon(Graphics g, JIconToggleButton b, Icon state, Rectangle iconRect) {\r
133   Icon icon = state; // the icon to paint\r
134                 \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
137                         * methods.\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
141                  */\r
142                 \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
145                 \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
149                 }\r
150 \r
151                 if(ab != null) // only paint it if a component is available\r
152                  icon.paintIcon(ab, g, iconRect.x, iconRect.y); \r
153 \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
157  }\r
158 }