# HG changeset patch # User matthias42@netbeans.org # Date 1324234454 -3600 # Node ID 0894e5a1cd16475e450ca4753098f6ced6e731c5 # Parent 6030c674e4f0d5e4228e9274dc28e54eea0de14f Add simple editor for CLOBs (based on string editor) diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/Bundle.properties b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/Bundle.properties --- a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/Bundle.properties +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/Bundle.properties @@ -41,6 +41,7 @@ nullLob.title=Set to NULL saveLob.title=Save to file loadLob.title=Set from file +editClob.title=Edit data loadLob.sourceNotReadable=Source file can't be read.\nCheck file/directory permissions. saveLob.unknownFormat=Unknown Format for Lob-Handling saveLob.targetNotWriteable=Target file can't be written.\nCheck file/directory permissions. 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 @@ -68,23 +68,27 @@ import javax.swing.JComboBox; import javax.swing.JFileChooser; import javax.swing.JMenuItem; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPopupMenu; +import javax.swing.JScrollPane; import javax.swing.JTable; +import javax.swing.JTextArea; import javax.swing.SwingConstants; import javax.swing.table.TableCellEditor; import org.netbeans.api.progress.ProgressUtils; import org.netbeans.modules.db.dataview.util.FileBackedClob; import org.openide.util.Exceptions; import org.openide.util.NbBundle; +import org.openide.windows.WindowManager; 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() { @@ -98,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); } @@ -112,7 +116,9 @@ protected JButton button; protected JPopupMenu popup; protected JTable table; - + protected int currentRow; + protected int currentColumn; + public ClobFieldTableCellEditor() { button = new JButton(); button.setActionCommand(EDIT); @@ -124,11 +130,11 @@ 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(BlobFieldTableCellEditor.class, "saveLob.title")); + final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(ClobFieldTableCellEditor.class, "saveLob.title")); miLobSaveAction.addActionListener(new ActionListener() { - + @Override public void actionPerformed(ActionEvent e) { saveLobToFile(currentValue); @@ -136,9 +142,19 @@ } }); popup.add(miLobSaveAction); - final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); + 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); + final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(ClobFieldTableCellEditor.class, "loadLob.title")); miLobLoadAction.addActionListener(new ActionListener() { - + @Override public void actionPerformed(ActionEvent e) { Object newValue = loadLobFromFile(); @@ -149,9 +165,9 @@ } }); popup.add(miLobLoadAction); - final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title")); + final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(ClobFieldTableCellEditor.class, "nullLob.title")); miLobNullAction.addActionListener(new ActionListener() { - + @Override public void actionPerformed(ActionEvent e) { currentValue = null; @@ -159,15 +175,16 @@ } }); 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; @@ -191,15 +208,17 @@ } else { button.setText(""); } + this.currentColumn = column; + this.currentRow = row; this.table = table; return button; } - + @Override public Object getCellEditorValue() { return currentValue; } - + @Override public boolean isCellEditable(EventObject anEvent) { if (anEvent instanceof MouseEvent) { @@ -207,7 +226,7 @@ } return super.isCellEditable(anEvent); } - + private void saveLobToFile(Clob b) { CharsetSelector charset = new CharsetSelector(); JFileChooser c = new JFileChooser(); @@ -230,7 +249,7 @@ } } } - + private Clob loadLobFromFile() { CharsetSelector charset = new CharsetSelector(); JFileChooser c = new JFileChooser(); @@ -275,6 +294,38 @@ } else if (t != null) { throw new RuntimeException(t); } - return ! ft.isCancel(); + return !ft.isCancel(); + } + + protected void editCell() { + String stringVal = ""; + if (currentValue != null) { + try { + 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) { + try { + table.setValueAt(new FileBackedClob(textArea.getText()), currentRow, currentColumn); + } catch (SQLException ex) { + Exceptions.printStackTrace(ex); + } + } + } else { + JOptionPane.showMessageDialog(parent, pane, table.getColumnName(currentColumn), JOptionPane.PLAIN_MESSAGE, null); + } } }