View Javadoc

1   package org.devaki.nextobjects.ui.components;
2   /*
3   
4   nextobjects Copyright (C) 2001-2005 Emmanuel Florent
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 as published by the
8   Free Software Foundation; either version 2 of the License, or (at your
9   option) any later version.
10  
11  This program is distributed in the hope that it will
12  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the GNU General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License along
17  with this program; if not, write to the Free Software Foundation, Inc., 59
18  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
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              // null pointer exception due to tab closed
54              // BadLocationException (req.)
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 }