001: /*
002: * @(#)PPCListPeer.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:
027: package sun.awt.pocketpc;
028:
029: import sun.awt.peer.*;
030: import java.awt.*;
031: import java.awt.event.*;
032:
033: /**
034: *
035: *
036: * @author Nicholas Allen
037: */
038:
039: class PPCListPeer extends PPCComponentPeer implements ListPeer {
040: private static native void initIDs();
041:
042: static {
043: initIDs();
044: }
045:
046: // ComponentPeer overrides
047:
048: public Dimension minimumSize() {
049: return minimumSize(4);
050: }
051:
052: public boolean isFocusTraversable() {
053: return true;
054: }
055:
056: // ListPeer implementation
057:
058: public int[] getSelectedIndexes() {
059: List l = (List) target;
060: int len = l.countItems();
061: int sel[] = new int[len];
062: int nsel = 0;
063: for (int i = 0; i < len; i++) {
064: if (isSelected(i)) {
065: sel[nsel++] = i;
066: }
067: }
068: int selected[] = new int[nsel];
069: System.arraycopy(sel, 0, selected, 0, nsel);
070: return selected;
071: }
072:
073: /* New method name for 1.1 */
074: public void add(String item, int index) {
075: addItem(item, index);
076: }
077:
078: /* New method name for 1.1 */
079: public void removeAll() {
080: clear();
081: }
082:
083: /* New method name for 1.1 */
084: public void setMultipleMode(boolean b) {
085: setMultipleSelections(b);
086: }
087:
088: /* New method name for 1.1 */
089: public Dimension getPreferredSize(int rows) {
090: return preferredSize(rows);
091: }
092:
093: /* New method name for 1.1 */
094: public Dimension getMinimumSize(int rows) {
095: return minimumSize(rows);
096: }
097:
098: private FontMetrics fm;
099:
100: public void addItem(String item, int index) {
101: addItemNative(item, index, fm.stringWidth(item));
102: }
103:
104: native void addItemNative(String item, int index, int width);
105:
106: public native void delItems(int start, int end);
107:
108: public void clear() {
109: List l = (List) target;
110: delItems(0, l.countItems() - 1);
111: }
112:
113: public native void select(int index);
114:
115: public native void deselect(int index);
116:
117: public native void makeVisible(int index);
118:
119: public native void setMultipleSelections(boolean v);
120:
121: public native int getMaxWidth();
122:
123: public Dimension preferredSize(int v) {
124: Dimension d = minimumSize(v);
125: d.width = Math.max(d.width, getMaxWidth() + 20);
126: return d;
127: }
128:
129: public Dimension minimumSize(int v) {
130: return new Dimension(20 + fm.stringWidth("0123456789abcde"),
131: (fm.getHeight() * v) + 4); // include borders
132: }
133:
134: // Toolkit & peer internals
135:
136: PPCListPeer(List target) {
137: super (target);
138: }
139:
140: void clearRectBeforePaint(Graphics g, Rectangle r) {
141: // Overload to do nothing for native components
142: }
143:
144: native void create(PPCComponentPeer parent);
145:
146: void initialize() {
147: List li = (List) target;
148:
149: fm = getFontMetrics(li.getFont());
150:
151: // add any items that were already inserted in the target.
152: int nitems = li.countItems();
153: for (int i = 0; i < nitems; i++) {
154: addItem(li.getItem(i), -1);
155: }
156:
157: // set whether this list should allow multiple selections.
158: setMultipleSelections(li.allowsMultipleSelections());
159:
160: // make the visible position visible.
161: int index = li.getVisibleIndex();
162: if (index >= 0) {
163: makeVisible(index);
164: }
165:
166: // select the item if necessary.
167: int sel[] = li.getSelectedIndexes();
168: for (int i = 0; i < sel.length; i++) {
169: select(sel[i]);
170: }
171:
172: super .initialize();
173: }
174:
175: private native void updateMaxItemWidth();
176:
177: /*public*/native boolean isSelected(int index);
178:
179: // update the fontmetrics when the font changes
180: public synchronized void setFont(Font f) {
181: super .setFont(f);
182: fm = getFontMetrics(((List) target).getFont());
183: updateMaxItemWidth();
184: }
185:
186: // native callbacks
187:
188: void handleAction(int index) {
189: List l = (List) target;
190: l.select(index);
191: PPCToolkit.postEvent(new ActionEvent(target,
192: ActionEvent.ACTION_PERFORMED, l.getItem(index)));
193: }
194:
195: void handleListChanged(int index) {
196: List l = (List) target;
197: PPCToolkit.postEvent(new ItemEvent(l,
198: ItemEvent.ITEM_STATE_CHANGED, new Integer(index),
199: isSelected(index) ? ItemEvent.SELECTED
200: : ItemEvent.DESELECTED));
201:
202: }
203: }
|