001: /*
002: * Copyright 1995-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.motif;
027:
028: import java.awt.*;
029: import java.awt.peer.*;
030: import java.awt.event.ItemEvent;
031:
032: class MChoicePeer extends MComponentPeer implements ChoicePeer {
033: boolean inUpCall = false;
034:
035: native void create(MComponentPeer parent);
036:
037: native void pReshape(int x, int y, int width, int height);
038:
039: native void pSelect(int index, boolean init);
040:
041: native void appendItems(String[] items);
042:
043: void initialize() {
044: Choice opt = (Choice) target;
045: int itemCount = opt.countItems();
046: String[] items = new String[itemCount];
047: for (int i = 0; i < itemCount; i++) {
048: items[i] = opt.getItem(i);
049: }
050: if (itemCount > 0) {
051: appendItems(items);
052: pSelect(opt.getSelectedIndex(), true);
053: }
054: super .initialize();
055: }
056:
057: public MChoicePeer(Choice target) {
058: super (target);
059: }
060:
061: public boolean isFocusable() {
062: return true;
063: }
064:
065: public Dimension getMinimumSize() {
066: FontMetrics fm = getFontMetrics(target.getFont());
067: Choice c = (Choice) target;
068: int w = 0;
069: for (int i = c.countItems(); i-- > 0;) {
070: w = Math.max(fm.stringWidth(c.getItem(i)), w);
071: }
072: return new Dimension(32 + w,
073: Math.max(fm.getHeight() + 8, 15) + 5);
074: }
075:
076: public native void setFont(Font f);
077:
078: public void add(String item, int index) {
079: addItem(item, index);
080: // Adding an item can change the size of the Choice, so do
081: // a reshape, based on the font size.
082: Rectangle r = target.getBounds();
083: reshape(r.x, r.y, 0, 0);
084: }
085:
086: public native void remove(int index);
087:
088: public native void removeAll();
089:
090: /**
091: * DEPRECATED, but for now, called by add(String, int).
092: */
093: public native void addItem(String item, int index);
094:
095: // public native void remove(int index);
096:
097: public native void setBackground(Color c);
098:
099: public native void setForeground(Color c);
100:
101: public void select(int index) {
102: if (!inUpCall) {
103: pSelect(index, false);
104: }
105: }
106:
107: void notifySelection(String item) {
108: Choice c = (Choice) target;
109: ItemEvent e = new ItemEvent(c, ItemEvent.ITEM_STATE_CHANGED,
110: item, ItemEvent.SELECTED);
111: postEvent(e);
112: }
113:
114: // NOTE: This method is called by privileged threads.
115: // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
116: void action(final int index) {
117: final Choice c = (Choice) target;
118: inUpCall = false; /* Used to prevent native selection. */
119: MToolkit.executeOnEventHandlerThread(c, new Runnable() {
120: public void run() {
121: String item;
122: synchronized (c) {
123: if (index >= c.getItemCount()) {
124: /* Nothing to do when the list is too short */
125: return;
126: }
127: inUpCall = true; /* Prevent native selection. */
128: c.select(index); /* set value in target */
129: item = c.getItem(index);
130: inUpCall = false;
131: }
132: notifySelection(item);
133: }
134: });
135: }
136:
137: /*
138: * Print the native component by rendering the Motif look ourselves.
139: * ToDo(aim): needs to query native motif for more accurate size and
140: * color information.
141: */
142: public void print(Graphics g) {
143: Choice ch = (Choice) target;
144: Dimension d = ch.size();
145: Color bg = ch.getBackground();
146: Color fg = ch.getForeground();
147:
148: g.setColor(bg);
149: g.fillRect(2, 2, d.width - 1, d.height - 1);
150: draw3DRect(g, bg, 1, 1, d.width - 2, d.height - 2, true);
151: draw3DRect(g, bg, d.width - 18, (d.height / 2) - 3, 10, 6, true);
152:
153: g.setColor(fg);
154: g.setFont(ch.getFont());
155: FontMetrics fm = g.getFontMetrics();
156: String lbl = ch.getSelectedItem();
157: if (lbl == null) {
158: lbl = "";
159: }
160: if (lbl != "") {
161: g.drawString(lbl, 5, (d.height + fm.getMaxAscent() - fm
162: .getMaxDescent()) / 2);
163: }
164:
165: target.print(g);
166: }
167:
168: /**
169: * DEPRECATED
170: */
171: public Dimension minimumSize() {
172: return getMinimumSize();
173: }
174:
175: protected void disposeImpl() {
176: freeNativeData();
177: super .disposeImpl();
178: }
179:
180: private native void freeNativeData();
181: }
|