View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.extensions;
17  
18  import java.awt.Color;
19  import java.awt.Component;
20  import java.awt.Graphics;
21  
22  import javax.swing.Icon;
23  import javax.swing.JRadioButton;
24  
25  /**
26   *
27   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
28   * @version $Id: NyxJRadioButton.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
29   */
30  public class NyxJRadioButton extends JRadioButton {
31  
32      /**
33       * The real background
34       */
35      private Color realBg;
36      /**
37       * should we return the real background ?
38       */
39      private boolean returnRealBackground;
40  
41      /**
42       *
43       */
44      public NyxJRadioButton() {
45          super();
46      }
47  
48      /**
49       * Set the real background for the icon.
50       * @param color the real background color
51       */
52      public void setRealBackground(Color color) {
53          this.realBg = color;
54      }
55  
56      /**
57       *
58       * @return the real background color for the icon
59       */
60      public Color getRealBackground() {
61          return this.realBg;
62      }
63  
64      /**
65       * Embed the icon in an iconstub
66       * @see javax.swing.AbstractButton#setIcon(javax.swing.Icon)
67       */
68      public void setIcon(Icon defaultIcon) {
69          super.setIcon(new IconStub(defaultIcon));
70      }
71  
72      /**
73       * embed the icon in an icon stub
74       * @see javax.swing.AbstractButton#setSelectedIcon(javax.swing.Icon)
75       */
76      public void setSelectedIcon(Icon selectedIcon) {
77          super.setSelectedIcon(new IconStub(selectedIcon));
78      }
79  
80  
81      /**
82       * A stub to surround an icon in, so they get painted
83       * with the correct background set in the widget
84       */
85      public class IconStub implements Icon {
86          /**
87           * the icon
88           */
89          private Icon icon;
90  
91          /**
92           *
93           * @param icon the icon
94           */
95          public IconStub(Icon icon) {
96              this.icon = icon;
97          }
98  
99          /**
100          * @see javax.swing.Icon#getIconHeight()
101          */
102         public int getIconHeight() {
103             if (icon == null) {
104                 return 0;
105             }
106             return icon.getIconHeight();
107         }
108 
109         /**
110          * @see javax.swing.Icon#getIconWidth()
111          */
112         public int getIconWidth() {
113             if (icon == null) {
114                 return 0;
115             }
116             return icon.getIconWidth();
117         }
118 
119         /**
120          * Set the color of the component just for radiobutton..
121          * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
122          */
123         public void paintIcon(Component c, Graphics g, int x, int y) {
124             JRadioButton cc = new JRadioButton();
125             cc.setSelected(isSelected());
126             if (NyxJRadioButton.this.getRealBackground() != null) {
127                 cc.setBackground(getRealBackground());
128             } else {
129                 cc.setBackground(getBackground());
130             }
131             icon.paintIcon(cc, g, x, y);
132             cc = null;
133         }
134     }
135 
136 
137 }