001: package isql;
002:
003: import java.awt.*;
004: import java.awt.event.*;
005: import javax.swing.*;
006: import java.awt.datatransfer.*;
007: import java.util.*;
008:
009: /**
010: * ExcelAdapter enables Copy-Paste Clipboard functionality on JTables.
011: * The clipboard data format used by the adapter is compatible with
012: * the clipboard format used by Excel. This provides for clipboard
013: * interoperability between enabled JTables and Excel.
014: * RK: Taken from javaworld.
015: */
016: public class ExcelAdapter implements ActionListener {
017: private String rowstring, value;
018: private Clipboard system;
019: private StringSelection stsel;
020: private JTable jTable1;
021:
022: /**
023: * The Excel Adapter is constructed with a
024: * JTable on which it enables Copy-Paste and acts
025: * as a Clipboard listener.
026: */
027: public ExcelAdapter(JTable myJTable) {
028: jTable1 = myJTable;
029: KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C,
030: ActionEvent.CTRL_MASK, false);
031: // Identifying the copy KeyStroke user can modify this
032: // to copy on some other Key combination.
033: KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V,
034: ActionEvent.CTRL_MASK, false);
035: // Identifying the Paste KeyStroke user can modify this
036: //to copy on some other Key combination.
037: jTable1.registerKeyboardAction(this , "Copy", copy,
038: JComponent.WHEN_FOCUSED);
039: jTable1.registerKeyboardAction(this , "Paste", paste,
040: JComponent.WHEN_FOCUSED);
041: system = Toolkit.getDefaultToolkit().getSystemClipboard();
042: }
043:
044: /**
045: * Public Accessor methods for the Table on which this adapter acts.
046: */
047: public JTable getJTable() {
048: return jTable1;
049: }
050:
051: public void setJTable(JTable jTable1) {
052: this .jTable1 = jTable1;
053: }
054:
055: /**
056: * This method is activated on the Keystrokes we are listening to
057: * in this implementation. Here it listens for Copy and Paste ActionCommands.
058: * Selections comprising non-adjacent cells result in invalid selection and
059: * then copy action cannot be performed.
060: * Paste is done by aligning the upper left corner of the selection with the
061: * 1st element in the current selection of the JTable.
062: */
063: public void actionPerformed(ActionEvent e) {
064: if (e.getActionCommand().compareTo("Copy") == 0) {
065: StringBuffer sbf = new StringBuffer();
066: // Check to ensure we have selected only a contiguous block of
067: // cells
068: int numcols = jTable1.getSelectedColumnCount();
069: int numrows = jTable1.getSelectedRowCount();
070: int[] rowsselected = jTable1.getSelectedRows();
071: int[] colsselected = jTable1.getSelectedColumns();
072: if (!((numrows - 1 == rowsselected[rowsselected.length - 1]
073: - rowsselected[0] && numrows == rowsselected.length) && (numcols - 1 == colsselected[colsselected.length - 1]
074: - colsselected[0] && numcols == colsselected.length))) {
075: JOptionPane.showMessageDialog(null,
076: "Invalid Copy Selection",
077: "Invalid Copy Selection",
078: JOptionPane.ERROR_MESSAGE);
079: return;
080: }
081: for (int i = 0; i < numrows; i++) {
082: for (int j = 0; j < numcols; j++) {
083: sbf.append(jTable1.getValueAt(rowsselected[i],
084: colsselected[j]));
085: if (j < numcols - 1)
086: sbf.append("\t");
087: }
088: sbf.append("\n");
089: }
090: stsel = new StringSelection(sbf.toString());
091: system = Toolkit.getDefaultToolkit().getSystemClipboard();
092: system.setContents(stsel, stsel);
093: }
094: if (e.getActionCommand().compareTo("Paste") == 0) {
095: System.out.println("Trying to Paste");
096: int startRow = (jTable1.getSelectedRows())[0];
097: int startCol = (jTable1.getSelectedColumns())[0];
098: try {
099: String trstring = (String) (system.getContents(this )
100: .getTransferData(DataFlavor.stringFlavor));
101: System.out.println("String is:" + trstring);
102: StringTokenizer st1 = new StringTokenizer(trstring,
103: "\n");
104: for (int i = 0; st1.hasMoreTokens(); i++) {
105: rowstring = st1.nextToken();
106: StringTokenizer st2 = new StringTokenizer(
107: rowstring, "\t");
108: for (int j = 0; st2.hasMoreTokens(); j++) {
109: value = (String) st2.nextToken();
110: if (startRow + i < jTable1.getRowCount()
111: && startCol + j < jTable1
112: .getColumnCount())
113: jTable1.setValueAt(value, startRow + i,
114: startCol + j);
115: System.out.println("Putting " + value
116: + "at row=" + startRow + i + "column="
117: + startCol + j);
118: }
119: }
120: } catch (Exception ex) {
121: ex.printStackTrace();
122: }
123: }
124: }
125: }
|