2 * BasicIconRadioButtonUI.java
\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
7 * $History: BasicIconRadioButtonUI.java $
\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
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
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
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
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
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
41 package org.tjworld.components;
\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
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
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
63 * @see JIconRadioButton
\r
64 * @see JRadioButtonMenuItem
\r
65 * @see BasicRadioButtonMenuItemUI
\r
66 * @see BasicIconToggleButtonUI
\r
67 * @see BasicIconButtonListener
\r
72 public class BasicIconRadioButtonUI extends BasicIconToggleButtonUI {
\r
74 /** Tracks status of UI Manager Look & Feel registration */
\r
75 private boolean defaults_initialized = false;
\r
77 /** One global instance of the UI Delegate */
\r
78 private final static BasicIconRadioButtonUI iconRadioButtonUI = new BasicIconRadioButtonUI();
\r
80 /** Masquarades as a RadioButton so it can fit in with standard Look & Feels */
\r
81 private final static String propertyPrefix = "RadioButton.";
\r
83 /** Creates new BasicIconRadioButtonUI */
\r
84 public BasicIconRadioButtonUI() {
\r
87 /** Creates a new BasicIconRadioButton
\r
88 * @returns The single static instance of a BasicIconRadioButton
\r
90 public static ComponentUI createUI(JComponent b) {
\r
91 return iconRadioButtonUI;
\r
94 /** Uses the RadioButton properties */
\r
95 protected String getPropertyPrefix() {
\r
96 return propertyPrefix;
\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
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
112 /** Uninstall from L&F
\r
113 * @param b The RadioButton
\r
115 protected void uninstallDefaults(JIconRadioButton b){
\r
116 super.uninstallDefaults(b);
\r
117 defaults_initialized = false;
\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
125 * @returns A JComponent subclass being the analog of the called class
\r
127 public JComponent createCompatibleLandFComponent(JIconToggleButton iconRB) {
\r
128 JRadioButton rb = new JRadioButton(); // create the analog
\r
130 ButtonModel my = iconRB.getModel();
\r
131 ButtonModel rbBM = rb.getModel(); // synchronize it's model
\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
138 rb.setBorder(iconRB.getBorder());
\r
139 rb.setMargin(iconRB.getMargin());
\r
140 rb.setSize(iconRB.getSize());
\r
142 return rb; // analog of JIconRadioButton
\r
146 * Paint the State (selected) Icon
\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
152 protected synchronized void paintStateIcon(Graphics g, JIconToggleButton b, Icon state, Rectangle iconRect) {
\r
153 Icon icon = state; // the icon to paint
\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
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
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
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
171 if(ab != null) // only paint it if a component is available
\r
172 icon.paintIcon(ab, g, iconRect.x, iconRect.y);
\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