KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > gui > AbstractTextArea


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22
23 package org.aspectj.debugger.gui;
24
25 import org.aspectj.debugger.base.*;
26
27 import java.awt.*;
28 import java.io.*;
29 import javax.swing.*;
30 import javax.swing.text.*;
31
32 public abstract class AbstractTextArea extends JPanel {
33
34     protected final static int TEXT_ROWS = 30;
35     protected final static int TEXT_COLS = 50;
36     protected final static int TEXT_SIZE = 12;
37     protected final static Dimension TEXT_DIM = new Dimension(TEXT_ROWS, TEXT_COLS);
38     protected final static int OUT = 0;
39     protected final static int NOTICE = 1;
40     protected final static int ERR = 2;
41     protected final static String JavaDoc NEWLINE = System.getProperty("line.separator");
42
43     protected PrintStream printStream = null;
44     protected JScrollPane scrollPane = null;
45     protected DefaultStyledDocument dsd = new DefaultStyledDocument();
46     protected JTextArea text; //new JTextPane(dsd);
47
protected SimpleAttributeSet[] attrs = null;
48     protected Color[] colors = { Color.black,
49                                  Color.blue.darker(),
50                                  Color.red.darker() };
51     protected int textSize = TEXT_SIZE;
52 // protected JScrollBar xScrollBar;
53
// protected JScrollBar yScrollBar;
54

55     protected GUIDebugger guiDebugger;
56
57     private int count = 0;
58
59     public final void superInit(GUIDebugger guiDebugger) {
60         this.guiDebugger = guiDebugger;
61         setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
62         text = new JTextArea(0,0);
63         text.setEditable(false);
64         printStream = createPrintStream();
65         scrollPane = new JScrollPane(text);
66 // xScrollBar = scrollPane.getHorizontalScrollBar();
67
// yScrollBar = scrollPane.getVerticalScrollBar();
68
scrollPane.setVerticalScrollBarPolicy
69             (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
70         add(scrollPane);
71         initAttrs();
72     }
73
74 // static int count_ = 0;
75

76     protected abstract PrintStream createPrintStream();
77
78     public PrintStream getPrintStream() {
79         return printStream;
80     }
81
82     public void setTextSize(int textSize) {
83         this.textSize = textSize;
84     }
85
86     public abstract void restorePrintStream();
87
88     public void shutDown() {
89         restorePrintStream();
90     }
91
92     protected void out(String JavaDoc s) {
93         append(s, OUT);
94     }
95
96     protected void outln(String JavaDoc s) {
97         append(s, OUT);
98         newline();
99     }
100
101     protected void err(String JavaDoc s) {
102         append(s, ERR);
103     }
104
105     protected void errln(String JavaDoc s) {
106         append(s, ERR);
107         newline();
108     }
109
110     protected void notice(String JavaDoc s) {
111         append(s, NOTICE);
112     }
113
114     protected void noticeln(String JavaDoc s) {
115         append(s, NOTICE);
116         newline();
117     }
118
119     protected void append(String JavaDoc s, int i) {
120         text.append(s);
121         //yScrollBar.setValue(yScrollBar.getMaximum());
122
//xScrollBar.setValue(xScrollBar.getMinimum());
123
}
124
125     protected void newline() {
126         append(NEWLINE, -1);
127     }
128
129     protected void initAttrs() {
130         attrs = new SimpleAttributeSet[colors.length];
131         for (int i = 0; i < colors.length; i++) {
132             attrs[i] = getAttr(colors[i]);
133         }
134     }
135
136     protected SimpleAttributeSet getAttr(Color c) {
137         SimpleAttributeSet attr = new SimpleAttributeSet();
138         StyleConstants.setForeground(attr, c);
139         StyleConstants.setFontSize(attr, textSize);
140         return attr;
141     }
142
143     protected static class TextPrintOutStream extends PrintStream {
144         public TextPrintOutStream(final AbstractTextArea text) {
145             super(new OutputStream() {
146                 public void write(int b) throws IOException {
147                     text.out(String.valueOf((char)b));
148                 }
149             });
150
151         }
152     }
153
154     protected static class TextPrintNoticeStream extends PrintStream {
155         public TextPrintNoticeStream(final AbstractTextArea text) {
156             super(new OutputStream() {
157                 public void write(int b) throws IOException {
158                     text.notice(String.valueOf((char)b));
159                 }
160             });
161
162         }
163     }
164
165     protected static class TextPrintErrStream extends PrintStream {
166         public TextPrintErrStream(final AbstractTextArea text) {
167             super(new OutputStream() {
168                     public void write(int b) throws IOException {
169                         text.err(String.valueOf((char)b));
170                     }
171                 });
172         }
173     }
174 }
175
Popular Tags