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

(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java (-1 / +11 lines)
Lines 43-48 Link Here
43
 */
43
 */
44
package org.netbeans.modules.db.dataview.table;
44
package org.netbeans.modules.db.dataview.table;
45
45
46
import org.netbeans.modules.db.dataview.table.celleditor.BlobFieldTableCellEditor;
47
import org.netbeans.modules.db.dataview.table.celleditor.StringTableCellEditor;
48
import org.netbeans.modules.db.dataview.table.celleditor.ClobFieldTableCellEditor;
49
import org.netbeans.modules.db.dataview.table.celleditor.NumberFieldEditor;
50
import org.netbeans.modules.db.dataview.table.celleditor.DateTimePickerCellEditor;
51
import org.netbeans.modules.db.dataview.table.celleditor.BooleanTableCellEditor;
46
import java.awt.Color;
52
import java.awt.Color;
47
import java.awt.event.KeyEvent;
53
import java.awt.event.KeyEvent;
48
import java.awt.event.KeyListener;
54
import java.awt.event.KeyListener;
Lines 266-272 Link Here
266
        return new ResultSetTableModel(this);
272
        return new ResultSetTableModel(this);
267
    }
273
    }
268
274
269
275
    @Override
276
    public boolean isEditable() {
277
        return dView.isEditable();
278
    }
279
    
270
    // This is mainly used for set Tooltip for column headers
280
    // This is mainly used for set Tooltip for column headers
271
281
272
    private class JTableHeaderImpl extends JXTableHeader {
282
    private class JTableHeaderImpl extends JXTableHeader {
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java (+232 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.table.celleditor;
43
44
import java.awt.Component;
45
import java.awt.Font;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.awt.event.MouseEvent;
49
import java.io.File;
50
import java.io.FileInputStream;
51
import java.io.FileOutputStream;
52
import java.io.IOException;
53
import java.io.InputStream;
54
import java.sql.Blob;
55
import java.sql.SQLException;
56
import java.util.EventObject;
57
import javax.swing.AbstractCellEditor;
58
import javax.swing.JButton;
59
import javax.swing.JFileChooser;
60
import javax.swing.JMenuItem;
61
import javax.swing.JPopupMenu;
62
import javax.swing.JTable;
63
import javax.swing.SwingConstants;
64
import javax.swing.table.TableCellEditor;
65
import org.netbeans.modules.db.dataview.util.FileBackedBlob;
66
import org.openide.util.Exceptions;
67
import org.openide.util.NbBundle;
68
69
public class BlobFieldTableCellEditor extends AbstractCellEditor
70
        implements TableCellEditor,
71
        ActionListener {
72
73
    protected static final String EDIT = "edit";
74
    protected Blob currentValue;
75
    protected JButton button;
76
    protected JPopupMenu popup;
77
    protected JTable table;
78
79
    public BlobFieldTableCellEditor() {
80
        button = new JButton();
81
        button.setActionCommand(EDIT);
82
        button.addActionListener(this);
83
        button.setContentAreaFilled(false);
84
        button.setOpaque(false);
85
        button.setBorderPainted(false);
86
        button.setRolloverEnabled(false);
87
        button.setAlignmentX(0);
88
        button.setHorizontalAlignment(SwingConstants.LEFT);
89
        button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9));
90
91
        popup = new JPopupMenu();
92
        final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title"));
93
        miLobSaveAction.addActionListener(new ActionListener() {
94
95
            @Override
96
            public void actionPerformed(ActionEvent e) {
97
                saveLobToFile(currentValue);
98
                fireEditingCanceled();
99
            }
100
        });
101
        popup.add(miLobSaveAction);
102
        final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title"));
103
        miLobLoadAction.addActionListener(new ActionListener() {
104
105
            @Override
106
            public void actionPerformed(ActionEvent e) {
107
                Object newValue = loadLobFromFile();
108
                if (newValue != null) {
109
                    currentValue = (Blob) newValue;
110
                }
111
                fireEditingStopped();
112
            }
113
        });
114
        popup.add(miLobLoadAction);
115
        final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title"));
116
        miLobNullAction.addActionListener(new ActionListener() {
117
118
            @Override
119
            public void actionPerformed(ActionEvent e) {
120
                currentValue = null;
121
                fireEditingStopped();
122
            }
123
        });
124
        popup.add(miLobNullAction);
125
126
    }
127
128
    public void actionPerformed(ActionEvent e) {
129
        if (EDIT.equals(e.getActionCommand())) {
130
            popup.show(button, 0, button.getHeight());
131
        }
132
    }
133
134
    @Override
135
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
136
        currentValue = (java.sql.Blob) value;
137
        if (currentValue != null) {
138
            try {
139
                long size = currentValue.length();
140
                StringBuilder stringValue = new StringBuilder();
141
                stringValue.append("<BLOB ");
142
                if (size < 1000) {
143
                    stringValue.append(String.format("%1$d bytes", size));
144
                } else if (size < 1000000) {
145
                    stringValue.append(String.format("%1$d kB", size / 1000));
146
                } else {
147
                    stringValue.append(String.format("%1$d MB", size / 1000000));
148
                }
149
                stringValue.append(">");
150
                button.setText(stringValue.toString());
151
            } catch (SQLException ex) {
152
                button.setText("<BLOB of unknown size>");
153
            }
154
        } else {
155
            button.setText("<NULL>");
156
        }
157
        this.table = table;
158
        return button;
159
    }
160
161
    @Override
162
    public Object getCellEditorValue() {
163
        return currentValue;
164
    }
165
166
    @Override
167
    public boolean isCellEditable(EventObject anEvent) {
168
        if (anEvent instanceof MouseEvent) {
169
            return ((MouseEvent) anEvent).getClickCount() >= 2;
170
        }
171
        return super.isCellEditable(anEvent);
172
    }
173
174
    private void saveLobToFile(Blob b) {
175
        JFileChooser c = new JFileChooser();
176
        int fileDialogState = c.showSaveDialog(table);
177
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
178
            File f = c.getSelectedFile();
179
            InputStream is = null;
180
            FileOutputStream fos = null;
181
            try {
182
                is = b.getBinaryStream();
183
                fos = new FileOutputStream(f);
184
                int read = 0;
185
                byte[] buffer = new byte[1024];
186
                while((read = is.read(buffer)) > 0) {
187
                    fos.write(buffer, 0, read);
188
                }
189
            } catch (IOException ex) {
190
                throw new RuntimeException(ex);
191
            } catch (SQLException ex) {
192
                throw new RuntimeException(ex);
193
            } finally {
194
                try {
195
                    if(fos != null) fos.close();
196
                } catch (IOException ex) {
197
                    Exceptions.printStackTrace(ex);
198
                }
199
                try {
200
                    if(is != null) is.close();
201
                } catch (IOException ex) {
202
                    Exceptions.printStackTrace(ex);
203
                }
204
            }
205
        }
206
    }
