001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.fakepeer;
043:
044: import java.awt.*;
045:
046: /**
047: *
048: * @author Tran Duc Trung
049: */
050: class FakeTextAreaPeer extends FakeTextComponentPeer {
051: FakeTextAreaPeer(TextArea target) {
052: super (target);
053: }
054:
055: @Override
056: Component createDelegate() {
057: return new Delegate();
058: }
059:
060: public void insert(String text, int pos) {
061: }
062:
063: public void replaceRange(String text, int start, int end) {
064: }
065:
066: public Dimension getPreferredSize(int rows, int columns) {
067: return new Dimension(100, 80);
068: }
069:
070: public Dimension getMinimumSize(int rows, int columns) {
071: return new Dimension(100, 80);
072: }
073:
074: public void insertText(String txt, int pos) {
075: insert(txt, pos);
076: }
077:
078: public void replaceText(String txt, int start, int end) {
079: replaceRange(txt, start, end);
080: }
081:
082: public Dimension preferredSize(int rows, int cols) {
083: return getPreferredSize(rows, cols);
084: }
085:
086: public Dimension minimumSize(int rows, int cols) {
087: return getMinimumSize(rows, cols);
088: }
089:
090: //
091: //
092: //
093:
094: private class Delegate extends FakeTextComponentPeer.Delegate {
095: @Override
096: public void paint(Graphics g) {
097: super .paint(g);
098:
099: TextArea target = (TextArea) _target;
100: Dimension sz = target.getSize();
101: int w = sz.width;
102: int h = sz.height;
103: String text = target.getText();
104:
105: if (text != null) {
106: g.setFont(target.getFont());
107: g.setColor(target.getForeground());
108:
109: FontMetrics fm = g.getFontMetrics();
110: int th = fm.getHeight();
111: int ty = th;
112: int i = target.getCaretPosition();
113: int len = text.length();
114:
115: StringBuffer buf = new StringBuffer(len);
116:
117: for (; i < len; i++) {
118: char ch = text.charAt(i);
119: if (ch != '\n' && ch != '\r')
120: buf.append(ch);
121: else if (ch == '\n') {
122: g.drawString(buf.toString(), 4, ty);
123: if (ty > h)
124: break;
125: ty += th;
126: buf.delete(0, buf.length());
127: }
128: }
129: g.drawString(buf.toString(), 4, ty);
130: }
131:
132: if (sz.width > FakePeerUtils.SCROLL_W * 2
133: && sz.height > FakePeerUtils.SCROLL_H * 2) {
134: g.setColor(SystemColor.controlHighlight);
135: FakePeerUtils.drawScrollbar(g, 2, h
136: - FakePeerUtils.SCROLL_H - 2, w - 4
137: - FakePeerUtils.SCROLL_W,
138: FakePeerUtils.SCROLL_H, Scrollbar.HORIZONTAL,
139: false, true, 0, 0, 0);
140:
141: g.setColor(SystemColor.controlHighlight);
142: FakePeerUtils.drawScrollbar(g, w
143: - FakePeerUtils.SCROLL_W - 2, 2,
144: FakePeerUtils.SCROLL_W, h - 4
145: - FakePeerUtils.SCROLL_H,
146: Scrollbar.VERTICAL, false, true, 0, 0, 0);
147:
148: g.setColor(SystemColor.controlHighlight);
149: g.fillRect(w - FakePeerUtils.SCROLL_W - 2, h
150: - FakePeerUtils.SCROLL_H - 2,
151: FakePeerUtils.SCROLL_W, FakePeerUtils.SCROLL_H);
152: }
153: }
154:
155: @Override
156: public Dimension getMinimumSize() {
157: TextArea target = (TextArea) _target;
158: return FakeTextAreaPeer.this.getMinimumSize(target
159: .getColumns(), target.getRows());
160: }
161: }
162: }
|