View Javadoc

1   /*
2    * $Id: LogPanel.java,v 1.9 2007/06/13 15:36:33 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.output;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Font;
26  import java.io.OutputStream;
27  
28  import javax.swing.JScrollPane;
29  import javax.swing.JTextArea;
30  import javax.swing.text.Element;
31  
32  /***
33   * Log Panel.
34   * 
35   * @version $Revision: 1.9 $, $Date: 2007/06/13 15:36:33 $
36   * @author Edwin Dankert <edankert@gmail.com>
37   */
38  
39  public class LogPanel extends SearchablePanel {
40  	private static final long serialVersionUID = -5024093857377606353L;
41  
42      private OutputStream stream 	= null;
43  	private LogPane logPane 		= null;
44  
45  	/***
46  	 * Constructs a new Input Panel.
47  	 */
48  	public LogPanel() {
49  		super( new BorderLayout());
50  		
51  //		setBorder( new EmptyBorder( 10, 10, 10, 10));
52   		stream = new OutputStream() {
53   			private StringBuffer buffer = new StringBuffer();
54  
55   			public void write(int b) {
56   				buffer.append( (char)b);
57   			}
58   	
59   			public void flush () {
60   				logPane.append( buffer.toString());
61   				buffer = new StringBuffer();
62   			}
63   		};
64  		
65  		logPane = new LogPane();
66          JScrollPane scrollPane = new JScrollPane( logPane,
67                                    JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
68                                    JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
69  		add( scrollPane, BorderLayout.CENTER);
70  	}
71  	
72  	public OutputStream getOutputStream() {
73  		return stream;
74  	}
75      
76      public boolean search(String search, boolean matchCase, boolean forward) {
77          return search(logPane, search, matchCase, forward);
78      }
79  
80  	 public class LogPane extends JTextArea {
81  	 	
82  	 	private static final long serialVersionUID = 6426276169097189726L;
83  
84          public LogPane() {
85  //	 		setBorder( new EtchedBorder( EtchedBorder.LOWERED));
86  			setFont( new Font( "monospaced", Font.PLAIN, 12));
87  	 	}
88  		
89  	 	public void startLogging( String text) {
90  	 		setText( text+"\n");
91  	 	}
92  
93  	 	public void stopLogging( String text) {
94  	 		append( "\n"+text);
95  	 	}
96  
97  	 	public void clear() {
98  	 		setText("");
99  	 	}
100 
101 	 	public void updatePreferences() {
102 			setFont( new Font( "monospaced", Font.PLAIN, 12));
103 	 	}
104 	 	
105 	 	public void append( String text) {
106 	 		super.append(text);
107 	 		
108 	 		Element root = getDocument().getDefaultRootElement();
109 	 		Element last = root.getElement( root.getElementCount()-1);
110 	 		final int pos = last.getStartOffset();
111 	 		setCaretPosition( pos);
112 //
113 //	 		if ( SwingUtilities.isEventDispatchThread()) {
114 //	 	 		setCaretPosition( pos);
115 //			} else {
116 //				SwingUtilities.invokeLater( new Runnable() {
117 //					public void run() {
118 //				 		setCaretPosition( pos);
119 //					}
120 //				});
121 //			}
122 	 	}
123 	}
124 }