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.impl.wsdl.mock;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Component;
17  import java.awt.Dimension;
18  import java.awt.event.ActionEvent;
19  import java.util.Date;
20  
21  import javax.swing.AbstractAction;
22  import javax.swing.BorderFactory;
23  import javax.swing.JComponent;
24  import javax.swing.JLabel;
25  import javax.swing.JPanel;
26  import javax.swing.JScrollPane;
27  import javax.swing.JSplitPane;
28  import javax.swing.JTabbedPane;
29  import javax.swing.JTable;
30  
31  import com.eviware.soapui.SoapUI;
32  import com.eviware.soapui.impl.wsdl.panels.request.StringToStringMapTableModel;
33  import com.eviware.soapui.support.UISupport;
34  import com.eviware.soapui.support.xml.JXEditTextArea;
35  import com.eviware.soapui.support.xml.XmlUtils;
36  import com.eviware.soapui.ui.desktop.DesktopPanel;
37  import com.eviware.soapui.ui.support.DefaultDesktopPanel;
38  import com.jgoodies.forms.builder.ButtonBarBuilder;
39  
40  /***
41   * Shows a desktop-panel with the MessageExchange for a WsdlMockResult
42   * 
43   * @author Ole.Matzura
44   */
45  
46  public class ViewWsdlMockResultAction extends AbstractAction
47  {
48  	private final WsdlMockResult result;
49  	private DefaultDesktopPanel desktopPanel;
50  
51  	public ViewWsdlMockResultAction( WsdlMockResult result )
52  	{
53  		super( "Show Results" );
54  
55  		this.result = result;
56  	}
57  
58  	public void actionPerformed( ActionEvent e )
59  	{
60  		try
61  		{
62  			if( result.isDiscarded() )
63  				UISupport.showInfoMessage( "Request has been discarded.." );
64  			else
65  				UISupport.showDesktopPanel( buildFrame() );
66  		}
67  		catch( Exception ex )
68  		{
69  			SoapUI.logError( ex );
70  		}
71  	}
72  
73  	private DesktopPanel buildFrame()
74  	{
75  		if( desktopPanel == null )
76  		{
77  			String title = "Mock Result for [" + result.getMockResponse().getName() + "]";
78  			desktopPanel = new DefaultDesktopPanel( title, title, buildContent() );
79  		}
80  
81  		return desktopPanel;
82  	}
83  
84  	private JComponent buildContent()
85  	{
86  		JTabbedPane messageTabs = new JTabbedPane();
87  		messageTabs.addTab( "Request", buildRequestTab() );
88  		messageTabs.addTab( "Response", buildResponseTab() );
89  		messageTabs.setPreferredSize( new Dimension( 500, 400 ) );
90  
91  		JPanel panel = new JPanel( new BorderLayout() );
92  		panel.add( UISupport.createTabPanel( messageTabs, true ), BorderLayout.CENTER );
93  
94  		ButtonBarBuilder builder = new ButtonBarBuilder();
95  		builder.addFixed( new JLabel( "Mock Request handled at " + new Date( result.getTimestamp() ) + ", time taken: "
96  				+ result.getTimeTaken() + "ms" ) );
97  		builder.addGlue();
98  		builder.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) );
99  		panel.add( builder.getPanel(), BorderLayout.PAGE_START );
100 
101 		return panel;
102 	}
103 
104 	private Component buildResponseTab()
105 	{
106 		JXEditTextArea responseArea = JXEditTextArea.createXmlEditor( false );
107 		responseArea.setText( XmlUtils.prettyPrintXml( result.getResponseContent() ) );
108 		responseArea.setEditable( false );
109 		responseArea.setToolTipText( "Response Content" );
110 		JScrollPane scrollPane = new JScrollPane( responseArea );
111 
112 		JSplitPane split = UISupport.createVerticalSplit( new JScrollPane( new JTable( new StringToStringMapTableModel(
113 				result.getResponseHeaders(), "Header", "Value", false ) ) ), scrollPane );
114 		split.setDividerLocation( 150 );
115 		return split;
116 	}
117 
118 	private Component buildRequestTab()
119 	{
120 		JXEditTextArea resultArea = JXEditTextArea.createXmlEditor( false );
121 		resultArea.setText( XmlUtils.prettyPrintXml( result.getMockRequest().getRequestContent() ) );
122 		resultArea.setEditable( false );
123 		resultArea.setToolTipText( "Request Content" );
124 		JScrollPane scrollPane = new JScrollPane( resultArea );
125 
126 		JSplitPane split = UISupport.createVerticalSplit( new JScrollPane( new JTable( new StringToStringMapTableModel(
127 				result.getMockRequest().getRequestHeaders(), "Header", "Value", false ) ) ), scrollPane );
128 		split.setDividerLocation( 150 );
129 		return split;
130 	}
131 }