207
    
208
    private Blob loadLobFromFile() {
209
        JFileChooser c = new JFileChooser();
210
        Blob result = null;
211
        int fileDialogState = c.showOpenDialog(table);
212
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
213
            File f = c.getSelectedFile();
214
            FileInputStream fis = null;
215
            try {
216
                fis = new FileInputStream(f);
217
                result = new FileBackedBlob(fis);
218
            } catch (IOException ex) {
219
                throw new RuntimeException(ex);
220
            } catch (SQLException ex) {
221
                throw new RuntimeException(ex);
222
            } finally {
223
                try {
224
                    if(fis != null) fis.close();
225
                } catch (IOException ex) {
226
                    Exceptions.printStackTrace(ex);
227
                }
228
            }
229
        }
230
        return result;
231
    }
232
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BooleanTableCellEditor.java (+68 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.table.celleditor;
43
44
import java.awt.Component;
45
import javax.swing.BorderFactory;
46
import javax.swing.JComponent;
47
import javax.swing.JTable;
48
import javax.swing.table.TableCellEditor;
49
import org.jdesktop.swingx.renderer.JRendererCheckBox;
50
51
public class BooleanTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor {
52
53
    public BooleanTableCellEditor(JRendererCheckBox cb) {
54
        super(cb);
55
        cb.setHorizontalAlignment(0);
56
    }
57
58
    @Override
59
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
60
        this.table = table;
61
        Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column);
62
        setEditable(column, c, table.isCellEditable(row, column));
63
        if (isGtk && c instanceof JComponent) {
64
            ((JComponent) c).setBorder(BorderFactory.createEmptyBorder());
65
        }
66
        return c;
67
    }
68
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ClobFieldTableCellEditor.java (+273 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.table.celleditor;
43
44
import java.awt.Component;
45
import java.awt.Font;
46
import java.awt.event.ActionEvent;
47
import java.awt.event.ActionListener;
48
import java.awt.event.MouseEvent;
49
import java.io.File;
50
import java.io.FileInputStream;
51
import java.io.FileOutputStream;
52
import java.io.IOException;
53
import java.io.InputStreamReader;
54
import java.io.OutputStreamWriter;
55
import java.io.Reader;
56
import java.io.Writer;
57
import java.nio.charset.Charset;
58
import java.sql.Clob;
59
import java.sql.SQLException;
60
import java.util.ArrayList;
61
import java.util.Collections;
62
import java.util.Comparator;
63
import java.util.EventObject;
64
import java.util.List;
65
import javax.swing.AbstractCellEditor;
66
import javax.swing.DefaultComboBoxModel;
67
import javax.swing.JButton;
68
import javax.swing.JComboBox;
69
import javax.swing.JFileChooser;
70
import javax.swing.JMenuItem;
71
import javax.swing.JPanel;
72
import javax.swing.JPopupMenu;
73
import javax.swing.JTable;
74
import javax.swing.SwingConstants;
75
import javax.swing.table.TableCellEditor;
76
import org.netbeans.modules.db.dataview.util.FileBackedClob;
77
import org.openide.util.Exceptions;
78
import org.openide.util.NbBundle;
79
80
public class ClobFieldTableCellEditor extends AbstractCellEditor
81
        implements TableCellEditor,
82
        ActionListener {
83
84
    private class CharsetSelector extends JPanel {
85
        private JComboBox charsetSelect;
86
        
87
        CharsetSelector() {
88
            List<Charset> charset = new ArrayList<Charset>(Charset.availableCharsets().values());
89
            Collections.sort(charset, new Comparator<Charset>() {
90
                @Override
91
                public int compare(Charset o1, Charset o2) {
92
                    return o1.displayName().compareTo(o2.displayName());
93
                }
94
            });
95
            charsetSelect = new JComboBox();
96
            charsetSelect.setModel(new DefaultComboBoxModel(charset.toArray()));
97
            charsetSelect.setSelectedItem(Charset.defaultCharset());
98
            this.add(charsetSelect);
99
        }
100
101
        public Charset getSelectedCharset() {
102
            return (Charset) charsetSelect.getSelectedItem();
103
        }
104
105
        public void setSelectedCharset(Charset selectedCharset) {
106
            charsetSelect.setSelectedItem(selectedCharset);
107
        }
108
    }
109
    
110
    protected static final String EDIT = "edit";
111
    protected Clob currentValue;
112
    protected JButton button;
113
    protected JPopupMenu popup;
114
    protected JTable table;
115
116
    public ClobFieldTableCellEditor() {
117
        button = new JButton();
118
        button.setActionCommand(EDIT);
119
        button.addActionListener(this);
120
        button.setContentAreaFilled(false);
121
        button.setOpaque(false);
122
        button.setBorderPainted(false);
123
        button.setRolloverEnabled(false);
124
        button.setAlignmentX(0);
125
        button.setHorizontalAlignment(SwingConstants.LEFT);
126
        button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9));
127
128
        popup = new JPopupMenu();
129
        final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title"));
130
        miLobSaveAction.addActionListener(new ActionListener() {
131
132
            @Override
133
            public void actionPerformed(ActionEvent e) {
134
                saveLobToFile(currentValue);
135
                fireEditingCanceled();
136
            }
137
        });
138
        popup.add(miLobSaveAction);
139
        final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title"));
140
        miLobLoadAction.addActionListener(new ActionListener() {
141
142
            @Override
143
            public void actionPerformed(ActionEvent e) {
144
                Object newValue = loadLobFromFile();
145
                if (newValue != null) {
146
                    currentValue = (Clob) newValue;
147
                }
148
                fireEditingStopped();
149
            }
150
        });
151
        popup.add(miLobLoadAction);
152
        final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title"));
153
        miLobNullAction.addActionListener(new ActionListener() {
154
155
            @Override
156
            public void actionPerformed(ActionEvent e) {
157
                currentValue = null;
158
                fireEditingStopped();
159
            }
160
        });
161
        popup.add(miLobNullAction);
162
163
    }
164
165
    public void actionPerformed(ActionEvent e) {
166
        if (EDIT.equals(e.getActionCommand())) {
167
            popup.show(button, 0, button.getHeight());
168
        }
169
    }
170
171
    @Override
172
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
173
        currentValue = (java.sql.Clob) value;
174
        if (currentValue != null) {
175
            try {
176
                long size = currentValue.length();
177
                StringBuilder stringValue = new StringBuilder();
178
                stringValue.append("<CLOB ");
179
                if (size < 1000) {
180
                    stringValue.append(String.format("%1$d Chars", size));
181
                } else if (size < 1000000) {
182
                    stringValue.append(String.format("%1$d kChars", size / 1000));
183
                } else {
184
                    stringValue.append(String.format("%1$d MChars", size / 1000000));
185
                }
186
                stringValue.append(">");
187
                button.setText(stringValue.toString());
188
            } catch (SQLException ex) {
189
                button.setText("<CLOB of unknown size>");
190
            }
191
        } else {
192
            button.setText("<NULL>");
193
        }
