# HG changeset patch # User mblaesing@prometheus # Date 1323891487 -3600 # Node ID 854949c53a03e024d7ab334a8d756354dbe91290 # Parent 1bf9d8ace606731e080d90a09d8f92a3f2e4c9da Break massive CellEditor Class apart and move currently non-public classes into seperate package and files diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java b/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java --- a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetJXTable.java @@ -43,6 +43,12 @@ */ package org.netbeans.modules.db.dataview.table; +import org.netbeans.modules.db.dataview.table.celleditor.BlobFieldTableCellEditor; +import org.netbeans.modules.db.dataview.table.celleditor.StringTableCellEditor; +import org.netbeans.modules.db.dataview.table.celleditor.ClobFieldTableCellEditor; +import org.netbeans.modules.db.dataview.table.celleditor.NumberFieldEditor; +import org.netbeans.modules.db.dataview.table.celleditor.DateTimePickerCellEditor; +import org.netbeans.modules.db.dataview.table.celleditor.BooleanTableCellEditor; import java.awt.Color; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; @@ -266,7 +272,11 @@ return new ResultSetTableModel(this); } - + @Override + public boolean isEditable() { + return dView.isEditable(); + } + // This is mainly used for set Tooltip for column headers private class JTableHeaderImpl extends JXTableHeader { 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 new file mode 100644 --- /dev/null +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BlobFieldTableCellEditor.java @@ -0,0 +1,232 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.db.dataview.table.celleditor; + +import java.awt.Component; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.sql.Blob; +import java.sql.SQLException; +import java.util.EventObject; +import javax.swing.AbstractCellEditor; +import javax.swing.JButton; +import javax.swing.JFileChooser; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; +import javax.swing.JTable; +import javax.swing.SwingConstants; +import javax.swing.table.TableCellEditor; +import org.netbeans.modules.db.dataview.util.FileBackedBlob; +import org.openide.util.Exceptions; +import org.openide.util.NbBundle; + +public class BlobFieldTableCellEditor extends AbstractCellEditor + implements TableCellEditor, + ActionListener { + + protected static final String EDIT = "edit"; + protected Blob currentValue; + protected JButton button; + protected JPopupMenu popup; + protected JTable table; + + public BlobFieldTableCellEditor() { + button = new JButton(); + button.setActionCommand(EDIT); + button.addActionListener(this); + button.setContentAreaFilled(false); + button.setOpaque(false); + button.setBorderPainted(false); + button.setRolloverEnabled(false); + 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")); + miLobSaveAction.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + saveLobToFile(currentValue); + fireEditingCanceled(); + } + }); + popup.add(miLobSaveAction); + final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); + miLobLoadAction.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + Object newValue = loadLobFromFile(); + if (newValue != null) { + currentValue = (Blob) newValue; + } + fireEditingStopped(); + } + }); + popup.add(miLobLoadAction); + final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title")); + miLobNullAction.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + currentValue = null; + fireEditingStopped(); + } + }); + popup.add(miLobNullAction); + + } + + 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.Blob) value; + if (currentValue != null) { + try { + long size = currentValue.length(); + StringBuilder stringValue = new StringBuilder(); + stringValue.append(""); + button.setText(stringValue.toString()); + } catch (SQLException ex) { + button.setText(""); + } + } else { + button.setText(""); + } + this.table = table; + return button; + } + + @Override + public Object getCellEditorValue() { + return currentValue; + } + + @Override + public boolean isCellEditable(EventObject anEvent) { + if (anEvent instanceof MouseEvent) { + return ((MouseEvent) anEvent).getClickCount() >= 2; + } + return super.isCellEditable(anEvent); + } + + private void saveLobToFile(Blob b) { + JFileChooser c = new JFileChooser(); + int fileDialogState = c.showSaveDialog(table); + if (fileDialogState == JFileChooser.APPROVE_OPTION) { + File f = c.getSelectedFile(); + InputStream is = null; + FileOutputStream fos = null; + try { + is = b.getBinaryStream(); + fos = new FileOutputStream(f); + int read = 0; + byte[] buffer = new byte[1024]; + while((read = is.read(buffer)) > 0) { + fos.write(buffer, 0, read); + } + } catch (IOException ex) { + throw new RuntimeException(ex); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } finally { + try { + if(fos != null) fos.close(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + try { + if(is != null) is.close(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } + } + + private Blob loadLobFromFile() { + JFileChooser c = new JFileChooser(); + Blob result = null; + int fileDialogState = c.showOpenDialog(table); + if (fileDialogState == JFileChooser.APPROVE_OPTION) { + File f = c.getSelectedFile(); + FileInputStream fis = null; + try { + fis = new FileInputStream(f); + result = new FileBackedBlob(fis); + } catch (IOException ex) { + throw new RuntimeException(ex); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } finally { + try { + if(fis != null) fis.close(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } + return result; + } +} \ No newline at end of file diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BooleanTableCellEditor.java b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BooleanTableCellEditor.java new file mode 100644 --- /dev/null +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/BooleanTableCellEditor.java @@ -0,0 +1,68 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.db.dataview.table.celleditor; + +import java.awt.Component; +import javax.swing.BorderFactory; +import javax.swing.JComponent; +import javax.swing.JTable; +import javax.swing.table.TableCellEditor; +import org.jdesktop.swingx.renderer.JRendererCheckBox; + +public class BooleanTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor { + + public BooleanTableCellEditor(JRendererCheckBox cb) { + super(cb); + cb.setHorizontalAlignment(0); + } + + @Override + public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { + this.table = table; + Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column); + setEditable(column, c, table.isCellEditable(row, column)); + if (isGtk && c instanceof JComponent) { + ((JComponent) c).setBorder(BorderFactory.createEmptyBorder()); + } + return c; + } +} 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 new file mode 100644 --- /dev/null +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ClobFieldTableCellEditor.java @@ -0,0 +1,273 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.db.dataview.table.celleditor; + +import java.awt.Component; +import java.awt.Font; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.Reader; +import java.io.Writer; +import java.nio.charset.Charset; +import java.sql.Clob; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.EventObject; +import java.util.List; +import javax.swing.AbstractCellEditor; +import javax.swing.DefaultComboBoxModel; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JFileChooser; +import javax.swing.JMenuItem; +import javax.swing.JPanel; +import javax.swing.JPopupMenu; +import javax.swing.JTable; +import javax.swing.SwingConstants; +import javax.swing.table.TableCellEditor; +import org.netbeans.modules.db.dataview.util.FileBackedClob; +import org.openide.util.Exceptions; +import org.openide.util.NbBundle; + +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() { + @Override + public int compare(Charset o1, Charset o2) { + return o1.displayName().compareTo(o2.displayName()); + } + }); + charsetSelect = new JComboBox(); + charsetSelect.setModel(new DefaultComboBoxModel(charset.toArray())); + charsetSelect.setSelectedItem(Charset.defaultCharset()); + this.add(charsetSelect); + } + + public Charset getSelectedCharset() { + return (Charset) charsetSelect.getSelectedItem(); + } + + public void setSelectedCharset(Charset selectedCharset) { + charsetSelect.setSelectedItem(selectedCharset); + } + } + + protected static final String EDIT = "edit"; + protected Clob currentValue; + protected JButton button; + protected JPopupMenu popup; + protected JTable table; + + public ClobFieldTableCellEditor() { + button = new JButton(); + button.setActionCommand(EDIT); + button.addActionListener(this); + button.setContentAreaFilled(false); + button.setOpaque(false); + button.setBorderPainted(false); + button.setRolloverEnabled(false); + 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")); + miLobSaveAction.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + saveLobToFile(currentValue); + fireEditingCanceled(); + } + }); + popup.add(miLobSaveAction); + final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); + miLobLoadAction.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + Object newValue = loadLobFromFile(); + if (newValue != null) { + currentValue = (Clob) newValue; + } + fireEditingStopped(); + } + }); + popup.add(miLobLoadAction); + final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title")); + miLobNullAction.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + currentValue = null; + fireEditingStopped(); + } + }); + popup.add(miLobNullAction); + + } + + 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) { + try { + long size = currentValue.length(); + StringBuilder stringValue = new StringBuilder(); + stringValue.append(""); + button.setText(stringValue.toString()); + } catch (SQLException ex) { + button.setText(""); + } + } else { + button.setText(""); + } + this.table = table; + return button; + } + + @Override + public Object getCellEditorValue() { + return currentValue; + } + + @Override + public boolean isCellEditable(EventObject anEvent) { + if (anEvent instanceof MouseEvent) { + return ((MouseEvent) anEvent).getClickCount() >= 2; + } + return super.isCellEditable(anEvent); + } + + private void saveLobToFile(Clob b) { + CharsetSelector charset = new CharsetSelector(); + JFileChooser c = new JFileChooser(); + c.setAccessory(charset); + int fileDialogState = c.showSaveDialog(table); + if (fileDialogState == JFileChooser.APPROVE_OPTION) { + File f = c.getSelectedFile(); + Reader r = null; + Writer w = null; + try { + r = b.getCharacterStream(); + w = new OutputStreamWriter(new FileOutputStream(f), charset.getSelectedCharset()); + int read = 0; + char[] buffer = new char[1024]; + while((read = r.read(buffer)) > 0) { + w.write(buffer, 0, read); + } + } catch (IOException ex) { + throw new RuntimeException(ex); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } finally { + try { + if(w != null) w.close(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + try { + if(r != null) r.close(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } + } + + private Clob loadLobFromFile() { + CharsetSelector charset = new CharsetSelector(); + JFileChooser c = new JFileChooser(); + c.setAccessory(charset); + Clob result = null; + int fileDialogState = c.showOpenDialog(table); + if (fileDialogState == JFileChooser.APPROVE_OPTION) { + File f = c.getSelectedFile(); + Reader r = null; + try { + r = new InputStreamReader(new FileInputStream(f), charset.getSelectedCharset()); + result = new FileBackedClob(r); + } catch (IOException ex) { + throw new RuntimeException(ex); + } catch (SQLException ex) { + throw new RuntimeException(ex); + } finally { + try { + if(r != null) r.close(); + } catch (IOException ex) { + Exceptions.printStackTrace(ex); + } + } + } + return result; + } +} diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/DateTimePickerCellEditor.java b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/DateTimePickerCellEditor.java new file mode 100644 --- /dev/null +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/DateTimePickerCellEditor.java @@ -0,0 +1,230 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.db.dataview.table.celleditor; + +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.sql.Timestamp; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.EventObject; +import javax.swing.AbstractCellEditor; +import javax.swing.BorderFactory; +import javax.swing.JTable; +import javax.swing.UIManager; +import javax.swing.table.TableCellEditor; +import org.jdesktop.swingx.JXDatePicker; +import org.netbeans.modules.db.dataview.meta.DBColumn; +import org.netbeans.modules.db.dataview.table.ResultSetJXTable; +import org.netbeans.modules.db.dataview.util.DataViewUtils; +import org.netbeans.modules.db.dataview.util.JXDateTimePicker; +import org.netbeans.modules.db.dataview.util.TimestampType; + +public class DateTimePickerCellEditor extends AbstractCellEditor implements TableCellEditor { + + private boolean editable = true; + private JXDateTimePicker datePicker; + private DateFormat dateFormat; + private ActionListener pickerActionListener; + private boolean ignoreAction; + private JTable table; + + public DateTimePickerCellEditor() { + this(new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN)); + } + + /** + * Instantiates an editor with the given dateFormat. If + * null, the datePickers default is used. + * + * @param dateFormat + */ + public DateTimePickerCellEditor(DateFormat dateFormat) { + + // JW: the copy is used to synchronize .. can + // we use something else? + this.dateFormat = dateFormat != null ? dateFormat : new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN); + datePicker = new JXDateTimePicker(); + // default border crushes the editor/combo + datePicker.getEditor().setBorder( + BorderFactory.createEmptyBorder(0, 1, 0, 1)); + // should be fixed by j2se 6.0 + datePicker.setFont(UIManager.getDefaults().getFont("TextField.font")); + if (dateFormat != null) { + datePicker.setFormats(dateFormat); + } + datePicker.addActionListener(getPickerActionListener()); + } + + @Override + public Timestamp getCellEditorValue() { + return datePicker.getDateTime(); + } + + @Override + public boolean isCellEditable(EventObject anEvent) { + if (anEvent instanceof MouseEvent) { + return ((MouseEvent) anEvent).getClickCount() >= 2; + } + return super.isCellEditable(anEvent); + } + + @Override + public boolean stopCellEditing() { + ignoreAction = true; + boolean canCommit = commitChange(); + ignoreAction = false; + if (canCommit) { + datePicker.setDateTime(null); + return super.stopCellEditing(); + } + return false; + } + + @Override + public Component getTableCellEditorComponent(final JTable table, Object value, + boolean isSelected, int row, int column) { + this.table = table; + ignoreAction = true; + datePicker.setDateTime(getValueAsTimestamp(value)); + + ignoreAction = false; + setEditable(column, datePicker, table.isCellEditable(row, column)); + return datePicker; + } + + protected Timestamp getValueAsTimestamp(Object value) { + if (isEmpty(value) || DataViewUtils.isSQLConstantString(value)) { + return new Timestamp(System.currentTimeMillis()); + } + + if (value instanceof Timestamp) { + return (Timestamp) value; + } + if (value instanceof Long) { + return new Timestamp((Long) value); + } + if (value instanceof String) { + try { + + return new Timestamp(dateFormat.parse((String) value).getTime()); + } catch (ParseException e) { + //mLogger.log(Level.SEVERE, e.getMessage(), e.getMessage()); + } + } + + return new Timestamp(System.currentTimeMillis()); + } + + protected boolean isEmpty(Object value) { + return value == null || value instanceof String && ((String) value).length() == 0; + } + + protected boolean commitChange() { + try { + datePicker.commitEdit(); + return true; + } catch (ParseException e) { + } + return false; + } + + public DateFormat[] getFormats() { + return datePicker.getFormats(); + } + + public void setFormats(DateFormat... formats) { + datePicker.setFormats(formats); + } + + private ActionListener getPickerActionListener() { + if (pickerActionListener == null) { + pickerActionListener = createPickerActionListener(); + } + return pickerActionListener; + } + + protected ActionListener createPickerActionListener() { + ActionListener l = new ActionListener() { + + @Override + public void actionPerformed(final ActionEvent e) { + // avoid duplicate trigger from + // commit in stopCellEditing + if (ignoreAction) { + return; + } + terminateEdit(e); + } + + private void terminateEdit(final ActionEvent e) { + if ((e != null) && (JXDatePicker.COMMIT_KEY.equals(e.getActionCommand()))) { + stopCellEditing(); + } else { + cancelCellEditing(); + } + } + }; + return l; + } + + protected void setEditable(int column, JXDateTimePicker c, boolean celleditable) { + assert table != null; + DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column); + if (dbCol.isGenerated()) { + editable = false; + } else if (! celleditable) { + editable = false; + } else { + editable = dbCol.isEditable(); + } + c.setEditable(editable); + } + + public void addKeyListener(KeyListener kl) { + datePicker.addKeyListener(kl); + } +} \ No newline at end of file diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/NumberFieldEditor.java b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/NumberFieldEditor.java new file mode 100644 --- /dev/null +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/NumberFieldEditor.java @@ -0,0 +1,67 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.db.dataview.table.celleditor; + +import java.awt.Component; +import javax.swing.BorderFactory; +import javax.swing.JComponent; +import javax.swing.JTable; +import javax.swing.JTextField; + +public class NumberFieldEditor extends ResultSetTableCellEditor { + + public NumberFieldEditor(final JTextField textField) { + super(textField); + ((JTextField) getComponent()).setHorizontalAlignment(JTextField.RIGHT); + } + + @Override + public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) { + this.table = table; + Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column); + if (isGtk && c instanceof JComponent) { + ((JComponent) c).setBorder(BorderFactory.createEmptyBorder()); + } + setEditable(column, c, table.isCellEditable(row, column)); + return c; + } +} \ No newline at end of file diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetTableCellEditor.java b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ResultSetTableCellEditor.java rename from db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetTableCellEditor.java rename to db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ResultSetTableCellEditor.java --- a/db.dataview/src/org/netbeans/modules/db/dataview/table/ResultSetTableCellEditor.java +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/ResultSetTableCellEditor.java @@ -39,83 +39,23 @@ * * Portions Copyrighted 2009-2010 Sun Microsystems, Inc. */ -package org.netbeans.modules.db.dataview.table; +package org.netbeans.modules.db.dataview.table.celleditor; -import java.awt.BorderLayout; import java.awt.Component; -import java.awt.Dimension; -import java.awt.Font; -import java.awt.Insets; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; import java.awt.event.MouseEvent; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.Reader; -import java.io.Writer; -import java.nio.charset.Charset; -import java.sql.Blob; -import java.sql.Clob; -import java.sql.SQLException; -import java.sql.Timestamp; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; import java.util.EventObject; -import java.util.List; -import javax.swing.AbstractCellEditor; -import javax.swing.Action; -import javax.swing.ActionMap; -import javax.swing.BorderFactory; import javax.swing.DefaultCellEditor; -import javax.swing.DefaultComboBoxModel; -import javax.swing.InputMap; -import javax.swing.JButton; -import javax.swing.JComboBox; import javax.swing.JComponent; -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.JTextField; -import javax.swing.KeyStroke; -import javax.swing.SwingConstants; -import javax.swing.SwingUtilities; import javax.swing.UIManager; -import javax.swing.table.TableCellEditor; -import org.jdesktop.swingx.JXButton; -import org.jdesktop.swingx.JXDatePicker; -import org.jdesktop.swingx.JXPanel; import org.jdesktop.swingx.renderer.JRendererCheckBox; import org.netbeans.modules.db.dataview.meta.DBColumn; -import org.netbeans.modules.db.dataview.util.FileBackedBlob; -import org.netbeans.modules.db.dataview.util.FileBackedClob; +import org.netbeans.modules.db.dataview.table.ResultSetJXTable; import org.netbeans.modules.db.dataview.util.DBReadWriteHelper; import org.netbeans.modules.db.dataview.util.DataViewUtils; -import org.netbeans.modules.db.dataview.util.JXDateTimePicker; -import org.netbeans.modules.db.dataview.util.TimestampType; import org.openide.awt.StatusDisplayer; -import org.openide.util.Exceptions; -import org.openide.util.NbBundle; -import org.openide.windows.WindowManager; -/** - * @author Ahimanikya Satapathy - */ public class ResultSetTableCellEditor extends DefaultCellEditor { protected Object val; @@ -203,13 +143,13 @@ checkBox.addActionListener(delegate); } - protected void setEditable(int column, Component c) { + protected void setEditable(int column, Component c, boolean celleditable) { assert table != null; DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column); if (dbCol.isGenerated()) { editable = false; } - if (!((ResultSetJXTable) table).dView.isEditable()) { + if (! celleditable) { editable = false; } else { editable = dbCol.isEditable(); @@ -221,656 +161,4 @@ ((JComponent) c).setEnabled(editable); } } -} - -class BooleanTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor { - - public BooleanTableCellEditor(JRendererCheckBox cb) { - super(cb); - cb.setHorizontalAlignment(0); - } - - @Override - public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { - this.table = table; - Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column); - setEditable(column, c); - if (isGtk && c instanceof JComponent) { - ((JComponent) c).setBorder(BorderFactory.createEmptyBorder()); - } - return c; - } -} - -class StringTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor, ActionListener { - - private JXButton customEditorButton = new JXButton("..."); - private int row, column; - - public StringTableCellEditor(final JTextField textField) { - super(textField); - customEditorButton.addActionListener(this); - - // ui-tweaking - customEditorButton.setFocusable(false); - customEditorButton.setFocusPainted(false); - customEditorButton.setMargin(new Insets(0, 0, 0, 0)); - customEditorButton.setPreferredSize(new Dimension(20, 10)); - } - - @Override - public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) { - this.table = table; - final JComponent c = (JComponent) super.getTableCellEditorComponent(table, value, isSelected, row, column); - setEditable(column, c); - - JXPanel panel = new JXPanel(new BorderLayout()) { - - @Override - public void addNotify() { - super.addNotify(); - c.requestFocus(); - } - - @Override - protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) { - InputMap map = c.getInputMap(condition); - ActionMap am = c.getActionMap(); - - if (map != null && am != null && isEnabled()) { - Object binding = map.get(ks); - Action action = (binding == null) ? null : am.get(binding); - if (action != null) { - return SwingUtilities.notifyAction(action, ks, e, c, - e.getModifiers()); - } - } - return false; - } - }; - panel.add(c); - if (isGtk) { - c.setBorder(BorderFactory.createEmptyBorder()); - } - panel.add(customEditorButton, BorderLayout.EAST); - panel.revalidate(); - panel.repaint(); - - this.row = row; - this.column = column; - return panel; - } - - @Override - public final void actionPerformed(ActionEvent e) { - assert table != null; - super.cancelCellEditing(); - editCell(table, row, column); - } - - protected void editCell(JTable table, int row, int column) { - JTextArea textArea = new JTextArea(10, 50); - Object value = table.getValueAt(row, column); - if (value != null) { - textArea.setText(value.toString()); - textArea.setCaretPosition(0); - textArea.setEditable(editable); - } - JScrollPane pane = new JScrollPane(textArea); - Component parent = WindowManager.getDefault().getMainWindow(); - - if (editable) { - int result = JOptionPane.showOptionDialog(parent, pane, table.getColumnName(column), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); - if (result == JOptionPane.OK_OPTION) { - table.setValueAt(textArea.getText(), row, column); - } - } else { - JOptionPane.showMessageDialog(parent, pane, table.getColumnName(column), JOptionPane.PLAIN_MESSAGE, null); - } - } -} - -class DateTimePickerCellEditor extends AbstractCellEditor implements TableCellEditor { - - private boolean editable = true; - private JXDateTimePicker datePicker; - private DateFormat dateFormat; - private ActionListener pickerActionListener; - private boolean ignoreAction; - private JTable table; - - public DateTimePickerCellEditor() { - this(new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN)); - } - - /** - * Instantiates an editor with the given dateFormat. If - * null, the datePickers default is used. - * - * @param dateFormat - */ - public DateTimePickerCellEditor(DateFormat dateFormat) { - - // JW: the copy is used to synchronize .. can - // we use something else? - this.dateFormat = dateFormat != null ? dateFormat : new SimpleDateFormat (TimestampType.DEFAULT_FORMAT_PATTERN); - datePicker = new JXDateTimePicker(); - // default border crushes the editor/combo - datePicker.getEditor().setBorder( - BorderFactory.createEmptyBorder(0, 1, 0, 1)); - // should be fixed by j2se 6.0 - datePicker.setFont(UIManager.getDefaults().getFont("TextField.font")); - if (dateFormat != null) { - datePicker.setFormats(dateFormat); - } - datePicker.addActionListener(getPickerActionListener()); - } - - @Override - public Timestamp getCellEditorValue() { - return datePicker.getDateTime(); - } - - @Override - public boolean isCellEditable(EventObject anEvent) { - if (anEvent instanceof MouseEvent) { - return ((MouseEvent) anEvent).getClickCount() >= 2; - } - return super.isCellEditable(anEvent); - } - - @Override - public boolean stopCellEditing() { - ignoreAction = true; - boolean canCommit = commitChange(); - ignoreAction = false; - if (canCommit) { - datePicker.setDateTime(null); - return super.stopCellEditing(); - } - return false; - } - - @Override - public Component getTableCellEditorComponent(final JTable table, Object value, - boolean isSelected, int row, int column) { - this.table = table; - ignoreAction = true; - datePicker.setDateTime(getValueAsTimestamp(value)); - - ignoreAction = false; - setEditable(column, datePicker); - return datePicker; - } - - protected Timestamp getValueAsTimestamp(Object value) { - if (isEmpty(value) || DataViewUtils.isSQLConstantString(value)) { - return new Timestamp(System.currentTimeMillis()); - } - - if (value instanceof Timestamp) { - return (Timestamp) value; - } - if (value instanceof Long) { - return new Timestamp((Long) value); - } - if (value instanceof String) { - try { - - return new Timestamp(dateFormat.parse((String) value).getTime()); - } catch (ParseException e) { - //mLogger.log(Level.SEVERE, e.getMessage(), e.getMessage()); - } - } - - return new Timestamp(System.currentTimeMillis()); - } - - protected boolean isEmpty(Object value) { - return value == null || value instanceof String && ((String) value).length() == 0; - } - - protected boolean commitChange() { - try { - datePicker.commitEdit(); - return true; - } catch (ParseException e) { - } - return false; - } - - public DateFormat[] getFormats() { - return datePicker.getFormats(); - } - - public void setFormats(DateFormat... formats) { - datePicker.setFormats(formats); - } - - private ActionListener getPickerActionListener() { - if (pickerActionListener == null) { - pickerActionListener = createPickerActionListener(); - } - return pickerActionListener; - } - - protected ActionListener createPickerActionListener() { - ActionListener l = new ActionListener() { - - @Override - public void actionPerformed(final ActionEvent e) { - // avoid duplicate trigger from - // commit in stopCellEditing - if (ignoreAction) { - return; - } - terminateEdit(e); - } - - private void terminateEdit(final ActionEvent e) { - if ((e != null) && (JXDatePicker.COMMIT_KEY.equals(e.getActionCommand()))) { - stopCellEditing(); - } else { - cancelCellEditing(); - } - } - }; - return l; - } - - protected void setEditable(int column, JXDateTimePicker c) { - assert table != null; - DBColumn dbCol = ((ResultSetJXTable) table).getDBColumn(column); - if (dbCol.isGenerated()) { - editable = false; - } else if (!((ResultSetJXTable) table).dView.isEditable()) { - editable = false; - } else { - editable = dbCol.isEditable(); - } - c.setEditable(editable); - } - - protected void addKeyListener(KeyListener kl) { - datePicker.addKeyListener(kl); - } -} - -class NumberFieldEditor extends ResultSetTableCellEditor { - - public NumberFieldEditor(final JTextField textField) { - super(textField); - ((JTextField) getComponent()).setHorizontalAlignment(JTextField.RIGHT); - } - - @Override - public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) { - this.table = table; - Component c = super.getTableCellEditorComponent(table, value, isSelected, row, column); - if (isGtk && c instanceof JComponent) { - ((JComponent) c).setBorder(BorderFactory.createEmptyBorder()); - } - setEditable(column, c); - return c; - } -} - -class BlobFieldTableCellEditor extends AbstractCellEditor - implements TableCellEditor, - ActionListener { - - protected static final String EDIT = "edit"; - protected Blob currentValue; - protected JButton button; - protected JPopupMenu popup; - protected JTable table; - - public BlobFieldTableCellEditor() { - button = new JButton(); - button.setActionCommand(EDIT); - button.addActionListener(this); - button.setContentAreaFilled(false); - button.setOpaque(false); - button.setBorderPainted(false); - button.setRolloverEnabled(false); - 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")); - miLobSaveAction.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - saveLobToFile(currentValue); - fireEditingCanceled(); - } - }); - popup.add(miLobSaveAction); - final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); - miLobLoadAction.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - Object newValue = loadLobFromFile(); - if (newValue != null) { - currentValue = (Blob) newValue; - } - fireEditingStopped(); - } - }); - popup.add(miLobLoadAction); - final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title")); - miLobNullAction.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - currentValue = null; - fireEditingStopped(); - } - }); - popup.add(miLobNullAction); - - } - - 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.Blob) value; - if (currentValue != null) { - try { - long size = currentValue.length(); - StringBuilder stringValue = new StringBuilder(); - stringValue.append(""); - button.setText(stringValue.toString()); - } catch (SQLException ex) { - button.setText(""); - } - } else { - button.setText(""); - } - this.table = table; - return button; - } - - @Override - public Object getCellEditorValue() { - return currentValue; - } - - @Override - public boolean isCellEditable(EventObject anEvent) { - if (anEvent instanceof MouseEvent) { - return ((MouseEvent) anEvent).getClickCount() >= 2; - } - return super.isCellEditable(anEvent); - } - - private void saveLobToFile(Blob b) { - JFileChooser c = new JFileChooser(); - int fileDialogState = c.showSaveDialog(table); - if (fileDialogState == JFileChooser.APPROVE_OPTION) { - File f = c.getSelectedFile(); - InputStream is = null; - FileOutputStream fos = null; - try { - is = b.getBinaryStream(); - fos = new FileOutputStream(f); - int read = 0; - byte[] buffer = new byte[1024]; - while((read = is.read(buffer)) > 0) { - fos.write(buffer, 0, read); - } - } catch (IOException ex) { - throw new RuntimeException(ex); - } catch (SQLException ex) { - throw new RuntimeException(ex); - } finally { - try { - if(fos != null) fos.close(); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); - } - try { - if(is != null) is.close(); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); - } - } - } - } - - private Blob loadLobFromFile() { - JFileChooser c = new JFileChooser(); - Blob result = null; - int fileDialogState = c.showOpenDialog(table); - if (fileDialogState == JFileChooser.APPROVE_OPTION) { - File f = c.getSelectedFile(); - FileInputStream fis = null; - try { - fis = new FileInputStream(f); - result = new FileBackedBlob(fis); - } catch (IOException ex) { - throw new RuntimeException(ex); - } catch (SQLException ex) { - throw new RuntimeException(ex); - } finally { - try { - if(fis != null) fis.close(); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); - } - } - } - return result; - } -} - -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() { - @Override - public int compare(Charset o1, Charset o2) { - return o1.displayName().compareTo(o2.displayName()); - } - }); - charsetSelect = new JComboBox(); - charsetSelect.setModel(new DefaultComboBoxModel(charset.toArray())); - charsetSelect.setSelectedItem(Charset.defaultCharset()); - this.add(charsetSelect); - } - - public Charset getSelectedCharset() { - return (Charset) charsetSelect.getSelectedItem(); - } - - public void setSelectedCharset(Charset selectedCharset) { - charsetSelect.setSelectedItem(selectedCharset); - } - } - - protected static final String EDIT = "edit"; - protected Clob currentValue; - protected JButton button; - protected JPopupMenu popup; - protected JTable table; - - public ClobFieldTableCellEditor() { - button = new JButton(); - button.setActionCommand(EDIT); - button.addActionListener(this); - button.setContentAreaFilled(false); - button.setOpaque(false); - button.setBorderPainted(false); - button.setRolloverEnabled(false); - 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")); - miLobSaveAction.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - saveLobToFile(currentValue); - fireEditingCanceled(); - } - }); - popup.add(miLobSaveAction); - final JMenuItem miLobLoadAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "loadLob.title")); - miLobLoadAction.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - Object newValue = loadLobFromFile(); - if (newValue != null) { - currentValue = (Clob) newValue; - } - fireEditingStopped(); - } - }); - popup.add(miLobLoadAction); - final JMenuItem miLobNullAction = new JMenuItem(NbBundle.getMessage(BlobFieldTableCellEditor.class, "nullLob.title")); - miLobNullAction.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent e) { - currentValue = null; - fireEditingStopped(); - } - }); - popup.add(miLobNullAction); - - } - - 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) { - try { - long size = currentValue.length(); - StringBuilder stringValue = new StringBuilder(); - stringValue.append(""); - button.setText(stringValue.toString()); - } catch (SQLException ex) { - button.setText(""); - } - } else { - button.setText(""); - } - this.table = table; - return button; - } - - @Override - public Object getCellEditorValue() { - return currentValue; - } - - @Override - public boolean isCellEditable(EventObject anEvent) { - if (anEvent instanceof MouseEvent) { - return ((MouseEvent) anEvent).getClickCount() >= 2; - } - return super.isCellEditable(anEvent); - } - - private void saveLobToFile(Clob b) { - CharsetSelector charset = new CharsetSelector(); - JFileChooser c = new JFileChooser(); - c.setAccessory(charset); - int fileDialogState = c.showSaveDialog(table); - if (fileDialogState == JFileChooser.APPROVE_OPTION) { - File f = c.getSelectedFile(); - Reader r = null; - Writer w = null; - try { - r = b.getCharacterStream(); - w = new OutputStreamWriter(new FileOutputStream(f), charset.getSelectedCharset()); - int read = 0; - char[] buffer = new char[1024]; - while((read = r.read(buffer)) > 0) { - w.write(buffer, 0, read); - } - } catch (IOException ex) { - throw new RuntimeException(ex); - } catch (SQLException ex) { - throw new RuntimeException(ex); - } finally { - try { - if(w != null) w.close(); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); - } - try { - if(r != null) r.close(); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); - } - } - } - } - - private Clob loadLobFromFile() { - CharsetSelector charset = new CharsetSelector(); - JFileChooser c = new JFileChooser(); - c.setAccessory(charset); - Clob result = null; - int fileDialogState = c.showOpenDialog(table); - if (fileDialogState == JFileChooser.APPROVE_OPTION) { - File f = c.getSelectedFile(); - Reader r = null; - try { - r = new InputStreamReader(new FileInputStream(f), charset.getSelectedCharset()); - result = new FileBackedClob(r); - } catch (IOException ex) { - throw new RuntimeException(ex); - } catch (SQLException ex) { - throw new RuntimeException(ex); - } finally { - try { - if(r != null) r.close(); - } catch (IOException ex) { - Exceptions.printStackTrace(ex); - } - } - } - return result; - } } \ No newline at end of file diff --git a/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/StringTableCellEditor.java b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/StringTableCellEditor.java new file mode 100644 --- /dev/null +++ b/db.dataview/src/org/netbeans/modules/db/dataview/table/celleditor/StringTableCellEditor.java @@ -0,0 +1,154 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2011 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2011 Sun Microsystems, Inc. + */ +package org.netbeans.modules.db.dataview.table.celleditor; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Dimension; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; +import javax.swing.Action; +import javax.swing.ActionMap; +import javax.swing.BorderFactory; +import javax.swing.InputMap; +import javax.swing.JComponent; +import javax.swing.JOptionPane; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.KeyStroke; +import javax.swing.SwingUtilities; +import javax.swing.table.TableCellEditor; +import org.jdesktop.swingx.JXButton; +import org.jdesktop.swingx.JXPanel; +import org.openide.windows.WindowManager; + +public class StringTableCellEditor extends ResultSetTableCellEditor implements TableCellEditor, ActionListener { + + private JXButton customEditorButton = new JXButton("..."); + private int row, column; + + public StringTableCellEditor(final JTextField textField) { + super(textField); + customEditorButton.addActionListener(this); + + // ui-tweaking + customEditorButton.setFocusable(false); + customEditorButton.setFocusPainted(false); + customEditorButton.setMargin(new Insets(0, 0, 0, 0)); + customEditorButton.setPreferredSize(new Dimension(20, 10)); + } + + @Override + public Component getTableCellEditorComponent(final JTable table, Object value, boolean isSelected, int row, int column) { + this.table = table; + final JComponent c = (JComponent) super.getTableCellEditorComponent(table, value, isSelected, row, column); + setEditable(column, c, table.isCellEditable(row, column)); + + JXPanel panel = new JXPanel(new BorderLayout()) { + + @Override + public void addNotify() { + super.addNotify(); + c.requestFocus(); + } + + @Override + protected boolean processKeyBinding(KeyStroke ks, KeyEvent e, int condition, boolean pressed) { + InputMap map = c.getInputMap(condition); + ActionMap am = c.getActionMap(); + + if (map != null && am != null && isEnabled()) { + Object binding = map.get(ks); + Action action = (binding == null) ? null : am.get(binding); + if (action != null) { + return SwingUtilities.notifyAction(action, ks, e, c, + e.getModifiers()); + } + } + return false; + } + }; + panel.add(c); + if (isGtk) { + c.setBorder(BorderFactory.createEmptyBorder()); + } + panel.add(customEditorButton, BorderLayout.EAST); + panel.revalidate(); + panel.repaint(); + + this.row = row; + this.column = column; + return panel; + } + + @Override + public final void actionPerformed(ActionEvent e) { + assert table != null; + super.cancelCellEditing(); + editCell(table, row, column); + } + + protected void editCell(JTable table, int row, int column) { + JTextArea textArea = new JTextArea(10, 50); + Object value = table.getValueAt(row, column); + if (value != null) { + textArea.setText(value.toString()); + textArea.setCaretPosition(0); + textArea.setEditable(editable); + } + JScrollPane pane = new JScrollPane(textArea); + Component parent = WindowManager.getDefault().getMainWindow(); + + if (editable) { + int result = JOptionPane.showOptionDialog(parent, pane, table.getColumnName(column), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); + if (result == JOptionPane.OK_OPTION) { + table.setValueAt(textArea.getText(), row, column); + } + } else { + JOptionPane.showMessageDialog(parent, pane, table.getColumnName(column), JOptionPane.PLAIN_MESSAGE, null); + } + } +} \ No newline at end of file