View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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  import java.util.List;
17  
18  import javax.swing.JComponent;
19  
20  import org.apache.log4j.spi.LoggingEvent;
21  
22  import com.eviware.soapui.support.components.Inspector;
23  import com.eviware.soapui.support.components.JComponentInspector;
24  import com.eviware.soapui.support.components.JInspectorPanel;
25  import com.eviware.soapui.support.components.JInspectorPanelFactory;
26  
27  /***
28   * JTabbedPane that displays Log4J output in different tabs
29   * 
30   * @author Ole.Matzura
31   */
32  
33  public class InspectorLog4JMonitor implements JInspectorPanel, Log4JMonitor
34  {
35  	private JLogList defaultLogArea;
36  	private JInspectorPanel inspectorPanel;
37  
38  	public InspectorLog4JMonitor( JComponent content )
39  	{
40  		inspectorPanel = JInspectorPanelFactory.build( content );
41  
42  		setResizeWeight( 0.9F );
43  	}
44  
45  	public JLogList addLogArea( String title, String loggerName, boolean isDefault )
46  	{
47  		JLogList logArea = new JLogList( title );
48  		logArea.addLogger( loggerName, !isDefault );
49  		JComponentInspector<JLogList> inspector = new JComponentInspector<JLogList>( logArea, title, null, true );
50  		addInspector( inspector );
51  
52  		if( isDefault )
53  		{
54  			defaultLogArea = logArea;
55  			activate( inspector );
56  			setDividerLocation( 500 );
57  		}
58  
59  		return logArea;
60  	}
61  
62  	public void logEvent( Object msg )
63  	{
64  		if( msg instanceof LoggingEvent )
65  		{
66  			LoggingEvent event = ( LoggingEvent )msg;
67  			String loggerName = event.getLoggerName();
68  
69  			for( Inspector inspector : inspectorPanel.getInspectors() )
70  			{
71  				Component tabComponent = inspector.getComponent();
72  				if( tabComponent instanceof JLogList )
73  				{
74  					JLogList logArea = ( JLogList )tabComponent;
75  					if( logArea.monitors( loggerName ) )
76  					{
77  						logArea.addLine( msg );
78  					}
79  				}
80  			}
81  		}
82  		else if( defaultLogArea != null )
83  		{
84  			defaultLogArea.addLine( msg );
85  		}
86  	}
87  
88  	public JLogList getLogArea( String title )
89  	{
90  		Inspector inspector = inspectorPanel.getInspectorByTitle( title );
91  		return ( JLogList )( title == null ? null : inspector.getComponent() );
92  	}
93  
94  	public boolean hasLogArea( String loggerName )
95  	{
96  		for( Inspector inspector : getInspectors() )
97  		{
98  			Component tabComponent = inspector.getComponent();
99  			if( tabComponent instanceof JLogList )
100 			{
101 				JLogList logArea = ( JLogList )tabComponent;
102 				if( logArea.monitors( loggerName ) )
103 				{
104 					return true;
105 				}
106 			}
107 		}
108 
109 		return false;
110 	}
111 
112 	public JComponent getComponent()
113 	{
114 		return inspectorPanel.getComponent();
115 	}
116 
117 	public Inspector getCurrentInspector()
118 	{
119 		return inspectorPanel.getCurrentInspector();
120 	}
121 
122 	public Inspector getInspectorByTitle( String title )
123 	{
124 		return inspectorPanel.getInspectorByTitle( title );
125 	}
126 
127 	public List<Inspector> getInspectors()
128 	{
129 		return inspectorPanel.getInspectors();
130 	}
131 
132 	public void setCurrentInspector( String s )
133 	{
134 		inspectorPanel.setCurrentInspector( s );
135 	}
136 
137 	public void setDefaultDividerLocation( float v )
138 	{
139 		inspectorPanel.setDefaultDividerLocation( v );
140 	}
141 
142 	public void setDividerLocation( int i )
143 	{
144 		inspectorPanel.setDividerLocation( i );
145 	}
146 
147 	public void setResizeWeight( double v )
148 	{
149 		inspectorPanel.setResizeWeight( v );
150 	}
151 
152 	public void setCurrentLog( JLogList lastLog )
153 	{
154 		for( Inspector inspector : getInspectors() )
155 		{
156 			Component tabComponent = inspector.getComponent();
157 			if( tabComponent == lastLog )
158 			{
159 				activate( inspector );
160 				return;
161 			}
162 		}
163 
164 		inspectorPanel.deactivate();
165 	}
166 
167 	public void activate( Inspector inspector )
168 	{
169 		inspectorPanel.activate( inspector );
170 	}
171 
172 	public <T extends Inspector> T addInspector( T inspector )
173 	{
174 		return inspectorPanel.addInspector( inspector );
175 	}
176 
177 	public void deactivate()
178 	{
179 		inspectorPanel.deactivate();
180 	}
181 
182 	public void removeInspector( Inspector inspector )
183 	{
184 		inspectorPanel.removeInspector( inspector );
185 	}
186 
187 	public JLogList getCurrentLog()
188 	{
189 		return ( JLogList )( inspectorPanel.getCurrentInspector() == null ? null : inspectorPanel.getCurrentInspector()
190 				.getComponent() );
191 	}
192 
193 	public boolean removeLogArea( String loggerName )
194 	{
195 		for( Inspector inspector : getInspectors() )
196 		{
197 			JLogList logList = ( ( JLogList )( ( JComponentInspector<?> )inspector ).getComponent() );
198 			if( logList.getLogger( loggerName ) != null )
199 			{
200 				logList.removeLogger( loggerName );
201 				inspectorPanel.removeInspector( inspector );
202 
203 				return true;
204 			}
205 		}
206 
207 		return false;
208 	}
209 
210 	public int getDividerLocation()
211 	{
212 		return inspectorPanel.getDividerLocation();
213 	}
214 
215 	public void setContentComponent( JComponent component )
216 	{
217 		inspectorPanel.setContentComponent( component );
218 	}
219 
220 	public void release()
221 	{
222 		inspectorPanel.release();
223 	}
224 
225 	public void setResetDividerLocation()
226 	{
227 		inspectorPanel.setResetDividerLocation();
228 	}
229 
230 	public void setInspectorVisible( Inspector inspector, boolean b )
231 	{
232 		inspectorPanel.setInspectorVisible( inspector, b );
233 	}
234 
235 	public Inspector getInspector( String inspectorId )
236 	{
237 		return inspectorPanel.getInspector( inspectorId );
238 	}
239 }