View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support.log;
14  
15  import java.awt.Component;
16  
17  import javax.swing.JComponent;
18  import javax.swing.JTabbedPane;
19  
20  import org.apache.log4j.spi.LoggingEvent;
21  
22  /***
23   * JTabbedPane that displays Log4J output in different tabs
24   * 
25   * @author Ole.Matzura
26   */
27  
28  public class TabbedLog4JMonitor extends JTabbedPane implements Log4JMonitor
29  {
30  	private JLogList defaultLogArea;
31  
32  	public TabbedLog4JMonitor()
33  	{
34  		super( JTabbedPane.BOTTOM, JTabbedPane.SCROLL_TAB_LAYOUT );
35  	}
36  	
37  	public JLogList addLogArea( String title, String loggerName, boolean isDefault )
38  	{
39  		JLogList logArea = new JLogList( title );
40  		logArea.addLogger( loggerName, !isDefault );
41  		addTab( title, logArea);
42  		
43  		if( isDefault )
44  			defaultLogArea = logArea;
45  		
46  		return logArea;
47  	}
48  
49  	public void logEvent(Object msg)
50  	{
51  		if( msg instanceof LoggingEvent )
52  		{
53  			LoggingEvent event = (LoggingEvent) msg;
54  			String loggerName = event.getLoggerName();
55  			
56  			for( int c = 0; c < getTabCount(); c++ )
57  			{
58  				Component tabComponent = getComponentAt( c );
59  				if( tabComponent instanceof JLogList )
60  				{
61  					JLogList logArea = (JLogList) tabComponent;
62  					if( logArea.monitors( loggerName ))
63  					{
64  						logArea.addLine( msg );
65  					}
66  				}
67  			}
68  		}
69  		else if( defaultLogArea != null )
70  		{
71  			defaultLogArea.addLine( msg );
72  		}
73  	}
74  	
75  	public JLogList getLogArea( String title )
76  	{
77  		int ix = indexOfTab( title );
78  		return ( JLogList ) ( ix == -1 ? null : getComponentAt( ix ) );
79  	}
80  
81  	public boolean hasLogArea(String loggerName)
82  	{
83  		for( int c = 0; c < getTabCount(); c++ )
84  		{
85  			Component tabComponent = getComponentAt( c );
86  			if( tabComponent instanceof JLogList )
87  			{
88  				JLogList logArea = (JLogList) tabComponent;
89  				if( logArea.monitors( loggerName ))
90  				{
91  					return true;
92  				}
93  			}
94  		}
95  		
96  		return false;
97  	}
98  
99  	public JComponent getComponent()
100 	{
101 		return this;
102 	}
103 
104 	public JLogList getCurrentLog()
105 	{
106 		int ix = getSelectedIndex();
107 		return ix == -1 ? null : getLogArea( getTitleAt( ix ));
108 	}
109 
110 	public void setCurrentLog( JLogList lastLog )
111 	{
112 		for( int c = 0; c < getTabCount(); c++ )
113 		{
114 			Component tabComponent = getComponentAt( c );
115 			if( tabComponent == lastLog  )
116 			{
117 				setSelectedComponent( tabComponent );
118 			}
119 		}
120 	}
121 
122 	public boolean removeLogArea( String loggerName )
123 	{
124 		for( int c = 0; c < getTabCount(); c++ )
125 		{
126 			JLogList tabComponent = ( JLogList ) getComponentAt( c );
127 			if( tabComponent.getLogger( loggerName ) != null )
128 			{
129 				removeTabAt( c );
130 				return true;
131 			}
132 		}
133 		
134 		return false;
135 	}
136 }