001: package org.dbbrowser.ui.helper.exporthelper;
002:
003: import infrastructure.logging.Log;
004: import javax.swing.event.ChangeEvent;
005: import javax.swing.event.ChangeListener;
006: import javax.swing.table.AbstractTableModel;
007: import org.dbbrowser.ui.helper.exporthelper.wizard.WizardState;
008: import java.io.BufferedWriter;
009: import java.io.File;
010: import java.io.FileWriter;
011: import java.io.IOException;
012: import java.util.List;
013:
014: /**
015: * Class used to export a table to CSV file
016: */
017: public class ExporterForCSVFile implements ExportHelper {
018: private boolean stop = false;
019:
020: /**
021: * Export the data in the table to the specified file as a PDF file
022: * @param abstractTableModel
023: * @param changeListener
024: * @param fileToExportTo
025: * @throws ExportHelperException
026: */
027: public void export(AbstractTableModel abstractTableModel,
028: ChangeListener changeListener, File fileToExportTo)
029: throws ExportHelperException {
030: this .stop = false;
031: Log.getInstance().infoMessage("Starting CSV Export...",
032: ExporterForCSVFile.class.getName());
033:
034: StringBuffer buffer = new StringBuffer();
035: int rowCount = abstractTableModel.getRowCount();
036: int columnCount = abstractTableModel.getColumnCount();
037: List listOfColumns = (List) WizardState.getInstance().getState(
038: "List of columns to include");
039:
040: //Processing column names - start from 1 as the first column is the row number
041: for (int columnIndex = 1; columnIndex < columnCount; columnIndex++) {
042: String columnName = abstractTableModel
043: .getColumnName(columnIndex);
044:
045: //If the user wants this columns, then include it
046: if (listOfColumns.contains(columnName)) {
047: //Strip the (PK) from the column name
048: if (columnName.endsWith("(PK)")) {
049: columnName = columnName.substring(0, columnName
050: .length() - 4);
051: }
052:
053: buffer.append(columnName);
054:
055: //if it is the last column, add the new line character, else add a comma
056: if (columnIndex + 1 == columnCount) {
057: buffer.append("\n");
058: } else {
059: buffer.append(",");
060: }
061: }
062: }
063:
064: String rowHeaderString = buffer.toString();
065:
066: //Write the contents of the header
067: BufferedWriter writer = null;
068: try {
069: writer = new BufferedWriter(new FileWriter(fileToExportTo));
070:
071: //Write the header string into the stream
072: writer.write(rowHeaderString, 0, rowHeaderString.length());
073:
074: //Process each row
075: for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
076: //If stopped, exit and export incomplete table
077: if (stop == true) {
078: break;
079: }
080:
081: Log.getInstance().debugMessage(
082: "Exporting row " + (rowIndex + 1) + " of "
083: + rowCount + " as CSV...",
084: ExporterForCSVFile.class.getName());
085:
086: buffer = new StringBuffer();
087:
088: //Process each column in the row - start from 1 as the first column is the row number
089: for (int columnIndex = 1; columnIndex < columnCount; columnIndex++) {
090: Object cell = abstractTableModel.getValueAt(
091: rowIndex, columnIndex);
092: String columnName = abstractTableModel
093: .getColumnName(columnIndex);
094: //If the user wants this columns, then include it
095: if (listOfColumns.contains(columnName)) {
096: if (cell != null) {
097: buffer
098: .append("\"" + cell.toString()
099: + "\"");
100: } else {
101: buffer.append("\"\"");
102: }
103:
104: //if it is the last column, add the new line character, else add a comma
105: if (columnIndex + 1 == columnCount) {
106: buffer.append("\n");
107: } else {
108: buffer.append(",");
109: }
110: }
111: }
112:
113: String rowString = buffer.toString();
114: writer.write(rowString, 0, rowString.length());
115:
116: //Inform the change listener
117: changeListener.stateChanged(new ChangeEvent(
118: new Integer(rowIndex)));
119: }
120: } catch (IOException exc) {
121: throw new ExportHelperException(exc.getMessage());
122: } finally {
123: try {
124: writer.flush();
125: writer.close();
126: } catch (IOException exc) {
127: //Cant do anything
128: }
129: }
130: Log.getInstance().infoMessage("Finished CSV export",
131: ExporterForCSVFile.class.getName());
132: }
133:
134: /**
135: * Call this to stop the export. This is required as it may be a long process and it runs in a seperate thread
136: */
137: public void stop() {
138: this .stop = true;
139: }
140: }
|