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 219011
Collapse All | Expand All

(-)db.dataview/src/org/netbeans/modules/db/dataview/output/InsertRecordDialog.java (-55 / +55 lines)
Lines 100-116 Link Here
100
class InsertRecordDialog extends javax.swing.JDialog {
100
class InsertRecordDialog extends javax.swing.JDialog {
101
101
102
    private final DataView dataView;
102
    private final DataView dataView;
103
    InsertRecordTableUI jTable1;
103
    InsertRecordTableUI insertRecordTableUI;
104
    private JXTableRowHeader rowHeader;
104
    private JXTableRowHeader rowHeader;
105
105
106
    public InsertRecordDialog(DataView dataView) {
106
    public InsertRecordDialog(DataView dataView) {
107
        super(WindowManager.getDefault().getMainWindow(), true);
107
        super(WindowManager.getDefault().getMainWindow(), true);
108
        this.dataView = dataView;
108
        this.dataView = dataView;
109
        jTable1 = new InsertRecordTableUI(dataView) {
109
        insertRecordTableUI = new InsertRecordTableUI(dataView) {
110
110
111
            @Override
111
            @Override
112
            public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
112
            public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) {
113
                if (rowIndex != -1 && columnIndex != -1 && ((DefaultTableModel) jTable1.getModel()).getRowCount() > 1) {
113
                if (rowIndex != -1 && columnIndex != -1 && ((DefaultTableModel) insertRecordTableUI.getModel()).getRowCount() > 1) {
114
                    removeBtn.setEnabled(true);
114
                    removeBtn.setEnabled(true);
115
                }
115
                }
116
                AWTEvent awtEvent = EventQueue.getCurrentEvent();
116
                AWTEvent awtEvent = EventQueue.getCurrentEvent();
Lines 135-143 Link Here
135
        };
135
        };
136
        initComponents();
136
        initComponents();
137
        addInputFields();
137
        addInputFields();
138
        jTable1.addKeyListener(new TableKeyListener());
138
        insertRecordTableUI.addKeyListener(new TableKeyListener());
139
139
140
        jTable1.getModel().addTableModelListener(new TableListener());
140
        insertRecordTableUI.getModel().addTableModelListener(new TableListener());
141
141
142
        jSplitPane1.setBottomComponent(null);
142
        jSplitPane1.setBottomComponent(null);
143
143
Lines 331-342 Link Here
331
    }// </editor-fold>//GEN-END:initComponents
331
    }// </editor-fold>//GEN-END:initComponents
332
332
333
private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtnActionPerformed
333
private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtnActionPerformed
334
    DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
334
    DefaultTableModel model = (DefaultTableModel) insertRecordTableUI.getModel();
335
    model.addRow(jTable1.createNewRow());
335
    model.addRow(insertRecordTableUI.createNewRow());
336
}//GEN-LAST:event_addBtnActionPerformed
336
}//GEN-LAST:event_addBtnActionPerformed
337
337
338
private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeBtnActionPerformed
338
private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeBtnActionPerformed
339
    jTable1.removeRows();
339
    insertRecordTableUI.removeRows();
340
    removeBtn.setEnabled(false);
340
    removeBtn.setEnabled(false);
341
}//GEN-LAST:event_removeBtnActionPerformed
341
}//GEN-LAST:event_removeBtnActionPerformed
342
342
Lines 362-369 Link Here
362
    }
362
    }
