001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.loadtest.data.actions;
014:
015: import java.awt.event.ActionEvent;
016: import java.io.File;
017: import java.io.IOException;
018: import java.io.PrintWriter;
019:
020: import javax.swing.AbstractAction;
021: import javax.swing.Action;
022: import javax.swing.table.TableModel;
023:
024: import com.eviware.soapui.SoapUI;
025: import com.eviware.soapui.support.UISupport;
026:
027: /**
028: * Simple statistics exporter, creates a comma-separated file containing a header row
029: * and values for each test step
030: *
031: * @author Ole.Matzura
032: */
033:
034: public class ExportStatisticsAction extends AbstractAction {
035: private final TableModel model;
036:
037: public ExportStatisticsAction(TableModel model) {
038: this .model = model;
039: putValue(Action.SMALL_ICON, UISupport
040: .createImageIcon("/export.gif"));
041: putValue(Action.SHORT_DESCRIPTION,
042: "Export statistics to a file");
043: }
044:
045: public void actionPerformed(ActionEvent e) {
046: try {
047: if (model.getRowCount() == 0) {
048: UISupport.showErrorMessage("No data to export!");
049: return;
050: }
051:
052: File file = UISupport.getFileDialogs().saveAs(this ,
053: "Select file for export");
054: if (file == null)
055: return;
056:
057: int cnt = exportToFile(file);
058: UISupport.showInfoMessage("Saved " + cnt
059: + " rows to file [" + file.getName() + "]");
060: } catch (IOException e1) {
061: SoapUI.logError(e1);
062: }
063: }
064:
065: public int exportToFile(File file) throws IOException {
066: PrintWriter writer = new PrintWriter(file);
067: writerHeader(writer);
068: int cnt = writeData(writer);
069: writer.flush();
070: writer.close();
071: return cnt;
072: }
073:
074: private int writeData(PrintWriter writer) {
075: int c = 0;
076: for (; c < model.getRowCount(); c++) {
077: for (int i = 1; i < model.getColumnCount(); i++) {
078: if (i > 1)
079: writer.print(',');
080:
081: writer.print(model.getValueAt(c, i));
082: }
083:
084: writer.println();
085: }
086:
087: return c;
088: }
089:
090: private void writerHeader(PrintWriter writer) {
091: for (int i = 1; i < model.getColumnCount(); i++) {
092: if (i > 1)
093: writer.print(',');
094:
095: writer.print(model.getColumnName(i));
096: }
097:
098: writer.println();
099: }
100: }
|