package org.statcato.spreadsheet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.datatransfer.*;
import java.util.*;
/**
* ExcelAdapter enables Copy-Paste Clipboard functionality on JTables.
* The clipboard data format used by the adapter is compatible with
* the clipboard format used by Excel. This provides for clipboard
* interoperability between enabled JTables and Excel.
*
* Modified to work with Cut in a spreadsheet.
*
* Java Tip 77: Enable copy and paste functionality between Swing's JTables and Excel
* This adapter makes it easy to transfer data from one type of spreadsheet to the other
* By Ashok Banerjee and Jignesh Mehta
*/
public class ExcelAdapter implements ActionListener
{
private String rowstring,value;
private Clipboard system;
private StringSelection stsel;
private Spreadsheet jTable1 ;
/**
* The Excel Adapter is constructed with a
* JTable on which it enables Copy-Paste and acts
* as a Clipboard listener.
*/
public ExcelAdapter(Spreadsheet myJTable)
{
jTable1 = myJTable;
KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK,false);
// Identifying the copy KeyStroke user can modify this
// to copy on some other Key combination.
KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK,false);
// Identifying the Paste KeyStroke user can modify this
//to copy on some other Key combination.
KeyStroke cut = KeyStroke.getKeyStroke(KeyEvent.VK_X,ActionEvent.CTRL_MASK,false);
// Identifying the cut KeyStroke user can modify this
//to copy on some other Key combination.
jTable1.registerKeyboardAction(this,"Copy",copy,JComponent.WHEN_FOCUSED);
jTable1.registerKeyboardAction(this,"Paste",paste,JComponent.WHEN_FOCUSED);
jTable1.registerKeyboardAction(this,"Cut",cut,JComponent.WHEN_FOCUSED);
system = Toolkit.getDefaultToolkit().getSystemClipboard();
}
/**
* Public Accessor methods for the Table on which this adapter acts.
*/
public JTable getJTable() {return jTable1;}
public void setJTable(Spreadsheet jTable1) {this.jTable1=jTable1;}
/**
* This method is activated on the Keystrokes we are listening to
* in this implementation. Here it listens for Copy and Paste ActionCommands.
* Selections comprising non-adjacent cells result in invalid selection and
* then copy action cannot be performed.
* Paste is done by aligning the upper left corner of the selection with the
* 1st element in the current selection of the JTable.
*/
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().compareTo("Copy")==0)
{
StringBuffer sbf=new StringBuffer();
// Check to ensure we have selected only a contiguous block of
// cells
int numcols=jTable1.getSelectedColumnCount();
int numrows=jTable1.getSelectedRowCount();
int[] rowsselected=jTable1.getSelectedRows();
int[] colsselected=jTable1.getSelectedColumns();
if (!((numrows-1==rowsselected[rowsselected.length-1]-rowsselected[0] &&
numrows==rowsselected.length) &&
(numcols-1==colsselected[colsselected.length-1]-colsselected[0] &&
numcols==colsselected.length)))
{
JOptionPane.showMessageDialog(null, "Invalid Copy Selection",
"Invalid Copy Selection",
JOptionPane.ERROR_MESSAGE);
return;
}
for (int i=0;i
Ads help maintain this website.