194
        this.table = table;
195
        return button;
196
    }
197
198
    @Override
199
    public Object getCellEditorValue() {
200
        return currentValue;
201
    }
202
203
    @Override
204
    public boolean isCellEditable(EventObject anEvent) {
205
        if (anEvent instanceof MouseEvent) {
206
            return ((MouseEvent) anEvent).getClickCount() >= 2;
207
        }
208
        return super.isCellEditable(anEvent);
209
    }
210
    
211
    private void saveLobToFile(Clob b) {
212
        CharsetSelector charset = new CharsetSelector();
213
        JFileChooser c = new JFileChooser();
214
        c.setAccessory(charset);
215
        int fileDialogState = c.showSaveDialog(table);
216
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
217
            File f = c.getSelectedFile();
218
            Reader r = null;
219
            Writer w = null;
220
            try {
221
                r = b.getCharacterStream();
222
                w = new OutputStreamWriter(new FileOutputStream(f), charset.getSelectedCharset());
223
                int read = 0;
224
                char[] buffer = new char[1024];
225
                while((read = r.read(buffer)) > 0) {
226
                    w.write(buffer, 0, read);
227
                }
228
            } catch (IOException ex) {
229
                throw new RuntimeException(ex);
230
            } catch (SQLException ex) {
231
                throw new RuntimeException(ex);
232
            } finally {
233
                try {
234
                    if(w != null) w.close();
235
                } catch (IOException ex) {
236
                    Exceptions.printStackTrace(ex);
237
                }
238
                try {
239
                    if(r != null) r.close();
240
                } catch (IOException ex) {
241
                    Exceptions.printStackTrace(ex);
242
                }
243
            }
244
        }
245
    }
246
    
247
    private Clob loadLobFromFile() {
248
        CharsetSelector charset = new CharsetSelector();
249
        JFileChooser c = new JFileChooser();
250
        c.setAccessory(charset);
251
        Clob result = null;
252
        int fileDialogState = c.showOpenDialog(table);
253
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
254
            File f = c.getSelectedFile();
255
            Reader r = null;
256
            try {
257
                r = new InputStreamReader(new FileInputStream(f), charset.getSelectedCharset());
258
                result = new FileBackedClob(r);
259
            } catch (IOException ex) {
260
                throw new RuntimeException(ex);
261
            } catch (SQLException ex) {
262
                throw new RuntimeException(ex);
263
            } finally {
264
                try {
265
                    if(r != null) r.close();
266
                } catch (IOException ex) {
267
                    Exceptions.printStackTrace(ex);
268
                }
269
            }
270
        }
271
        return result;
272
    }
273
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/DateTimePickerCellEditor.java (+230 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.table.celleditor;
43
44
import java.awt.Component;
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.awt.event.KeyListener;
48
import java.awt.event.MouseEvent;
49
import java.sql.Timestamp;
50
import java.text.DateFormat;
51
import java.text.ParseException;
52
import java.text.SimpleDateFormat;
53
import java.util.EventObject;
54
import javax.swing.AbstractCellEditor;
55
import javax.swing.BorderFactory;
56
import javax.swing.JTable;
57
import javax.swing.UIManager;
58
import javax.swing.table.TableCellEditor;
59
import org.jdesktop.swingx.JXDatePicker;
60
import org.netbeans.modules.db.dataview.meta.DBColumn;
61
import org.netbeans.modules.db.dataview.table.ResultSetJXTable;
62
import org.netbeans.modules.db.dataview.util.DataViewUtils;
63
import org.netbeans.modules.db.dataview.util.JXDateTimePicker;
64
import org.netbeans.modules.db.dataview.util.TimestampType;
65
66
public class DateTimePickerCellEditor extends AbstractCellEditor implements TableCellEditor {
67
68
    private boolean editable = true;
69
    private JXDateTimePicker datePicker;
70
    private DateFormat dateFormat;
71
    private ActionListener pickerActionListener;
72
    private boolean ignoreAction;
73
    private JTable table;
74
75
    public DateTimePickerCellEditor() {
76
        this(new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN));
77
    }
78
79
    /**
80
     * Instantiates an editor with the given dateFormat. If
81
     * null, the datePickers default is used.
82
     * 
83
     * @param dateFormat
84
     */
85
    public DateTimePickerCellEditor(DateFormat dateFormat) {
86
87
        // JW: the copy is used to synchronize .. can 
88
        // we use something else?
89
        this.dateFormat = dateFormat != null ? dateFormat : new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN);
90
        datePicker = new JXDateTimePicker();
91
        // default border crushes the editor/combo
92
        datePicker.getEditor().setBorder(
93
                BorderFactory.createEmptyBorder(0, 1, 0, 1));
94
        // should be fixed by j2se 6.0
95
        datePicker.setFont(UIManager.getDefaults().getFont("TextField.font"));
96
        if (dateFormat != null) {
97
            datePicker.setFormats(dateFormat);
98
        }
99
        datePicker.addActionListener(getPickerActionListener());
100
    }
101
102
    @Override
103
    public Timestamp getCellEditorValue() {
104
        return datePicker.getDateTime();
105
    }
106
107
    @Override
108
    public boolean isCellEditable(EventObject anEvent) {
109
        if (anEvent instanceof MouseEvent) {
110
            return ((MouseEvent) anEvent).getClickCount() >= 2;
111
        }
112
        return super.isCellEditable(anEvent);
113
    }
114
115
    @Override
116
    public boolean stopCellEditing() {
117
        ignoreAction = true;
118
        boolean canCommit = commitChange();
119
        ignoreAction = false;
120
        if (canCommit) {
121
            datePicker.setDateTime(null);
122
            return super.stopCellEditing();
123
        }
124
        return false;
125
    }
126
127
    @Override
128
    public Component getTableCellEditorComponent(final JTable table, Object value,
129
            boolean isSelected, int row, int column) {
130
        this.table = table;
131
        ignoreAction = true;
132
        datePicker.setDateTime(getValueAsTimestamp(value));
133
134
        ignoreAction = false;
135
        setEditable(column, datePicker, table.isCellEditable(row, column));
136
        return datePicker;
137
    }
