Lines 39-74
Link Here
|
39 |
* |
39 |
* |
40 |
* Portions Copyrighted 2009-2010 Sun Microsystems, Inc. |
40 |
* Portions Copyrighted 2009-2010 Sun Microsystems, Inc. |
41 |
*/ |
41 |
*/ |
42 |
|
|
|
43 |
package org.netbeans.modules.db.dataview.table; |
42 |
package org.netbeans.modules.db.dataview.table; |
44 |
|
43 |
|
45 |
import java.awt.BorderLayout; |
44 |
import java.awt.BorderLayout; |
46 |
import java.awt.Component; |
45 |
import java.awt.Component; |
47 |
import java.awt.Dimension; |
46 |
import java.awt.Dimension; |
|
|
47 |
import java.awt.Font; |
48 |
import java.awt.Insets; |
48 |
import java.awt.Insets; |
49 |
import java.awt.event.ActionEvent; |
49 |
import java.awt.event.ActionEvent; |
50 |
import java.awt.event.ActionListener; |
50 |
import java.awt.event.ActionListener; |
51 |
import java.awt.event.KeyEvent; |
51 |
import java.awt.event.KeyEvent; |
52 |
import java.awt.event.KeyListener; |
52 |
import java.awt.event.KeyListener; |
53 |
import java.awt.event.MouseEvent; |
53 |
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; |
54 |
import java.sql.Timestamp; |
67 |
import java.sql.Timestamp; |
55 |
import java.text.DateFormat; |
68 |
import java.text.DateFormat; |
56 |
import java.text.ParseException; |
69 |
import java.text.ParseException; |
57 |
import java.text.SimpleDateFormat; |
70 |
import java.text.SimpleDateFormat; |
|
|
71 |
import java.util.ArrayList; |
72 |
import java.util.Collections; |
73 |
import java.util.Comparator; |
58 |
import java.util.EventObject; |
74 |
import java.util.EventObject; |
|
|
75 |
import java.util.List; |
59 |
import javax.swing.AbstractCellEditor; |
76 |
import javax.swing.AbstractCellEditor; |
60 |
import javax.swing.Action; |
77 |
import javax.swing.Action; |
61 |
import javax.swing.ActionMap; |
78 |
import javax.swing.ActionMap; |
62 |
import javax.swing.BorderFactory; |
79 |
import javax.swing.BorderFactory; |
63 |
import javax.swing.DefaultCellEditor; |
80 |
import javax.swing.DefaultCellEditor; |
|
|
81 |
import javax.swing.DefaultComboBoxModel; |
64 |
import javax.swing.InputMap; |
82 |
import javax.swing.InputMap; |
|
|
83 |
import javax.swing.JButton; |
84 |
import javax.swing.JComboBox; |
65 |
import javax.swing.JComponent; |
85 |
import javax.swing.JComponent; |
|
|
86 |
import javax.swing.JFileChooser; |
87 |
import javax.swing.JMenuItem; |
66 |
import javax.swing.JOptionPane; |
88 |
import javax.swing.JOptionPane; |
|
|
89 |
import javax.swing.JPanel; |
90 |
import javax.swing.JPopupMenu; |
67 |
import javax.swing.JScrollPane; |
91 |
import javax.swing.JScrollPane; |
68 |
import javax.swing.JTable; |
92 |
import javax.swing.JTable; |
69 |
import javax.swing.JTextArea; |
93 |
import javax.swing.JTextArea; |
70 |
import javax.swing.JTextField; |
94 |
import javax.swing.JTextField; |
71 |
import javax.swing.KeyStroke; |
95 |
import javax.swing.KeyStroke; |
|
|
96 |
import javax.swing.SwingConstants; |
72 |
import javax.swing.SwingUtilities; |
97 |
import javax.swing.SwingUtilities; |
73 |
import javax.swing.UIManager; |
98 |
import javax.swing.UIManager; |
74 |
import javax.swing.table.TableCellEditor; |
99 |
import javax.swing.table.TableCellEditor; |
Lines 77-87
Link Here
|
77 |
import org.jdesktop.swingx.JXPanel; |
102 |
import org.jdesktop.swingx.JXPanel; |
78 |
import org.jdesktop.swingx.renderer.JRendererCheckBox; |
103 |
import org.jdesktop.swingx.renderer.JRendererCheckBox; |
79 |
import org.netbeans.modules.db.dataview.meta.DBColumn; |
104 |
import org.netbeans.modules.db.dataview.meta.DBColumn; |
|
|
105 |
import org.netbeans.modules.db.dataview.util.FileBackedBlob; |
106 |
import org.netbeans.modules.db.dataview.util.FileBackedClob; |
80 |
import org.netbeans.modules.db.dataview.util.DBReadWriteHelper; |
107 |
import org.netbeans.modules.db.dataview.util.DBReadWriteHelper; |
81 |
import org.netbeans.modules.db.dataview.util.DataViewUtils; |
108 |
import org.netbeans.modules.db.dataview.util.DataViewUtils; |
82 |
import org.netbeans.modules.db.dataview.util.JXDateTimePicker; |
109 |
import org.netbeans.modules.db.dataview.util.JXDateTimePicker; |
83 |
import org.netbeans.modules.db.dataview.util.TimestampType; |
110 |
import org.netbeans.modules.db.dataview.util.TimestampType; |
84 |
import org.openide.awt.StatusDisplayer; |
111 |
import org.openide.awt.StatusDisplayer; |
|
|
112 |
import org.openide.util.Exceptions; |
113 |
import org.openide.util.NbBundle; |
85 |
import org.openide.windows.WindowManager; |
114 |
import org.openide.windows.WindowManager; |
86 |
|
115 |
|
87 |
/** |
116 |
/** |
Lines 486-489
Link Here
|
486 |
} |
515 |
} |
487 |
} |
516 |
} |
488 |
|
517 |
|
|
|
518 |
class BlobFieldTableCellEditor extends AbstractCellEditor |
519 |
implements TableCellEditor, |
520 |
ActionListener { |
489 |
|
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 |
} |