001: /*
002: * @(#)GListPeer.java 1.20 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:
028: package sun.awt.gtk;
029:
030: import sun.awt.peer.*;
031: import java.awt.*;
032: import java.awt.event.*;
033:
034: /**
035: *
036: *
037: * @author Nicholas Allen
038: */
039:
040: class GListPeer extends GComponentPeer implements ListPeer {
041: private static native void initIDs();
042:
043: static {
044: initIDs();
045: }
046:
047: /** Creates a new GListPeer. */
048:
049: public GListPeer(GToolkit toolkit, List target) {
050: super (toolkit, target);
051: int itemCount = target.getItemCount();
052: for (int i = 0; i < itemCount; i++)
053: add(target.getItem(i), i);
054: setMultipleMode(target.isMultipleMode());
055: int[] selected = target.getSelectedIndexes();
056: if (selected != null) {
057: for (int i = 0; i < selected.length; i++)
058: select(selected[i]);
059: }
060: makeVisible(target.getVisibleIndex());
061: }
062:
063: private native void createNative(int rowHeight);
064:
065: protected void create() {
066: FontMetrics fm = getFontMetrics(((List) target).getFont());
067: createNative(fm.getHeight());
068: }
069:
070: public boolean isFocusTraversable() {
071: return true;
072: }
073:
074: public native int[] getSelectedIndexes();
075:
076: public void add(String item, int index) {
077: if (item != null)
078: addNative(stringToNulMultiByte(item), index);
079: }
080:
081: private native void setRowHeight(int rowHeight);
082:
083: public void setFont(Font f) {
084: super .setFont(f);
085: FontMetrics fm = getFontMetrics(f);
086: setRowHeight(fm.getHeight());
087: }
088:
089: protected native void addNative(byte[] item, int index);
090:
091: public native void delItems(int start, int end);
092:
093: public native void removeAll();
094:
095: public native void select(int index);
096:
097: public native void deselect(int index);
098:
099: public native void makeVisible(int index);
100:
101: public native void setMultipleMode(boolean b);
102:
103: final static int MARGIN = 2;
104: final static int SPACE = 1;
105: final static int SCROLLBAR = 20;
106:
107: public Dimension getPreferredSize(int rows) {
108: FontMetrics fm = getFontMetrics(((List) target).getFont());
109: return new Dimension(SCROLLBAR + 2 * MARGIN
110: + fm.stringWidth("0123456789abcde"),
111: ((fm.getHeight() + 2 * SPACE) * rows) + 2 * MARGIN);
112: }
113:
114: public Dimension getMinimumSize(int rows) {
115: FontMetrics fm = getFontMetrics(((List) target).getFont());
116: return new Dimension(SCROLLBAR + 2 * MARGIN
117: + fm.stringWidth("0123456789"),
118: ((fm.getHeight() + 2 * SPACE) * rows) + 2 * MARGIN);
119: }
120:
121: private void postItemEvent(int row, boolean selected,
122: boolean postActionEvent) {
123: List list = (List) target;
124: if (list.isMultipleMode() || selected) {
125: GToolkit.postEvent(new ItemEvent(list,
126: ItemEvent.ITEM_STATE_CHANGED, new Integer(row),
127: selected ? ItemEvent.SELECTED
128: : ItemEvent.DESELECTED));
129: if (postActionEvent)
130: GToolkit
131: .postEvent(new ActionEvent(list,
132: ActionEvent.ACTION_PERFORMED, list
133: .getItem(row)));
134: }
135: }
136:
137: // added method for Bug #4685270
138: private void postActionEvent(int row) {
139: List list = (List) target;
140: GToolkit.postEvent(new ActionEvent(list,
141: ActionEvent.ACTION_PERFORMED, list.getItem(row)));
142: }
143: }
|