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
    51  	public WSIReportPanel( File reportFile, String configFile, File logFile ) throws Exception
    52  	{
    53  		super( new BorderLayout() );
    54
    55  		this.reportFile = reportFile;
    56  		this.configFile = configFile;
    57  		this.logFile = logFile;
    58
    59  		add( buildToolbar(), BorderLayout.NORTH );
    60  		add( buildContent(), BorderLayout.CENTER );
    61  	}
    62
    63  	private JComponent buildToolbar()
    64  	{
    65  		JXToolBar toolbar = UISupport.createToolbar();
    66
    67  		toolbar.addFixed( UISupport.createToolbarButton( new SaveReportAction() ));
    68  		toolbar.addGlue();
    69  		toolbar.setBorder( BorderFactory.createEmptyBorder( 3, 3, 3, 3 ));
    70
    71  		return toolbar;
    72  	}
    73
    74  	private JComponent buildContent() throws Exception
    75  	{
    76  		JTabbedPane tabs = new JTabbedPane( JTabbedPane.BOTTOM );
    77
    78  		editorPane = new JEditorPane();
    79  		editorPane.setEditorKit( new HTMLEditorKit() );
    80  		editorPane.setEditable( false );
    81  		editorPane.setPage( reportFile.toURL() );
    82  		editorPane.addHyperlinkListener( new DefaultHyperlinkListener( editorPane ));
    83
    84  		JTextArea configContent = new JTextArea( );
    85  		configContent.setEditable( false );
    86  		configContent.setText( configFile );
    87
    88  		tabs.addTab( "Report", new JScrollPane( editorPane ));
    89  		tabs.addTab( "Config", new JScrollPane( configContent ));
    90
    91  		if( logFile != null )
    92  		{
    93  			String logFileContent = XmlObject.Factory.parse( logFile ).toString();
    94  			JTextArea logContent = new JTextArea( );
    95  			logContent.setEditable( false );
    96  			logContent.setText( logFileContent );
    97
    98  			tabs.addTab( "Log", new JScrollPane( logContent ));
    99  		}
    100
    101 		return UISupport.createTabPanel( tabs, true );
    102 	}
    103
    104 	private class SaveReportAction extends AbstractAction
    105 	{
    106 		public SaveReportAction()
    107 		{
    108 			putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/export.gif"));
    109 	      putValue( Action.SHORT_DESCRIPTION, "Saves this report to a file" );
    110 		}
    111
    112 		public void actionPerformed(ActionEvent e)
    113 		{
    114 			File file = UISupport.getFileDialogs().saveAs( this, "Save Report", "html", "HTML files", null );
    115 			if( file == null )
    116 				return;
    117
    118 			try
    119 			{
    120 				FileWriter writer = new FileWriter( file );
    121 				writer.write( editorPane.getText() );
    122 				writer.close();
    123
    124 				UISupport.showInfoMessage( "Report saved to [" + file.getAbsolutePath() + "]" );
    125 			}
    126 			catch (Exception e1)
    127 			{
    128 				SoapUI.logError( e1 );
    129 			}
    130 		}
    131 	}
    132 }