This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 219117
Collapse All | Expand All

(-)db.dataview/src/org/netbeans/modules/db/dataview/output/DataViewTableUI.java (-53 / +3 lines)
Lines 326-332 Link Here
326
            public void actionPerformed(ActionEvent e) {
326
            public void actionPerformed(ActionEvent e) {
327
                try {
327
                try {
328
                    Object o = getValueAt(selectedRow, selectedColumn);
328
                    Object o = getValueAt(selectedRow, selectedColumn);
329
                    String output = (o != null) ? o.toString() : ""; //NOI18N
329
                    // TODO: Evaluate if 1 MB/1 Million Characters are a good limit
330
                    String output = convertToClipboardString(o, 1024 * 1024);
330
331
331
                    ExClipboard clipboard = Lookup.getDefault().lookup(ExClipboard.class);
332
                    ExClipboard clipboard = Lookup.getDefault().lookup(ExClipboard.class);
332
                    StringSelection strSel = new StringSelection(output);
333
                    StringSelection strSel = new StringSelection(output);
Lines 390-396 Link Here
390
                        int modelIndex = convertRowIndexToModel(rows[j]);
391
                        int modelIndex = convertRowIndexToModel(rows[j]);
391
                        Object[] insertRow = dataView.getDataViewPageContext().getCurrentRows().get(modelIndex);
392
                        Object[] insertRow = dataView.getDataViewPageContext().getCurrentRows().get(modelIndex);
392
                        String sql = dataView.getSQLStatementGenerator().generateRawInsertStatement(insertRow);
393
                        String sql = dataView.getSQLStatementGenerator().generateRawInsertStatement(insertRow);
393
                        insertSQL += sql.replaceAll("\n", "").replaceAll("\t", "") + ";\n"; // NOI18N
394
                        insertSQL += sql + ";\n"; // NOI18N
394
                    }
395
                    }
395
                    ShowSQLDialog dialog = new ShowSQLDialog();
396
                    ShowSQLDialog dialog = new ShowSQLDialog();
396
                    dialog.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
397
                    dialog.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
Lines 532-586 Link Here
532
            }
533
            }
533
        });
534
        });
534
    }
535
    }
535
536
    private void copyRowValues(boolean withHeader) {
537
        try {
538
            int[] rows = getSelectedRows();
539
            int[] columns;
540
            if (getRowSelectionAllowed()) {
541
                columns = new int[getColumnCount()];
542
                for (int a = 0; a < columns.length; a++) {
543
                    columns[a] = a;
544
                }
536
                }
545
            } else {
546
                columns = getSelectedColumns();
547
            }
548
            if (rows != null && columns != null) {
549
                StringBuilder output = new StringBuilder();
550
551
                if (withHeader) {
552
                    for (int column = 0; column < columns.length; column++) {
553
                        if (column > 0) {
554
                            output.append('\t'); //NOI18N
555
556
                        }
557
                        Object o = getColumnModel().getColumn(column).getIdentifier();
558
                        output.append(o != null ? o.toString() : ""); //NOI18N
559
560
                    }
561
                    output.append('\n'); //NOI18N
562
563
                }
564
565
                for (int row = 0; row < rows.length; row++) {
566
                    for (int column = 0; column < columns.length; column++) {
567
                        if (column > 0) {
568
                            output.append('\t'); //NOI18N
569
570
                        }
571
                        Object o = getValueAt(rows[row], columns[column]);
572
                        output.append(o != null ? o.toString() : ""); //NOI18N
573
574
                    }
575
                    output.append('\n'); //NOI18N
576
577
                }
578
                ExClipboard clipboard = Lookup.getDefault().lookup(ExClipboard.class);
579
                StringSelection strSel = new StringSelection(output.toString());
580
                clipboard.setContents(strSel, strSel);
581
            }
582
        } catch (ArrayIndexOutOfBoundsException exc) {
583
            Exceptions.printStackTrace(exc);
584
        }
585
    }
586
}
(-)db.dataview/src/org/netbeans/modules/db/dataview/output/SQLStatementGenerator.java (-3 / +25 lines)
Lines 43-52 Link Here
43
 */
43
 */
