1 package org.wcb.util;
2 /***
3 * Copyright (C) 1999 Walter Bogaardt
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 *
19 */
20
21
22 import java.awt.Toolkit;
23 import java.awt.event.ActionListener;
24 import java.awt.event.ActionEvent;
25 import java.awt.event.KeyEvent;
26 import javax.swing.KeyStroke;
27 import javax.swing.JTable;
28 import javax.swing.JComponent;
29 import javax.swing.JOptionPane;
30 import java.awt.datatransfer.Clipboard;
31 import java.awt.datatransfer.StringSelection;
32 import java.awt.datatransfer.DataFlavor;
33 import java.util.StringTokenizer;
34
35 /***
36 * ExcelAdapter enables Copy-Paste Clipboard functionality on JTables.
37 * The clipboard data format used by the adapter is compatible with
38 * the clipboard format used by Excel. This provides for clipboard
39 * interoperability between enabled JTables and Excel.
40 */
41 public class ExcelAdapter implements ActionListener
42 {
43 private String rowstring, value;
44 private Clipboard system;
45 private StringSelection stsel;
46 private JTable jTable1;
47
48 /***
49 * The Excel Adapter is constructed with a
50 * JTable on which it enables Copy-Paste and acts
51 * as a Clipboard listener.
52 * @param myJTable the table to apply the ExcelAdapter.
53 */
54 public ExcelAdapter(JTable myJTable)
55 {
56 jTable1 = myJTable;
57 KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, false);
58
59
60
61 KeyStroke paste =
62 KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK, false);
63
64
65
66
67 jTable1.registerKeyboardAction(this, "Copy", copy, JComponent.WHEN_FOCUSED);
68
69
70 jTable1.registerKeyboardAction(this, "Paste", paste, JComponent.WHEN_FOCUSED);
71
72 system = Toolkit.getDefaultToolkit().getSystemClipboard();
73 }
74
75 /***
76 * Public Accessor methods for the Table on which this adapter acts.
77 * @return the JTable component
78 */
79 public JTable getJTable() {
80 return jTable1;
81 }
82
83 /***
84 * Allows setting of the JTable component
85 * @param jtTable The component to allow excel cut and paste
86 */
87 public void setJTable(JTable jtTable) {
88 this.jTable1 = jtTable;
89 }
90
91 /***
92 * This method is activated on the Keystrokes we are listening to
93 * in this implementation. Here it listens for Copy and Paste ActionCommands.
94 * Selections comprising non-adjacent cells result in invalid selection and
95
96 * then copy action cannot be performed.
97 * Paste is done by aligning the upper left corner of the selection with the
98 * 1st element in the current selection of the JTable.
99 * @param e The action event
100 */
101 public void actionPerformed(ActionEvent e) {
102 if (e.getActionCommand().compareTo("Copy") == 0)
103 {
104 StringBuffer sbf = new StringBuffer();
105
106
107
108 int numcols = jTable1.getSelectedColumnCount();
109 int numrows = jTable1.getSelectedRowCount();
110 int[] rowsselected = jTable1.getSelectedRows();
111 int[] colsselected = jTable1.getSelectedColumns();
112
113 if (!((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0]
114 && numrows == rowsselected.length)
115 && (numcols - 1 == colsselected[colsselected.length - 1] - colsselected[0]
116 && numcols == colsselected.length)))
117 {
118 JOptionPane.showMessageDialog(null, "Invalid Copy Selection",
119 "Invalid Copy Selection",
120 JOptionPane.ERROR_MESSAGE);
121
122 return;
123 }
124
125 for (int i = 0; i < numrows; i++)
126 {
127 for (int j = 0; j < numcols; j++)
128 {
129 sbf.append(jTable1.getValueAt(rowsselected[i], colsselected[j]));
130 if (j < numcols - 1)
131 {
132 sbf.append("\t");
133 }
134 }
135 sbf.append("\n");
136 }
137 stsel = new StringSelection(sbf.toString());
138 system = Toolkit.getDefaultToolkit().getSystemClipboard();
139 system.setContents(stsel, stsel);
140 }
141
142 if (e.getActionCommand().compareTo("Paste") == 0)
143 {
144 System.out.println("Trying to Paste");
145 int startRow = (jTable1.getSelectedRows())[0];
146 int startCol = (jTable1.getSelectedColumns())[0];
147 try
148 {
149 String trstring = (String) (system.getContents(this).getTransferData(DataFlavor.stringFlavor));
150
151 System.out.println("String is:" + trstring);
152 StringTokenizer st1 = new StringTokenizer(trstring, "\n");
153 for (int i = 0; st1.hasMoreTokens(); i++)
154 {
155 rowstring = st1.nextToken();
156 StringTokenizer st2 = new StringTokenizer(rowstring, "\t");
157
158 for (int j = 0; st2.hasMoreTokens(); j++)
159 {
160 value = st2.nextToken();
161 if (startRow + i < jTable1.getRowCount() && startCol + j < jTable1.getColumnCount())
162 {
163 jTable1.setValueAt(value, startRow + i, startCol + j);
164 }
165 System.out.println("Putting " + value + "at row=" + startRow + i + "column=" + startCol + j);
166 }
167 }
168 }
169 catch (Exception ex)
170 {
171 ex.printStackTrace();
172 }
173 }
174 }
175 }
176
177
178
179
180