View Javadoc

1    /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.Component;
16  import java.awt.Dimension;
17  import java.awt.event.ActionEvent;
18  import java.lang.ref.SoftReference;
19  import java.util.Date;
20  
21  import javax.swing.AbstractAction;
22  import javax.swing.JComponent;
23  import javax.swing.JScrollPane;
24  import javax.swing.JSplitPane;
25  import javax.swing.JTabbedPane;
26  import javax.swing.JTable;
27  
28  import com.eviware.soapui.impl.wsdl.panels.request.StringToStringMapTableModel;
29  import com.eviware.soapui.model.iface.MessageExchange;
30  import com.eviware.soapui.support.UISupport;
31  import com.eviware.soapui.support.types.StringToStringMap;
32  import com.eviware.soapui.support.xml.JXEditTextArea;
33  import com.eviware.soapui.support.xml.XmlUtils;
34  import com.eviware.soapui.ui.desktop.DesktopPanel;
35  import com.eviware.soapui.ui.support.DefaultDesktopPanel;
36  
37  /***
38   * Shows a desktop-panel with the TestStepResult for a WsdlTestRequestStepResult
39   * 
40   * @author Ole.Matzura
41   */
42  
43  public class ShowMessageExchangeAction extends AbstractAction
44  {
45  	private DefaultDesktopPanel desktopPanel;
46  	private final SoftReference<MessageExchange> reference;
47  	private final String ownerName;
48  
49  	public ShowMessageExchangeAction(MessageExchange messageExchange, String ownerName)
50  	{
51  		super( "Show Results" );
52  		this.ownerName = ownerName;
53  		this.reference = new SoftReference<MessageExchange>( messageExchange );
54  	}
55  
56  	public void actionPerformed(ActionEvent e)
57  	{
58  		try
59  		{
60  			if( reference.get() == null )
61  				UISupport.showInfoMessage( "Message has been discarded.." );
62  			else
63  				UISupport.showDesktopPanel(buildFrame());
64  		}
65  		catch (Exception ex)
66  		{
67  			ex.printStackTrace();
68  		}		
69  	}
70  
71  	private DesktopPanel buildFrame()
72  	{
73  		if( desktopPanel == null )
74  		{
75  			desktopPanel = new DefaultDesktopPanel( "Message Viewer", 
76  						"Message for " + ownerName, buildContent() );
77  		}
78  		
79  		return desktopPanel;
80  	}
81  
82  	private JComponent buildContent()
83  	{
84  		JTabbedPane messageTabs = new JTabbedPane();
85  		messageTabs.addTab( "Request Message", buildRequestTab() );
86  		messageTabs.addTab( "Response Message", buildResponseTab() );
87  		messageTabs.addTab( "Properties", buildPropertiesTab() );
88  		messageTabs.setPreferredSize( new Dimension( 500, 400 ));
89  		
90  		return UISupport.createTabPanel( messageTabs, true );
91  	}
92  
93  	private Component buildPropertiesTab()
94  	{
95  		MessageExchange messageExchange = reference.get();
96  		StringToStringMap properties = new StringToStringMap();
97  		if( messageExchange != null && messageExchange.getProperties() != null )
98  		{
99  			properties.putAll( messageExchange.getProperties() );
100 		
101 			properties.put( "Timestamp", new Date( messageExchange.getTimestamp() ).toString() );
102 			properties.put( "Time Taken", String.valueOf( messageExchange.getTimeTaken() ) );
103 		}
104 		JTable table = new JTable( new StringToStringMapTableModel( properties, "Name", "Value", false ));
105 		return new JScrollPane( table );
106 	}
107 
108 	private Component buildResponseTab()
109 	{
110 		JXEditTextArea resultArea = JXEditTextArea.createXmlEditor();
111       MessageExchange messageExchange = reference.get();
112       if( messageExchange != null )
113       	resultArea.setText( XmlUtils.prettyPrintXml( messageExchange.getResponseContent() ) );
114       else
115       	resultArea.setText( "- null -" );
116       resultArea.setEditable( false );
117       resultArea.setToolTipText( "Response Content" );
118 		JScrollPane scrollPane = new JScrollPane( resultArea );
119 		
120 		if( messageExchange != null )
121 		{
122 			JSplitPane split = UISupport.createVerticalSplit( new JScrollPane( new JTable( 
123 						new StringToStringMapTableModel( messageExchange.getResponseHeaders(), "Header", "Value", false)) ), scrollPane );
124 			split.setDividerLocation( 150 );
125 			return split;
126 		}
127 		
128 		return scrollPane;
129 	}
130 
131 	private Component buildRequestTab()
132 	{
133 		JXEditTextArea resultArea = JXEditTextArea.createXmlEditor();
134       MessageExchange messageExchange = reference.get();
135       if( messageExchange != null )
136       	resultArea.setText( XmlUtils.prettyPrintXml( messageExchange.getRequestContent() ) );
137       else 
138       	resultArea.setText( "- null -" );
139       resultArea.setEditable( false );
140       resultArea.setToolTipText( "Request Content" );
141 		JScrollPane scrollPane = new JScrollPane( resultArea );
142 		
143 		if( messageExchange != null )
144 		{
145 			JSplitPane split = UISupport.createVerticalSplit( new JScrollPane( new JTable( 
146 						new StringToStringMapTableModel( messageExchange.getRequestHeaders(), "Header", "Value", false)) ), scrollPane );
147 			split.setDividerLocation( 150 );
148 			return split;
149 		}
150 		
151 		return scrollPane;
152 	}
153 }