44
package org.netbeans.modules.db.dataview.output;
44
package org.netbeans.modules.db.dataview.output;
45
45
46
import java.sql.Blob;
47
import java.sql.Clob;
46
import java.sql.Connection;
48
import java.sql.Connection;
49
import java.sql.SQLException;
47
import java.sql.Types;
50
import java.sql.Types;
48
import java.util.List;
51
import java.util.List;
49
import java.util.Map;
52
import java.util.Map;
53
import java.util.logging.Level;
54
import java.util.logging.Logger;
50
import javax.swing.table.TableModel;
55
import javax.swing.table.TableModel;
51
import org.netbeans.modules.db.dataview.meta.DBColumn;
56
import org.netbeans.modules.db.dataview.meta.DBColumn;
52
import org.netbeans.modules.db.dataview.meta.DBConnectionFactory;
57
import org.netbeans.modules.db.dataview.meta.DBConnectionFactory;
Lines 54-59 Link Here
54
import org.netbeans.modules.db.dataview.meta.DBMetaDataFactory;
59
import org.netbeans.modules.db.dataview.meta.DBMetaDataFactory;
55
import org.netbeans.modules.db.dataview.meta.DBPrimaryKey;
60
import org.netbeans.modules.db.dataview.meta.DBPrimaryKey;
56
import org.netbeans.modules.db.dataview.meta.DBTable;
61
import org.netbeans.modules.db.dataview.meta.DBTable;
62
import org.netbeans.modules.db.dataview.util.BinaryToStringConverter;
57
import org.netbeans.modules.db.dataview.util.DataViewUtils;
63
import org.netbeans.modules.db.dataview.util.DataViewUtils;
58
import org.openide.util.NbBundle;
64
import org.openide.util.NbBundle;
59
65
Lines 63-69 Link Here
63
 * @author Ahimanikya Satapathy
69
 * @author Ahimanikya Satapathy
64
 */
70
 */
65
class SQLStatementGenerator {
71
class SQLStatementGenerator {
66
72
    private static final Logger LOG= Logger.getLogger(SQLStatementGenerator.class.getName());
67
    private DataViewDBTable tblMeta;
73
    private DataViewDBTable tblMeta;
68
    private DataView dataView;
74
    private DataView dataView;
69
75
Lines 458-467 Link Here
458
            return "b'" + val + "'"; // NOI18N
464
            return "b'" + val + "'"; // NOI18N
459
        } else if (DataViewUtils.isNumeric(type)) {
465
        } else if (DataViewUtils.isNumeric(type)) {
460
            return val;
466
            return val;
461
        } else {
467
        } else if (val instanceof Clob) {
462
            return "'" + val + "'"; // NOI18N
468
            try {
469
                Clob lob = (Clob) val;
470
                String result = lob.getSubString(1, (int) lob.length());
471
                return "'" + result.replace("'", "''") + "'"; // NOI18N
472
            } catch (SQLException ex) {
473
                LOG.log(Level.INFO, "Failed to read CLOB", ex);
463
        }
474
        }
475
        } else if (val instanceof Blob) {
476
            try {
477
                Blob lob = (Blob) val;
478
                byte[] result = lob.getBytes(1, (int) lob.length());
479
                return "x'" + BinaryToStringConverter.convertToString(result, 16, false) + "'"; // NOI18N
480
            } catch (SQLException ex) {
481
                LOG.log(Level.INFO, "Failed to read BLOB", ex);
464
    }
482
    }
483
        }
484
        // Fallback if previous converts fail
485
        return "'" + val.toString().replace("'", "''") + "'"; // NOI18N
486
    }
