001: /*
002: * @(#)PPCCheckboxPeer.java 1.3 02/12/09
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package sun.awt.pocketpc;
028:
029: import java.awt.*;
030: import java.awt.event.*;
031: import sun.awt.peer.*;
032:
033: class PPCCheckboxPeer extends PPCComponentPeer implements CheckboxPeer {
034: private static native void initIDs();
035:
036: static {
037: initIDs();
038: }
039:
040: /** Creates a new PPCCheckboxPeer. */
041:
042: PPCCheckboxPeer(Checkbox target) {
043: super (target);
044: //perhaps not necessary?
045: //setState(target.getState());
046: //setLabel(target.getLabel());
047: }
048:
049: native void create(PPCComponentPeer parent);
050:
051: public boolean isFocusTraversable() {
052: return true;
053: }
054:
055: public native void setState(boolean state);
056:
057: public native void setCheckboxGroup(CheckboxGroup g);
058:
059: private native void setLabelNative(String label);
060:
061: public void setLabel(String label) {
062: setLabelNative(label);
063: }
064:
065: // not in pjava - may not be necessary
066: private void postItemEvent(boolean state) {
067: Checkbox cb = (Checkbox) target;
068: CheckboxGroup cbg = cb.getCheckboxGroup();
069: /* Bugid 4039594. If this is the current Checkbox in a CheckboxGroup,
070: * then return to prevent deselection. Otherwise, it's logical state
071: * will be turned off, but it will appear on.
072: */
073: if ((cbg != null) && (cbg.getSelectedCheckbox() == cb)
074: && cb.getState()) {
075: cb.setState(true);
076: return;
077: }
078: cb.setState(state);
079: // For some reason Java 2 only sends selected events is the checkbox
080: // is in a chekbox group. I don't know why this is but implemented
081: // this anyway to be consistent.
082:
083: if (cbg == null || state)
084: PPCToolkit.postEvent(new ItemEvent(cb,
085: ItemEvent.ITEM_STATE_CHANGED, cb.getLabel(),
086: state ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
087: }
088:
089: // pjava code
090: public Dimension getMinimumSize() {
091: String lbl = ((Checkbox) target).getLabel();
092: if (lbl != null) {
093: FontMetrics fm = getFontMetrics(((Checkbox) target)
094: .getFont());
095: return new Dimension(30 + fm.stringWidth(lbl), Math.max(fm
096: .getHeight() + 8, 15));
097: }
098: return new Dimension(20, 15);
099: }
100:
101: // pjava code
102: void initialize() {
103: Checkbox t = (Checkbox) target;
104: setState(t.getState());
105: setCheckboxGroup(t.getCheckboxGroup());
106:
107: Color bg = ((Component) target).getBackground();
108: if (bg != null) {
109: setBackground(bg);
110: }
111:
112: super .initialize();
113: }
114:
115: // pjava code
116: void clearRectBeforePaint(Graphics g, Rectangle r) {
117: // Overload to do nothing for native components
118: }
119:
120: // pjava native callbacks
121: void handleAction(boolean state) {
122: Checkbox cb = (Checkbox) this .target;
123: cb.setState(state);
124: PPCToolkit.postEvent(new ItemEvent(cb,
125: ItemEvent.ITEM_STATE_CHANGED, cb.getLabel(),
126: state ? ItemEvent.SELECTED : ItemEvent.DESELECTED));
127:
128: }
129:
130: // pjava code
131: public Dimension minimumSize() {
132: return getMinimumSize();
133: }
134: }
|