/*
* Created on 2004/08/08
* Author aki@www.xucker.jpn.org
* License Apache2.0 or Common Public License
*/
package org.jpn.xucker.commons.swt.ui;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableCursor;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
/**
*
*
*/
public class CopyTable implements SelectionListener,MouseListener,KeyListener{
private Table table;
private TableCursor cursor;
private MenuItem copyItem;
public CopyTable(Composite parent,int style){
table=new Table(parent,style);
cursor=new TableCursor(table,SWT.NULL);
cursor.addSelectionListener(this);
cursor.setBackground(new Color(table.getDisplay(),200,200,200));
Menu menu1=new Menu(cursor);
cursor.setMenu(menu1);
copyItem=new MenuItem(menu1,SWT.PUSH);
copyItem.setText("Rs[");
copyItem.addSelectionListener(this);
cursor.addKeyListener(this);
}
public TableCursor getTableCursor(){
return cursor;
}
public Table getTable(){
return table;
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.MouseListener#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
*/
public void mouseDoubleClick(MouseEvent arg0) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.MouseListener#mouseDown(org.eclipse.swt.events.MouseEvent)
*/
public void mouseDown(MouseEvent arg0) {
/*
int selection=table1.getSelectionIndex();
if(selection==-1){
cursor.setVisible(false); //-1 -1 NG
}else{
cursor.setVisible(true);
}
*/
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.MouseListener#mouseUp(org.eclipse.swt.events.MouseEvent)
*/
public void mouseUp(MouseEvent arg0) {
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
*/
public void keyPressed(KeyEvent event) {
// TODO Auto-generated method stub
}
public void execCopy() {
TableItem item=cursor.getRow();
if(item!=null){
String result=item.getText(cursor.getColumn());
Clipboard cp = Toolkit.getDefaultToolkit().getSystemClipboard();
cp.setContents(new StringSelection(result), null);
}
}
public void widgetDefaultSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
*/
public void keyReleased(KeyEvent event) {
if(event.stateMask==SWT.CTRL){
//System.out.println("CTRL");
//System.out.println((char)event.keyCode);
if((char)event.keyCode=='c'){
//System.out.println("copy");
execCopy();
}
}
}
public void widgetSelected(SelectionEvent event) {
Object target=event.getSource();
if(target==copyItem){
execCopy();
}
}
}
|