001: package net.sourceforge.squirrel_sql.fw.gui.action;
002:
003: /*
004: * Copyright (C) 2005 Gerd Wagner
005: * gerdwagner@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: import java.awt.Toolkit;
023: import java.awt.datatransfer.StringSelection;
024: import java.sql.Types;
025: import java.util.Calendar;
026:
027: import javax.swing.JTable;
028: import javax.swing.table.TableColumn;
029:
030: import net.sourceforge.squirrel_sql.fw.util.ICommand;
031: import net.sourceforge.squirrel_sql.fw.datasetviewer.ExtTableColumn;
032: import net.sourceforge.squirrel_sql.fw.datasetviewer.ColumnDisplayDefinition;
033:
034: /**
035: * This command gets the current selected text from a <TT>JTable</TT>
036: * and formats it as HTML table and places it on the system clipboard.
037: *
038: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
039: */
040: public class TableCopyInStatementCommand implements ICommand {
041: /**
042: * The table we are copying data from.
043: */
044: private JTable _table;
045:
046: /**
047: * Ctor specifying the <TT>JTable</TT> to get the data from.
048: *
049: * @param table The <TT>JTable</TT> to get data from.
050: * @throws IllegalArgumentException Thrown if <tt>null</tt> <tt>JTable</tt> passed.
051: */
052: public TableCopyInStatementCommand(JTable table) {
053: super ();
054: if (table == null) {
055: throw new IllegalArgumentException("JTable == null");
056: }
057: _table = table;
058: }
059:
060: /**
061: * Execute this command.
062: */
063: public void execute() {
064: int nbrSelRows = _table.getSelectedRowCount();
065: int nbrSelCols = _table.getSelectedColumnCount();
066: int[] selRows = _table.getSelectedRows();
067: int[] selCols = _table.getSelectedColumns();
068: if (selRows.length != 0 && selCols.length != 0) {
069: StringBuffer buf = new StringBuffer();
070: for (int colIdx = 0; colIdx < nbrSelCols; ++colIdx) {
071: TableColumn col = _table.getColumnModel().getColumn(
072: selCols[colIdx]);
073:
074: ColumnDisplayDefinition colDef = null;
075: if (col instanceof ExtTableColumn) {
076: colDef = ((ExtTableColumn) col)
077: .getColumnDisplayDefinition();
078: }
079:
080: int lastLength = buf.length();
081: buf.append("(");
082: for (int rowIdx = 0; rowIdx < nbrSelRows; ++rowIdx) {
083: if (0 < rowIdx) {
084: buf.append(",");
085: if (100 < buf.length() - lastLength) {
086: lastLength = buf.length();
087: buf.append("\n");
088: }
089: }
090:
091: final Object cellObj = _table.getValueAt(
092: selRows[rowIdx], selCols[colIdx]);
093: buf.append(getData(colDef, cellObj));
094: }
095: buf.append(")\n");
096: }
097: final StringSelection ss = new StringSelection(buf
098: .toString());
099: Toolkit.getDefaultToolkit().getSystemClipboard()
100: .setContents(ss, ss);
101: }
102: }
103:
104: private String getData(ColumnDisplayDefinition colDef,
105: Object cellObj) {
106: if (cellObj == null) {
107: return "null";
108: } else {
109: if (null == colDef) {
110: return "'" + cellObj.toString().replaceAll("'", "''")
111: + "'";
112: } else {
113: if (colDef.getSqlType() == Types.SMALLINT
114: || colDef.getSqlType() == Types.INTEGER
115: || colDef.getSqlType() == Types.DECIMAL
116: || colDef.getSqlType() == Types.DOUBLE
117: || colDef.getSqlType() == Types.BIGINT
118: || colDef.getSqlType() == Types.NUMERIC
119: || colDef.getSqlType() == Types.TINYINT
120: || colDef.getSqlType() == Types.BIT
121: || colDef.getSqlType() == Types.REAL) {
122: return cellObj.toString();
123: } else if (colDef.getSqlType() == Types.TIME
124: && cellObj instanceof java.util.Date) {
125: java.util.Date date = (java.util.Date) cellObj;
126: Calendar cal = Calendar.getInstance();
127: cal.setTime(date);
128: return "{t '"
129: + prefixNulls(
130: cal.get(Calendar.HOUR_OF_DAY), 2)
131: + ":"
132: + prefixNulls(cal.get(Calendar.MINUTE), 2)
133: + ":"
134: + prefixNulls(cal.get(Calendar.SECOND), 2)
135: + "'}";
136: } else if (colDef.getSqlType() == Types.DATE
137: && cellObj instanceof java.util.Date) {
138: java.util.Date date = (java.util.Date) cellObj;
139: Calendar cal = Calendar.getInstance();
140: cal.setTime(date);
141: return "{d '"
142: + prefixNulls(cal.get(Calendar.YEAR), 4)
143: + "-"
144: + prefixNulls(cal.get(Calendar.MONTH) + 1,
145: 2)
146: + "-"
147: + prefixNulls(cal
148: .get(Calendar.DAY_OF_MONTH), 2)
149: + "'}";
150: } else if (colDef.getSqlType() == Types.TIMESTAMP
151: && cellObj instanceof java.util.Date) {
152: java.util.Date date = (java.util.Date) cellObj;
153: Calendar cal = Calendar.getInstance();
154: cal.setTime(date);
155: return "{ts '"
156: + prefixNulls(cal.get(Calendar.YEAR), 4)
157: + "-"
158: + prefixNulls(cal.get(Calendar.MONTH) + 1,
159: 2)
160: + "-"
161: + prefixNulls(cal
162: .get(Calendar.DAY_OF_MONTH), 2)
163: + " "
164: + prefixNulls(
165: cal.get(Calendar.HOUR_OF_DAY), 2)
166: + ":"
167: + prefixNulls(cal.get(Calendar.MINUTE), 2)
168: + ":"
169: + prefixNulls(cal.get(Calendar.SECOND), 2)
170: + "'}";
171: } else {
172: return "'"
173: + cellObj.toString().replaceAll("'", "''")
174: + "'";
175: }
176: }
177: }
178: }
179:
180: private String prefixNulls(int toPrefix, int digitCount) {
181: String ret = "" + toPrefix;
182:
183: while (ret.length() < digitCount) {
184: ret = 0 + ret;
185: }
186:
187: return ret;
188: }
189:
190: }
|