KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > fakepeer > FakeChoicePeer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.form.fakepeer;
22
23 import java.awt.*;
24
25 /**
26  *
27  * @author Tran Duc Trung
28  */

29 class FakeChoicePeer extends FakeComponentPeer
30 {
31     FakeChoicePeer(Choice target) {
32         super(target);
33     }
34
35     Component createDelegate() {
36         return new Delegate();
37     }
38
39     public void add(String JavaDoc item, int index) {
40     }
41
42     public void remove(int index) {
43     }
44
45     // JDK 1.3
46
public void removeAll() {
47     }
48
49     public void select(int index) {
50     }
51
52     public void addItem(String JavaDoc item, int index) {
53         add(item, index);
54     }
55
56     //
57
//
58
//
59

60     private class Delegate extends Component
61     {
62         Delegate() {
63             this.setBackground(SystemColor.window);
64             this.setForeground(SystemColor.controlText);
65         }
66         
67         public void paint(Graphics g) {
68             Choice target =(Choice) _target;
69             Dimension sz = target.getSize();
70             
71             FontMetrics fm = g.getFontMetrics();
72             int w = sz.width,
73                 h = sz.height,
74                 fh = fm.getHeight(), // font's height
75
comph = h > fh+4 ? fh+4 : h, // component's height
76
y = (h-comph)/2; // component's vertical position
77

78             g.setColor(target.getBackground());
79             FakePeerUtils.drawLoweredBox(g, 0,y,w,comph);
80
81             String JavaDoc item = target.getSelectedItem();
82             if (item != null) {
83                 if (target.isEnabled()) {
84                     g.setColor(target.getForeground());
85                 }
86                 else {
87                     g.setColor(SystemColor.controlShadow);
88                 }
89                 g.setFont(target.getFont());
90
91                 g.setClip(2,y+2,w-4,comph-4);
92                 int ih = fh - fm.getDescent(), // item's height
93
iy = y + 1 + ih;
94
95                 g.drawString(item, 4, iy);
96             }
97
98             // combo-box button (Windows style)
99
FakePeerUtils.drawArrowButton(
100                 g, w-BUT_W-2, y+2, BUT_W, comph-4, 4, target.isEnabled());
101         }
102
103         public Dimension getMinimumSize() {
104             String JavaDoc label = ((Choice)_target).getSelectedItem();
105
106             FontMetrics fm = this.getFontMetrics(this.getFont());
107             int w = label != null ? fm.stringWidth(label)+5 : 8,
108                 h = fm.getHeight();
109
110             return new Dimension(w + 4 + BUT_W, h + 4);
111         }
112     }
113
114     private static final int BUT_W = 16, BUT_H = 16;
115 }
116
Popular Tags