138
139
    protected Timestamp getValueAsTimestamp(Object value) {
140
        if (isEmpty(value) || DataViewUtils.isSQLConstantString(value)) {
141
            return new Timestamp(System.currentTimeMillis());
142
        }
143
144
        if (value instanceof Timestamp) {
145
            return (Timestamp) value;
146
        }
147
        if (value instanceof Long) {
148
            return new Timestamp((Long) value);
149
        }
150
        if (value instanceof String) {
151
            try {
152
153
                return new Timestamp(dateFormat.parse((String) value).getTime());
154
            } catch (ParseException e) {
155
                //mLogger.log(Level.SEVERE, e.getMessage(), e.getMessage());
156
            }
157
        }
158
159
        return new Timestamp(System.currentTimeMillis());
160
    }
161
162
    protected boolean isEmpty(Object value) {
163
        return value == null || value instanceof String && ((String) value).length() == 0;
164
    }
165
166
    protected boolean commitChange() {
167
        try {
168
            datePicker.commitEdit();
169
            return true;
170
        } catch (ParseException e) {
171
        }
172
        return false;
173
    }
174
175
    public DateFormat[] getFormats() {
176
        return datePicker.getFormats();
177
    }
178
179
    public void setFormats(DateFormat... formats) {
180
        datePicker.setFormats(formats);
181
    }
182
183
    private ActionListener getPickerActionListener() {
184
        if (pickerActionListener == null) {
185
            pickerActionListener = createPickerActionListener();
186
        }
187
        return pickerActionListener;
188
    }
189
190
    protected ActionListener createPickerActionListener() {
191
        ActionListener l = new ActionListener() {
192
193
            @Override
194
            public void actionPerformed(final ActionEvent e) {
195
                // avoid duplicate trigger from
196
                // commit in stopCellEditing
197
                if (ignoreAction) {
198
                    return;
199
                }
200
                terminateEdit(e);
201
            }
202
203
            private void terminateEdit(final ActionEvent e) {
204
                if ((e != null) && (JXDatePicker.COMMIT_KEY.equals(e.getActionCommand()))) {
205
                    stopCellEditing();
206
                } else {
207
                    cancelCellEditing();
208
                }
209
            }
210
        };
211
        return l;
212
    }
213
214
    protected void setEditable(int column, JXDateTimePicker c, boolean celleditable) {
215
        assert table != null;
216
        DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column);
217
        if (dbCol.isGenerated()) {
218
            editable = false;
219
        } else if (! celleditable) {
220
            editable = false;
221
        } else {
222
            editable = dbCol.isEditable();
223
        }
224
        c.setEditable(editable);
225
    }
226
227
    public void addKeyListener(KeyListener kl) {
228
        datePicker.addKeyListener(kl);
229
    }
230
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/NumberFieldEditor.java (+67 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.table.celleditor;
43
44
import java.awt.Component;
45
import javax.swing.BorderFactory;
46
import javax.swing.JComponent;
47
import javax.swing.JTable;
48
import javax.swing.JTextField;
49
50
public class NumberFieldEditor extends ResultSetTableCellEditor {
51
52
    public NumberFieldEditor(final JTextField textField) {
53
        super(textField);
54
        ((JTextField) getComponent()).setHorizontalAlignment(JTextField.RIGHT);
55
    }
56
57
    @Override
58
    public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) {
59
        this.table = table;
60
        Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column);
61
        if (isGtk && c instanceof JComponent) {
62
            ((JComponent) c).setBorder(BorderFactory.createEmptyBorder());
63
        }
64
        setEditable(column, c, table.isCellEditable(row, column));
65
        return c;
66
    }
67
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetTableCellEditor.java (-716 / +4 lines)
Lines 39-121 Link Here
39
 * 
39
 * 
40
 * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
40
 * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
41
 */
41
 */
42
package org.netbeans.modules.db.dataview.table;
42
package org.netbeans.modules.db.dataview.table.celleditor;
43
43
44
import java.awt.BorderLayout;
45
import java.awt.Component;
44
import java.awt.Component;
46
import java.awt.Dimension;
47
import java.awt.Font;
48
import java.awt.Insets;
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51
import java.awt.event.KeyEvent;
52
import java.awt.event.KeyListener;
53
import java.awt.event.MouseEvent;
45
import java.awt.event.MouseEvent;
54
import java.io.File;
55
import java.io.FileInputStream;
56
import java.io.FileOutputStream;
57
import java.io.IOException;
58
import java.io.InputStream;
59
import java.io.InputStreamReader;
60
import java.io.OutputStreamWriter;
61
import java.io.Reader;
62
import java.io.Writer;
63
import java.nio.charset.Charset;
64
import java.sql.Blob;
65
import java.sql.Clob;
66
import java.sql.SQLException;
67
import java.sql.Timestamp;
68
import java.text.DateFormat;
69
import java.text.ParseException;
70
import java.text.SimpleDateFormat;
71
import java.util.ArrayList;
72
import java.util.Collections;
73
import java.util.Comparator;
74
import java.util.EventObject;
46
import java.util.EventObject;
75
import java.util.List;
76
import javax.swing.AbstractCellEditor;
77
import javax.swing.Action;
78
import javax.swing.ActionMap;
79
import javax.swing.BorderFactory;
80
import javax.swing.DefaultCellEditor;
47
import javax.swing.DefaultCellEditor;
81
import javax.swing.DefaultComboBoxModel;
82
import javax.swing.InputMap;
83
import javax.swing.JButton;
84
import javax.swing.JComboBox;
85
import javax.swing.JComponent;
48
import javax.swing.JComponent;
86
import javax.swing.JFileChooser;
87
import javax.swing.JMenuItem;
88
import javax.swing.JOptionPane;
89
import javax.swing.JPanel;
90
import javax.swing.JPopupMenu;
91
import javax.swing.JScrollPane;
92
import javax.swing.JTable;
49
import javax.swing.JTable;
93
import javax.swing.JTextArea;
94
import javax.swing.JTextField;
50
import javax.swing.JTextField;
95
import javax.swing.KeyStroke;
96
import javax.swing.SwingConstants;
97
import javax.swing.SwingUtilities;
98
import javax.swing.UIManager;
51
import javax.swing.UIManager;
99
import javax.swing.table.TableCellEditor;
100
import org.jdesktop.swingx.JXButton;
101
import org.jdesktop.swingx.JXDatePicker;
102
import org.jdesktop.swingx.JXPanel;
103
import org.jdesktop.swingx.renderer.JRendererCheckBox;
52
import org.jdesktop.swingx.renderer.JRendererCheckBox;
104
import org.netbeans.modules.db.dataview.meta.DBColumn;
53
import org.netbeans.modules.db.dataview.meta.DBColumn;
105
import org.netbeans.modules.db.dataview.util.FileBackedBlob;
54
import org.netbeans.modules.db.dataview.table.ResultSetJXTable;
106
import org.netbeans.modules.db.dataview.util.FileBackedClob;
107
import org.netbeans.modules.db.dataview.util.DBReadWriteHelper;
55
import org.netbeans.modules.db.dataview.util.DBReadWriteHelper;
108
import org.netbeans.modules.db.dataview.util.DataViewUtils;
56
import org.netbeans.modules.db.dataview.util.DataViewUtils;
109
import org.netbeans.modules.db.dataview.util.JXDateTimePicker;
110
import org.netbeans.modules.db.dataview.util.TimestampType;
111
import org.openide.awt.StatusDisplayer;
57
import org.openide.awt.StatusDisplayer;
112
import org.openide.util.Exceptions;
113
import org.openide.util.NbBundle;
114
import org.openide.windows.WindowManager;
115
58
116
/**
117
 * @author Ahimanikya Satapathy
118
 */
