KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > fakepeer > FakeCheckboxPeer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.form.fakepeer;
22
23 import java.awt.*;
24
25 /**
26  *
27  * @author Tran Duc Trung
28  */

29 class FakeCheckboxPeer extends FakeComponentPeer
30 {
31     FakeCheckboxPeer(Checkbox target) {
32         super(target);
33     }
34
35     Component createDelegate() {
36         return new Delegate();
37     }
38
39     public void setState(boolean state) {
40     }
41
42     public void setCheckboxGroup(CheckboxGroup g) {
43     }
44
45     public void setLabel(String JavaDoc label) {
46     }
47
48     //
49
//
50
//
51

52     private class Delegate extends Component
53     {
54         Delegate() {
55 // this.setBackground(SystemColor.control);
56
this.setForeground(SystemColor.controlText);
57         }
58         
59         public void paint(Graphics g) {
60             Checkbox target = (Checkbox) _target;
61             Dimension sz = target.getSize();
62             int bx = 0;
63             int by = (sz.height - BOX_H) / 2;
64
65             g.setColor(target.getBackground());
66             g.fillRect(0, 0, sz.width, sz.height);
67
68             String JavaDoc label = target.getLabel();
69             
70             if (label != null) {
71                 g.setFont(target.getFont());
72
73                 FontMetrics fm = g.getFontMetrics();
74                 int h = fm.getHeight() - fm.getDescent(),
75                     x = 18,
76                     y = (sz.height - h) / 2 + h - 2;
77             
78                 if (target.isEnabled()) {
79                     g.setColor(target.getForeground());
80                 }
81                 else {
82                     g.setColor(SystemColor.controlLtHighlight);
83                     g.drawString(label, x+1, y+1);
84                     g.setColor(SystemColor.controlShadow);
85                 }
86
87                 g.drawString(label, x, y);
88                 by = y - h + 2;
89             }
90
91             // the check-box (Windows like - lowered, white background)
92

93             if (target.getCheckboxGroup() == null) {
94                 g.setColor(SystemColor.window);
95                 FakePeerUtils.drawLoweredBox(g,bx,by,BOX_W,BOX_H);
96
97                 if (target.getState()) { // checkbox is checked
98
g.setColor(SystemColor.controlText);
99                     for (int i=1; i < drCheckPosX_W.length; i++)
100                         g.drawLine(drCheckPosX_W[i-1]+bx,drCheckPosY_W[i-1]+by,
101                                    drCheckPosX_W[i]+bx,drCheckPosY_W[i]+by);
102                 }
103             }
104             else { // radio button
105
if (radButtIcon1 == null || radButtIcon2 == null)
106                     initRBImages();
107                 g.drawImage(target.getState() ? radButtIcon2:radButtIcon1, bx+1, by+1, this);
108             }
109         }
110
111         public Dimension getMinimumSize() {
112             String JavaDoc label = ((Checkbox)_target).getLabel();
113
114             FontMetrics fm = this.getFontMetrics(this.getFont());
115             int w = fm.stringWidth(label);
116             int h = fm.getHeight();
117
118             return new Dimension(w + 6+BOX_W+4, h + 4);
119         }
120
121         void initRBImages() {
122             Toolkit toolkit = Toolkit.getDefaultToolkit();
123             java.net.URL JavaDoc source = this.getClass().getResource("radbutt1.gif");
124             radButtIcon1 = toolkit.getImage(source);
125             source = this.getClass().getResource("radbutt2.gif");
126             radButtIcon2 = toolkit.getImage(source);
127
128             MediaTracker mt = new MediaTracker(this);
129             mt.addImage(radButtIcon1,0);
130             mt.addImage(radButtIcon2,1);
131             try {
132                 mt.waitForAll();
133             } catch (java.lang.InterruptedException JavaDoc e) {
134             }
135         }
136     }
137
138     private static final int BOX_W = 16, BOX_H = 16;
139     private static final int[] drCheckPosX_W = { 4,6,10,10,6,4,4,6,10 };
140     private static final int[] drCheckPosY_W = { 6,8,4,5,9,7,8,10,6 };
141
142     private static Image radButtIcon1 = null;
143     private static Image radButtIcon2 = null;
144 }
145
Popular Tags