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