net.sourceforge.squirrel_sql.fw.gui.action.TableCopyInStatementCommand Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fw Show documentation
Show all versions of fw Show documentation
The framework library contains utility classes that are generic and useful for building applications
that introspect a database via JDBC. These are not intended to be SQuirreLSQL-specific and could be
used by other projects JDBC front-end applications. This project is guaranteed to have no code
dependencies on other SQuirreLSQL projects and could therefore be used when building a different
JDBC front-end application.
package net.sourceforge.squirrel_sql.fw.gui.action;
/*
* Copyright (C) 2005 Gerd Wagner
* [email protected]
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import net.sourceforge.squirrel_sql.fw.util.ICommand;
import net.sourceforge.squirrel_sql.fw.datasetviewer.ExtTableColumn;
import net.sourceforge.squirrel_sql.fw.datasetviewer.ColumnDisplayDefinition;
/**
* This command gets the current selected text from a JTable
* and formats it as HTML table and places it on the system clipboard.
*
* @author Colin Bell
*/
public class TableCopyInStatementCommand extends TableCopySqlPartCommandBase implements ICommand
{
/**
* The table we are copying data from.
*/
private JTable _table;
/**
* Ctor specifying the JTable to get the data from.
*
* @param table The JTable to get data from.
* @throws IllegalArgumentException Thrown if null JTable passed.
*/
public TableCopyInStatementCommand(JTable table)
{
super();
if (table == null)
{
throw new IllegalArgumentException("JTable == null");
}
_table = table;
}
/**
* Execute this command.
*/
public void execute()
{
int nbrSelRows = _table.getSelectedRowCount();
int nbrSelCols = _table.getSelectedColumnCount();
int[] selRows = _table.getSelectedRows();
int[] selCols = _table.getSelectedColumns();
if (selRows.length != 0 && selCols.length != 0)
{
StringBuffer buf = new StringBuffer();
for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx)
{
TableColumn col = _table.getColumnModel().getColumn(selCols[colIdx]);
ColumnDisplayDefinition colDef = null;
if(col instanceof ExtTableColumn)
{
colDef = ((ExtTableColumn) col).getColumnDisplayDefinition();
}
int lastLength = buf.length();
buf.append("(");
for (int rowIdx = 0; rowIdx < nbrSelRows; ++rowIdx)
{
if(0 < rowIdx)
{
buf.append(",");
if(100 < buf.length() - lastLength)
{
lastLength = buf.length();
buf.append("\n");
}
}
final Object cellObj = _table.getValueAt(selRows[rowIdx], selCols[colIdx]);
buf.append(getData(colDef, cellObj, StatType.IN));
}
buf.append(")\n");
}
final StringSelection ss = new StringSelection(buf.toString());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
}
}
}