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