KickJava   Java API By Example, From Geeks To Geeks.

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


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 FakeTextFieldPeer extends FakeTextComponentPeer
30 {
31     FakeTextFieldPeer(TextField target) {
32         super(target);
33     }
34
35     Component createDelegate() {
36         return new Delegate();
37     }
38
39     public void setEchoChar(char echoChar) {
40     }
41
42     public Dimension getPreferredSize(int columns) {
43         return _delegate.getMinimumSize(); //new Dimension(100, 20);
44
}
45
46     public Dimension getMinimumSize(int columns) {
47         return _delegate.getMinimumSize(); //new Dimension(100, 20);
48
}
49
50     public void setEchoCharacter(char c) {
51         setEchoChar(c);
52     }
53
54     public Dimension preferredSize(int cols) {
55         return getPreferredSize(cols);
56     }
57
58     public Dimension minimumSize(int cols) {
59         return getMinimumSize(cols);
60     }
61
62     //
63
//
64
//
65

66     private class Delegate extends FakeTextComponentPeer.Delegate
67     {
68         public void paint(Graphics g) {
69             super.paint(g);
70
71             TextField target =(TextField) _target;
72             String JavaDoc text = target.getText();
73
74             if (text != null) { // draw the text
75
String JavaDoc textOut = text.substring(target.getCaretPosition());
76 // Dimension sz = target.getSize();
77
g.setFont(target.getFont());
78
79                 FontMetrics fm = g.getFontMetrics();
80                 int h = fm.getHeight() - fm.getDescent();
81                 g.drawString(textOut, 4, 1 + h); //(sz.height - h) / 2 + h -2);
82
}
83         }
84
85         public Dimension getMinimumSize() {
86             String JavaDoc text = ((TextField)_target).getText();
87
88             FontMetrics fm = this.getFontMetrics(this.getFont());
89             int w = fm.stringWidth(text);
90             int h = fm.getHeight();
91
92             return new Dimension(w > 92 ? 100 : w+8, h + 4);
93         }
94     }
95 }
96
Popular Tags