01: package net.sourceforge.squirrel_sql.fw.gui.action;
02:
03: /*
04: * Copyright (C) 2001-2003 Colin Bell
05: * colbell@users.sourceforge.net
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: */
21: import java.awt.Toolkit;
22: import java.awt.datatransfer.StringSelection;
23:
24: import javax.swing.JTable;
25: import javax.swing.table.TableCellRenderer;
26:
27: import net.sourceforge.squirrel_sql.fw.util.ICommand;
28: import net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent.SquirrelTableCellRenderer;
29:
30: public class TableCopyCommand implements ICommand {
31: /** Internationalized strings for this class. */
32: // private static final StringManager s_stringMgr =
33: // StringManagerFactory.getStringManager(TableCopyCommand.class);
34: private final static String NULL_CELL = "<null>";
35:
36: private JTable _table;
37: private boolean _withHeaders;
38:
39: public TableCopyCommand(JTable table, boolean withHeaders) {
40: super ();
41: if (table == null) {
42: throw new IllegalArgumentException("JTable == null");
43: }
44: _table = table;
45: _withHeaders = withHeaders;
46: }
47:
48: public void execute() {
49: int nbrSelRows = _table.getSelectedRowCount();
50: int nbrSelCols = _table.getSelectedColumnCount();
51: int[] selRows = _table.getSelectedRows();
52: int[] selCols = _table.getSelectedColumns();
53: if (selRows.length != 0 && selCols.length != 0) {
54: StringBuffer buf = new StringBuffer();
55: //if (nbrSelCols > 1 && nbrSelRows > 1)
56: if (_withHeaders) {
57: for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx) {
58: buf.append(_table.getColumnName(selCols[colIdx]));
59: if (colIdx < nbrSelCols - 1) {
60: buf.append('\t');
61: }
62: }
63: buf.append('\n');
64: }
65: for (int rowIdx = 0; rowIdx < nbrSelRows; ++rowIdx) {
66: for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx) {
67: TableCellRenderer cellRenderer = _table
68: .getCellRenderer(selRows[rowIdx],
69: selCols[colIdx]);
70: Object cellObj = _table.getValueAt(selRows[rowIdx],
71: selCols[colIdx]);
72:
73: if (cellRenderer instanceof SquirrelTableCellRenderer) {
74: cellObj = ((SquirrelTableCellRenderer) cellRenderer)
75: .renderValue(cellObj);
76: }
77:
78: buf.append(cellObj != null ? cellObj : NULL_CELL);
79: if (nbrSelCols > 1 && colIdx < nbrSelCols - 1) {
80: buf.append('\t');
81: }
82: }
83: if (nbrSelRows > 1) {
84: buf.append('\n');
85: }
86: }
87: StringSelection ss = new StringSelection(buf.toString());
88: Toolkit.getDefaultToolkit().getSystemClipboard()
89: .setContents(ss, ss);
90: }
91: }
92: }
|