Visual Color Imbalance Detector: Reconstructed project directories and files
[VistaCID.git] / org / tjworld / components / BasicIconRadioButtonUI.java
1 /*\r
2         * BasicIconRadioButtonUI.java\r
3  *\r
4         * Created on 31 October 2001, 22:21\r
5         * $Header: /VisTA/CID/Components/BasicIconRadioButtonUI.java 6     7/11/01 16:49 Tj $\r
6         *\r
7         * $History: BasicIconRadioButtonUI.java $\r
8  * \r
9  * *****************  Version 6  *****************\r
10  * User: Tj           Date: 7/11/01    Time: 16:49\r
11  * Updated in $/VisTA/CID/Components\r
12  * Verified propertyPrefix being read\r
13  * \r
14  * *****************  Version 5  *****************\r
15  * User: Tj           Date: 2/11/01    Time: 2:21\r
16  * Updated in $/VisTA/CID/Components\r
17  * Created paintStateIcon(...) override to deal with L&Fs casting to\r
18  * JRadioButton in their paintIcon(...) methods\r
19  * \r
20  * *****************  Version 4  *****************\r
21  * User: Tj           Date: 2/11/01    Time: 1:03\r
22  * Updated in $/VisTA/CID/Components\r
23  * Tested OK, removed console debug messages\r
24  * \r
25  * *****************  Version 3  *****************\r
26  * User: Tj           Date: 1/11/01    Time: 22:53\r
27  * Updated in $/VisTA/CID/Components\r
28  * Ready for Bean Testing\r
29  * \r
30  * *****************  Version 2  *****************\r
31  * User: Tj           Date: 1/11/01    Time: 0:46\r
32  * Updated in $/VisTA/CID/Components\r
33  * Added install/uninstall L&F methods\r
34  * \r
35  * *****************  Version 1  *****************\r
36  * User: Tj           Date: 31/10/01   Time: 22:43\r
37  * Created in $/VisTA/CID/Components\r
38  * Supporting UI deligate for for BasicIconRadioButton\r
39  */\r
40 \r
41 package org.tjworld.components;\r
42 \r
43 import java.awt.Graphics;\r
44 import java.awt.Rectangle;\r
45 import javax.swing.ButtonModel;\r
46 import javax.swing.Icon;\r
47 import javax.swing.JComponent;\r
48 import javax.swing.JRadioButton;\r
49 import javax.swing.AbstractButton;\r
50 import javax.swing.DebugGraphics;\r
51 import javax.swing.plaf.ComponentUI;\r
52 import javax.swing.AbstractButton;\r
53 import javax.swing.UIManager;\r
54 \r
55 /**\r
56  * Draws a Radio button that incorporates a State Icon and a User Icon to visually indicate functionality\r
57         * in the same way that BasicRadioButtonMenuItemUI does for JRadioButtonMenuItem components.\r
58         *\r
59         * Except for overriding and enhancing the BasicButtonUI.installDefaults() method to get the L&F's\r
60         * RadioButton-specific settings, this class is a wrapper for all the functionality of \r
61         * BasicIconToggleButtonUI.\r
62         *\r
63         * @see JIconRadioButton\r
64         * @see JRadioButtonMenuItem\r
65         * @see BasicRadioButtonMenuItemUI\r
66         * @see BasicIconToggleButtonUI\r
67         * @see BasicIconButtonListener\r
68         *\r
69         * @author  TJ\r
70  * @version 1.0\r
71  */\r
72 public class BasicIconRadioButtonUI extends BasicIconToggleButtonUI {\r
73         \r
74         /** Tracks status of UI Manager Look & Feel registration */\r
75  private boolean defaults_initialized = false;\r
76 \r
77         /** One global instance of the UI Delegate */\r
78         private final static BasicIconRadioButtonUI iconRadioButtonUI = new BasicIconRadioButtonUI();\r
79 \r
80  /** Masquarades as a RadioButton so it can fit in with standard Look & Feels */\r
81         private final static String propertyPrefix = "RadioButton.";\r
82 \r
83         /** Creates new BasicIconRadioButtonUI */\r
84  public BasicIconRadioButtonUI() {\r
85  }\r
86 \r
87  /** Creates a new BasicIconRadioButton \r
88                 * @returns The single static instance of a BasicIconRadioButton\r
89                 */\r
90  public static ComponentUI createUI(JComponent b) {\r
91   return iconRadioButtonUI;\r
92  }\r
93 \r
94  /** Uses the RadioButton properties */\r
95  protected String getPropertyPrefix() {\r
96   return propertyPrefix;\r
97  }\r
98         \r
99  /** Install into L&F\r
100                 * gets a reference to the L&F's (usually) dynamic factory-Icon that paints itself according to\r
101                 * the state of the (JIcon)RadioButton it is associated with.\r
102                 * @param b The Radio Button\r
103                 */\r
104  protected void installDefaults(AbstractButton b){\r
105   super.installDefaults(b);\r
106   if(!defaults_initialized) {\r
107    stateIcon = UIManager.getIcon(getPropertyPrefix() + "icon");\r
108    defaults_initialized = true;\r
109   }\r
110  }\r
111 \r
112         /** Uninstall from L&F\r
113                 * @param b The RadioButton\r
114                 */\r
115  protected void uninstallDefaults(JIconRadioButton b){\r
116   super.uninstallDefaults(b);\r
117   defaults_initialized = false;\r
118  }\r
119 \r
120         /**\r
121                 * Creates a standard Java component that is an analog of the specialist JICon... components,\r
122                 * such that the paint() method can then implement the correct L&F paintButtonPressed() method\r
123                 * Should be overridden in subclasses to return the correct analog\r
124                 *\r
125                 * @returns A JComponent subclass being the analog of the called class\r
126                 */\r
127  public JComponent createCompatibleLandFComponent(JIconToggleButton iconRB) {\r
128                 JRadioButton rb = new JRadioButton(); // create the analog\r
129 \r
130                 ButtonModel my = iconRB.getModel();\r
131                 ButtonModel rbBM = rb.getModel(); // synchronize it's model\r
132 \r
133                 rbBM.setArmed(my.isArmed());\r
134                 rbBM.setPressed(my.isPressed());\r
135                 rbBM.setSelected(my.isSelected());\r
136                 rbBM.setRollover(my.isRollover());\r
137                 \r
138                 rb.setBorder(iconRB.getBorder());\r
139                 rb.setMargin(iconRB.getMargin());\r
140                 rb.setSize(iconRB.getSize());\r
141                 \r
142                 return rb; // analog of JIconRadioButton\r
143         }\r
144                 \r
145         /**\r
146                 * Paint the State (selected) Icon\r
147                 *\r
148                 * @param g The Graphics context to draw into\r
149                 * @param b The button that requires the Icon to be drawn\r
150                 * @param iconRect The bounds within in which to paint the Icon\r
151                 */\r
152  protected synchronized void paintStateIcon(Graphics g, JIconToggleButton b, Icon state, Rectangle iconRect) {\r
153   Icon icon = state; // the icon to paint\r
154                 \r
155                 /* remember that L&Fs might be drawing this rather than BITBLTing pixel arrays (GIF/JPGs etc.)\r
156                         * 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
157                         * methods.\r
158                         * Metal L&F (Java 1.4 beta 2) will throw an exception because it casts to a JRadioButton when the JIconToggleButton\r
159                         * is passed without conversion.\r
160                         * It could get away with casting to an AbstractButton and save my woes!\r
161                  */\r
162                 \r
163   // need to fool some L&F's into thinking the Component it's painting for is a descendent of java.awt.Component \r
164                 AbstractButton ab = null;\r
165                 \r
166   if(b instanceof JIconRadioButton) { // Metal L&F for one casts the Component passed to paintIcon() to a JRadioButton\r
167                         JRadioButton c = new JRadioButton(b.getText(), icon, b.isSelected()); // so create a stand-in and mimic this button's current state\r
168                         ab = (AbstractButton) c; // and cast it back to the common base that is passed to paintIcon(...)\r
169                 }\r
170 \r
171                 if(ab != null) // only paint it if a component is available\r
172                  icon.paintIcon(ab, g, iconRect.x, iconRect.y); \r
173 \r
174                 // graphics debugging info\r
175                 if((b.getDebugGraphicsOptions() & DebugGraphics.LOG_OPTION) == DebugGraphics.LOG_OPTION)\r
176                         System.out.println("Painting State Icon at " + iconRect);\r
177  }\r
178 }