465
487
466
    private String getAutoIncrementText(int dbType) throws Exception {
488
    private String getAutoIncrementText(int dbType) throws Exception {
467
        switch (dbType) {
489
        switch (dbType) {
(-)db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java (-3 / +152 lines)
Lines 42-63 Link Here
42
package org.netbeans.modules.db.dataview.table;
42
package org.netbeans.modules.db.dataview.table;
43
43
44
import java.awt.Color;
44
import java.awt.Color;
45
import java.awt.datatransfer.StringSelection;
46
import java.awt.datatransfer.Transferable;
45
import java.awt.event.KeyEvent;
47
import java.awt.event.KeyEvent;
46
import java.awt.event.KeyListener;
48
import java.awt.event.KeyListener;
47
import java.awt.event.MouseEvent;
49
import java.awt.event.MouseEvent;
48
import java.sql.Blob;
50
import java.sql.Blob;
49
import java.sql.Clob;
51
import java.sql.Clob;
52
import java.sql.SQLException;
50
import java.sql.Timestamp;
53
import java.sql.Timestamp;
54
import java.text.DateFormat;
51
import java.text.SimpleDateFormat;
55
import java.text.SimpleDateFormat;
52
import java.util.ArrayList;
56
import java.util.ArrayList;
53
import java.util.List;
57
import java.util.List;
54
import java.util.logging.Level;
58
import java.util.logging.Level;
55
import java.util.logging.Logger;
59
import java.util.logging.Logger;
60
import javax.swing.JComponent;
56
import javax.swing.JLabel;
61
import javax.swing.JLabel;
57
import javax.swing.JTable;
62
import javax.swing.JTable;
58
import javax.swing.JTextField;
63
import javax.swing.JTextField;
59
import javax.swing.RowSorter;
64
import javax.swing.RowSorter;
60
import javax.swing.SwingUtilities;
65
import javax.swing.SwingUtilities;
66
import javax.swing.TransferHandler;
67
import javax.swing.plaf.UIResource;
61
import javax.swing.table.*;
68
import javax.swing.table.*;
62
import org.jdesktop.swingx.JXTable;
69
import org.jdesktop.swingx.JXTable;
63
import org.jdesktop.swingx.JXTableHeader;
70
import org.jdesktop.swingx.JXTableHeader;
Lines 71-87 Link Here
71
import org.netbeans.modules.db.dataview.meta.DBColumn;
78
import org.netbeans.modules.db.dataview.meta.DBColumn;
72
import org.netbeans.modules.db.dataview.output.DataView;
79
import org.netbeans.modules.db.dataview.output.DataView;
73
import org.netbeans.modules.db.dataview.table.celleditor.*;
80
import org.netbeans.modules.db.dataview.table.celleditor.*;
81
import org.netbeans.modules.db.dataview.util.BinaryToStringConverter;
74
import org.netbeans.modules.db.dataview.util.DataViewUtils;
82
import org.netbeans.modules.db.dataview.util.DataViewUtils;
75
import org.netbeans.modules.db.dataview.util.DateType;
83
import org.netbeans.modules.db.dataview.util.DateType;
84
import org.netbeans.modules.db.dataview.util.TimeType;
76
import org.netbeans.modules.db.dataview.util.TimestampType;
85
import org.netbeans.modules.db.dataview.util.TimestampType;
86
import org.openide.util.Exceptions;
87
import org.openide.util.Lookup;
88
import org.openide.util.datatransfer.ExClipboard;
77
89
78
/**
90
/**
79
 * A better-looking table than JTable, implements JXTable and a decorator to draw empty rows 
91
 * A better-looking table than JTable, implements JXTable and a decorator to
92
 * draw empty rows
80
 *
93
 *
81
 * @author Ahimanikya Satapathy
94
 * @author Ahimanikya Satapathy
82
 */
95
 */
83
public class ResultSetJXTable extends JXTableDecorator {
96
public class ResultSetJXTable extends JXTableDecorator {
84
97
98
    private DateFormat timeFormat = new SimpleDateFormat(TimeType.DEFAULT_FOMAT_PATTERN);
99
    private DateFormat dateFormat = new SimpleDateFormat(DateType.DEFAULT_FOMAT_PATTERN);
100
    private DateFormat timestampFormat = new SimpleDateFormat(TimestampType.DEFAULT_FORMAT_PATTERN);
101
    
85
    private String[] columnToolTips;
102
    private String[] columnToolTips;
86
    private final int multiplier;
103
    private final int multiplier;
87
    private static final String data = "WE WILL EITHER FIND A WAY, OR MAKE ONE."; // NOI18N
104
    private static final String data = "WE WILL EITHER FIND A WAY, OR MAKE ONE."; // NOI18N
Lines 92-97 Link Here
92
109
93
    @SuppressWarnings("OverridableMethodCallInConstructor")
110
    @SuppressWarnings("OverridableMethodCallInConstructor")
94
    public ResultSetJXTable(final DataView dataView) {
111
    public ResultSetJXTable(final DataView dataView) {
112
        this.setTransferHandler(new TableTransferHandler());
113
        
95
        this.dView = dataView;
114
        this.dView = dataView;
96
115
97
        setShowGrid(true, true);
116
        setShowGrid(true, true);
Lines 291-298 Link Here
291
        return false;
310
        return false;
292
    }
311
    }
293
    
312
    
294
    // This is mainly used for set Tooltip for column headers
313
    /**
314
     * Quote string for use in TSV (tab-separated values file
315
     *
316
     * Assumptions: column separator is \t and row separator is \n
317
     */
318
    protected String quoteIfNecessary(String value) {
319
        if (value.contains("\t") || value.contains("\n")) { //NOI18N
320
            return "\"" + value.replace("\"", "\"\"") + "\"";
321
        } else {
322
            return value;
323
        }
324
    }
295
325
326
    /**
327
     * Convert object to string representation
328
     *
329
     * @param o object to convert
330
     * @param limitSize in case of CLOBs and BLOBs limit to limitSize
331
     * bytes/chars
332
     * @return string representation of o
333
     */
334
    protected String convertToClipboardString(Object o, int limitSize) {
335
        if (o instanceof Blob) {
336
            Blob b = (Blob) o;
337
            try {
338
                if (b.length() <= limitSize) {
339
                    return BinaryToStringConverter.convertToString(b.getBytes(1, (int) b.length()), 16, false);
340
                }
341
            } catch (SQLException ex) {
342
            }
343
        } else if (o instanceof Clob) {
344
            Clob c = (Clob) o;
345
            try {
346
                if (c.length() <= limitSize) {
347
                    return c.getSubString(1, (int) c.length());
348
                }
349
            } catch (SQLException ex) {
350
            }
351
        } else if (o instanceof java.sql.Time) {
352
            return timeFormat.format((java.util.Date) o);
353
        } else if (o instanceof java.sql.Date) {
354
            return dateFormat.format((java.util.Date) o);
355
        } else if (o instanceof java.util.Date) {
356
            return timestampFormat.format((java.util.Date) o);
357
        } else if (o == null) {
358
            return "";  //NOI18N
359
        }
360
        return o.toString();
361
    }
362
363
    /**
364
     * Create TSV (tab-separated values) string from row data
365
     * 
366
     * @param withHeader include column headers?
367
     * @return Transferable for clipboard transfer
368
     */
369
    private StringSelection createTransferableTSV(boolean withHeader) {
370
        try {
371
            int[] rows = getSelectedRows();
372
            int[] columns;
373
            if (getRowSelectionAllowed()) {
374
                columns = new int[getColumnCount()];
375
                for (int a = 0; a < columns.length; a++) {
376
                    columns[a] = a;
377
                }
378
            } else {
379
                columns = getSelectedColumns();
380
            }
381
            if (rows != null && columns != null) {
382
                StringBuilder output = new StringBuilder();
383
384
                if (withHeader) {
385
                    for (int column = 0; column < columns.length; column++) {
386
                        if (column > 0) {
387
                            output.append('\t'); //NOI18N
388
389
                        }
390
                        Object o = getColumnModel().getColumn(column).getIdentifier();
391
                        String s = o != null ? o.toString() : "";
392
                        output.append(quoteIfNecessary(s));
393
                    }
394
                    output.append('\n'); //NOI18N
395
396
                }
397
398
                for (int row = 0; row < rows.length; row++) {
399
                    for (int column = 0; column < columns.length; column++) {
400
                        if (column > 0) {
401
                            output.append('\t'); //NOI18N
402
403
                        }
404
                        Object o = getValueAt(rows[row], columns[column]);
405
                        // TODO: Evaluate if 1 MB/1 Million Characters are a good limit
406
                        String s = convertToClipboardString(o, 1024 * 1024);
407
                        output.append(quoteIfNecessary(s));
408
409
                    }
410
                    output.append('\n'); //NOI18N
411
412
                }
413
                return new StringSelection(output.toString());
414
            }
415
            return null;
416
        } catch (ArrayIndexOutOfBoundsException exc) {
417
            Exceptions.printStackTrace(exc);
418
            return null;
419
        }
420
    }
421
    
422
    protected void copyRowValues(boolean withHeader) {
423
        ExClipboard clipboard = Lookup.getDefault().lookup(ExClipboard.class);
424
        StringSelection selection = createTransferableTSV(withHeader);
425
        clipboard.setContents(selection, selection);
426
    }
427
    
428
    // This is mainly used for set Tooltip for column headers
296
    private class JTableHeaderImpl extends JXTableHeader {
429
    private class JTableHeaderImpl extends JXTableHeader {
297
430
298
        public JTableHeaderImpl(TableColumnModel cm) {
431
        public JTableHeaderImpl(TableColumnModel cm) {
Lines 316-321 Link Here
316
            }
449
            }
317
        }
450
        }
318
    }
451
    }
452
453
    private class TableTransferHandler extends TransferHandler implements UIResource {
454
        /**
455
         * Map Transferable to createTransferableTSV from ResultSetJXTable
456
         * 
457
         * This is needed so that CTRL-C Action of JTable gets the same treatment
458
         * as the transfer via the copy Methods of DataTableUI
459
         */
460
        @Override
461
        protected Transferable createTransferable(JComponent c) {
462
            return createTransferableTSV(false);
319
}
463
}
320
464
321
465
        @Override
466
        public int getSourceActions(JComponent c) {
467
            return COPY;
468
        }
469
    }
470
}
(-)db.dataview/src/org/netbeans/modules/db/dataview/util/BinaryToStringConverter.java (-2 / +2 lines)
Lines 74-80 Link Here
74
    /**
74
    /**
75
     * Convert from an array of Bytes into a string.
75
     * Convert from an array of Bytes into a string.
76
     */
76
     */
77
    public static String convertToString(Byte[] data, int base, boolean showAscii) {
77
    public static String convertToString(byte[] data, int base, boolean showAscii) {
78
78
79
        if (data == null) {
79
        if (data == null) {
80
            return null;
80
            return null;
Lines 85-91 Link Here
85
85
86
        // Convert each byte and put into string buffer
86
        // Convert each byte and put into string buffer
87
        for (int i = 0; i < data.length; i++) {
87
        for (int i = 0; i < data.length; i++) {
88
            int value = data[i].byteValue();
88
            int value = data[i];
89
            String s = null;
89
            String s = null;
90
90
91
            // if user wants to see ASCII chars as characters,
91
            // if user wants to see ASCII chars as characters,
(-)db.dataview/src/org/netbeans/modules/db/dataview/util/DBReadWriteHelper.java (-1 / +1 lines)
Lines 192-198 Link Here
192
                if (rs.wasNull() || bdata == null) {
192
                if (rs.wasNull() || bdata == null) {
193
                    return null;
193
                    return null;
194
                } else {
194
                } else {
195
                    Byte[] internal = new Byte[bdata.length];
195
                    byte[] internal = new byte[bdata.length];
196
                    for (int i = 0; i < bdata.length; i++) {
196
                    for (int i = 0; i < bdata.length; i++) {
197
                        internal[i] = new Byte(bdata[i]);
197
                        internal[i] = new Byte(bdata[i]);
198
                    }
198
                    }
(-)db.dataview/test/unit/src/org/netbeans/modules/db/dataview/util/BinaryToStringConverterTest.java (-4 / +21 lines)
Lines 39-48 Link Here
39
 * 
39
 * 
40
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
40
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
41
 */
41
 */
42
43
package org.netbeans.modules.db.dataview.util;
42
package org.netbeans.modules.db.dataview.util;
44
43
44
import java.io.UnsupportedEncodingException;
45
import org.netbeans.junit.NbTestCase;
45
import org.netbeans.junit.NbTestCase;
46
import org.netbeans.modules.db.dataview.meta.DBException;
46
47
47
/**
48
/**
48
 *
49
 *
Lines 54-61 Link Here
54
        super(testName);
55
        super(testName);
55
    }
56
    }
56
57
57
    public static  org.netbeans.junit.NbTest suite() {
58
    public static org.netbeans.junit.NbTest suite() {
58
         org.netbeans.junit.NbTestSuite suite = new  org.netbeans.junit.NbTestSuite(BinaryToStringConverterTest.class);
59
        org.netbeans.junit.NbTestSuite suite = new org.netbeans.junit.NbTestSuite(BinaryToStringConverterTest.class);
59
        return suite;
60
        return suite;
60
    }
61
    }
61
62
Lines 73-79 Link Here
73
     * Test of convertToString method, of class BinaryToStringConverter.
74
     * Test of convertToString method, of class BinaryToStringConverter.
74
     */
75
     */
75
    public void testConvertToString() {
76
    public void testConvertToString() {
76
        Byte[] data = new Byte[1];
77
        byte[] data = new byte[1];
77
        int base = 10;
78
        int base = 10;
78
        boolean showAscii = false;
79
        boolean showAscii = false;
79
        String expResult = "022";
80
        String expResult = "022";
Lines 82-85 Link Here
82
        assertEquals(expResult, result);
83
        assertEquals(expResult, result);
83
    }
84
    }
84
85
86
    /**
87
     * Test SQL hexadecimal encoding - adapted from http://dev.mysql.com/doc/refman/5.0/en/hexadecimal-literals.html
88
     */
89
    public void testConvertToString2() throws UnsupportedEncodingException, DBException {
90
        byte[] data = "\u0000MySQL".getBytes("ASCII");
91
        int base = 16;
92
        boolean showAscii = false;
93
        String expResult = "004d7953514c";
94
        String result = BinaryToStringConverter.convertToString(data, base, showAscii);
95
        assertEquals(expResult, result);
96
        
97
        data = "Paul".getBytes("ASCII");
98
        expResult = "5061756c";
99
        result = BinaryToStringConverter.convertToString(data, base, showAscii);
100
        assertEquals(expResult, result);
85
}
101
}
102
}

Return to bug 219117