363
363
364
    private void executeBtnActionPerformed(java.awt.event.ActionEvent evt) {
364
    private void executeBtnActionPerformed(java.awt.event.ActionEvent evt) {
365
        if (jTable1.isEditing()) {
365
        if (insertRecordTableUI.isEditing()) {
366
            jTable1.getCellEditor().stopCellEditing();
366
            insertRecordTableUI.getCellEditor().stopCellEditing();
367
        }
367
        }
368
        // Get out of AWT thread because SQLExecutionHelper does calls to AWT
368
        // Get out of AWT thread because SQLExecutionHelper does calls to AWT
369
        // and we need to wait here to show possible exceptions.
369
        // and we need to wait here to show possible exceptions.
Lines 374-380 Link Here
374
                String insertSQL = null;
374
                String insertSQL = null;
375
                SQLStatementGenerator stmtBldr = dataView.getSQLStatementGenerator();
375
                SQLStatementGenerator stmtBldr = dataView.getSQLStatementGenerator();
376
                SQLExecutionHelper execHelper = dataView.getSQLExecutionHelper();
376
                SQLExecutionHelper execHelper = dataView.getSQLExecutionHelper();
377
                for (int i = 0; i < jTable1.getRowCount(); i++) {
377
                for (int i = 0; i < insertRecordTableUI.getRowCount(); i++) {
378
                    boolean wasException = false;
378
                    boolean wasException = false;
379
                    try {
379
                    try {
380
                        Object[] insertedRow = getInsertValues(i);
380
                        Object[] insertedRow = getInsertValues(i);
Lines 390-396 Link Here
390
                    if (wasException) {
390
                    if (wasException) {
391
                        // remove i already inserted
391
                        // remove i already inserted
392
                        for (int j = 0; j < i; j++) {
392
                        for (int j = 0; j < i; j++) {
393
                            ((DefaultTableModel) jTable1.getModel()).removeRow(0);
393
                            ((DefaultTableModel) insertRecordTableUI.getModel()).removeRow(0);
394
                        }
394
                        }
395
                        // return without closing
395
                        // return without closing
396
                        return;
396
                        return;
Lines 419-425 Link Here
419
            String sqlText = "";
419
            String sqlText = "";
420
            if (jSplitPane1.getBottomComponent() != null) {
420
            if (jSplitPane1.getBottomComponent() != null) {
421
                SQLStatementGenerator stmtBldr = dataView.getSQLStatementGenerator();
421
                SQLStatementGenerator stmtBldr = dataView.getSQLStatementGenerator();
422
                for (int i = 0; i < jTable1.getRowCount(); i++) {
422
                for (int i = 0; i < insertRecordTableUI.getRowCount(); i++) {
423
                    String sql = stmtBldr.generateRawInsertStatement(getInsertValues(i));
423
                    String sql = stmtBldr.generateRawInsertStatement(getInsertValues(i));
424
                    sqlText = sqlText + sql + "\n";
424
                    sqlText = sqlText + sql + "\n";
425
                }
425
                }
Lines 442-451 Link Here
442
442
443
    private void addInputFields() {
443
    private void addInputFields() {
444
        List<Object[]> rows = new ArrayList<Object[]>();
444
        List<Object[]> rows = new ArrayList<Object[]>();
445
        rows.add(jTable1.createNewRow());
445
        rows.add(insertRecordTableUI.createNewRow());
446
        jScrollPane1.setViewportView(jTable1);
446
        jScrollPane1.setViewportView(insertRecordTableUI);
447
        rowHeader = new JXTableRowHeader(jTable1);
447
        rowHeader = new JXTableRowHeader(insertRecordTableUI);
448
        final Component order[] = new Component[]{rowHeader, jTable1};
448
        final Component order[] = new Component[]{rowHeader, insertRecordTableUI};
449
        FocusTraversalPolicy policy = new FocusTraversalPolicy() {
449
        FocusTraversalPolicy policy = new FocusTraversalPolicy() {
450
450
451
            List componentList = Arrays.asList(order);
451
            List componentList = Arrays.asList(order);
Lines 463-473 Link Here
463
            @Override
463
            @Override
464
            public Component getComponentAfter(Container focusCycleRoot, Component aComponent) {              
464
            public Component getComponentAfter(Container focusCycleRoot, Component aComponent) {              
465
                if (aComponent instanceof JXTableRowHeader) {
465
                if (aComponent instanceof JXTableRowHeader) {
466
                    int rowIndex = jTable1.getRowCount() - 1;
466
                    int rowIndex = insertRecordTableUI.getRowCount() - 1;
467
                    jTable1.editCellAt(rowIndex, 0);
467
                    insertRecordTableUI.editCellAt(rowIndex, 0);
468
                    jTable1.setRowSelectionInterval(rowIndex, 0);
468
                    insertRecordTableUI.setRowSelectionInterval(rowIndex, 0);
469
                }
469
                }
470
                return jTable1;
470
                return insertRecordTableUI;
471
            }
471
            }
472
472
473
            @Override
473
            @Override
Lines 484-501 Link Here
484
        setFocusTraversalPolicy(policy);
484
        setFocusTraversalPolicy(policy);
485
        jScrollPane1.setRowHeaderView(rowHeader);
485
        jScrollPane1.setRowHeaderView(rowHeader);
486
        jScrollPane1.setCorner(JScrollPane.UPPER_LEFT_CORNER, rowHeader.getTableHeader());
486
        jScrollPane1.setCorner(JScrollPane.UPPER_LEFT_CORNER, rowHeader.getTableHeader());
487
        jTable1.createTableModel(rows, rowHeader);
487
        insertRecordTableUI.createTableModel(rows, rowHeader);
488
    }
488
    }
489
489
490
    private Object[] getInsertValues(int row) throws DBException {
490
    private Object[] getInsertValues(int row) throws DBException {
491
        DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
491
        DefaultTableModel model = (DefaultTableModel) insertRecordTableUI.getModel();
492
        int rsColumnCount = jTable1.getRSColumnCount();
492
        int rsColumnCount = insertRecordTableUI.getRSColumnCount();
493
        Object[] insertData = new Object[rsColumnCount];
493
        Object[] insertData = new Object[rsColumnCount];
494
        if (jTable1.getRowCount() <= 0) {
494
        if (insertRecordTableUI.getRowCount() <= 0) {
495
            return insertData;
495
            return insertData;
496
        }
496
        }
497
        for (int i = 0; i < rsColumnCount; i++) {
497
        for (int i = 0; i < rsColumnCount; i++) {
498
            DBColumn col = jTable1.getDBColumn(i);
498
            DBColumn col = insertRecordTableUI.getDBColumn(i);
499
            Object val = model.getValueAt(row, i);
499
            Object val = model.getValueAt(row, i);
500
500
501
            // Check for Constant e.g <NULL>, <DEFAULT>, <CURRENT_TIMESTAMP> etc
501
            // Check for Constant e.g <NULL>, <DEFAULT>, <CURRENT_TIMESTAMP> etc
Lines 541-547 Link Here
541
            paste();
541
            paste();
542
        }
542
        }
543
        if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_DELETE) {
543
        if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_DELETE) {
544
            jTable1.removeRows();
544
            insertRecordTableUI.removeRows();
545
        } else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
545
        } else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
546
            setFocusable(false);
546
            setFocusable(false);
547
        } else if (e.isControlDown() && e.getKeyChar() == KeyEvent.VK_0) {
547
        } else if (e.isControlDown() && e.getKeyChar() == KeyEvent.VK_0) {
Lines 555-564 Link Here
555
555
556
    private void copy() {
556
    private void copy() {
557
        StringBuilder strBuffer = new StringBuilder();
557
        StringBuilder strBuffer = new StringBuilder();
558
        int numcols = jTable1.getSelectedColumnCount();
558
        int numcols = insertRecordTableUI.getSelectedColumnCount();
559
        int numrows = jTable1.getSelectedRowCount();
559
        int numrows = insertRecordTableUI.getSelectedRowCount();
560
        int[] rowsselected = jTable1.getSelectedRows();
560
        int[] rowsselected = insertRecordTableUI.getSelectedRows();
561
        int[] colsselected = jTable1.getSelectedColumns();
561
        int[] colsselected = insertRecordTableUI.getSelectedColumns();
562
        if (!((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0] && numrows == rowsselected.length) &&
562
        if (!((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0] && numrows == rowsselected.length) &&
563
                (numcols - 1 == colsselected[colsselected.length - 1] - colsselected[0] && numcols == colsselected.length))) {
563
                (numcols - 1 == colsselected[colsselected.length - 1] - colsselected[0] && numcols == colsselected.length))) {
564
            JOptionPane.showMessageDialog(null, "Invalid Copy Selection", "Invalid Copy Selection", JOptionPane.ERROR_MESSAGE);
564
            JOptionPane.showMessageDialog(null, "Invalid Copy Selection", "Invalid Copy Selection", JOptionPane.ERROR_MESSAGE);
Lines 566-572 Link Here
566
        }
566
        }
567
        for (int i = 0; i < numrows; i++) {
567
        for (int i = 0; i < numrows; i++) {
568
            for (int j = 0; j < numcols; j++) {
568
            for (int j = 0; j < numcols; j++) {
569
                strBuffer.append(jTable1.getValueAt(rowsselected[i], colsselected[j]));
569
                strBuffer.append(insertRecordTableUI.getValueAt(rowsselected[i], colsselected[j]));
570
                if (j < numcols - 1) {
570
                if (j < numcols - 1) {
571
                    strBuffer.append("\t");
571
                    strBuffer.append("\t");
572
                }
572
                }
Lines 580-592 Link Here
580
580
581
    private void paste() {
581
    private void paste() {
582
        String rowstring, value;
582
        String rowstring, value;
583
        int startRow = (jTable1.getSelectedRows())[0];
583
        int startRow = (insertRecordTableUI.getSelectedRows())[0];
584
        int startCol = (jTable1.getSelectedColumns())[0];
584
        int startCol = (insertRecordTableUI.getSelectedColumns())[0];
585
        try {
585
        try {
586
            String trstring = (String) (clipBoard.getContents(this).getTransferData(DataFlavor.stringFlavor));
586
            String trstring = (String) (clipBoard.getContents(this).getTransferData(DataFlavor.stringFlavor));
587
            StringTokenizer st1 = new StringTokenizer(trstring, "\n");
587
            StringTokenizer st1 = new StringTokenizer(trstring, "\n");
588
            if (jTable1.getSelectedRows().length < st1.countTokens()) {
588
            if (insertRecordTableUI.getSelectedRows().length < st1.countTokens()) {
589
                int rowCnt = st1.countTokens() - jTable1.getSelectedRows().length;
589
                int rowCnt = st1.countTokens() - insertRecordTableUI.getSelectedRows().length;
590
                for (int cnt = 0; cnt < rowCnt; cnt++) {
590
                for (int cnt = 0; cnt < rowCnt; cnt++) {
591
                    addBtnActionPerformed(null);
591
                    addBtnActionPerformed(null);
592
                }
592
                }
Lines 596-603 Link Here
596
                StringTokenizer st2 = new StringTokenizer(rowstring, "\t");
596
                StringTokenizer st2 = new StringTokenizer(rowstring, "\t");
597
                for (int j = 0; st2.hasMoreTokens(); j++) {
597
                for (int j = 0; st2.hasMoreTokens(); j++) {
598
                    value = st2.nextToken();
598
                    value = st2.nextToken();
599
                    if (startRow + i < jTable1.getRowCount() && startCol + j < jTable1.getColumnCount()) {
599
                    if (startRow + i < insertRecordTableUI.getRowCount() && startCol + j < insertRecordTableUI.getColumnCount()) {
600
                        jTable1.setValueAt(value, startRow + i, startCol + j);
600
                        insertRecordTableUI.setValueAt(value, startRow + i, startCol + j);
601
                    }
601
                    }
602
                }
602
                }
603
            }
603
            }
Lines 607-657 Link Here
607
    }
607
    }
608
608
609
    private void control0Event() {
609
    private void control0Event() {
610
        int row = jTable1.getSelectedRow();
610
        int row = insertRecordTableUI.getSelectedRow();
611
        int col = jTable1.getSelectedColumn();
611
        int col = insertRecordTableUI.getSelectedColumn();
612
        if (row == -1) {
612
        if (row == -1) {
613
            return;
613
            return;
614
        }
614
        }
615
        jTable1.editCellAt(row, col);
615
        insertRecordTableUI.editCellAt(row, col);
616
        TableCellEditor editor = jTable1.getCellEditor();
616
        TableCellEditor editor = insertRecordTableUI.getCellEditor();
617
        if (editor != null) {
617
        if (editor != null) {
618
            DBColumn dbcol = dataView.getDataViewDBTable().getColumn(col);
618
            DBColumn dbcol = dataView.getDataViewDBTable().getColumn(col);
619
            if (dbcol.isGenerated() || !dbcol.isNullable()) {
619
            if (dbcol.isGenerated() || !dbcol.isNullable()) {
620
                Toolkit.getDefaultToolkit().beep();
620
                Toolkit.getDefaultToolkit().beep();
621
                editor.stopCellEditing();
621
                editor.stopCellEditing();
622
            } else {
622
            } else {
623
                editor.getTableCellEditorComponent(jTable1, null, jTable1.isRowSelectionAllowed, row, col);
623
                editor.getTableCellEditorComponent(insertRecordTableUI, null, insertRecordTableUI.isRowSelectionAllowed, row, col);
624
                jTable1.setValueAt(null, row, col);
624
                insertRecordTableUI.setValueAt(null, row, col);
625
                editor.stopCellEditing();
625
                editor.stopCellEditing();
626
            }
626
            }
627
            jTable1.setRowSelectionInterval(row, row);
627
            insertRecordTableUI.setRowSelectionInterval(row, row);
628
        }
628
        }
629
    }
629
    }
630
630
631
    private void control1Event() {
631
    private void control1Event() {
632
        int row = jTable1.getSelectedRow();
632
        int row = insertRecordTableUI.getSelectedRow();
633
        int col = jTable1.getSelectedColumn();
633
        int col = insertRecordTableUI.getSelectedColumn();
634
        if (row == -1) {
634
        if (row == -1) {
635
            return;
635
            return;
636
        }
636
        }
637
        jTable1.editCellAt(row, col);
637
        insertRecordTableUI.editCellAt(row, col);
638
        TableCellEditor editor = jTable1.getCellEditor();
638
        TableCellEditor editor = insertRecordTableUI.getCellEditor();
639
        if (editor != null) {
639
        if (editor != null) {
640
            DBColumn dbcol = dataView.getDataViewDBTable().getColumn(col);
640
            DBColumn dbcol = dataView.getDataViewDBTable().getColumn(col);
641
            Object val = jTable1.getValueAt(row, col);
641
            Object val = insertRecordTableUI.getValueAt(row, col);
642
            if (dbcol.isGenerated() || !dbcol.hasDefault()) {
642
            if (dbcol.isGenerated() || !dbcol.hasDefault()) {
643
                Toolkit.getDefaultToolkit().beep();
643
                Toolkit.getDefaultToolkit().beep();
644
                editor.stopCellEditing();
644
                editor.stopCellEditing();
645
            } else if (val != null && val instanceof String && ((String) val).equals("<DEFAULT>")) {
645
            } else if (val != null && val instanceof String && ((String) val).equals("<DEFAULT>")) {
646
                editor.getTableCellEditorComponent(jTable1, "", jTable1.isRowSelectionAllowed, row, col);
646
                editor.getTableCellEditorComponent(insertRecordTableUI, "", insertRecordTableUI.isRowSelectionAllowed, row, col);
647
                jTable1.setValueAt(null, row, col);
647
                insertRecordTableUI.setValueAt(null, row, col);
648
                editor.stopCellEditing();
648
                editor.stopCellEditing();
649
            } else {
649
            } else {
650
                editor.getTableCellEditorComponent(jTable1, "<DEFAULT>", jTable1.isRowSelectionAllowed, row, col);
650
                editor.getTableCellEditorComponent(insertRecordTableUI, "<DEFAULT>", insertRecordTableUI.isRowSelectionAllowed, row, col);
651
                jTable1.setValueAt("<DEFAULT>", row, col);
651
                insertRecordTableUI.setValueAt("<DEFAULT>", row, col);
652
                editor.stopCellEditing();
652
                editor.stopCellEditing();
653
            }
653
            }
654
            jTable1.setRowSelectionInterval(row, row);
654
            insertRecordTableUI.setRowSelectionInterval(row, row);
655
        }
655
        }
656
    }
656
    }
657
    // Variables declaration - do not modify//GEN-BEGIN:variables
657
    // Variables declaration - do not modify//GEN-BEGIN:variables
(-)db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java (+6 lines)
Lines 56-61 Link Here
56
import javax.swing.JLabel;
56
import javax.swing.JLabel;
57
import javax.swing.JTable;
57
import javax.swing.JTable;
58
import javax.swing.JTextField;
58
import javax.swing.JTextField;
59
import javax.swing.RowSorter;
59
import javax.swing.SwingUtilities;
60
import javax.swing.SwingUtilities;
60
import javax.swing.table.*;
61
import javax.swing.table.*;
61
import org.jdesktop.swingx.JXTable;
62
import org.jdesktop.swingx.JXTable;
Lines 122-127 Link Here
122
        return new JTableHeaderImpl(columnModel);
123
        return new JTableHeaderImpl(columnModel);
123
    }
124
    }
124
125
126
    @Override
127
    protected RowSorter<? extends TableModel> createDefaultRowSorter() {
128
        return new StringFallbackRowSorter(this.getModel());
129
    }
130
125
    public void createTableModel(List<Object[]> rows, final JXTableRowHeader rowHeader) {
131
    public void createTableModel(List<Object[]> rows, final JXTableRowHeader rowHeader) {
126
        assert SwingUtilities.isEventDispatchThread() : "Must be called from AWT thread";  //NOI18N
132
        assert SwingUtilities.isEventDispatchThread() : "Must be called from AWT thread";  //NOI18N
127
        assert rows != null;
133
        assert rows != null;
(-)db.dataview/src/org/netbeans/modules/db/dataview/table/StringFallbackRowSorter.java (+91 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.table;
43
44
import java.util.Comparator;
45
import javax.swing.table.TableModel;
46
import javax.swing.table.TableRowSorter;
47
48
/**
49
 * RowSorter that falls back to comparing values by their string representation if normal comparison fails.
50
 * 
51
 * The Sorter is necessary to prevent exceptions when columns in ResultSetJXTable
52
 * are sorted and different types are present (for example string and date)
53
 */
54
public class StringFallbackRowSorter extends TableRowSorter<TableModel> {
55
56
    public StringFallbackRowSorter() {
57
    }
58
59
    public StringFallbackRowSorter(TableModel model) {
60
        super(model);
61
    }
62
63
    @Override
64
    public Comparator<?> getComparator(int column) {
65
        return new StringFallBackComparator(super.getComparator(column));
66
    }
67
    
68
69
}
70
class StringFallBackComparator implements Comparator<Object> {
71
72
    private Comparator delegate;
73
74
    public StringFallBackComparator(Comparator delegate) {
75
        this.delegate = delegate;
76
    }
77
78
    public int compare(Object t, Object t1) {
79
        try {
80
            return delegate.compare(t, t1);
81
        } catch (Exception ex) {
82
            String s1 = t != null ? t.toString() : "";
83
            String s2 = t1 != null ? t1.toString() : "";
84
            return s1.compareTo(s2);
85
        }
86
    }
87
88
    public boolean equals(Object o) {
89
        return delegate.equals(o);
90
    }
91
}
(-)db.dataview/test/unit/src/org/netbeans/modules/db/dataview/output/InsertRecordTableUITest.java (-134 / +22 lines)
Lines 42-81 Link Here
42
42
43
package org.netbeans.modules.db.dataview.output;
43
package org.netbeans.modules.db.dataview.output;
44
44
45
import java.awt.Component;
45
import java.sql.Connection;
46
import java.sql.Connection;
46
import java.sql.ResultSet;
47
import java.sql.Date;
47
import java.sql.SQLException;
48
import java.util.List;
48
import java.util.Collection;
49
import javax.swing.SortOrder;
50
import javax.swing.table.DefaultTableModel;
49
import org.netbeans.api.db.explorer.DatabaseConnection;
51
import org.netbeans.api.db.explorer.DatabaseConnection;
50
import org.netbeans.junit.MockServices;
52
import org.netbeans.junit.MockServices;
51
import org.netbeans.junit.NbTestCase;
53
import org.netbeans.junit.NbTestCase;
52
import org.netbeans.modules.db.dataview.meta.DBMetaDataFactory;
53
import org.netbeans.modules.db.dataview.meta.DBTable;
54
import org.netbeans.modules.db.dataview.spi.DBConnectionProviderImpl;
54
import org.netbeans.modules.db.dataview.spi.DBConnectionProviderImpl;
55
import org.netbeans.modules.db.dataview.table.ResultSetJXTable;
55
import org.netbeans.modules.db.dataview.util.DBTestUtil;
56
import org.netbeans.modules.db.dataview.util.DBTestUtil;
56
import org.netbeans.modules.db.dataview.util.DbUtil;
57
import org.netbeans.modules.db.dataview.util.DbUtil;
57
import org.netbeans.modules.db.dataview.util.TestCaseContext;
58
import org.netbeans.modules.db.dataview.util.TestCaseContext;
58
import org.openide.util.Exceptions;
59
59
60
/**
60
/**
61
 *
61
 * Test for InsertRecordTableUI
62
 * @author jawed
63
 */
62
 */
64
public class DataViewTest extends NbTestCase {
63
public class InsertRecordTableUITest extends NbTestCase {
65
    
64
    
66
    Connection conn;
65
    Connection conn;
67
    DatabaseConnection dbconn;
66
    DatabaseConnection dbconn;
68
    TestCaseContext context;
67
    TestCaseContext context;
69
    
68
    
70
    public DataViewTest(String testName) {
69
    public InsertRecordTableUITest(String testName) {
71
        super(testName);
70
        super(testName);
72
    }
71
    }
73
72
74
    public static  org.netbeans.junit.NbTest suite() {
75
         org.netbeans.junit.NbTestSuite suite = new  org.netbeans.junit.NbTestSuite(DataViewTest.class);
76
        return suite;
77
    }
78
79
    @Override
73
    @Override
80
    public boolean runInEQ () {
74
    public boolean runInEQ () {
81
        return true;
75
        return true;
Lines 104-231 Link Here
104
98
105
    public void testCreate() {
99
    public void testCreate() {
106
        String sqlString = context.getSqlSelect();
100
        String sqlString = context.getSqlSelect();
107
        int pageSize = 5;
101
        DataView dv = DataView.create(dbconn, sqlString, 5);
108
        DataView result = DataView.create(dbconn, sqlString, pageSize);
102
        dv.createComponents();
109
        assertNotNull(result);
110
    }
111
103
112
    public void testHasExceptions() {
104
        InsertRecordDialog ird = new InsertRecordDialog(dv);
113
        String sqlString = context.getSqlSelect();
114
        int pageSize = 5;
115
        DataView instance = DataView.create(dbconn, sqlString, pageSize);
116
        boolean expResult = false;
117
        boolean result = instance.hasExceptions();
118
        assertEquals(expResult, result);
119
    }
120
105
121
    public void testHasResultSet() {
106
        DefaultTableModel model = (DefaultTableModel) ird.insertRecordTableUI.getModel();
122
        String sqlString = context.getSqlSelect();
107
        ird.insertRecordTableUI.createNewRow();
123
        int pageSize = 5;
108
        model.addRow(ird.insertRecordTableUI.createNewRow());
124
        DataView instance = DataView.create(dbconn, sqlString, pageSize);
109
        model.addRow(ird.insertRecordTableUI.createNewRow());
125
        boolean expResult = true;
110
        // Column 5 is the date column => Insert a "real" Date
126
        boolean result = instance.hasResultSet();
111
        // => creates conflict with String inserted by "createNewRow"
127
        assertEquals(expResult, result);
112
        ird.insertRecordTableUI.setValueAt(new Date(new java.util.Date().getTime()), 1, 5);
128
    }
129
130
    public void testGetUpdateCount() {
131
        String updateStr = "update simpletable set tinyintc='-40' where tinyintc='-80'";
132
        int pageSize = 5;
133
        DataView instance = DataView.create(dbconn, updateStr, pageSize);
134
        int expResult = 1;
135
        int result = instance.getUpdateCount();
136
        assertEquals(expResult, result);
137
    }
138
139
//    public void testGetExecutionTime() {
140
//        System.out.println("getExecutionTime");
141
//        String sqlStr = "select count(*) from simpletable";
142
//        int pageSize = 4;
143
//        DataView instance = DataView.create(dbconn, sqlStr, pageSize);
144
//        long expResult = (long)0.032;
145
//        long result = instance.getExecutionTime();
146
//        assertEquals(expResult, result);
147
//    }
148
149
    public void testGetDataViewDBTable() {
150
        try {
113
        try {
151
            String sqlStr = context.getSqlSelect();
114
            ird.insertRecordTableUI.setSortOrder(5, SortOrder.ASCENDING);
152
            int pageSize = 4;
115
        } catch (ClassCastException ex) {
153
            DataView instance = DataView.create(dbconn, sqlStr, pageSize);
116
            assert false : "Bug 219011 - should not be reached!";
154
            java.sql.Statement stmt = conn.createStatement();
155
            DBMetaDataFactory dbMeta = new DBMetaDataFactory(conn);
156
            ResultSet rset = stmt.executeQuery(sqlStr);
157
            Collection<DBTable> tables = dbMeta.generateDBTables(rset, sqlStr, true); //generateDBTables(rset);
158
            DataViewDBTable expResult = new DataViewDBTable(tables);
159
            DataViewDBTable result = instance.getDataViewDBTable();
160
            assertEquals(expResult.getQualifiedName(0, false), result.getQualifiedName(0, false));
161
            assertEquals(expResult.getColumnCount(), result.getColumnCount());
162
            assertEquals(expResult.getColumnType(2), result.getColumnType(2));
163
            rset.close();
164
        } catch (SQLException ex) {
165
            Exceptions.printStackTrace(ex);
166
        }
117
        }
167
        }
118
        }
168
169
    public void testGetDataViewPageContext() {
170
        String sqlStr =context.getSqlSelect();
171
        int pageSize = 4;
172
        DataView instance = DataView.create(dbconn, sqlStr, pageSize);
173
        DataViewPageContext result = instance.getDataViewPageContext();
174
        assertEquals(1, result.getTotalRows());
175
        assertTrue(result.hasDataRows());
176
    }
119
    }
177
178
    public void testGetDatabaseConnection() {
179
        String sqlStr = context.getSqlSelect();
180
        int pagSize = 4;
181
        DataView instance = DataView.create(dbconn, sqlStr, pagSize);
182
        DatabaseConnection expResult = dbconn;
183
        DatabaseConnection result = instance.getDatabaseConnection();
184
        assertNotNull(result);
185
        assertEquals(expResult, result);
186
    }
187
188
    public void testGetSQLString() {
189
        String sqlStr = context.getSqlSelect();
190
        int pagSize = 4;
191
        DataView instance = DataView.create(dbconn, sqlStr, pagSize);
192
        String expResult = "select * from simpletable";
193
        String result = instance.getSQLString();
194
        assertEquals(expResult, result);
195
    }
196
197
    public void testGetUpdatedRowContext() {
198
        String selectStr = "select * from simpletable";
199
        int pageSize = 5;
200
        DataView instance = DataView.create(dbconn, selectStr, pageSize);
201
        instance.createComponents();
202
        UpdatedRowContext result = instance.getUpdatedRowContext();
203
        assertNotNull(result);
204
    }
205
206
    /**
207
     * Test of getSQLExecutionHelper method, of class DataView.
208
     */
209
    public void testGetSQLExecutionHelper() {
210
        String selectStr = "select * from simpletable";
211
        int pageSize = 5;
212
        DataView instance = DataView.create(dbconn, selectStr, pageSize);
213
        SQLExecutionHelper result = instance.getSQLExecutionHelper();
214
        assertFalse(instance.hasExceptions());
215
        assertNotNull(instance);
216
        assertNotNull(result);
217
218
    }
219
220
    /**
221
     * Test of getSQLStatementGenerator method, of class DataView.
222
     */
223
    public void testGetSQLStatementGenerator() {
224
        String sqlStr = "select * from simpletable";
225
        int pageSize = 5;
226
        DataView instance = DataView.create(dbconn, sqlStr, pageSize);
227
        SQLStatementGenerator expResult = new SQLStatementGenerator(instance);
228
        SQLStatementGenerator result = instance.getSQLStatementGenerator();
229
        assertEquals(SQLStatementGenerator.getCountSQLQuery(sqlStr), SQLStatementGenerator.getCountSQLQuery(sqlStr));
230
    }
231
}

Return to bug 219011