KickJava   Java API By Example, From Geeks To Geeks.

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


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 FakeTextAreaPeer extends FakeTextComponentPeer
30 {
31     FakeTextAreaPeer(TextArea target) {
32         super(target);
33     }
34
35     Component createDelegate() {
36         return new Delegate();
37     }
38
39     public void insert(String JavaDoc text, int pos) {
40     }
41
42     public void replaceRange(String JavaDoc text, int start, int end) {
43     }
44
45     public Dimension getPreferredSize(int rows, int columns) {
46         return new Dimension(100, 80);
47     }
48
49     public Dimension getMinimumSize(int rows, int columns) {
50         return new Dimension(100, 80);
51     }
52
53     public void insertText(String JavaDoc txt, int pos) {
54         insert(txt, pos);
55     }
56
57     public void replaceText(String JavaDoc txt, int start, int end) {
58         replaceRange(txt, start, end);
59     }
60
61     public Dimension preferredSize(int rows, int cols) {
62         return getPreferredSize(rows, cols);
63     }
64
65     public Dimension minimumSize(int rows, int cols) {
66         return getMinimumSize(rows, cols);
67     }
68
69     //
70
//
71
//
72

73     private class Delegate extends FakeTextComponentPeer.Delegate
74     {
75         public void paint(Graphics g) {
76             super.paint(g);
77
78             TextArea target = (TextArea) _target;
79             Dimension sz = target.getSize();
80             int w = sz.width;
81             int h = sz.height;
82             String JavaDoc text = target.getText();
83
84             if (text != null) {
85                 g.setFont(target.getFont());
86                 g.setColor(target.getForeground());
87                 
88                 FontMetrics fm = g.getFontMetrics();
89                 int th = fm.getHeight();
90                 int ty = th;
91                 int i = target.getCaretPosition();
92                 int len = text.length();
93                 
94                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc(len);
95
96                 for ( ; i < len; i++) {
97                     char ch = text.charAt(i);
98                     if (ch != '\n' && ch != '\r') buf.append(ch);
99                     else if (ch == '\n') {
100                         g.drawString(buf.toString(),4,ty);
101                         if (ty > h)
102                             break;
103                         ty += th;
104                         buf.delete(0,buf.length());
105                     }
106                 }
107                 g.drawString(buf.toString(), 4, ty);
108             }
109
110             if (sz.width > FakePeerUtils.SCROLL_W*2 &&
111                 sz.height > FakePeerUtils.SCROLL_H*2) {
112                 g.setColor(SystemColor.controlHighlight);
113                 FakePeerUtils.drawScrollbar(g,2,h-FakePeerUtils.SCROLL_H-2,
114                                             w-4-FakePeerUtils.SCROLL_W,FakePeerUtils.SCROLL_H,
115                                             Scrollbar.HORIZONTAL,false,true,0,0,0);
116
117                 g.setColor(SystemColor.controlHighlight);
118                 FakePeerUtils.drawScrollbar(g,w-FakePeerUtils.SCROLL_W-2,2,
119                                             FakePeerUtils.SCROLL_W,h-4-FakePeerUtils.SCROLL_H,
120                                             Scrollbar.VERTICAL,false,true,0,0,0);
121
122                 g.setColor(SystemColor.controlHighlight);
123                 g.fillRect(w-FakePeerUtils.SCROLL_W-2,h-FakePeerUtils.SCROLL_H-2,
124                            FakePeerUtils.SCROLL_W,FakePeerUtils.SCROLL_H);
125             }
126         }
127
128         public Dimension getMinimumSize() {
129             TextArea target = (TextArea)_target;
130             return FakeTextAreaPeer.this.getMinimumSize(target.getColumns(),target.getRows());
131         }
132     }
133 }
134
Popular Tags