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