001: /*
002: * @(#)PPCChoicePeer.java 1.8 06/10/10
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: package sun.awt.pocketpc;
027:
028: import java.awt.*;
029: import sun.awt.peer.*;
030: import java.awt.event.ItemEvent;
031:
032: class PPCChoicePeer extends PPCComponentPeer implements ChoicePeer {
033:
034: private static native void initIDs();
035:
036: static {
037: initIDs();
038: }
039:
040: // PPCComponentPeer overrides
041:
042: public Dimension getMinimumSize() {
043: FontMetrics fm = getFontMetrics(((Choice) target).getFont());
044: Choice c = (Choice) target;
045: int w = 0;
046: for (int i = c.getItemCount(); i-- > 0;) {
047: w = Math.max(fm.stringWidth(c.getItem(i)), w);
048: }
049: int offset = PPCToolkit.getComboHeightOffset();
050: return new Dimension(28 + w, Math.max(fm.getHeight() + offset
051: + 6, 21 + offset));
052: }
053:
054: public boolean isFocusTraversable() {
055: return true;
056: }
057:
058: // ChoicePeer implementation
059:
060: public native void select(int index);
061:
062: public void add(String item, int index) {
063: addItem(item, index);
064: return;
065: }
066:
067: public native void remove(int index);
068:
069: void clearRectBeforePaint(Graphics g, Rectangle r) {
070: // Overload to do nothing for native components
071: return;
072: }
073:
074: /**
075: * DEPRECATED, but for now, called by add(String, int).
076: */
077: public native void addItem(String item, int index);
078:
079: public native void reshape(int x, int y, int width, int height);
080:
081: // Toolkit & peer internals
082:
083: PPCChoicePeer(Choice target) {
084: super (target);
085: }
086:
087: native void create(PPCComponentPeer parent);
088:
089: void initialize() {
090: Choice opt = (Choice) target;
091: int itemCount = opt.getItemCount();
092: for (int i = 0; i < itemCount; i++) {
093: add(opt.getItem(i), i);
094: }
095: if (itemCount > 0 && opt.getSelectedIndex() >= 0) {
096: select(opt.getSelectedIndex());
097: }
098: super .initialize();
099: return;
100: }
101:
102: // native callbacks
103:
104: void handleAction(int index) {
105: Choice c = (Choice) target;
106: c.select(index);
107: PPCToolkit.postEvent(new ItemEvent(c,
108: ItemEvent.ITEM_STATE_CHANGED, c.getItem(index),
109: ItemEvent.SELECTED));
110: return;
111: }
112:
113: int getDropDownHeight() {
114: Choice c = (Choice) target;
115: FontMetrics fm = getFontMetrics(c.getFont());
116: int maxItems = Math.min(c.getItemCount(), 8);
117: return fm.getHeight() * maxItems;
118: }
119:
120: /**
121: * DEPRECATED
122: */
123: public Dimension minimumSize() {
124: return getMinimumSize();
125: }
126:
127: }
|