1 package org.devaki.nextobjects.ui.components;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import java.io.OutputStream;
22
23 import javax.swing.JTextArea;
24 /***
25 * This class provide the textarea used for the log panel.
26 */
27 public class JTextAreaOutputStream extends OutputStream
28 {
29 /***
30 * The textarea where to log a stream
31 */
32 private final JTextArea ta;
33 /***
34 * Initialise an OutputStream as entry for a given jTextarea
35 * @param pTa the textarea
36 */
37 public JTextAreaOutputStream(final JTextArea pTa)
38 {
39 this.ta = pTa;
40 }
41 /***
42 * Scroll text to visible
43 */
44 private void scrollDown()
45 {
46 try
47 {
48 ta.scrollRectToVisible(
49 ta.modelToView(ta.getDocument().getLength()));
50 }
51 catch (Exception e)
52 {
53
54
55 System.out.println("I can't show the log panel :(");
56 }
57 }
58 /***
59 * data to be logged entry point
60 * @param b ascii char
61 */
62 public final void write(final int b)
63 {
64 write(String.valueOf((char) b));
65 scrollDown();
66 }
67 /***
68 * data to be logged entry point
69 * @param b message as byte array
70 */
71 public final void write(final byte[] b)
72 {
73 write(new String(b));
74 scrollDown();
75 }
76 /***
77 * data to be logged entry point
78 * @param b message as a byte array
79 * @param off where to start
80 * @param len lengt to log
81 */
82 public final void write(final byte[] b, final int off, final int len)
83 {
84 write(new String(b, off, len));
85 scrollDown();
86 }
87 /***
88 * Write as string
89 * @param msg the string
90 */
91 public final void write(final String msg)
92 {
93 ta.append(msg);
94 scrollDown();
95 }
96 /***
97 * Close operation (nothing to do)
98 */
99 public void close()
100 {
101 }
102 /***
103 * Flush operation (nothing to do)
104 */
105 public void flush()
106 {
107 }
108 }