# This patch file was generated by NetBeans IDE # Following Index: paths are relative to: /home/matthias/NetBeansProjects/core-main # This patch can be applied using context Tools: Patch action on respective folder. # It uses platform neutral UTF-8 encoding and \n newlines. # Above lines and this line are ignored by the patching process. Index: db.dataview/src/org/netbeans/modules/db/dataview/output/InsertRecordDialog.java --- db.dataview/src/org/netbeans/modules/db/dataview/output/InsertRecordDialog.java Base (BASE) +++ db.dataview/src/org/netbeans/modules/db/dataview/output/InsertRecordDialog.java Locally Modified (Based On LOCAL) @@ -100,17 +100,17 @@ class InsertRecordDialog extends javax.swing.JDialog { private final DataView dataView; - InsertRecordTableUI jTable1; + InsertRecordTableUI insertRecordTableUI; private JXTableRowHeader rowHeader; public InsertRecordDialog(DataView dataView) { super(WindowManager.getDefault().getMainWindow(), true); this.dataView = dataView; - jTable1 = new InsertRecordTableUI(dataView) { + insertRecordTableUI = new InsertRecordTableUI(dataView) { @Override public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) { - if (rowIndex != -1 && columnIndex != -1 && ((DefaultTableModel) jTable1.getModel()).getRowCount() > 1) { + if (rowIndex != -1 && columnIndex != -1 && ((DefaultTableModel) insertRecordTableUI.getModel()).getRowCount() > 1) { removeBtn.setEnabled(true); } AWTEvent awtEvent = EventQueue.getCurrentEvent(); @@ -135,9 +135,9 @@ }; initComponents(); addInputFields(); - jTable1.addKeyListener(new TableKeyListener()); + insertRecordTableUI.addKeyListener(new TableKeyListener()); - jTable1.getModel().addTableModelListener(new TableListener()); + insertRecordTableUI.getModel().addTableModelListener(new TableListener()); jSplitPane1.setBottomComponent(null); @@ -331,12 +331,12 @@ }// //GEN-END:initComponents private void addBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBtnActionPerformed - DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); - model.addRow(jTable1.createNewRow()); + DefaultTableModel model = (DefaultTableModel) insertRecordTableUI.getModel(); + model.addRow(insertRecordTableUI.createNewRow()); }//GEN-LAST:event_addBtnActionPerformed private void removeBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeBtnActionPerformed - jTable1.removeRows(); + insertRecordTableUI.removeRows(); removeBtn.setEnabled(false); }//GEN-LAST:event_removeBtnActionPerformed @@ -362,8 +362,8 @@ } private void executeBtnActionPerformed(java.awt.event.ActionEvent evt) { - if (jTable1.isEditing()) { - jTable1.getCellEditor().stopCellEditing(); + if (insertRecordTableUI.isEditing()) { + insertRecordTableUI.getCellEditor().stopCellEditing(); } // Get out of AWT thread because SQLExecutionHelper does calls to AWT // and we need to wait here to show possible exceptions. @@ -374,7 +374,7 @@ String insertSQL = null; SQLStatementGenerator stmtBldr = dataView.getSQLStatementGenerator(); SQLExecutionHelper execHelper = dataView.getSQLExecutionHelper(); - for (int i = 0; i < jTable1.getRowCount(); i++) { + for (int i = 0; i < insertRecordTableUI.getRowCount(); i++) { boolean wasException = false; try { Object[] insertedRow = getInsertValues(i); @@ -390,7 +390,7 @@ if (wasException) { // remove i already inserted for (int j = 0; j < i; j++) { - ((DefaultTableModel) jTable1.getModel()).removeRow(0); + ((DefaultTableModel) insertRecordTableUI.getModel()).removeRow(0); } // return without closing return; @@ -419,7 +419,7 @@ String sqlText = ""; if (jSplitPane1.getBottomComponent() != null) { SQLStatementGenerator stmtBldr = dataView.getSQLStatementGenerator(); - for (int i = 0; i < jTable1.getRowCount(); i++) { + for (int i = 0; i < insertRecordTableUI.getRowCount(); i++) { String sql = stmtBldr.generateRawInsertStatement(getInsertValues(i)); sqlText = sqlText + sql + "\n"; } @@ -442,10 +442,10 @@ private void addInputFields() { List rows = new ArrayList(); - rows.add(jTable1.createNewRow()); - jScrollPane1.setViewportView(jTable1); - rowHeader = new JXTableRowHeader(jTable1); - final Component order[] = new Component[]{rowHeader, jTable1}; + rows.add(insertRecordTableUI.createNewRow()); + jScrollPane1.setViewportView(insertRecordTableUI); + rowHeader = new JXTableRowHeader(insertRecordTableUI); + final Component order[] = new Component[]{rowHeader, insertRecordTableUI}; FocusTraversalPolicy policy = new FocusTraversalPolicy() { List componentList = Arrays.asList(order); @@ -463,11 +463,11 @@ @Override public Component getComponentAfter(Container focusCycleRoot, Component aComponent) { if (aComponent instanceof JXTableRowHeader) { - int rowIndex = jTable1.getRowCount() - 1; - jTable1.editCellAt(rowIndex, 0); - jTable1.setRowSelectionInterval(rowIndex, 0); + int rowIndex = insertRecordTableUI.getRowCount() - 1; + insertRecordTableUI.editCellAt(rowIndex, 0); + insertRecordTableUI.setRowSelectionInterval(rowIndex, 0); } - return jTable1; + return insertRecordTableUI; } @Override @@ -484,18 +484,18 @@ setFocusTraversalPolicy(policy); jScrollPane1.setRowHeaderView(rowHeader); jScrollPane1.setCorner(JScrollPane.UPPER_LEFT_CORNER, rowHeader.getTableHeader()); - jTable1.createTableModel(rows, rowHeader); + insertRecordTableUI.createTableModel(rows, rowHeader); } private Object[] getInsertValues(int row) throws DBException { - DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); - int rsColumnCount = jTable1.getRSColumnCount(); + DefaultTableModel model = (DefaultTableModel) insertRecordTableUI.getModel(); + int rsColumnCount = insertRecordTableUI.getRSColumnCount(); Object[] insertData = new Object[rsColumnCount]; - if (jTable1.getRowCount() <= 0) { + if (insertRecordTableUI.getRowCount() <= 0) { return insertData; } for (int i = 0; i < rsColumnCount; i++) { - DBColumn col = jTable1.getDBColumn(i); + DBColumn col = insertRecordTableUI.getDBColumn(i); Object val = model.getValueAt(row, i); // Check for Constant e.g , , etc @@ -541,7 +541,7 @@ paste(); } if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_DELETE) { - jTable1.removeRows(); + insertRecordTableUI.removeRows(); } else if (e.getKeyCode() == KeyEvent.VK_ENTER) { setFocusable(false); } else if (e.isControlDown() && e.getKeyChar() == KeyEvent.VK_0) { @@ -555,10 +555,10 @@ private void copy() { StringBuilder strBuffer = new StringBuilder(); - int numcols = jTable1.getSelectedColumnCount(); - int numrows = jTable1.getSelectedRowCount(); - int[] rowsselected = jTable1.getSelectedRows(); - int[] colsselected = jTable1.getSelectedColumns(); + int numcols = insertRecordTableUI.getSelectedColumnCount(); + int numrows = insertRecordTableUI.getSelectedRowCount(); + int[] rowsselected = insertRecordTableUI.getSelectedRows(); + int[] colsselected = insertRecordTableUI.getSelectedColumns(); if (!((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0] && numrows == rowsselected.length) && (numcols - 1 == colsselected[colsselected.length - 1] - colsselected[0] && numcols == colsselected.length))) { JOptionPane.showMessageDialog(null, "Invalid Copy Selection", "Invalid Copy Selection", JOptionPane.ERROR_MESSAGE); @@ -566,7 +566,7 @@ } for (int i = 0; i < numrows; i++) { for (int j = 0; j < numcols; j++) { - strBuffer.append(jTable1.getValueAt(rowsselected[i], colsselected[j])); + strBuffer.append(insertRecordTableUI.getValueAt(rowsselected[i], colsselected[j])); if (j < numcols - 1) { strBuffer.append("\t"); } @@ -580,13 +580,13 @@ private void paste() { String rowstring, value; - int startRow = (jTable1.getSelectedRows())[0]; - int startCol = (jTable1.getSelectedColumns())[0]; + int startRow = (insertRecordTableUI.getSelectedRows())[0]; + int startCol = (insertRecordTableUI.getSelectedColumns())[0]; try { String trstring = (String) (clipBoard.getContents(this).getTransferData(DataFlavor.stringFlavor)); StringTokenizer st1 = new StringTokenizer(trstring, "\n"); - if (jTable1.getSelectedRows().length < st1.countTokens()) { - int rowCnt = st1.countTokens() - jTable1.getSelectedRows().length; + if (insertRecordTableUI.getSelectedRows().length < st1.countTokens()) { + int rowCnt = st1.countTokens() - insertRecordTableUI.getSelectedRows().length; for (int cnt = 0; cnt < rowCnt; cnt++) { addBtnActionPerformed(null); } @@ -596,8 +596,8 @@ StringTokenizer st2 = new StringTokenizer(rowstring, "\t"); for (int j = 0; st2.hasMoreTokens(); j++) { value = st2.nextToken(); - if (startRow + i < jTable1.getRowCount() && startCol + j < jTable1.getColumnCount()) { - jTable1.setValueAt(value, startRow + i, startCol + j); + if (startRow + i < insertRecordTableUI.getRowCount() && startCol + j < insertRecordTableUI.getColumnCount()) { + insertRecordTableUI.setValueAt(value, startRow + i, startCol + j); } } } @@ -607,51 +607,51 @@ } private void control0Event() { - int row = jTable1.getSelectedRow(); - int col = jTable1.getSelectedColumn(); + int row = insertRecordTableUI.getSelectedRow(); + int col = insertRecordTableUI.getSelectedColumn(); if (row == -1) { return; } - jTable1.editCellAt(row, col); - TableCellEditor editor = jTable1.getCellEditor(); + insertRecordTableUI.editCellAt(row, col); + TableCellEditor editor = insertRecordTableUI.getCellEditor(); if (editor != null) { DBColumn dbcol = dataView.getDataViewDBTable().getColumn(col); if (dbcol.isGenerated() || !dbcol.isNullable()) { Toolkit.getDefaultToolkit().beep(); editor.stopCellEditing(); } else { - editor.getTableCellEditorComponent(jTable1, null, jTable1.isRowSelectionAllowed, row, col); - jTable1.setValueAt(null, row, col); + editor.getTableCellEditorComponent(insertRecordTableUI, null, insertRecordTableUI.isRowSelectionAllowed, row, col); + insertRecordTableUI.setValueAt(null, row, col); editor.stopCellEditing(); } - jTable1.setRowSelectionInterval(row, row); + insertRecordTableUI.setRowSelectionInterval(row, row); } } private void control1Event() { - int row = jTable1.getSelectedRow(); - int col = jTable1.getSelectedColumn(); + int row = insertRecordTableUI.getSelectedRow(); + int col = insertRecordTableUI.getSelectedColumn(); if (row == -1) { return; } - jTable1.editCellAt(row, col); - TableCellEditor editor = jTable1.getCellEditor(); + insertRecordTableUI.editCellAt(row, col); + TableCellEditor editor = insertRecordTableUI.getCellEditor(); if (editor != null) { DBColumn dbcol = dataView.getDataViewDBTable().getColumn(col); - Object val = jTable1.getValueAt(row, col); + Object val = insertRecordTableUI.getValueAt(row, col); if (dbcol.isGenerated() || !dbcol.hasDefault()) { Toolkit.getDefaultToolkit().beep(); editor.stopCellEditing(); } else if (val != null && val instanceof String && ((String) val).equals("")) { - editor.getTableCellEditorComponent(jTable1, "", jTable1.isRowSelectionAllowed, row, col); - jTable1.setValueAt(null, row, col); + editor.getTableCellEditorComponent(insertRecordTableUI, "", insertRecordTableUI.isRowSelectionAllowed, row, col); + insertRecordTableUI.setValueAt(null, row, col); editor.stopCellEditing(); } else { - editor.getTableCellEditorComponent(jTable1, "", jTable1.isRowSelectionAllowed, row, col); - jTable1.setValueAt("", row, col); + editor.getTableCellEditorComponent(insertRecordTableUI, "", insertRecordTableUI.isRowSelectionAllowed, row, col); + insertRecordTableUI.setValueAt("", row, col); editor.stopCellEditing(); } - jTable1.setRowSelectionInterval(row, row); + insertRecordTableUI.setRowSelectionInterval(row, row); } } // Variables declaration - do not modify//GEN-BEGIN:variables Index: db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java --- db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java Base (BASE) +++ db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java Locally Modified (Based On LOCAL) @@ -56,6 +56,7 @@ import javax.swing.JLabel; import javax.swing.JTable; import javax.swing.JTextField; +import javax.swing.RowSorter; import javax.swing.SwingUtilities; import javax.swing.table.*; import org.jdesktop.swingx.JXTable; @@ -122,6 +123,11 @@ return new JTableHeaderImpl(columnModel); } + @Override + protected RowSorter createDefaultRowSorter() { + return new StringFallbackRowSorter(this.getModel()); + } + public void createTableModel(List rows, final JXTableRowHeader rowHeader) { assert SwingUtilities.isEventDispatchThread() : "Must be called from AWT thread"; //NOI18N assert rows != null; Index: db.dataview/src/org/netbeans/modules/db/dataview/table/StringFallbackRowSorter.java --- db.dataview/src/org/netbeans/modules/db/dataview/table/StringFallbackRowSorter.java Base (BASE) +++ db.dataview/src/org/netbeans/modules/db/dataview/table/StringFallbackRowSorter.java Locally New @@ -0,0 +1,91 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2012 Sun Microsystems, Inc. + */ +package org.netbeans.modules.db.dataview.table; + +import java.util.Comparator; +import javax.swing.table.TableModel; +import javax.swing.table.TableRowSorter; + +/** + * RowSorter that falls back to comparing values by their string representation if normal comparison fails. + * + * The Sorter is necessary to prevent exceptions when columns in ResultSetJXTable + * are sorted and different types are present (for example string and date) + */ +public class StringFallbackRowSorter extends TableRowSorter { + + public StringFallbackRowSorter() { + } + + public StringFallbackRowSorter(TableModel model) { + super(model); + } + + @Override + public Comparator getComparator(int column) { + return new StringFallBackComparator(super.getComparator(column)); + } + + +} +class StringFallBackComparator implements Comparator { + + private Comparator delegate; + + public StringFallBackComparator(Comparator delegate) { + this.delegate = delegate; + } + + public int compare(Object t, Object t1) { + try { + return delegate.compare(t, t1); + } catch (Exception ex) { + String s1 = t != null ? t.toString() : ""; + String s2 = t1 != null ? t1.toString() : ""; + return s1.compareTo(s2); + } + } + + public boolean equals(Object o) { + return delegate.equals(o); + } +} \ No newline at end of file Index: db.dataview/test/unit/src/org/netbeans/modules/db/dataview/output/InsertRecordTableUITest.java --- db.dataview/test/unit/src/org/netbeans/modules/db/dataview/output/InsertRecordTableUITest.java Base (DataViewTest.java) +++ db.dataview/test/unit/src/org/netbeans/modules/db/dataview/output/InsertRecordTableUITest.java Locally Copied @@ -42,40 +42,34 @@ package org.netbeans.modules.db.dataview.output; +import java.awt.Component; import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.Collection; +import java.sql.Date; +import java.util.List; +import javax.swing.SortOrder; +import javax.swing.table.DefaultTableModel; import org.netbeans.api.db.explorer.DatabaseConnection; import org.netbeans.junit.MockServices; import org.netbeans.junit.NbTestCase; -import org.netbeans.modules.db.dataview.meta.DBMetaDataFactory; -import org.netbeans.modules.db.dataview.meta.DBTable; import org.netbeans.modules.db.dataview.spi.DBConnectionProviderImpl; +import org.netbeans.modules.db.dataview.table.ResultSetJXTable; import org.netbeans.modules.db.dataview.util.DBTestUtil; import org.netbeans.modules.db.dataview.util.DbUtil; import org.netbeans.modules.db.dataview.util.TestCaseContext; -import org.openide.util.Exceptions; /** - * - * @author jawed + * Test for InsertRecordTableUI */ -public class DataViewTest extends NbTestCase { +public class InsertRecordTableUITest extends NbTestCase { Connection conn; DatabaseConnection dbconn; TestCaseContext context; - public DataViewTest(String testName) { + public InsertRecordTableUITest(String testName) { super(testName); } - public static org.netbeans.junit.NbTest suite() { - org.netbeans.junit.NbTestSuite suite = new org.netbeans.junit.NbTestSuite(DataViewTest.class); - return suite; - } - @Override public boolean runInEQ () { return true; @@ -104,128 +98,22 @@ public void testCreate() { String sqlString = context.getSqlSelect(); - int pageSize = 5; - DataView result = DataView.create(dbconn, sqlString, pageSize); - assertNotNull(result); - } + DataView dv = DataView.create(dbconn, sqlString, 5); + dv.createComponents(); - public void testHasExceptions() { - String sqlString = context.getSqlSelect(); - int pageSize = 5; - DataView instance = DataView.create(dbconn, sqlString, pageSize); - boolean expResult = false; - boolean result = instance.hasExceptions(); - assertEquals(expResult, result); - } + InsertRecordDialog ird = new InsertRecordDialog(dv); - public void testHasResultSet() { - String sqlString = context.getSqlSelect(); - int pageSize = 5; - DataView instance = DataView.create(dbconn, sqlString, pageSize); - boolean expResult = true; - boolean result = instance.hasResultSet(); - assertEquals(expResult, result); - } - - public void testGetUpdateCount() { - String updateStr = "update simpletable set tinyintc='-40' where tinyintc='-80'"; - int pageSize = 5; - DataView instance = DataView.create(dbconn, updateStr, pageSize); - int expResult = 1; - int result = instance.getUpdateCount(); - assertEquals(expResult, result); - } - -// public void testGetExecutionTime() { -// System.out.println("getExecutionTime"); -// String sqlStr = "select count(*) from simpletable"; -// int pageSize = 4; -// DataView instance = DataView.create(dbconn, sqlStr, pageSize); -// long expResult = (long)0.032; -// long result = instance.getExecutionTime(); -// assertEquals(expResult, result); -// } - - public void testGetDataViewDBTable() { + DefaultTableModel model = (DefaultTableModel) ird.insertRecordTableUI.getModel(); + ird.insertRecordTableUI.createNewRow(); + model.addRow(ird.insertRecordTableUI.createNewRow()); + model.addRow(ird.insertRecordTableUI.createNewRow()); + // Column 5 is the date column => Insert a "real" Date + // => creates conflict with String inserted by "createNewRow" + ird.insertRecordTableUI.setValueAt(new Date(new java.util.Date().getTime()), 1, 5); try { - String sqlStr = context.getSqlSelect(); - int pageSize = 4; - DataView instance = DataView.create(dbconn, sqlStr, pageSize); - java.sql.Statement stmt = conn.createStatement(); - DBMetaDataFactory dbMeta = new DBMetaDataFactory(conn); - ResultSet rset = stmt.executeQuery(sqlStr); - Collection tables = dbMeta.generateDBTables(rset, sqlStr, true); //generateDBTables(rset); - DataViewDBTable expResult = new DataViewDBTable(tables); - DataViewDBTable result = instance.getDataViewDBTable(); - assertEquals(expResult.getQualifiedName(0, false), result.getQualifiedName(0, false)); - assertEquals(expResult.getColumnCount(), result.getColumnCount()); - assertEquals(expResult.getColumnType(2), result.getColumnType(2)); - rset.close(); - } catch (SQLException ex) { - Exceptions.printStackTrace(ex); + ird.insertRecordTableUI.setSortOrder(5, SortOrder.ASCENDING); + } catch (ClassCastException ex) { + assert false : "Bug 219011 - should not be reached!"; } } - - public void testGetDataViewPageContext() { - String sqlStr =context.getSqlSelect(); - int pageSize = 4; - DataView instance = DataView.create(dbconn, sqlStr, pageSize); - DataViewPageContext result = instance.getDataViewPageContext(); - assertEquals(1, result.getTotalRows()); - assertTrue(result.hasDataRows()); } - - public void testGetDatabaseConnection() { - String sqlStr = context.getSqlSelect(); - int pagSize = 4; - DataView instance = DataView.create(dbconn, sqlStr, pagSize); - DatabaseConnection expResult = dbconn; - DatabaseConnection result = instance.getDatabaseConnection(); - assertNotNull(result); - assertEquals(expResult, result); - } - - public void testGetSQLString() { - String sqlStr = context.getSqlSelect(); - int pagSize = 4; - DataView instance = DataView.create(dbconn, sqlStr, pagSize); - String expResult = "select * from simpletable"; - String result = instance.getSQLString(); - assertEquals(expResult, result); - } - - public void testGetUpdatedRowContext() { - String selectStr = "select * from simpletable"; - int pageSize = 5; - DataView instance = DataView.create(dbconn, selectStr, pageSize); - instance.createComponents(); - UpdatedRowContext result = instance.getUpdatedRowContext(); - assertNotNull(result); - } - - /** - * Test of getSQLExecutionHelper method, of class DataView. - */ - public void testGetSQLExecutionHelper() { - String selectStr = "select * from simpletable"; - int pageSize = 5; - DataView instance = DataView.create(dbconn, selectStr, pageSize); - SQLExecutionHelper result = instance.getSQLExecutionHelper(); - assertFalse(instance.hasExceptions()); - assertNotNull(instance); - assertNotNull(result); - - } - - /** - * Test of getSQLStatementGenerator method, of class DataView. - */ - public void testGetSQLStatementGenerator() { - String sqlStr = "select * from simpletable"; - int pageSize = 5; - DataView instance = DataView.create(dbconn, sqlStr, pageSize); - SQLStatementGenerator expResult = new SQLStatementGenerator(instance); - SQLStatementGenerator result = instance.getSQLStatementGenerator(); - assertEquals(SQLStatementGenerator.getCountSQLQuery(sqlStr), SQLStatementGenerator.getCountSQLQuery(sqlStr)); - } -}