KickJava   Java API By Example, From Geeks To Geeks.

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


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 FakeLabelPeer extends FakeComponentPeer
30 {
31     FakeLabelPeer(Label target) {
32         super(target);
33     }
34
35     Component createDelegate() {
36         return new Delegate();
37     }
38
39     public void setText(String JavaDoc label) {
40     }
41
42     public void setAlignment(int alignment) {
43     }
44
45     //
46
//
47
//
48

49     private class Delegate extends Component
50     {
51         Delegate() {
52 // this.setBackground(SystemColor.control);
53
this.setForeground(SystemColor.controlText);
54         }
55         
56         public void paint(Graphics g) {
57             Label target = (Label) _target;
58
59             Dimension sz = target.getSize();
60             g.setColor(target.getBackground());
61             g.fillRect(0, 0, sz.width, sz.height);
62
63             String JavaDoc label = target.getText();
64             if (label == null)
65                 return;
66
67             g.setFont(target.getFont());
68
69             FontMetrics fm = g.getFontMetrics();
70             int w = fm.stringWidth(label),
71                 h = fm.getHeight() - fm.getDescent(),
72                 x = 0,
73                 y = (sz.height - h) / 2 + h - 2,
74                 alignment = target.getAlignment();
75             
76             if (alignment == Label.RIGHT)
77                 x = sz.width - w;
78             else if (alignment == Label.CENTER)
79                 x =(sz.width - w) / 2;
80
81             if (target.isEnabled()) {
82                 g.setColor(target.getForeground());
83             }
84             else {
85                 g.setColor(SystemColor.controlLtHighlight);
86                 g.drawString(label, x+1, y+1);
87                 g.setColor(SystemColor.controlShadow);
88             }
89
90             g.drawString(label, x, y);
91         }
92
93         public Dimension getMinimumSize() {
94             String JavaDoc label = ((Label)_target).getText();
95
96             FontMetrics fm = this.getFontMetrics(this.getFont());
97             int w = fm.stringWidth(label);
98             int h = fm.getHeight();
99
100             return new Dimension(w + 4, h + 4);
101         }
102     }
103 }
104
Popular Tags