SQLStringCellEditor

Fixed the problems I reported having a few days ago. As a result, I am now proud to announce "SQLStringCellEditor". This class can be dropped into any module that contains a table containing a column providing an SQL string. When a double-click on the column is performed, the NetBeans SQL Editor opens, synchronized with the selected cell in the column in the table:
public class SQLStringCellEditor extends AbstractCellEditor implements
        TableCellEditor, ActionListener {

    private int clickCount = 2;
    private JButton button;
    private JTextComponent editor;
    private String value;
    private JTable table;
    private int row;
    private int col;

    public SQLStringCellEditor() {
        super();
        //Define the button shown in the table:
        button = new JButton();
        button.addActionListener(this);
        button.setBorderPainted(false);
        button.setHorizontalAlignment(SwingConstants.LEFT);
    }

    //Use the content of the editor to set the value
    //of the cell:
    @Override
    public Object getCellEditorValue() {
        return editor.getText();
    }

    //When the action is invoked, i.e., button clicked,
    //set the text in the editor to the value of the cell:
    @Override
    public void actionPerformed(ActionEvent e) {
        getSingleSQLEditorDocument().setText(value);
    }

    //Open one SQL Editor document only, regardless
    //of how many times the action is invoked:
    private JTextComponent getSingleSQLEditorDocument() {
        if (editor == null) {
            SQLEditorSupport.openSQLEditor(null, value, true);
            editor = EditorRegistry.lastFocusedComponent();
            //Listen for inserts and update the table:
            editor.getDocument().addDocumentListener(new DocumentListener() {
                @Override
                public void insertUpdate(DocumentEvent e) {
                    table.setValueAt(editor.getText(), row, col);
                }
                @Override
                public void removeUpdate(DocumentEvent e) {
                    table.setValueAt(editor.getText(), row, col);
                }
                @Override
                public void changedUpdate(DocumentEvent e) {
                    table.setValueAt(editor.getText(), row, col);
                }
            });
        }
        return editor;
    }

    //Use the value in the cell to set the button's text
    //and to define class variables holding the old value, the table, and the cell:
    @Override
    public Component getTableCellEditorComponent(JTable arg0, Object val,
            boolean arg2, int row, int col) {
        this.value = val.toString();
        this.table = arg0;
        this.row = row;
        this.col=col;
        button.setText(value);
        return button;
    }
    
    //Only allow editing on double-click:
    public void setClickCount(int count) {
        clickCount = count;
    }
    @Override
    public boolean isCellEditable(EventObject evt) {
        if (evt instanceof MouseEvent) {
            return ((MouseEvent) evt).getClickCount() <= clickCount;
        }
        return true;
    }
    
}

Usage:

TableColumn column = table.getColumnModel().getColumn(0);
column.setCellEditor(new SQLStringCellEditor());

Of course, the relevant modules need to be part of the application, such as the module providing the SQL Editor.

Comments:

I have no knowledge about the SQLEditor component of Netbeans, but I think the document listener must not be changing the table directly (insertUpdate, removeUpdate, changedUpdate), It must signal the event CellEditorListener.editingStopped() using fireEditingStopped when the window is closed of if there is an ok button (or fireEditingStopped if the editor has a way to cancel editing), and return the current edited value implementing CellEditor.getCellEditorValue()

Posted by Robert on February 10, 2011 at 02:50 AM PST #

That's all true. However, in this case, I have no button where the firing of editing stopped can take place, instead whenever a change is made I want the cell to immediately be updated.

Posted by Geertjan Wielenga on February 10, 2011 at 04:12 AM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today