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