001         package com.croftsoft.core.gui;
002    
003         import java.awt.*;
004    
005         import javax.swing.*;
006    
007         import com.croftsoft.core.lang.StringLib;
008         import com.croftsoft.core.lang.ThrowableLib;
009         import com.croftsoft.core.util.log.Log;
010    
011         /*********************************************************************
012         * A Log that records events to a JTextArea in a JPanel.
013         *
014         * @version
015         *   $Id: LogPanel.java,v 1.6 2006/11/15 22:39:35 croft Exp $
016         * @since
017         *   2001-07-30
018         * @author
019         *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
020         *********************************************************************/
021    
022         public class  LogPanel
023           extends JPanel
024           implements Log
025         //////////////////////////////////////////////////////////////////////
026         //////////////////////////////////////////////////////////////////////
027         {
028           
029         private static final long  serialVersionUID = 0L;
030         
031         //
032    
033         public static final int  DEFAULT_LINES_MAX = 100;
034    
035         //
036    
037         private final int        linesMax;
038    
039         private final JTextArea  jTextArea;
040    
041         //////////////////////////////////////////////////////////////////////
042         //////////////////////////////////////////////////////////////////////
043    
044         public  LogPanel (
045           int    linesMax,
046           Color  panelBackgroundColor,
047    //     Color  textFieldBackgroundColor,
048           Font   textAreaFont )
049         //////////////////////////////////////////////////////////////////////
050         {
051           super ( new BorderLayout ( ), true ); // isDoubleBuffered
052    
053           if ( linesMax < 1 )
054           {
055             throw new IllegalArgumentException ( "linesMax < 1" );
056           }
057    
058           this.linesMax = linesMax;
059    
060           if ( panelBackgroundColor != null )
061           {
062             setBackground ( panelBackgroundColor );
063           }
064    
065           add ( jTextArea = new JTextArea ( ), BorderLayout.CENTER );
066    
067           jTextArea.setEditable ( false );
068           
069           jTextArea.setLineWrap ( true );
070           
071           jTextArea.setWrapStyleWord ( true );
072    
073           if ( textAreaFont != null )
074           {
075             jTextArea.setFont ( textAreaFont );
076           }
077           
078           jTextArea.setBorder (
079             BorderFactory.createEmptyBorder ( 4, 6, 4, 6 ) );
080         }
081    
082         public  LogPanel ( final int  linesMax )
083         //////////////////////////////////////////////////////////////////////
084         {
085           this ( linesMax, null, null );       
086         }
087         
088         public  LogPanel ( )
089         //////////////////////////////////////////////////////////////////////
090         {
091           this ( DEFAULT_LINES_MAX, null, null );       
092         }
093         
094         //////////////////////////////////////////////////////////////////////
095         //////////////////////////////////////////////////////////////////////
096    
097         public synchronized void  record ( String  message )
098         //////////////////////////////////////////////////////////////////////
099         {
100           String [ ]  newTextStringArray
101             = StringLib.toStringArray ( message );
102               
103           String  oldText = jTextArea.getText ( );
104             
105           String [ ]  oldTextStringArray
106             = StringLib.toStringArray ( oldText );
107             
108           int  lineCount = oldTextStringArray.length;
109             
110           if ( oldText.trim ( ).equals ( "" ) )
111           {
112             lineCount = 0;
113           }
114           
115           if ( lineCount + newTextStringArray.length > linesMax )
116           {           
117             String  newText2
118               = newTextStringArray [ newTextStringArray.length - 1 ];
119               
120             int  lineCount2 = 1;
121               
122             for (
123               int  i = newTextStringArray.length - 2;
124               ( i > -1 ) && ( ++lineCount2 <= linesMax );
125               i-- )
126             {
127               newText2 = newTextStringArray [ i ] + "\n" + newText2;
128             }
129               
130             for (
131               int  i = oldTextStringArray.length - 1;
132               ( i > -1 ) && ( ++lineCount2 <= linesMax );
133               i-- )
134             {
135               newText2 = oldTextStringArray [ i ] + "\n" + newText2;
136             }
137               
138             jTextArea.setText ( newText2 );
139           }
140           else if ( oldText.trim ( ).equals ( "" ) )
141           {
142             jTextArea.setText ( message );
143           }
144           else
145           {
146             jTextArea.setText ( oldText + "\n" + message );
147           }
148         }
149    
150         public synchronized void  record ( Throwable  throwable )
151         //////////////////////////////////////////////////////////////////////
152         {
153           record ( ThrowableLib.getStackTrace ( throwable ) );
154         }
155    
156    
157         public synchronized void  record (
158           String  message, Throwable  throwable )
159         //////////////////////////////////////////////////////////////////////
160         {
161           record ( message + '\n'
162             + ThrowableLib.getStackTrace ( throwable ) );
163         }
164    
165         //////////////////////////////////////////////////////////////////////
166         //////////////////////////////////////////////////////////////////////
167         
168         public void  clear ( )
169         //////////////////////////////////////////////////////////////////////
170         {
171           jTextArea.setText ( "" );
172         }
173         
174         //////////////////////////////////////////////////////////////////////
175         //////////////////////////////////////////////////////////////////////
176         }