119
public class ResultSetTableCellEditor extends DefaultCellEditor {
59
public class ResultSetTableCellEditor extends DefaultCellEditor {
120
60
121
    protected Object val;
61
    protected Object val;
Lines 203-215 Link Here
203
        checkBox.addActionListener(delegate);
143
        checkBox.addActionListener(delegate);
204
    }
144
    }
205
145
206
    protected void setEditable(int column, Component c) {
146
    protected void setEditable(int column, Component c, boolean celleditable) {
207
        assert table != null;
147
        assert table != null;
208
        DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column);
148
        DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column);
209
        if (dbCol.isGenerated()) {
149
        if (dbCol.isGenerated()) {
210
            editable = false;
150
            editable = false;
211
        }
151
        }
212
        if (!((ResultSetJXTable) table).dView.isEditable()) {
152
        if (! celleditable) {
213
            editable = false;
153
            editable = false;
214
        } else {
154
        } else {
215
            editable = dbCol.isEditable();
155
            editable = dbCol.isEditable();
Lines 221-876 Link Here
221
            ((JComponent) c).setEnabled(editable);
161
            ((JComponent) c).setEnabled(editable);
222
        }
162
        }
223
    }
163
    }
224
}
225
226
class BooleanTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor {
227
228
    public BooleanTableCellEditor(JRendererCheckBox cb) {
229
        super(cb);
230
        cb.setHorizontalAlignment(0);
231
    }
232
233
    @Override
234
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
235
        this.table = table;
236
        Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column);
237
        setEditable(column, c);
238
        if (isGtk && c instanceof JComponent) {
239
            ((JComponent) c).setBorder(BorderFactory.createEmptyBorder());
240
        }
241
        return c;
242
    }
243
}
244
245
class StringTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor, ActionListener {
246
247
    private JXButton customEditorButton = new JXButton("...");
248
    private int row, column;
249
250
    public StringTableCellEditor(final JTextField textField) {
251
        super(textField);
252
        customEditorButton.addActionListener(this);
253
254
        // ui-tweaking
255
        customEditorButton.setFocusable(false);
256
        customEditorButton.setFocusPainted(false);
257
        customEditorButton.setMargin(new Insets(0, 0, 0, 0));
258
        customEditorButton.setPreferredSize(new Dimension(20, 10));
259
    }
260
261
    @Override
262
    public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) {
263
        this.table = table;
264
        final JComponent c = (JComponent) super.getTableCellEditorComponent(table, value, isSelected, row, column);
265
        setEditable(column, c);
266
267
        JXPanel panel = new JXPanel(new BorderLayout()) {
268
269
            @Override
270
            public void addNotify() {
271
                super.addNotify();
272
                c.requestFocus();
273
            }
274
275
            @Override
276
            protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
277
                InputMap map = c.getInputMap(condition);
278
                ActionMap am = c.getActionMap();
279
280
                if (map != null && am != null && isEnabled()) {
281
                    Object binding = map.get(ks);
282
                    Action action = (binding == null) ? null : am.get(binding);
283
                    if (action != null) {
284
                        return SwingUtilities.notifyAction(action, ks, e, c,
285
                                e.getModifiers());
286
                    }
287
                }
288
                return false;
289
            }
290
        };
291
        panel.add(c);
292
        if (isGtk) {
293
            c.setBorder(BorderFactory.createEmptyBorder());
294
        }
295
        panel.add(customEditorButton, BorderLayout.EAST);
296
        panel.revalidate();
297
        panel.repaint();
298
299
        this.row = row;
300
        this.column = column;
301
        return panel;
302
    }
303
304
    @Override
305
    public final void actionPerformed(ActionEvent e) {
306
        assert table != null;
307
        super.cancelCellEditing();
308
        editCell(table, row, column);
309
    }
310
311
    protected void editCell(JTable table, int row, int column) {
312
        JTextArea textArea = new JTextArea(10, 50);
313
        Object value = table.getValueAt(row, column);
314
        if (value != null) {
315
            textArea.setText(value.toString());
316
            textArea.setCaretPosition(0);
317
            textArea.setEditable(editable);
318
        }
319
        JScrollPane pane = new JScrollPane(textArea);
320
        Component parent = WindowManager.getDefault().getMainWindow();
321
322
        if (editable) {
323
            int result = JOptionPane.showOptionDialog(parent, pane, table.getColumnName(column), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
324
            if (result == JOptionPane.OK_OPTION) {
325
                table.setValueAt(textArea.getText(), row, column);
326
            }
327
        } else {
328
            JOptionPane.showMessageDialog(parent, pane, table.getColumnName(column), JOptionPane.PLAIN_MESSAGE, null);
329
        }
330
    }
331
}
332
333
class DateTimePickerCellEditor extends AbstractCellEditor implements TableCellEditor {
334
335
    private boolean editable = true;
336
    private JXDateTimePicker datePicker;
337
    private DateFormat dateFormat;
338
    private ActionListener pickerActionListener;
339
    private boolean ignoreAction;
340
    private JTable table;
341
342
    public DateTimePickerCellEditor() {
343
        this(new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN));
344
    }
345
346
    /**
347
     * Instantiates an editor with the given dateFormat. If
348
     * null, the datePickers default is used.
349
     * 
350
     * @param dateFormat
351
     */
352
    public DateTimePickerCellEditor(DateFormat dateFormat) {
353
354
        // JW: the copy is used to synchronize .. can 
355
        // we use something else?
356
        this.dateFormat = dateFormat != null ? dateFormat : new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN);
357
        datePicker = new JXDateTimePicker();
358
        // default border crushes the editor/combo
359
        datePicker.getEditor().setBorder(
360
                BorderFactory.createEmptyBorder(0, 1, 0, 1));
361
        // should be fixed by j2se 6.0
362
        datePicker.setFont(UIManager.getDefaults().getFont("TextField.font"));
363
        if (dateFormat != null) {
364
            datePicker.setFormats(dateFormat);
365
        }
366
        datePicker.addActionListener(getPickerActionListener());
367
    }
368
369
    @Override
