1
2
3
4
5
6
7
8
9
10
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 }