1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
2 | * JFlex 1.4.3 *
|
---|
3 | * Copyright (C) 1998-2009 Gerwin Klein <[email protected]> *
|
---|
4 | * All rights reserved. *
|
---|
5 | * *
|
---|
6 | * This program is free software; you can redistribute it and/or modify *
|
---|
7 | * it under the terms of the GNU General Public License. See the file *
|
---|
8 | * COPYRIGHT for more information. *
|
---|
9 | * *
|
---|
10 | * This program is distributed in the hope that it will be useful, *
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
---|
13 | * GNU General Public License for more details. *
|
---|
14 | * *
|
---|
15 | * You should have received a copy of the GNU General Public License along *
|
---|
16 | * with this program; if not, write to the Free Software Foundation, Inc., *
|
---|
17 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
---|
18 | * *
|
---|
19 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
---|
20 |
|
---|
21 | package JFlex;
|
---|
22 |
|
---|
23 |
|
---|
24 | import java.io.*;
|
---|
25 | import java.awt.TextArea;
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Convenience class for JFlex stdout, redirects output to a TextArea
|
---|
30 | * if in GUI mode.
|
---|
31 | *
|
---|
32 | * @author Gerwin Klein
|
---|
33 | * @version JFlex 1.4.3, $Revision: 433 $, $Date: 2009-01-31 19:52:34 +1100 (Sat, 31 Jan 2009) $
|
---|
34 | */
|
---|
35 | public final class StdOutWriter extends PrintWriter {
|
---|
36 |
|
---|
37 | /** text area to write to if in gui mode, gui mode = (text != null) */
|
---|
38 | private TextArea text;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * approximation of the current column in the text area
|
---|
42 | * for auto wrapping at <code>wrap</code> characters
|
---|
43 | **/
|
---|
44 | private int col;
|
---|
45 |
|
---|
46 | /** auto wrap lines in gui mode at this value */
|
---|
47 | private final static int wrap = 78;
|
---|
48 |
|
---|
49 | /** A StdOutWriter, attached to System.out, no gui mode */
|
---|
50 | public StdOutWriter() {
|
---|
51 | super(System.out,true);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** A StdOutWrite, attached to the specified output stream, no gui mode */
|
---|
55 | public StdOutWriter(OutputStream out) {
|
---|
56 | super(out,true);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Set the TextArea to write text to. Will continue
|
---|
61 | * to write to System.out if text is <code>null</code>.
|
---|
62 | *
|
---|
63 | * @param text the TextArea to write to
|
---|
64 | */
|
---|
65 | public void setGUIMode(TextArea text) {
|
---|
66 | this.text = text;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Write a single character. */
|
---|
70 | public void write(int c) {
|
---|
71 | if (text != null) {
|
---|
72 | text.append(String.valueOf((char) c));
|
---|
73 | if (++col > wrap) println();
|
---|
74 | }
|
---|
75 | else
|
---|
76 | super.write(c);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Write a portion of an array of characters. */
|
---|
80 | public void write(char buf[], int off, int len) {
|
---|
81 | if (text != null) {
|
---|
82 | text.append(new String(buf,off,len));
|
---|
83 | if ((col+=len) > wrap) println();
|
---|
84 | }
|
---|
85 | else
|
---|
86 | super.write(buf, off, len);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /** Write a portion of a string. */
|
---|
90 | public void write(String s, int off, int len) {
|
---|
91 | if (text != null) {
|
---|
92 | text.append(s.substring(off,off+len));
|
---|
93 | if ((col+=len) > wrap) println();
|
---|
94 | }
|
---|
95 | else {
|
---|
96 | super.write(s,off,len);
|
---|
97 | flush();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Begin a new line. Which actual character/s is/are written
|
---|
103 | * depends on the runtime platform.
|
---|
104 | */
|
---|
105 | public void println() {
|
---|
106 | if (text != null) {
|
---|
107 | text.append(Out.NL);
|
---|
108 | col = 0;
|
---|
109 | }
|
---|
110 | else
|
---|
111 | super.println();
|
---|
112 | }
|
---|
113 | }
|
---|