001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.fakepeer;
043:
044: import java.awt.*;
045:
046: /**
047: *
048: * @author Tran Duc Trung
049: */
050: class FakeCheckboxPeer extends FakeComponentPeer {
051: FakeCheckboxPeer(Checkbox target) {
052: super (target);
053: }
054:
055: Component createDelegate() {
056: return new Delegate();
057: }
058:
059: public void setState(boolean state) {
060: }
061:
062: public void setCheckboxGroup(CheckboxGroup g) {
063: }
064:
065: public void setLabel(String label) {
066: }
067:
068: //
069: //
070: //
071:
072: private class Delegate extends Component {
073: Delegate() {
074: // this.setBackground(SystemColor.control);
075: this .setForeground(SystemColor.controlText);
076: }
077:
078: @Override
079: public void paint(Graphics g) {
080: Checkbox target = (Checkbox) _target;
081: Dimension sz = target.getSize();
082: int bx = 0;
083: int by = (sz.height - BOX_H) / 2;
084:
085: g.setColor(target.getBackground());
086: g.fillRect(0, 0, sz.width, sz.height);
087:
088: String label = target.getLabel();
089:
090: if (label != null) {
091: g.setFont(target.getFont());
092:
093: FontMetrics fm = g.getFontMetrics();
094: int h = fm.getHeight() - fm.getDescent(), x = 18, y = (sz.height - h)
095: / 2 + h - 2;
096:
097: if (target.isEnabled()) {
098: g.setColor(target.getForeground());
099: } else {
100: g.setColor(SystemColor.controlLtHighlight);
101: g.drawString(label, x + 1, y + 1);
102: g.setColor(SystemColor.controlShadow);
103: }
104:
105: g.drawString(label, x, y);
106: by = y - h + 2;
107: }
108:
109: // the check-box (Windows like - lowered, white background)
110:
111: if (target.getCheckboxGroup() == null) {
112: g.setColor(SystemColor.window);
113: FakePeerUtils.drawLoweredBox(g, bx, by, BOX_W, BOX_H);
114:
115: if (target.getState()) { // checkbox is checked
116: g.setColor(SystemColor.controlText);
117: for (int i = 1; i < drCheckPosX_W.length; i++)
118: g.drawLine(drCheckPosX_W[i - 1] + bx,
119: drCheckPosY_W[i - 1] + by,
120: drCheckPosX_W[i] + bx, drCheckPosY_W[i]
121: + by);
122: }
123: } else { // radio button
124: if (radButtIcon1 == null || radButtIcon2 == null)
125: initRBImages();
126: g.drawImage(target.getState() ? radButtIcon2
127: : radButtIcon1, bx + 1, by + 1, this );
128: }
129: }
130:
131: @Override
132: public Dimension getMinimumSize() {
133: String label = ((Checkbox) _target).getLabel();
134:
135: FontMetrics fm = this .getFontMetrics(this .getFont());
136: int w = fm.stringWidth(label);
137: int h = fm.getHeight();
138:
139: return new Dimension(w + 6 + BOX_W + 4, h + 4);
140: }
141:
142: void initRBImages() {
143: Toolkit toolkit = Toolkit.getDefaultToolkit();
144: java.net.URL source = this .getClass().getResource(
145: "radbutt1.gif");
146: radButtIcon1 = toolkit.getImage(source);
147: source = this .getClass().getResource("radbutt2.gif");
148: radButtIcon2 = toolkit.getImage(source);
149:
150: MediaTracker mt = new MediaTracker(this );
151: mt.addImage(radButtIcon1, 0);
152: mt.addImage(radButtIcon2, 1);
153: try {
154: mt.waitForAll();
155: } catch (java.lang.InterruptedException e) {
156: }
157: }
158: }
159:
160: private static final int BOX_W = 16, BOX_H = 16;
161: private static final int[] drCheckPosX_W = { 4, 6, 10, 10, 6, 4, 4,
162: 6, 10 };
163: private static final int[] drCheckPosY_W = { 6, 8, 4, 5, 9, 7, 8,
164: 10, 6 };
165:
166: private static Image radButtIcon1 = null;
167: private static Image radButtIcon2 = null;
168: }
|