# HG changeset patch # User Matthias Bläsing # Date 1329599767 -3600 # Branch dbfixes # Node ID 2815ae789c4846d3674ba8cba7cbddb09e169bf9 # Parent 8b5e4d9846b34799ba182d9bf5efa919010c506d Fix NullPointerExceptions in Lob-Handling Code diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java --- a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java @@ -76,6 +76,7 @@ protected JButton button; protected JPopupMenu popup; protected JTable table; + protected JMenuItem saveContentMenuItem; public BlobFieldTableCellEditor() { button = new JButton(); @@ -99,6 +100,7 @@ fireEditingCanceled(); } }); + saveContentMenuItem = miLobSaveAction; popup.add(miLobSaveAction); final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); miLobLoadAction.addActionListener(new ActionListener() { @@ -136,6 +138,7 @@ public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { currentValue = (java.sql.Blob) value; if (currentValue != null) { + saveContentMenuItem.setEnabled(true); try { long size = currentValue.length(); StringBuilder stringValue = new StringBuilder(); @@ -153,6 +156,7 @@ button.setText(""); } } else { + saveContentMenuItem.setEnabled(false); button.setText(""); } this.table = table; @@ -173,6 +177,9 @@ } private void saveLobToFile(Blob b) { + if(b == null) { + return; + } JFileChooser c = new JFileChooser(); int fileDialogState = c.showSaveDialog(table); if (fileDialogState == JFileChooser.APPROVE_OPTION) { diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ClobFieldTableCellEditor.java b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ClobFieldTableCellEditor.java --- a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ClobFieldTableCellEditor.java +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ClobFieldTableCellEditor.java @@ -85,10 +85,10 @@ public class ClobFieldTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { - + private class CharsetSelector extends JPanel { private JComboBox charsetSelect; - + CharsetSelector() { List charset = new ArrayList(Charset.availableCharsets().values()); Collections.sort(charset, new Comparator() { @@ -102,11 +102,11 @@ charsetSelect.setSelectedItem(Charset.defaultCharset()); this.add(charsetSelect); } - + public Charset getSelectedCharset() { return (Charset) charsetSelect.getSelectedItem(); } - + public void setSelectedCharset(Charset selectedCharset) { charsetSelect.setSelectedItem(selectedCharset); } @@ -118,7 +118,8 @@ protected JTable table; protected int currentRow; protected int currentColumn; - + protected JMenuItem saveContentMenuItem; + public ClobFieldTableCellEditor() { button = new JButton(); button.setActionCommand(EDIT); @@ -130,31 +131,32 @@ button.setAlignmentX(0); button.setHorizontalAlignment(SwingConstants.LEFT); button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9)); - + popup = new JPopupMenu(); final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(ClobFieldTableCellEditor.class, "saveLob.title")); miLobSaveAction.addActionListener(new ActionListener() { - + @Override public void actionPerformed(ActionEvent e) { saveLobToFile(currentValue); fireEditingCanceled(); } }); + saveContentMenuItem = miLobSaveAction; popup.add(miLobSaveAction); final JMenuItem miLobEditAction = new JMenuItem(NbBundle.getMessage(ClobFieldTableCellEditor.class, "editClob.title")); miLobEditAction.addActionListener(new ActionListener() { - + @Override public void actionPerformed(ActionEvent e) { fireEditingStopped(); editCell(); } }); - popup.add(miLobEditAction); + popup.add(miLobEditAction); final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(ClobFieldTableCellEditor.class, "loadLob.title")); miLobLoadAction.addActionListener(new ActionListener() { - + @Override public void actionPerformed(ActionEvent e) { Object newValue = loadLobFromFile(); @@ -167,7 +169,7 @@ popup.add(miLobLoadAction); final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(ClobFieldTableCellEditor.class, "nullLob.title")); miLobNullAction.addActionListener(new ActionListener() { - + @Override public void actionPerformed(ActionEvent e) { currentValue = null; @@ -175,20 +177,21 @@ } }); popup.add(miLobNullAction); - + } - + @Override public void actionPerformed(ActionEvent e) { if (EDIT.equals(e.getActionCommand())) { popup.show(button, 0, button.getHeight()); } } - + @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { currentValue = (java.sql.Clob) value; if (currentValue != null) { + saveContentMenuItem.setEnabled(true); try { long size = currentValue.length(); StringBuilder stringValue = new StringBuilder(); @@ -206,6 +209,7 @@ button.setText(""); } } else { + saveContentMenuItem.setEnabled(false); button.setText(""); } this.currentColumn = column; @@ -213,12 +217,12 @@ this.table = table; return button; } - + @Override public Object getCellEditorValue() { return currentValue; } - + @Override public boolean isCellEditable(EventObject anEvent) { if (anEvent instanceof MouseEvent) { @@ -226,8 +230,11 @@ } return super.isCellEditable(anEvent); } - + private void saveLobToFile(Clob b) { + if (b == null) { + return; + } CharsetSelector charset = new CharsetSelector(); JFileChooser c = new JFileChooser(); c.setAccessory(charset); @@ -239,7 +246,7 @@ try { r = b.getCharacterStream(); w = new OutputStreamWriter(new FileOutputStream(f), charset.getSelectedCharset()); - if(! doTransfer(r, w, (int) b.length(), "Save to file: " + f.toString(), false)) { + if (!doTransfer(r, w, (int) b.length(), "Save to file: " + f.toString(), false)) { f.delete(); } } catch (IOException ex) { @@ -249,7 +256,7 @@ } } } - + private Clob loadLobFromFile() { CharsetSelector charset = new CharsetSelector(); JFileChooser c = new JFileChooser(); @@ -262,7 +269,7 @@ try { result = new FileBackedClob(); r = new InputStreamReader(new FileInputStream(f), charset.getSelectedCharset()); - if(! doTransfer(r, result.setCharacterStream(1), (int) f.length() / 2, "Load from file: " + f.toString(), true)) { + if (!doTransfer(r, result.setCharacterStream(1), (int) f.length() / 2, "Load from file: " + f.toString(), true)) { result = null; } } catch (IOException ex) { @@ -275,7 +282,7 @@ } /** - * @return true if transfer is complete and not iterrupted + * @return true if transfer is complete and not iterrupted */ private boolean doTransfer(Reader in, Writer out, Integer size, String title, boolean sizeEstimated) throws IOException { // Only pass size if it is _not_ estimated @@ -296,7 +303,7 @@ } return !ft.isCancel(); } - + protected void editCell() { String stringVal = ""; if (currentValue != null) { @@ -304,17 +311,17 @@ stringVal = currentValue.getSubString(1, (int) currentValue.length()); } catch (SQLException ex) { } - + } - + JTextArea textArea = new JTextArea(10, 50); textArea.setText(stringVal); textArea.setCaretPosition(0); textArea.setEditable(table.isCellEditable(currentRow, currentColumn)); - + JScrollPane pane = new JScrollPane(textArea); Component parent = WindowManager.getDefault().getMainWindow(); - + if (table.isCellEditable(currentRow, currentColumn)) { int result = JOptionPane.showOptionDialog(parent, pane, table.getColumnName(currentColumn), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); if (result == JOptionPane.OK_OPTION) { diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/util/DBReadWriteHelper.java b/db.dataview/src/org/netbeans/modules/db/dataview/util/DBReadWriteHelper.java --- a/db.dataview/src/org/netbeans/modules/db/dataview/util/DBReadWriteHelper.java +++ b/db.dataview/src/org/netbeans/modules/db/dataview/util/DBReadWriteHelper.java @@ -222,10 +222,9 @@ if (! rs.wasNull()) { result = new FileBackedBlob(blob.getBinaryStream()); + blob.free(); } - blob.free(); - return result; } catch (SQLException ex) { // Ok - can happen - the jdbc driver might not support @@ -265,7 +264,9 @@ if (rs.wasNull()) { return null; } else { - return new FileBackedClob(clob.getCharacterStream()); + Clob result = new FileBackedClob(clob.getCharacterStream()); + clob.free(); + return result; } } catch (SQLException ex) { // Ok - can happen - the jdbc driver might not support