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.impl.wsdl.panels.loadtest.JStatisticsGraph;
026: import com.eviware.soapui.support.UISupport;
027:
028: /**
029: * Simple samplesmodel exporter, creates a comma-separated file containing a header row
030: * and values for each test step
031: *
032: * @author Ole.Matzura
033: */
034:
035: public class ExportStatisticsHistoryAction extends AbstractAction {
036: private final JStatisticsGraph graph;
037:
038: public ExportStatisticsHistoryAction(
039: JStatisticsGraph statisticsGraph) {
040: this .graph = statisticsGraph;
041: putValue(Action.SMALL_ICON, UISupport
042: .createImageIcon("/export.gif"));
043: putValue(Action.SHORT_DESCRIPTION,
044: "Export statistics history to a file");
045: }
046:
047: public void actionPerformed(ActionEvent e) {
048: try {
049: TableModel model = graph.getModel();
050: if (model.getRowCount() == 0) {
051: UISupport.showErrorMessage("No data to export!");
052: return;
053: }
054:
055: File file = UISupport.getFileDialogs().saveAs(this ,
056: "Select file for export");
057: if (file == null)
058: return;
059:
060: int cnt = exportToFile(file, model);
061:
062: UISupport.showInfoMessage("Saved " + cnt
063: + " rows to file [" + file.getName() + "]");
064: } catch (IOException e1) {
065: SoapUI.logError(e1);
066: }
067: }
068:
069: private int exportToFile(File file, TableModel model)
070: throws IOException {
071: PrintWriter writer = new PrintWriter(file);
072: writerHeader(writer, model);
073: int cnt = writeData(writer, model);
074: writer.flush();
075: writer.close();
076: return cnt;
077: }
078:
079: private int writeData(PrintWriter writer, TableModel model) {
080: int c = 0;
081: for (; c < model.getRowCount(); c++) {
082: for (int i = 0; i < model.getColumnCount(); i++) {
083: if (i > 0)
084: writer.print(',');
085:
086: writer.print(model.getValueAt(c, i));
087: }
088:
089: writer.println();
090: }
091:
092: return c;
093: }
094:
095: private void writerHeader(PrintWriter writer, TableModel model) {
096: for (int i = 0; i < model.getColumnCount(); i++) {
097: if (i > 0)
098: writer.print(',');
099:
100: writer.print(model.getColumnName(i));
101: }
102:
103: writer.println();
104: }
105: }
|