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.JCheckBox;
24  
25  /**
26   * Overriding JCheckBox so the background is painted correctly
27   * based on the color set in the widget.
28   * If the size is bigger than the image that is being shown as the
29   * checkbox, it otherwise would paint the whole area in the background
30   * color.
31   *
32   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
33   * @version $Id: NyxJCheckBox.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
34   */
35  public class NyxJCheckBox extends JCheckBox {
36  
37      /**
38       * the real background color
39       */
40      private Color realBg;
41      /**
42       * return the real background ?
43       */
44      private boolean returnRealBackground;
45  
46      /**
47       * the main contructor.
48       *
49       */
50      public NyxJCheckBox() {
51          super();
52      }
53  
54      /**
55       * Set the real background for the icon.
56       * @param color the real background color
57       */
58      public void setRealBackground(Color color) {
59          this.realBg = color;
60      }
61  
62      /**
63       *
64       * @return the real background color for the icon
65       */
66      public Color getRealBackground() {
67          return this.realBg;
68      }
69  
70      /**
71       * Embed the icon in an iconstub
72       * @see javax.swing.AbstractButton#setIcon(javax.swing.Icon)
73       */
74      public void setIcon(Icon defaultIcon) {
75          super.setIcon(new IconStub(defaultIcon));
76      }
77  
78      /**
79       * embed the icon in an icon stub
80       * @see javax.swing.AbstractButton#setSelectedIcon(javax.swing.Icon)
81       */
82      public void setSelectedIcon(Icon selectedIcon) {
83          super.setSelectedIcon(new IconStub(selectedIcon));
84      }
85  
86      protected boolean focusTraversable = true;
87      
88      public void setFocusTraversable(boolean b) {
89          this.focusTraversable = b;
90      }
91      
92      /**
93       * @see java.awt.Component#isFocusTraversable()
94       */
95      public boolean isFocusTraversable() {
96          return focusTraversable;
97      }
98  
99  
100     /**
101      * A stub to surround an icon in, so they get painted
102      * with the correct background set in the widget
103      */
104     public class IconStub implements Icon {
105         /**
106          * the icon
107          */
108         private Icon icon;
109 
110         /**
111          * the icon stub
112          * @param icon the icon
113          */
114         public IconStub(Icon icon) {
115             this.icon = icon;
116         }
117 
118         /**
119          * @see javax.swing.Icon#getIconHeight()
120          */
121         public int getIconHeight() {
122             if (icon == null) {
123                 return 0;
124             }
125             return icon.getIconHeight();
126         }
127 
128         /**
129          * @see javax.swing.Icon#getIconWidth()
130          */
131         public int getIconWidth() {
132             if (icon == null) {
133                 return 0;
134             }
135             return icon.getIconWidth();
136         }
137 
138         /**
139          * Fill a rectangle of the size of the icon with the real
140          * background color.
141          * @see javax.swing.Icon#paintIcon(java.awt.Component, java.awt.Graphics, int, int)
142          */
143         public void paintIcon(Component c, Graphics g, int x, int y) {
144             if (NyxJCheckBox.this.getRealBackground() != null) {
145                 g.setColor(getRealBackground());
146             }
147             g.fillRect(x, y, getIconWidth(), getIconHeight());
148             icon.paintIcon(c, g, x, y);
149         }
150     }
151 }