|
1 /* |
|
2 * CDDL HEADER START |
|
3 * |
|
4 * The contents of this file are subject to the terms of the |
|
5 * Common Development and Distribution License (the "License"). |
|
6 * You may not use this file except in compliance with the License. |
|
7 * |
|
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 * or http://www.opensolaris.org/os/licensing. |
|
10 * See the License for the specific language governing permissions |
|
11 * and limitations under the License. |
|
12 * |
|
13 * When distributing Covered Code, include this CDDL HEADER in each |
|
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 * If applicable, add the following below this CDDL HEADER, with the |
|
16 * fields enclosed by brackets "[]" replaced with your own identifying |
|
17 * information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 * |
|
19 * CDDL HEADER END |
|
20 */ |
|
21 |
|
22 /* |
|
23 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. |
|
24 */ |
|
25 |
|
26 package com.oracle.solaris.vp.util.swing; |
|
27 |
|
28 import java.awt.datatransfer.*; |
|
29 import java.io.IOException; |
|
30 import java.util.Arrays; |
|
31 import javax.swing.*; |
|
32 import javax.swing.table.TableModel; |
|
33 import com.oracle.solaris.vp.util.misc.ArrayUtil; |
|
34 |
|
35 @SuppressWarnings({"serial"}) |
|
36 public class EditableTableTransferHandler extends TransferHandler { |
|
37 // |
|
38 // Inner classes |
|
39 // |
|
40 |
|
41 public static class TableRow { |
|
42 // |
|
43 // Instance data |
|
44 // |
|
45 |
|
46 public int index; |
|
47 public Object[] data; |
|
48 |
|
49 // Whether this row was successfully imported or not |
|
50 public boolean success = true; |
|
51 |
|
52 // |
|
53 // Constructors |
|
54 // |
|
55 |
|
56 public TableRow(int index, Object[] data) { |
|
57 this.index = index; |
|
58 this.data = data; |
|
59 } |
|
60 } |
|
61 |
|
62 public static class TableData { |
|
63 // |
|
64 // Instance data |
|
65 // |
|
66 |
|
67 private int tableHashCode; |
|
68 private TableRow[] rows; |
|
69 |
|
70 // |
|
71 // Constructors |
|
72 // |
|
73 |
|
74 public TableData(int tableHashCode, TableRow[] rows) { |
|
75 this.tableHashCode = tableHashCode; |
|
76 this.rows = rows; |
|
77 } |
|
78 |
|
79 // |
|
80 // TableData methods |
|
81 // |
|
82 |
|
83 public int getTableHashCode() { |
|
84 return tableHashCode; |
|
85 } |
|
86 |
|
87 public TableRow[] getRows() { |
|
88 return rows; |
|
89 } |
|
90 } |
|
91 |
|
92 public class EditableTableTransferable implements Transferable { |
|
93 // |
|
94 // Instance data |
|
95 // |
|
96 |
|
97 private TableData data; |
|
98 |
|
99 // |
|
100 // Constructors |
|
101 // |
|
102 |
|
103 public EditableTableTransferable(TableData data) { |
|
104 this.data = data; |
|
105 } |
|
106 |
|
107 // |
|
108 // Transferable methods |
|
109 // |
|
110 |
|
111 @Override |
|
112 public Object getTransferData(DataFlavor flavor) |
|
113 throws UnsupportedFlavorException { |
|
114 |
|
115 if (!isDataFlavorSupported(flavor)) { |
|
116 throw new UnsupportedFlavorException(flavor); |
|
117 } |
|
118 |
|
119 return data; |
|
120 } |
|
121 |
|
122 @Override |
|
123 public DataFlavor[] getTransferDataFlavors() { |
|
124 return new DataFlavor[] { |
|
125 localFlavor, serialFlavor |
|
126 }; |
|
127 } |
|
128 |
|
129 @Override |
|
130 public boolean isDataFlavorSupported(DataFlavor flavor) { |
|
131 return localFlavor.equals(flavor) || serialFlavor.equals(flavor); |
|
132 } |
|
133 } |
|
134 |
|
135 // |
|
136 // Static data |
|
137 // |
|
138 |
|
139 protected static DataFlavor localFlavor; |
|
140 static { |
|
141 String localType = DataFlavor.javaJVMLocalObjectMimeType + |
|
142 ";class=\"" + TableData.class.getName() + "\""; |
|
143 |
|
144 try { |
|
145 localFlavor = new DataFlavor(localType); |
|
146 } catch (ClassNotFoundException ignore) { |
|
147 } |
|
148 } |
|
149 |
|
150 protected static final DataFlavor serialFlavor = |
|
151 new DataFlavor(TableData.class, "TableData"); |
|
152 |
|
153 // |
|
154 // TransferHandler methods |
|
155 // |
|
156 |
|
157 @Override |
|
158 public boolean canImport(JComponent c, DataFlavor[] flavors) { |
|
159 return c instanceof JTable && |
|
160 ((JTable)c).getModel() instanceof EditableTableModel && |
|
161 (hasLocalFlavor(flavors) || hasSerialFlavor(flavors)); |
|
162 } |
|
163 |
|
164 @Override |
|
165 protected Transferable createTransferable(JComponent c) { |
|
166 EditableTableTransferable transferable = null; |
|
167 |
|
168 if (c instanceof JTable) { |
|
169 JTable table = (JTable)c; |
|
170 int[] selectedRows = table.getSelectedRows(); |
|
171 |
|
172 if (selectedRows.length != 0) { |
|
173 |
|
174 int nCols = table.getColumnCount(); |
|
175 if (nCols != 0) { |
|
176 |
|
177 Arrays.sort(selectedRows); |
|
178 TableModel model = table.getModel(); |
|
179 TableRow[] rows = new TableRow[selectedRows.length]; |
|
180 |
|
181 for (int i = 0; i < selectedRows.length; i++) { |
|
182 int row = selectedRows[i]; |
|
183 Object[] data = new Object[nCols]; |
|
184 |
|
185 for (int col = 0; col < nCols; col++) { |
|
186 data[col] = model.getValueAt(row, col); |
|
187 } |
|
188 |
|
189 rows[i] = new TableRow(row, data); |
|
190 } |
|
191 |
|
192 transferable = new EditableTableTransferable( |
|
193 new TableData(table.hashCode(), rows)); |
|
194 } |
|
195 } |
|
196 } |
|
197 |
|
198 return transferable; |
|
199 } |
|
200 |
|
201 @Override |
|
202 protected void exportDone(JComponent c, Transferable t, int action) { |
|
203 if (action == MOVE) { |
|
204 |
|
205 if (c instanceof JTable) { |
|
206 JTable table = (JTable)c; |
|
207 |
|
208 DataFlavor[] flavors = t.getTransferDataFlavors(); |
|
209 |
|
210 DataFlavor flavor = null; |
|
211 if (hasLocalFlavor(flavors)) { |
|
212 flavor = localFlavor; |
|
213 } else if (hasSerialFlavor(flavors)) { |
|
214 flavor = serialFlavor; |
|
215 } |
|
216 |
|
217 if (flavor != null) { |
|
218 try { |
|
219 TableData data = (TableData)t.getTransferData(flavor); |
|
220 TableRow[] rows = data.getRows(); |
|
221 TableModel model = table.getModel(); |
|
222 if (model instanceof EditableTableModel) { |
|
223 EditableTableModel editModel = |
|
224 (EditableTableModel)model; |
|
225 |
|
226 for (int i = rows.length - 1; i >= 0; i--) { |
|
227 TableRow row = rows[i]; |
|
228 if (row.success) { |
|
229 editModel.removeRow(row.index); |
|
230 } |
|
231 } |
|
232 } |
|
233 |
|
234 // Thrown by getTransferData |
|
235 } catch (IOException e) { |
|
236 |
|
237 // Thrown by getTransferData |
|
238 } catch (UnsupportedFlavorException e) { |
|
239 } |
|
240 } |
|
241 } |
|
242 } |
|
243 } |
|
244 |
|
245 @Override |
|
246 public int getSourceActions(JComponent c) { |
|
247 return COPY_OR_MOVE; |
|
248 } |
|
249 |
|
250 @Override |
|
251 public boolean importData(JComponent c, Transferable t) { |
|
252 boolean dropped = false; |
|
253 |
|
254 if (c instanceof JTable) { |
|
255 |
|
256 JTable target = (JTable)c; |
|
257 DataFlavor[] flavors = t.getTransferDataFlavors(); |
|
258 |
|
259 DataFlavor flavor = null; |
|
260 if (hasLocalFlavor(flavors)) { |
|
261 flavor = localFlavor; |
|
262 } else if (hasSerialFlavor(flavors)) { |
|
263 flavor = serialFlavor; |
|
264 } |
|
265 |
|
266 if (flavor != null) { |
|
267 JTable.DropLocation dropLoc = target.getDropLocation(); |
|
268 |
|
269 if (dropLoc != null) { |
|
270 int insertRow = dropLoc.getRow(); |
|
271 |
|
272 try { |
|
273 TableData data = (TableData)t.getTransferData(flavor); |
|
274 TableRow[] rows = data.getRows(); |
|
275 |
|
276 // Is the source table the same as the target table? |
|
277 boolean isSame = flavor == localFlavor && |
|
278 data.getTableHashCode() == target.hashCode(); |
|
279 |
|
280 // Rows cannot be dropped onto any row in selected range |
|
281 // if the source and target are the same table |
|
282 if (!isSame || insertRow <= rows[0].index || |
|
283 insertRow > rows[rows.length -1].index) { |
|
284 |
|
285 TableModel model = target.getModel(); |
|
286 if (model instanceof EditableTableModel) { |
|
287 EditableTableModel editModel = |
|
288 (EditableTableModel)model; |
|
289 |
|
290 int selectStart = insertRow; |
|
291 |
|
292 for (TableRow row : rows) { |
|
293 row.success = editModel.attemptInsertRow( |
|
294 insertRow, row.data); |
|
295 |
|
296 if (row.success) { |
|
297 if (isSame) { |
|
298 for (TableRow r : rows) { |
|
299 if (r.index >= insertRow) { |
|
300 r.index++; |
|
301 } |
|
302 } |
|
303 } |
|
304 insertRow++; |
|
305 } |
|
306 } |
|
307 |
|
308 int selectEnd = insertRow - 1; |
|
309 |
|
310 if (selectEnd >= selectStart) { |
|
311 target.setRowSelectionInterval( |
|
312 selectStart, selectEnd); |
|
313 } |
|
314 |
|
315 target.requestFocusInWindow(); |
|
316 |
|
317 dropped = true; |
|
318 } |
|
319 } |
|
320 |
|
321 // Thrown by getTransferData |
|
322 } catch (IOException e) { |
|
323 |
|
324 // Thrown by getTransferData |
|
325 } catch (UnsupportedFlavorException e) { |
|
326 } |
|
327 } |
|
328 } |
|
329 } |
|
330 |
|
331 return dropped; |
|
332 } |
|
333 |
|
334 // |
|
335 // Private methods |
|
336 // |
|
337 |
|
338 private boolean hasLocalFlavor(DataFlavor[] flavors) { |
|
339 return ArrayUtil.indexOf(flavors, localFlavor) != -1; |
|
340 } |
|
341 |
|
342 private boolean hasSerialFlavor(DataFlavor[] flavors) { |
|
343 return ArrayUtil.indexOf(flavors, serialFlavor) != -1; |
|
344 } |
|
345 } |