Search in sources :

Example 1 with BasicTransferable

use of org.jdesktop.swingx.plaf.basic.core.BasicTransferable in project zaproxy by zaproxy.

the class ScanProgressDialog method getCopyToClipboardButton.

private JButton getCopyToClipboardButton() {
    if (copyToClipboardButton == null) {
        copyToClipboardButton = new JButton(Constant.messages.getString("ascan.progress.copyclipboard.button.label"));
        copyToClipboardButton.setToolTipText(Constant.messages.getString("ascan.progress.copyclipboard.button.tooltip"));
        copyToClipboardButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent evt) {
                // Mimics the implementation of BasicTableUI.TableTransferHandler.createTransferable(JComponent) but copies
                // all rows (including column names), not just selected rows/columns (which are none in this case).
                StringBuilder plainContent = new StringBuilder();
                StringBuilder htmlContent = new StringBuilder();
                htmlContent.append("<html>\n<body>\n<table>\n");
                TableModel tableModel = getMainPanel().getModel();
                htmlContent.append("<tr>\n");
                for (int col = 0; col < tableModel.getColumnCount(); col++) {
                    String val = tableModel.getColumnName(col);
                    plainContent.append(val).append('\t');
                    htmlContent.append("  <td>").append(val).append("</td>\n");
                }
                plainContent.deleteCharAt(plainContent.length() - 1).append("\n");
                htmlContent.append("</tr>\n");
                for (int row = 0; row < tableModel.getRowCount(); row++) {
                    htmlContent.append("<tr>\n");
                    for (int col = 0; col < tableModel.getColumnCount(); col++) {
                        Object obj = tableModel.getValueAt(row, col);
                        String val = (obj == null) ? "" : obj.toString();
                        plainContent.append(val).append('\t');
                        htmlContent.append("  <td>").append(val).append("</td>\n");
                    }
                    plainContent.deleteCharAt(plainContent.length() - 1).append("\n");
                    htmlContent.append("</tr>\n");
                }
                plainContent.deleteCharAt(plainContent.length() - 1);
                htmlContent.append("</table>\n</body>\n</html>");
                Transferable transferable = new BasicTransferable(plainContent.toString(), htmlContent.toString());
                try {
                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(transferable, null);
                } catch (IllegalStateException e) {
                    View.getSingleton().showWarningDialog(ScanProgressDialog.this, Constant.messages.getString("ascan.progress.copyclipboard.error"));
                    log.warn("Failed to copy the contents to clipboard:", e);
                }
            }
        });
    }
    return copyToClipboardButton;
}
Also used : BasicTransferable(org.jdesktop.swingx.plaf.basic.core.BasicTransferable) ActionListener(java.awt.event.ActionListener) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) Transferable(java.awt.datatransfer.Transferable) BasicTransferable(org.jdesktop.swingx.plaf.basic.core.BasicTransferable) TableModel(javax.swing.table.TableModel)

Aggregations

Transferable (java.awt.datatransfer.Transferable)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 JButton (javax.swing.JButton)1 TableModel (javax.swing.table.TableModel)1 BasicTransferable (org.jdesktop.swingx.plaf.basic.core.BasicTransferable)1