370
    public Timestamp getCellEditorValue() {
371
        return datePicker.getDateTime();
372
    }
373
374
    @Override
375
    public boolean isCellEditable(EventObject anEvent) {
376
        if (anEvent instanceof MouseEvent) {
377
            return ((MouseEvent) anEvent).getClickCount() >= 2;
378
        }
379
        return super.isCellEditable(anEvent);
380
    }
381
382
    @Override
383
    public boolean stopCellEditing() {
384
        ignoreAction = true;
385
        boolean canCommit = commitChange();
386
        ignoreAction = false;
387
        if (canCommit) {
388
            datePicker.setDateTime(null);
389
            return super.stopCellEditing();
390
        }
391
        return false;
392
    }
393
394
    @Override
395
    public Component getTableCellEditorComponent(final JTable table, Object value,
396
            boolean isSelected, int row, int column) {
397
        this.table = table;
398
        ignoreAction = true;
399
        datePicker.setDateTime(getValueAsTimestamp(value));
400
401
        ignoreAction = false;
402
        setEditable(column, datePicker);
403
        return datePicker;
404
    }
405
406
    protected Timestamp getValueAsTimestamp(Object value) {
407
        if (isEmpty(value) || DataViewUtils.isSQLConstantString(value)) {
408
            return new Timestamp(System.currentTimeMillis());
409
        }
410
411
        if (value instanceof Timestamp) {
412
            return (Timestamp) value;
413
        }
414
        if (value instanceof Long) {
415
            return new Timestamp((Long) value);
416
        }
417
        if (value instanceof String) {
418
            try {
419
420
                return new Timestamp(dateFormat.parse((String) value).getTime());
421
            } catch (ParseException e) {
422
                //mLogger.log(Level.SEVERE, e.getMessage(), e.getMessage());
423
            }
424
        }
425
426
        return new Timestamp(System.currentTimeMillis());
427
    }
428
429
    protected boolean isEmpty(Object value) {
430
        return value == null || value instanceof String && ((String) value).length() == 0;
431
    }
432
433
    protected boolean commitChange() {
434
        try {
435
            datePicker.commitEdit();
436
            return true;
437
        } catch (ParseException e) {
438
        }
439
        return false;
440
    }
441
442
    public DateFormat[] getFormats() {
443
        return datePicker.getFormats();
444
    }
445
446
    public void setFormats(DateFormat... formats) {
447
        datePicker.setFormats(formats);
448
    }
449
450
    private ActionListener getPickerActionListener() {
451
        if (pickerActionListener == null) {
452
            pickerActionListener = createPickerActionListener();
453
        }
454
        return pickerActionListener;
455
    }
456
457
    protected ActionListener createPickerActionListener() {
458
        ActionListener l = new ActionListener() {
459
460
            @Override
461
            public void actionPerformed(final ActionEvent e) {
462
                // avoid duplicate trigger from
463
                // commit in stopCellEditing
464
                if (ignoreAction) {
465
                    return;
466
                }
467
                terminateEdit(e);
468
            }
469
470
            private void terminateEdit(final ActionEvent e) {
471
                if ((e != null) && (JXDatePicker.COMMIT_KEY.equals(e.getActionCommand()))) {
472
                    stopCellEditing();
473
                } else {
474
                    cancelCellEditing();
475
                }
476
            }
477
        };
478
        return l;
479
    }
480
481
    protected void setEditable(int column, JXDateTimePicker c) {
482
        assert table != null;
483
        DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column);
484
        if (dbCol.isGenerated()) {
485
            editable = false;
486
        } else if (!((ResultSetJXTable) table).dView.isEditable()) {
487
            editable = false;
488
        } else {
489
            editable = dbCol.isEditable();
490
        }
491
        c.setEditable(editable);
492
    }
493
494
    protected void addKeyListener(KeyListener kl) {
495
        datePicker.addKeyListener(kl);
496
    }
497
}
498
499
class NumberFieldEditor extends ResultSetTableCellEditor {
500
501
    public NumberFieldEditor(final JTextField textField) {
502
        super(textField);
503
        ((JTextField) getComponent()).setHorizontalAlignment(JTextField.RIGHT);
504
    }
505
506
    @Override
507
    public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) {
508
        this.table = table;
509
        Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column);
510
        if (isGtk && c instanceof JComponent) {
511
            ((JComponent) c).setBorder(BorderFactory.createEmptyBorder());
512
        }
513
        setEditable(column, c);
514
        return c;
515
    }
516
}
517
518
class BlobFieldTableCellEditor extends AbstractCellEditor
519
        implements TableCellEditor,
520
        ActionListener {
521
522
    protected static final String EDIT = "edit";
523
    protected Blob currentValue;
524
    protected JButton button;
525
    protected JPopupMenu popup;
526
    protected JTable table;
527
528
    public BlobFieldTableCellEditor() {
529
        button = new JButton();
530
        button.setActionCommand(EDIT);
531
        button.addActionListener(this);
532
        button.setContentAreaFilled(false);
533
        button.setOpaque(false);
534
        button.setBorderPainted(false);
535
        button.setRolloverEnabled(false);
536
        button.setAlignmentX(0);
537
        button.setHorizontalAlignment(SwingConstants.LEFT);
538
        button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9));
539
540
        popup = new JPopupMenu();
541
        final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title"));
542
        miLobSaveAction.addActionListener(new ActionListener() {
543
544
            @Override
545
            public void actionPerformed(ActionEvent e) {
546
                saveLobToFile(currentValue);
547
                fireEditingCanceled();
548
            }
549
        });
550
        popup.add(miLobSaveAction);
551
        final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title"));
552
        miLobLoadAction.addActionListener(new ActionListener() {
553
554
            @Override
555
            public void actionPerformed(ActionEvent e) {
556
                Object newValue = loadLobFromFile();
557
                if (newValue != null) {
558
                    currentValue = (Blob) newValue;
559
                }
560
                fireEditingStopped();
561
            }
562
        });
563
        popup.add(miLobLoadAction);
564
        final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title"));
565
        miLobNullAction.addActionListener(new ActionListener() {
566
567
            @Override
568
            public void actionPerformed(ActionEvent e) {
569
                currentValue = null;
570
                fireEditingStopped();
571
            }
572
        });
573
        popup.add(miLobNullAction);
574
575
    }
576
577
    public void actionPerformed(ActionEvent e) {
578
        if (EDIT.equals(e.getActionCommand())) {
579
            popup.show(button, 0, button.getHeight());
580
        }
581
    }
582
583
    @Override
584
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
585
        currentValue = (java.sql.Blob) value;
