Lines 42-63
Link Here
|
42 |
package org.netbeans.modules.db.dataview.table; |
42 |
package org.netbeans.modules.db.dataview.table; |
43 |
|
43 |
|
44 |
import java.awt.Color; |
44 |
import java.awt.Color; |
|
|
45 |
import java.awt.datatransfer.StringSelection; |
46 |
import java.awt.datatransfer.Transferable; |
45 |
import java.awt.event.KeyEvent; |
47 |
import java.awt.event.KeyEvent; |
46 |
import java.awt.event.KeyListener; |
48 |
import java.awt.event.KeyListener; |
47 |
import java.awt.event.MouseEvent; |
49 |
import java.awt.event.MouseEvent; |
48 |
import java.sql.Blob; |
50 |
import java.sql.Blob; |
49 |
import java.sql.Clob; |
51 |
import java.sql.Clob; |
|
|
52 |
import java.sql.SQLException; |
50 |
import java.sql.Timestamp; |
53 |
import java.sql.Timestamp; |
|
|
54 |
import java.text.DateFormat; |
51 |
import java.text.SimpleDateFormat; |
55 |
import java.text.SimpleDateFormat; |
52 |
import java.util.ArrayList; |
56 |
import java.util.ArrayList; |
53 |
import java.util.List; |
57 |
import java.util.List; |
54 |
import java.util.logging.Level; |
58 |
import java.util.logging.Level; |
55 |
import java.util.logging.Logger; |
59 |
import java.util.logging.Logger; |
|
|
60 |
import javax.swing.JComponent; |
56 |
import javax.swing.JLabel; |
61 |
import javax.swing.JLabel; |
57 |
import javax.swing.JTable; |
62 |
import javax.swing.JTable; |
58 |
import javax.swing.JTextField; |
63 |
import javax.swing.JTextField; |
59 |
import javax.swing.RowSorter; |
64 |
import javax.swing.RowSorter; |
60 |
import javax.swing.SwingUtilities; |
65 |
import javax.swing.SwingUtilities; |
|
|
66 |
import javax.swing.TransferHandler; |
67 |
import javax.swing.plaf.UIResource; |
61 |
import javax.swing.table.*; |
68 |
import javax.swing.table.*; |
62 |
import org.jdesktop.swingx.JXTable; |
69 |
import org.jdesktop.swingx.JXTable; |
63 |
import org.jdesktop.swingx.JXTableHeader; |
70 |
import org.jdesktop.swingx.JXTableHeader; |
Lines 71-87
Link Here
|
71 |
import org.netbeans.modules.db.dataview.meta.DBColumn; |
78 |
import org.netbeans.modules.db.dataview.meta.DBColumn; |
72 |
import org.netbeans.modules.db.dataview.output.DataView; |
79 |
import org.netbeans.modules.db.dataview.output.DataView; |
73 |
import org.netbeans.modules.db.dataview.table.celleditor.*; |
80 |
import org.netbeans.modules.db.dataview.table.celleditor.*; |
|
|
81 |
import org.netbeans.modules.db.dataview.util.BinaryToStringConverter; |
74 |
import org.netbeans.modules.db.dataview.util.DataViewUtils; |
82 |
import org.netbeans.modules.db.dataview.util.DataViewUtils; |
75 |
import org.netbeans.modules.db.dataview.util.DateType; |
83 |
import org.netbeans.modules.db.dataview.util.DateType; |
|
|
84 |
import org.netbeans.modules.db.dataview.util.TimeType; |
76 |
import org.netbeans.modules.db.dataview.util.TimestampType; |
85 |
import org.netbeans.modules.db.dataview.util.TimestampType; |
|
|
86 |
import org.openide.util.Exceptions; |
87 |
import org.openide.util.Lookup; |
88 |
import org.openide.util.datatransfer.ExClipboard; |
77 |
|
89 |
|
78 |
/** |
90 |
/** |
79 |
* A better-looking table than JTable, implements JXTable and a decorator to draw empty rows |
91 |
* A better-looking table than JTable, implements JXTable and a decorator to |
|
|
92 |
* draw empty rows |
80 |
* |
93 |
* |
81 |
* @author Ahimanikya Satapathy |
94 |
* @author Ahimanikya Satapathy |
82 |
*/ |
95 |
*/ |
83 |
public class ResultSetJXTable extends JXTableDecorator { |
96 |
public class ResultSetJXTable extends JXTableDecorator { |
84 |
|
97 |
|
|
|
98 |
private DateFormat timeFormat = new SimpleDateFormat(TimeType.DEFAULT_FOMAT_PATTERN); |
99 |
private DateFormat dateFormat = new SimpleDateFormat(DateType.DEFAULT_FOMAT_PATTERN); |
100 |
private DateFormat timestampFormat = new SimpleDateFormat(TimestampType.DEFAULT_FORMAT_PATTERN); |
101 |
|
85 |
private String[] columnToolTips; |
102 |
private String[] columnToolTips; |
86 |
private final int multiplier; |
103 |
private final int multiplier; |
87 |
private static final String data = "WE WILL EITHER FIND A WAY, OR MAKE ONE."; // NOI18N |
104 |
private static final String data = "WE WILL EITHER FIND A WAY, OR MAKE ONE."; // NOI18N |
Lines 92-97
Link Here
|
92 |
|
109 |
|
93 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
110 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
94 |
public ResultSetJXTable(final DataView dataView) { |
111 |
public ResultSetJXTable(final DataView dataView) { |
|
|
112 |
this.setTransferHandler(new TableTransferHandler()); |
113 |
|
95 |
this.dView = dataView; |
114 |
this.dView = dataView; |
96 |
|
115 |
|
97 |
setShowGrid(true, true); |
116 |
setShowGrid(true, true); |
Lines 291-298
Link Here
|
291 |
return false; |
310 |
return false; |
292 |
} |
311 |
} |
293 |
|
312 |
|
294 |
// This is mainly used for set Tooltip for column headers |
313 |
/** |
|
|
314 |
* Quote string for use in TSV (tab-separated values file |
315 |
* |
316 |
* Assumptions: column separator is \t and row separator is \n |
317 |
*/ |
318 |
protected String quoteIfNecessary(String value) { |
319 |
if (value.contains("\t") || value.contains("\n")) { //NOI18N |
320 |
return "\"" + value.replace("\"", "\"\"") + "\""; |
321 |
} else { |
322 |
return value; |
323 |
} |
324 |
} |
295 |
|
325 |
|
|
|
326 |
/** |
327 |
* Convert object to string representation |
328 |
* |
329 |
* @param o object to convert |
330 |
* @param limitSize in case of CLOBs and BLOBs limit to limitSize |
331 |
* bytes/chars |
332 |
* @return string representation of o |
333 |
*/ |
334 |
protected String convertToClipboardString(Object o, int limitSize) { |
335 |
if (o instanceof Blob) { |
336 |
Blob b = (Blob) o; |
337 |
try { |
338 |
if (b.length() <= limitSize) { |
339 |
return BinaryToStringConverter.convertToString(b.getBytes(1, (int) b.length()), 16, false); |
340 |
} |
341 |
} catch (SQLException ex) { |
342 |
} |
343 |
} else if (o instanceof Clob) { |
344 |
Clob c = (Clob) o; |
345 |
try { |
346 |
if (c.length() <= limitSize) { |
347 |
return c.getSubString(1, (int) c.length()); |
348 |
} |
349 |
} catch (SQLException ex) { |
350 |
} |
351 |
} else if (o instanceof java.sql.Time) { |
352 |
return timeFormat.format((java.util.Date) o); |
353 |
} else if (o instanceof java.sql.Date) { |
354 |
return dateFormat.format((java.util.Date) o); |
355 |
} else if (o instanceof java.util.Date) { |
356 |
return timestampFormat.format((java.util.Date) o); |
357 |
} else if (o == null) { |
358 |
return ""; //NOI18N |
359 |
} |
360 |
return o.toString(); |
361 |
} |
362 |
|
363 |
/** |
364 |
* Create TSV (tab-separated values) string from row data |
365 |
* |
366 |
* @param withHeader include column headers? |
367 |
* @return Transferable for clipboard transfer |
368 |
*/ |
369 |
private StringSelection createTransferableTSV(boolean withHeader) { |
370 |
try { |
371 |
int[] rows = getSelectedRows(); |
372 |
int[] columns; |
373 |
if (getRowSelectionAllowed()) { |
374 |
columns = new int[getColumnCount()]; |
375 |
for (int a = 0; a < columns.length; a++) { |
376 |
columns[a] = a; |
377 |
} |
378 |
} else { |
379 |
columns = getSelectedColumns(); |
380 |
} |
381 |
if (rows != null && columns != null) { |
382 |
StringBuilder output = new StringBuilder(); |
383 |
|
384 |
if (withHeader) { |
385 |
for (int column = 0; column < columns.length; column++) { |
386 |
if (column > 0) { |
387 |
output.append('\t'); //NOI18N |
388 |
|
389 |
} |
390 |
Object o = getColumnModel().getColumn(column).getIdentifier(); |
391 |
String s = o != null ? o.toString() : ""; |
392 |
output.append(quoteIfNecessary(s)); |
393 |
} |
394 |
output.append('\n'); //NOI18N |
395 |
|
396 |
} |
397 |
|
398 |
for (int row = 0; row < rows.length; row++) { |
399 |
for (int column = 0; column < columns.length; column++) { |
400 |
if (column > 0) { |
401 |
output.append('\t'); //NOI18N |
402 |
|
403 |
} |
404 |
Object o = getValueAt(rows[row], columns[column]); |
405 |
// TODO: Evaluate if 1 MB/1 Million Characters are a good limit |
406 |
String s = convertToClipboardString(o, 1024 * 1024); |
407 |
output.append(quoteIfNecessary(s)); |
408 |
|
409 |
} |
410 |
output.append('\n'); //NOI18N |
411 |
|
412 |
} |
413 |
return new StringSelection(output.toString()); |
414 |
} |
415 |
return null; |
416 |
} catch (ArrayIndexOutOfBoundsException exc) { |
417 |
Exceptions.printStackTrace(exc); |
418 |
return null; |
419 |
} |
420 |
} |
421 |
|
422 |
protected void copyRowValues(boolean withHeader) { |
423 |
ExClipboard clipboard = Lookup.getDefault().lookup(ExClipboard.class); |
424 |
StringSelection selection = createTransferableTSV(withHeader); |
425 |
clipboard.setContents(selection, selection); |
426 |
} |
427 |
|
428 |
// This is mainly used for set Tooltip for column headers |
296 |
private class JTableHeaderImpl extends JXTableHeader { |
429 |
private class JTableHeaderImpl extends JXTableHeader { |
297 |
|
430 |
|
298 |
public JTableHeaderImpl(TableColumnModel cm) { |
431 |
public JTableHeaderImpl(TableColumnModel cm) { |
Lines 316-321
Link Here
|
316 |
} |
449 |
} |
317 |
} |
450 |
} |
318 |
} |
451 |
} |
|
|
452 |
|
453 |
private class TableTransferHandler extends TransferHandler implements UIResource { |
454 |
/** |
455 |
* Map Transferable to createTransferableTSV from ResultSetJXTable |
456 |
* |
457 |
* This is needed so that CTRL-C Action of JTable gets the same treatment |
458 |
* as the transfer via the copy Methods of DataTableUI |
459 |
*/ |
460 |
@Override |
461 |
protected Transferable createTransferable(JComponent c) { |
462 |
return createTransferableTSV(false); |
319 |
} |
463 |
} |
320 |
|
464 |
|
321 |
|
465 |
@Override |
|
|
466 |
public int getSourceActions(JComponent c) { |
467 |
return COPY; |
468 |
} |
469 |
} |
470 |
} |