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.impl.wsdl.panels.loadtest.JStatisticsHistoryGraph;
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 ExportSamplesHistoryAction extends AbstractAction
35 {
36 private final JStatisticsHistoryGraph graph;
37
38 public ExportSamplesHistoryAction(JStatisticsHistoryGraph historyGraph)
39 {
40 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/export.gif"));
41 putValue( Action.SHORT_DESCRIPTION, "Export samples history to a file" );
42
43 this.graph = historyGraph;
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 e1.printStackTrace();
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 }