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.actions.iface.tools.wsi;
14  
15  import java.awt.BorderLayout;
16  import java.awt.event.ActionEvent;
17  import java.io.File;
18  import java.io.FileWriter;
19  
20  import javax.swing.AbstractAction;
21  import javax.swing.Action;
22  import javax.swing.BorderFactory;
23  import javax.swing.JComponent;
24  import javax.swing.JPanel;
25  import javax.swing.JScrollPane;
26  import javax.swing.JTabbedPane;
27  import javax.swing.JTextArea;
28  
29  import org.apache.xmlbeans.XmlObject;
30  
31  import com.eviware.soapui.SoapUI;
32  import com.eviware.soapui.support.UISupport;
33  import com.eviware.soapui.support.components.BrowserComponent;
34  import com.eviware.soapui.support.components.JXToolBar;
35  
36  /***
37   * Panel for displaying a WS-I Report
38   * 
39   * @author ole.matzura
40   */
41  
42  public class WSIReportPanel extends JPanel
43  {
44  	private File reportFile;
45  	// private JEditorPane editorPane;
46  	private final String configFile;
47  	private final File logFile;
48  	private SaveReportAction saveReportAction;
49  	private BrowserComponent browser;
50  
51  	public WSIReportPanel( File reportFile, String configFile, File logFile, boolean addToolbar ) throws Exception
52  	{
53  		super( new BorderLayout() );
54  
55  		this.reportFile = reportFile;
56  		this.configFile = configFile;
57  		this.logFile = logFile;
58  
59  		saveReportAction = new SaveReportAction();
60  
61  		if( addToolbar )
62  			add( buildToolbar(), BorderLayout.NORTH );
63  
64  		add( buildContent(), BorderLayout.CENTER );
65  	}
66  
67  	public SaveReportAction getSaveReportAction()
68  	{
69  		return saveReportAction;
70  	}
71  
72  	private JComponent buildToolbar()
73  	{
74  		JXToolBar toolbar = UISupport.createToolbar();
75  
76  		toolbar.addFixed( UISupport.createToolbarButton( saveReportAction ) );
77  		toolbar.addGlue();
78  		toolbar.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ) );
79  
80  		return toolbar;
81  	}
82  
83  	private JComponent buildContent() throws Exception
84  	{
85  		JTabbedPane tabs = new JTabbedPane( JTabbedPane.BOTTOM );
86  
87  		// editorPane = new JEditorPane();
88  		// editorPane.setEditorKit( new HTMLEditorKit() );
89  		// editorPane.setEditable( false );
90  		// editorPane.setPage( reportFile.toURI().toURL() );
91  		// editorPane.addHyperlinkListener( new DefaultHyperlinkListener(
92  		// editorPane ));
93  
94  		JTextArea configContent = new JTextArea();
95  		configContent.setEditable( false );
96  		configContent.setText( configFile );
97  
98  		browser = new BrowserComponent( false );
99  		browser.navigate( reportFile.toURI().toURL().toString(), null );
100 
101 		tabs.addTab( "Report", browser.getComponent() );
102 		tabs.addTab( "Config", new JScrollPane( configContent ) );
103 
104 		if( logFile != null )
105 		{
106 			String logFileContent = XmlObject.Factory.parse( logFile ).toString();
107 			JTextArea logContent = new JTextArea();
108 			logContent.setEditable( false );
109 			logContent.setText( logFileContent );
110 
111 			tabs.addTab( "Log", new JScrollPane( logContent ) );
112 		}
113 
114 		return UISupport.createTabPanel( tabs, true );
115 	}
116 
117 	public class SaveReportAction extends AbstractAction
118 	{
119 		public SaveReportAction()
120 		{
121 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/export.gif" ) );
122 			putValue( Action.SHORT_DESCRIPTION, "Saves this report to a file" );
123 		}
124 
125 		public void actionPerformed( ActionEvent e )
126 		{
127 			File file = UISupport.getFileDialogs().saveAs( this, "Save Report", "html", "HTML files", null );
128 			if( file == null )
129 				return;
130 
131 			try
132 			{
133 				FileWriter writer = new FileWriter( file );
134 				writer.write( browser.getContent() );
135 				writer.close();
136 
137 				UISupport.showInfoMessage( "Report saved to [" + file.getAbsolutePath() + "]" );
138 			}
139 			catch( Exception e1 )
140 			{
141 				SoapUI.logError( e1 );
142 			}
143 		}
144 	}
145 }