View Javadoc

1    /*
2    *  soapUI, copyright (C) 2004-2007 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.teststeps.actions;
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.JComponent;
23  import javax.swing.JLabel;
24  import javax.swing.JList;
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.model.iface.MessageExchange;
34  import com.eviware.soapui.support.UISupport;
35  import com.eviware.soapui.support.types.StringToStringMap;
36  import com.eviware.soapui.support.xml.JXEditTextArea;
37  import com.eviware.soapui.support.xml.XmlUtils;
38  import com.eviware.soapui.ui.desktop.DesktopPanel;
39  import com.eviware.soapui.ui.support.DefaultDesktopPanel;
40  
41  /***
42   * Shows a desktop-panel with the TestStepResult for a WsdlTestRequestStepResult
43   * 
44   * @author Ole.Matzura
45   */
46  
47  public class ShowMessageExchangeAction extends AbstractAction
48  {
49  	private DefaultDesktopPanel desktopPanel;
50  	private final MessageExchange messageExchange;
51  	private final String ownerName;
52  
53  	public ShowMessageExchangeAction(MessageExchange messageExchange, String ownerName)
54  	{
55  		super( "Show Results" );
56  		this.ownerName = ownerName;
57  		this.messageExchange = messageExchange;
58  	}
59  
60  	public void actionPerformed(ActionEvent e)
61  	{
62  		try
63  		{
64  			UISupport.showDesktopPanel(buildFrame());
65  		}
66  		catch (Exception ex)
67  		{
68  			SoapUI.logError( ex );
69  		}		
70  	}
71  
72  	private DesktopPanel buildFrame()
73  	{
74  		if( desktopPanel == null )
75  		{
76  			desktopPanel = new DefaultDesktopPanel( "Message Viewer", 
77  						"Message for " + ownerName, buildContent() );
78  		}
79  		
80  		return desktopPanel;
81  	}
82  
83  	private JComponent buildContent()
84  	{
85  		JTabbedPane messageTabs = new JTabbedPane();
86  		messageTabs.addTab( "Request Message", buildRequestTab() );
87  		messageTabs.addTab( "Response Message", buildResponseTab() );
88  		messageTabs.addTab( "Properties", buildPropertiesTab() );
89  		messageTabs.addTab( "Messages", buildMessagesTab() );
90  		
91  		messageTabs.setPreferredSize( new Dimension( 500, 400 ));
92  		
93  		JPanel tabPanel = UISupport.createTabPanel( messageTabs, true );
94  		
95  		Component descriptionPanel = UISupport.buildDescription( "MessageExchange Results", "See the request/response message below", null );
96  		tabPanel.add( descriptionPanel, BorderLayout.NORTH );
97  		
98  		return tabPanel;
99  	}
100 
101 	private Component buildPropertiesTab()
102 	{
103 		StringToStringMap properties = new StringToStringMap();
104 		if( messageExchange != null && messageExchange.getProperties() != null )
105 		{
106 			properties.putAll( messageExchange.getProperties() );
107 		
108 			properties.put( "Timestamp", new Date( messageExchange.getTimestamp() ).toString() );
109 			properties.put( "Time Taken", String.valueOf( messageExchange.getTimeTaken() ) );
110 		}
111 		JTable table = new JTable( new StringToStringMapTableModel( properties, "Name", "Value", false ));
112 		return new JScrollPane( table );
113 	}
114 	
115 	private Component buildMessagesTab()
116 	{
117 		String[] messages = messageExchange.getMessages();
118 		return messages == null || messages.length == 0 ? 
119 			new JLabel( "No messages to display" ) : new JScrollPane( new JList( messages ) );
120 	}
121 
122 	private Component buildResponseTab()
123 	{
124 		JXEditTextArea resultArea = JXEditTextArea.createXmlEditor();
125       if( messageExchange != null )
126       	resultArea.setText( XmlUtils.prettyPrintXml( messageExchange.getResponseContent() ) );
127       else
128       	resultArea.setText( "- null -" );
129       resultArea.setEditable( false );
130       resultArea.setToolTipText( "Response Content" );
131 		JScrollPane scrollPane = new JScrollPane( resultArea );
132 		
133 		if( messageExchange != null )
134 		{
135 			JSplitPane split = UISupport.createVerticalSplit( new JScrollPane( new JTable( 
136 						new StringToStringMapTableModel( messageExchange.getResponseHeaders(), "Header", "Value", false)) ), scrollPane );
137 			split.setDividerLocation( 150 );
138 			return split;
139 		}
140 		
141 		return scrollPane;
142 	}
143 
144 	private Component buildRequestTab()
145 	{
146 		JXEditTextArea resultArea = JXEditTextArea.createXmlEditor();
147       if( messageExchange != null )
148       	resultArea.setText( XmlUtils.prettyPrintXml( messageExchange.getRequestContent() ) );
149       else 
150       	resultArea.setText( "- null -" );
151       resultArea.setEditable( false );
152       resultArea.setToolTipText( "Request Content" );
153 		JScrollPane scrollPane = new JScrollPane( resultArea );
154 		
155 		if( messageExchange != null )
156 		{
157 			JSplitPane split = UISupport.createVerticalSplit( new JScrollPane( new JTable( 
158 						new StringToStringMapTableModel( messageExchange.getRequestHeaders(), "Header", "Value", false)) ), scrollPane );
159 			split.setDividerLocation( 150 );
160 			return split;
161 		}
162 		
163 		return scrollPane;
164 	}
165 }