Search in sources :

Example 1 with HtmlExportOptionsDlg

use of net.sourceforge.sqlexplorer.dialogs.HtmlExportOptionsDlg in project tdq-studio-se by Talend.

the class ExportHTMLAction method run.

/**
 * Copy all table data to clipboard
 * @see org.eclipse.jface.action.IAction#run()
 */
public void run() {
    final HtmlExportOptionsDlg dlg = new HtmlExportOptionsDlg(_table.getShell());
    if (dlg.open() != Window.OK)
        return;
    BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {

        public void run() {
            try {
                File file = new File(dlg.getFilename());
                if (file.exists()) {
                    // overwrite existing files
                    file.delete();
                }
                String charset = dlg.getCharacterSet();
                file.createNewFile();
                PrintStream writer = new PrintStream(file, charset);
                // $NON-NLS-1$
                StringBuffer buffer = new StringBuffer("");
                // get preferences
                boolean includeColumnNames = dlg.includeHeaders();
                boolean rtrim = dlg.trimSpaces();
                String nullValue = dlg.getNullValue();
                DataSet dataSet = (DataSet) _table.getData();
                if (dataSet == null) {
                    return;
                }
                // $NON-NLS-1$
                writer.println("<html>");
                // $NON-NLS-1$
                writer.println("<head>");
                // $NON-NLS-1$
                writer.print("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=");
                writer.print(charset);
                // $NON-NLS-1$
                writer.println("\">");
                // $NON-NLS-1$
                writer.println("<style type=\"text/css\">");
                // $NON-NLS-1$
                writer.println("TABLE {border-collapse: collapse;}");
                // $NON-NLS-1$
                writer.println("TH {background-color: rgb(240, 244, 245);}");
                // $NON-NLS-1$
                writer.println("TH, TD {border: 1px solid #D1D6D4;font-size: 10px;font-family: Verdana, Arial, Helvetica, sans-serif;}");
                // $NON-NLS-1$
                writer.println(".right {text-align: right;}");
                // $NON-NLS-1$
                writer.println("</style>");
                // $NON-NLS-1$
                writer.println("</head>");
                // $NON-NLS-1$
                writer.println("<body>");
                // $NON-NLS-1$
                writer.println("<table>");
                // export column names
                if (includeColumnNames) {
                    // $NON-NLS-1$
                    buffer.append("<tr>");
                    DataSet.Column[] columns = dataSet.getColumns();
                    for (int i = 0; i < columns.length; i++) {
                        // $NON-NLS-1$
                        buffer.append("<th>");
                        buffer.append(TextUtil.htmlEscape(columns[i].getCaption()));
                        // $NON-NLS-1$
                        buffer.append("</th>");
                    }
                    // $NON-NLS-1$
                    buffer.append("</tr>");
                    writer.println(buffer.toString());
                }
                // export column data
                int columnCount = _table.getColumnCount();
                for (int i = 0; i < dataSet.getRowCount(); i++) {
                    // $NON-NLS-1$
                    buffer = new StringBuffer("<tr>");
                    DataSetRow row = dataSet.getRow(i);
                    for (int j = 0; j < columnCount; j++) {
                        Object o = row.getRawObjectValue(j);
                        if (o instanceof Double || o instanceof Integer)
                            // right align numbers
                            // $NON-NLS-1$
                            buffer.append("<td class=\"right\">");
                        else
                            // $NON-NLS-1$
                            buffer.append("<td>");
                        String t = o == null ? nullValue : o.toString();
                        if (rtrim)
                            t = TextUtil.rtrim(t);
                        buffer.append(TextUtil.htmlEscape(t));
                        // $NON-NLS-1$
                        buffer.append("</td>");
                    }
                    // $NON-NLS-1$
                    buffer.append("</tr>");
                    writer.println(buffer.toString());
                }
                // $NON-NLS-1$
                writer.println("</table>");
                // $NON-NLS-1$
                writer.println("</body>");
                // $NON-NLS-1$
                writer.println("</html>");
                writer.close();
            } catch (final Exception e) {
                _table.getShell().getDisplay().asyncExec(new Runnable() {

                    public void run() {
                        MessageDialog.openError(_table.getShell(), Messages.getString("SQLResultsView.Error.Export.Title"), e.getMessage());
                        SQLExplorerPlugin.error(Messages.getString("SQLResultsView.Error.Export.Title"), e);
                    }
                });
            }
        }
    });
}
Also used : PrintStream(java.io.PrintStream) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) HtmlExportOptionsDlg(net.sourceforge.sqlexplorer.dialogs.HtmlExportOptionsDlg) File(java.io.File) DataSetRow(net.sourceforge.sqlexplorer.dataset.DataSetRow)

Aggregations

File (java.io.File)1 PrintStream (java.io.PrintStream)1 DataSet (net.sourceforge.sqlexplorer.dataset.DataSet)1 DataSetRow (net.sourceforge.sqlexplorer.dataset.DataSetRow)1 HtmlExportOptionsDlg (net.sourceforge.sqlexplorer.dialogs.HtmlExportOptionsDlg)1