package com.lgnortel.network.networkview;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.StringTokenizer;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.KeyStroke;
/**
* Copyright (c) 2008 LG-Nortel, Inc. All Rights Reserved.
*
* CONFIDENTIALITY AND LIMITED USE: This software, including any software of <br>
* third parties embodied herein, contains code, information, data and concepts <br>
* which are confidential and/or proprietary to LG-Nortel and such third <br>
* parties. This software is licensed for use solely in accordance with the <br>
* terms and conditions of the applicable license agreement with LG-Nortel or <br>
* its authorized distributor, and not for any other use or purpose. No <br>
* redistribution of this software by any party is permitted. <br>
*
* Title: CommonTableCellRenderer.java<br>
* Description: <br>
* Copyright: Copyright(c) 2008 LG-NORTEL ALL Rights Reserved<br>
* Company: LG-Nortel<br>
*
* Title: TableCutPasteListener.java<br>
* Description: <br>
* Copyright: Copyright(c) 2008 LG-NORTEL ALL Rights Reserved<br>
* Company: LG-Nortel<br>
*
* @author JungGu Lee
* @version 0.1
* @created 2009. 1. 21.
* @modified 2009. 1. 21.
* @product EFA R4.0 EMS
* @sw_block
*/
public class TableCutPasteListener implements ActionListener {
private String rowstring, value;
private Clipboard system;
private StringSelection stsel;
private JTable table;
/**
* Constructor of this class
*/
public TableCutPasteListener(JTable jTable) {
table = jTable;
KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, false);
KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK, false);
KeyStroke cut = KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK, false);
table.registerKeyboardAction(this, "Copy", copy, JComponent.WHEN_FOCUSED);
table.registerKeyboardAction(this, "Paste", paste, JComponent.WHEN_FOCUSED);
table.registerKeyboardAction(this, "Cut", cut, JComponent.WHEN_FOCUSED);
system = Toolkit.getDefaultToolkit().getSystemClipboard();
}
/**
* action performed
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().compareTo("Copy") == 0) {
StringBuffer stringBuffer = new StringBuffer();
int numcols = table.getSelectedColumnCount();
int numrows = table.getSelectedRowCount();
int[] rowsselected = table.getSelectedRows();
int[] colsselected = table.getSelectedColumns();
for (int i = 0; i < numrows; i++) {
for (int j = 0; j < numcols; j++) {
stringBuffer.append(table.getValueAt(rowsselected[i], colsselected[j]));
if (j < numcols - 1)
stringBuffer.append("\t");
}
stringBuffer.append("\n");
}
stsel = new StringSelection(stringBuffer.toString());
system = Toolkit.getDefaultToolkit().getSystemClipboard();
system.setContents(stsel, stsel);
}
else if (e.getActionCommand().compareTo("Paste") == 0) {
int startRow = (table.getSelectedRows())[0];
int startCol = (table.getSelectedColumns())[0];
try {
String data = (String) (system.getContents(this).getTransferData(DataFlavor.stringFlavor));
StringTokenizer newLineToken = new StringTokenizer(data, "\n");
for (int i = 0; newLineToken.hasMoreTokens(); i++) {
rowstring = newLineToken.nextToken();
StringTokenizer tabToken = new StringTokenizer(rowstring, "\t");
for (int j = 0; tabToken.hasMoreTokens(); j++) {
value = (String) tabToken.nextToken();
if (startRow + i < table.getRowCount() && startCol + j < table.getColumnCount())
table.setValueAt(value, startRow + i, startCol + j);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
else if (e.getActionCommand().compareTo("Cut") == 0) {
int numcols = table.getSelectedColumnCount();
int numrows = table.getSelectedRowCount();
int[] rowsselected = table.getSelectedRows();
int[] colsselected = table.getSelectedColumns();
for (int i = 0; i < numrows; i++) {
for (int j = 0; j < numcols; j++) {
table.setValueAt("", rowsselected[i], colsselected[j]);
}
}
} else {
int row = table.getSelectedRow();
int col = table.getSelectedColumn();
String value = table.getValueAt(row, col).toString();
table.setValueAt(value, row, col);
}
}
}
|