586
        if (currentValue != null) {
587
            try {
588
                long size = currentValue.length();
589
                StringBuilder stringValue = new StringBuilder();
590
                stringValue.append("<BLOB ");
591
                if (size < 1000) {
592
                    stringValue.append(String.format("%1$d bytes", size));
593
                } else if (size < 1000000) {
594
                    stringValue.append(String.format("%1$d kB", size / 1000));
595
                } else {
596
                    stringValue.append(String.format("%1$d MB", size / 1000000));
597
                }
598
                stringValue.append(">");
599
                button.setText(stringValue.toString());
600
            } catch (SQLException ex) {
601
                button.setText("<BLOB of unknown size>");
602
            }
603
        } else {
604
            button.setText("<NULL>");
605
        }
606
        this.table = table;
607
        return button;
608
    }
609
610
    @Override
611
    public Object getCellEditorValue() {
612
        return currentValue;
613
    }
614
615
    @Override
616
    public boolean isCellEditable(EventObject anEvent) {
617
        if (anEvent instanceof MouseEvent) {
618
            return ((MouseEvent) anEvent).getClickCount() >= 2;
619
        }
620
        return super.isCellEditable(anEvent);
621
    }
622
623
    private void saveLobToFile(Blob b) {
624
        JFileChooser c = new JFileChooser();
625
        int fileDialogState = c.showSaveDialog(table);
626
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
627
            File f = c.getSelectedFile();
628
            InputStream is = null;
629
            FileOutputStream fos = null;
630
            try {
631
                is = b.getBinaryStream();
632
                fos = new FileOutputStream(f);
633
                int read = 0;
634
                byte[] buffer = new byte[1024];
635
                while((read = is.read(buffer)) > 0) {
636
                    fos.write(buffer, 0, read);
637
                }
638
            } catch (IOException ex) {
639
                throw new RuntimeException(ex);
640
            } catch (SQLException ex) {
641
                throw new RuntimeException(ex);
642
            } finally {
643
                try {
644
                    if(fos != null) fos.close();
645
                } catch (IOException ex) {
646
                    Exceptions.printStackTrace(ex);
647
                }
648
                try {
649
                    if(is != null) is.close();
650
                } catch (IOException ex) {
651
                    Exceptions.printStackTrace(ex);
652
                }
653
            }
654
        }
655
    }
656
    
657
    private Blob loadLobFromFile() {
658
        JFileChooser c = new JFileChooser();
659
        Blob result = null;
660
        int fileDialogState = c.showOpenDialog(table);
661
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
662
            File f = c.getSelectedFile();
663
            FileInputStream fis = null;
664
            try {
665
                fis = new FileInputStream(f);
666
                result = new FileBackedBlob(fis);
667
            } catch (IOException ex) {
668
                throw new RuntimeException(ex);
669
            } catch (SQLException ex) {
670
                throw new RuntimeException(ex);
671
            } finally {
672
                try {
673
                    if(fis != null) fis.close();
674
                } catch (IOException ex) {
675
                    Exceptions.printStackTrace(ex);
676
                }
677
            }
678
        }
679
        return result;
680
    }
681
}
682
683
class ClobFieldTableCellEditor extends AbstractCellEditor
684
        implements TableCellEditor,
685
        ActionListener {
686
687
    private class CharsetSelector extends JPanel {
688
        private JComboBox charsetSelect;
689
        
690
        CharsetSelector() {
691
            List<Charset> charset = new ArrayList<Charset>(Charset.availableCharsets().values());
692
            Collections.sort(charset, new Comparator<Charset>() {
693
                @Override
694
                public int compare(Charset o1, Charset o2) {
695
                    return o1.displayName().compareTo(o2.displayName());
696
                }
697
            });
698
            charsetSelect = new JComboBox();
699
            charsetSelect.setModel(new DefaultComboBoxModel(charset.toArray()));
700
            charsetSelect.setSelectedItem(Charset.defaultCharset());
701
            this.add(charsetSelect);
702
        }
703
704
        public Charset getSelectedCharset() {
705
            return (Charset) charsetSelect.getSelectedItem();
706
        }
707
708
        public void setSelectedCharset(Charset selectedCharset) {
709
            charsetSelect.setSelectedItem(selectedCharset);
710
        }
711
    }
712
    
713
    protected static final String EDIT = "edit";
714
    protected Clob currentValue;
715
    protected JButton button;
716
    protected JPopupMenu popup;
717
    protected JTable table;
718
719
    public ClobFieldTableCellEditor() {
720
        button = new JButton();
721
        button.setActionCommand(EDIT);
722
        button.addActionListener(this);
723
        button.setContentAreaFilled(false);
724
        button.setOpaque(false);
725
        button.setBorderPainted(false);
726
        button.setRolloverEnabled(false);
727
        button.setAlignmentX(0);
728
        button.setHorizontalAlignment(SwingConstants.LEFT);
729
        button.setFont(new Font(button.getFont().getFamily(), Font.ITALIC, 9));
730
731
        popup = new JPopupMenu();
732
        final JMenuItem miLobSaveAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "saveLob.title"));
733
        miLobSaveAction.addActionListener(new ActionListener() {
734
735
            @Override
736
            public void actionPerformed(ActionEvent e) {
737
                saveLobToFile(currentValue);
738
                fireEditingCanceled();
739
            }
740
        });
741
        popup.add(miLobSaveAction);
742
        final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title"));
743
        miLobLoadAction.addActionListener(new ActionListener() {
744
745
            @Override
746
            public void actionPerformed(ActionEvent e) {
747
                Object newValue = loadLobFromFile();
748
                if (newValue != null) {
749
                    currentValue = (Clob) newValue;
750
                }
751
                fireEditingStopped();
752
            }
753
        });
754
        popup.add(miLobLoadAction);
755
        final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title"));
756
        miLobNullAction.addActionListener(new ActionListener() {
757
758
            @Override
759
            public void actionPerformed(ActionEvent e) {
760
                currentValue = null;
761
                fireEditingStopped();
762
            }
763
        });
764
        popup.add(miLobNullAction);
765
766
    }
767
768
    public void actionPerformed(ActionEvent e) {
769
        if (EDIT.equals(e.getActionCommand())) {
770
            popup.show(button, 0, button.getHeight());
771
        }
772
    }
773
774
    @Override
775
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
776
        currentValue = (java.sql.Clob) value;
777
        if (currentValue != null) {
778
            try {
779
                long size = currentValue.length();
780
                StringBuilder stringValue = new StringBuilder();
781
                stringValue.append("<CLOB ");
782
                if (size < 1000) {
783
                    stringValue.append(String.format("%1$d Chars", size));
784
                } else if (size < 1000000) {
785
                    stringValue.append(String.format("%1$d kChars", size / 1000));
786
                } else {
787
                    stringValue.append(String.format("%1$d MChars", size / 1000000));
788
                }
789
                stringValue.append(">");
790
                button.setText(stringValue.toString());
791
            } catch (SQLException ex) {
792
                button.setText("<CLOB of unknown size>");
793
            }
794
        } else {
795
            button.setText("<NULL>");
796
        }
