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