KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > ui > JConsoleWindow


1 /*
2     =========================================================================
3
4     This module is developed and maintained by PlanetaMessenger.org.
5     Specs, New and updated versions can be found in
6     http://www.planetamessenger.org
7     If you want contact the Team please send a email to Project Manager
8     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
9
10     Copyright (C) since 2001 by PlanetaMessenger.org
11     
12     This library is free software; you can redistribute it and/or
13     modify it under the terms of the GNU Lesser General Public
14     License as published by the Free Software Foundation; either
15     version 2.1 of the License, or (at your option) any later version.
16
17     This library is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20     Lesser General Public License for more details.
21
22     You should have received a copy of the GNU Lesser General Public
23     License along with this library; if not, write to the Free Software
24     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
26     =========================================================================
27 */

28 /**
29  *
30  * $Id: JConsoleWindow.java,v 1.11 2007/01/28 17:39:22 popolony2k Exp $
31  * $Author: popolony2k $
32  * $Name: $
33  * $Revision: 1.11 $
34  * $State: Exp $
35  *
36  */

37
38 package org.planetamessenger.ui;
39
40
41 public class JConsoleWindow extends java.io.PrintStream JavaDoc {
42   
43   private static final int WIDTH = 400;
44   private static final int HEIGHT = 300;
45   
46   javax.swing.text.SimpleAttributeSet JavaDoc attrs;
47   javax.swing.JTextPane JavaDoc dataPane;
48   javax.swing.JScrollPane JavaDoc scrollPane;
49   javax.swing.JFrame JavaDoc frame;
50   boolean bAutoScrolling;
51   
52
53   /**
54    * Constructor. Creates and initializes
55    * all class data;
56    */

57   public JConsoleWindow() {
58     
59     super( System.out );
60
61     attrs = new javax.swing.text.SimpleAttributeSet JavaDoc();
62     frame = new javax.swing.JFrame JavaDoc();
63     dataPane = new javax.swing.JTextPane JavaDoc();
64     scrollPane = new javax.swing.JScrollPane JavaDoc( dataPane );
65     bAutoScrolling = true;
66
67     dataPane.setBackground( java.awt.Color.black );
68     dataPane.setEditable( false );
69
70     javax.swing.text.StyleConstants.setForeground( attrs, java.awt.Color.green );
71     frame.getContentPane().add( scrollPane );
72     frame.addWindowListener( new java.awt.event.WindowAdapter JavaDoc() {
73       public void windowClosing( java.awt.event.WindowEvent JavaDoc evt ) {
74         hide();
75       }
76     } );
77     
78     frame.setSize( WIDTH, HEIGHT );
79     // For use in 1.6 and higher
80
//frame.setModalExclusionType( ModalExclusionType.APPLICATION_EXCLUDE );
81
}
82
83   /**
84    * Set auto scroll content view.
85    * @param bAutoScrolling The new auto scroll
86    * value;
87    */

88   public void setAutoScrolling( boolean bAutoScrolling ) {
89     
90     this.bAutoScrolling = bAutoScrolling;
91   }
92
93   /**
94    * Get auto scroll content view
95    * status.
96    */

97   public boolean getAutoScrolling() {
98     
99     return bAutoScrolling;
100   }
101
102   /**
103    * Set window title.
104    * @param strTitle The window title;
105    */

106   public void setTitle( String JavaDoc strTitle ) {
107     
108     if( frame != null )
109       frame.setTitle( strTitle );
110   }
111
112   /**
113    * open the console window.
114    */

115   public void show() {
116
117     frame.setVisible( true );
118   }
119
120   /**
121    * Close the console window.
122    */

123   public void hide() {
124     
125     frame.setVisible( false );
126   }
127
128   /**
129    * Destroy the console window.
130    */

131   public void destroy() {
132
133     frame.dispose();
134     frame = null;
135     dataPane = null;
136     scrollPane = null;
137   }
138   
139   /**
140    * Perform a complete UI update in
141    * window.
142    */

143   public void updateUI() {
144     
145     javax.swing.SwingUtilities.updateComponentTreeUI( frame );
146     frame.invalidate();
147     frame.validate();
148     frame.repaint();
149   }
150
151   /**
152    * Override the PrintStream.println
153    * method to print a line into console
154    * window.
155    * @param x The object to print;
156    */

157   public void println( java.lang.Object JavaDoc x ) {
158
159     println( x.toString() );
160   }
161
162   /**
163    * Override the PrintStream.println
164    * method to print a line into console
165    * window.
166    * @param x The string to print;
167    */

168   public void println( java.lang.String JavaDoc x ) {
169   
170     _print( x + "\n" );
171   }
172   
173   // Private methods
174
/**
175    * Print a message to console
176    * internal pane;
177    * @param strMsg The text to print;
178    */

179   private void _print( String JavaDoc strMsg ) {
180    
181     try {
182       javax.swing.text.Document JavaDoc doc = dataPane.getDocument();
183       
184       doc.insertString( doc.getLength(), strMsg, attrs );
185       
186       if( bAutoScrolling )
187         dataPane.setCaretPosition( doc.getLength() );
188       
189     } catch( javax.swing.text.BadLocationException JavaDoc e ) {
190       e.printStackTrace();
191     }
192   }
193 }
194
195 // JConsoleWindow class
Popular Tags