797
        this.table = table;
798
        return button;
799
    }
800
801
    @Override
802
    public Object getCellEditorValue() {
803
        return currentValue;
804
    }
805
806
    @Override
807
    public boolean isCellEditable(EventObject anEvent) {
808
        if (anEvent instanceof MouseEvent) {
809
            return ((MouseEvent) anEvent).getClickCount() >= 2;
810
        }
811
        return super.isCellEditable(anEvent);
812
    }
813
    
814
    private void saveLobToFile(Clob b) {
815
        CharsetSelector charset = new CharsetSelector();
816
        JFileChooser c = new JFileChooser();
817
        c.setAccessory(charset);
818
        int fileDialogState = c.showSaveDialog(table);
819
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
820
            File f = c.getSelectedFile();
821
            Reader r = null;
822
            Writer w = null;
823
            try {
824
                r = b.getCharacterStream();
825
                w = new OutputStreamWriter(new FileOutputStream(f), charset.getSelectedCharset());
826
                int read = 0;
827
                char[] buffer = new char[1024];
828
                while((read = r.read(buffer)) > 0) {
829
                    w.write(buffer, 0, read);
830
                }
831
            } catch (IOException ex) {
832
                throw new RuntimeException(ex);
833
            } catch (SQLException ex) {
834
                throw new RuntimeException(ex);
835
            } finally {
836
                try {
837
                    if(w != null) w.close();
838
                } catch (IOException ex) {
839
                    Exceptions.printStackTrace(ex);
840
                }
841
                try {
842
                    if(r != null) r.close();
843
                } catch (IOException ex) {
844
                    Exceptions.printStackTrace(ex);
845
                }
846
            }
847
        }
848
    }
849
    
850
    private Clob loadLobFromFile() {
851
        CharsetSelector charset = new CharsetSelector();
852
        JFileChooser c = new JFileChooser();
853
        c.setAccessory(charset);
854
        Clob result = null;
855
        int fileDialogState = c.showOpenDialog(table);
856
        if (fileDialogState == JFileChooser.APPROVE_OPTION) {
857
            File f = c.getSelectedFile();
858
            Reader r = null;
859
            try {
860
                r = new InputStreamReader(new FileInputStream(f), charset.getSelectedCharset());
861
                result = new FileBackedClob(r);
862
            } catch (IOException ex) {
863
                throw new RuntimeException(ex);
864
            } catch (SQLException ex) {
865
                throw new RuntimeException(ex);
866
            } finally {
867
                try {
868
                    if(r != null) r.close();
869
                } catch (IOException ex) {
870
                    Exceptions.printStackTrace(ex);
871
                }
872
            }
873
        }
874
        return result;
875
    }
876
}
164
}
(-)a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/StringTableCellEditor.java (+154 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 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 2011 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.db.dataview.table.celleditor;
43
44
import java.awt.BorderLayout;
45
import java.awt.Component;
46
import java.awt.Dimension;
47
import java.awt.Insets;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.awt.event.KeyEvent;
51
import javax.swing.Action;
52
import javax.swing.ActionMap;
53
import javax.swing.BorderFactory;
54
import javax.swing.InputMap;
55
import javax.swing.JComponent;
56
import javax.swing.JOptionPane;
57
import javax.swing.JScrollPane;
58
import javax.swing.JTable;
59
import javax.swing.JTextArea;
60
import javax.swing.JTextField;
61
import javax.swing.KeyStroke;
62
import javax.swing.SwingUtilities;
63
import javax.swing.table.TableCellEditor;
64
import org.jdesktop.swingx.JXButton;
65
import org.jdesktop.swingx.JXPanel;
66
import org.openide.windows.WindowManager;
67
68
public class StringTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor, ActionListener {
69
70
    private JXButton customEditorButton = new JXButton("...");
71
    private int row, column;
72
73
    public StringTableCellEditor(final JTextField textField) {
74
        super(textField);
75
        customEditorButton.addActionListener(this);
76
77
        // ui-tweaking
78
        customEditorButton.setFocusable(false);
79
        customEditorButton.setFocusPainted(false);
80
        customEditorButton.setMargin(new Insets(0, 0, 0, 0));
81
        customEditorButton.setPreferredSize(new Dimension(20, 10));
82
    }
83
84
    @Override
85
    public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) {
86
        this.table = table;
87
        final JComponent c = (JComponent) super.getTableCellEditorComponent(table, value, isSelected, row, column);
88
        setEditable(column, c, table.isCellEditable(row, column));
89
90
        JXPanel panel = new JXPanel(new BorderLayout()) {
91
92
            @Override
93
            public void addNotify() {
94
                super.addNotify();
95
                c.requestFocus();
96
            }
97
98
            @Override
99
            protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) {
100
                InputMap map = c.getInputMap(condition);
101
                ActionMap am = c.getActionMap();
102
103
                if (map != null && am != null && isEnabled()) {
104
                    Object binding = map.get(ks);
105
                    Action action = (binding == null) ? null : am.get(binding);
106
                    if (action != null) {
107
                        return SwingUtilities.notifyAction(action, ks, e, c,
108
                                e.getModifiers());
109
                    }
110
                }
111
                return false;
112
            }
113
        };
114
        panel.add(c);
115
        if (isGtk) {
116
            c.setBorder(BorderFactory.createEmptyBorder());
117
        }
118
        panel.add(customEditorButton, BorderLayout.EAST);
119
        panel.revalidate();
120
        panel.repaint();
121
122
        this.row = row;
123
        this.column = column;
124
        return panel;
125
    }
126
127
    @Override
128
    public final void actionPerformed(ActionEvent e) {
129
        assert table != null;
130
        super.cancelCellEditing();
131
        editCell(table, row, column);
132
    }
133
134
    protected void editCell(JTable table, int row, int column) {
135
        JTextArea textArea = new JTextArea(10, 50);
136
        Object value = table.getValueAt(row, column);
137
        if (value != null) {
138
            textArea.setText(value.toString());
139
            textArea.setCaretPosition(0);
140
            textArea.setEditable(editable);
141
        }
142
        JScrollPane pane = new JScrollPane(textArea);
143
        Component parent = WindowManager.getDefault().getMainWindow();
144
145
        if (editable) {
146
            int result = JOptionPane.showOptionDialog(parent, pane, table.getColumnName(column), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
147
            if (result == JOptionPane.OK_OPTION) {
148
                table.setValueAt(textArea.getText(), row, column);
149
            }
150
        } else {
151
            JOptionPane.showMessageDialog(parent, pane, table.getColumnName(column), JOptionPane.PLAIN_MESSAGE, null);
152
        }
153
    }
154
}

Return to bug 206233