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.loadtest.data.actions;
    14
    15  import java.awt.event.ActionEvent;
    16  import java.io.File;
    17  import java.io.IOException;
    18  import java.io.PrintWriter;
    19
    20  import javax.swing.AbstractAction;
    21  import javax.swing.Action;
    22  import javax.swing.table.TableModel;
    23
    24  import com.eviware.soapui.SoapUI;
    25  import com.eviware.soapui.support.UISupport;
    26
    27  /***
    28   * Simple statistics exporter, creates a comma-separated file containing a header row
    29   * and values for each test step
    30   *
    31   * @author Ole.Matzura
    32   */
    33
    34  public class ExportStatisticsAction extends AbstractAction
    35  {
    36  	private final TableModel model;
    37
    38  	public ExportStatisticsAction(TableModel model)
    39     {
    40  		this.model = model;
    41  		putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/export.gif"));
    42        putValue( Action.SHORT_DESCRIPTION, "Export statistics to a file" );
    43     }
    44
    45  	public void actionPerformed(ActionEvent e)
    46  	{
    47  		try
    48  		{
    49  			if( model.getRowCount() == 0 )
    50  			{
    51  				UISupport.showErrorMessage( "No data to export!" );
    52  				return;
    53  			}
    54
    55           File file = UISupport.getFileDialogs().saveAs(this, "Select file for export");
    56           if( file == null )
    57              return;
    58
    59  			int cnt = exportToFile(file);
    60  			UISupport.showInfoMessage( "Saved " + cnt + " rows to file [" + file.getName() + "]" );
    61  		}
    62  		catch (IOException e1)
    63  		{
    64  			SoapUI.logError( e1 );
    65  		}
    66     }
    67
    68  	public int exportToFile(File file) throws IOException
    69  	{
    70  		PrintWriter writer = new PrintWriter(file);
    71  		writerHeader(writer);
    72  		int cnt = writeData( writer);
    73  		writer.flush();
    74  		writer.close();
    75  		return cnt;
    76  	}
    77
    78  	private int writeData(PrintWriter writer)
    79  	{
    80  		int c = 0;
    81  		for(; c < model.getRowCount(); c++)
    82  		{
    83  			for( int i = 1; i < model.getColumnCount(); i++ )
    84  			{
    85  				if( i > 1 )
    86  					writer.print( ',' );
    87
    88  				writer.print( model.getValueAt( c, i ));
    89  			}
    90
    91  			writer.println();
    92  		}
    93
    94  		return c;
    95  	}
    96
    97  	private void writerHeader(PrintWriter writer)
    98  	{
    99  		for( int i = 1; i < model.getColumnCount(); i++ )
    100 		{
    101 			if( i > 1 )
    102 				writer.print( ',' );
    103
    104 			writer.print( model.getColumnName( i ));
    105 		}
    106
    107 		writer.println();
    108 	}
    109 }