PageRenderTime 38ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/tables/ExcelAdapter.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 146 lines | 80 code | 32 blank | 34 comment | 20 complexity | 3c129b28f4baa598e995ac28a2e265dc MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, GPL-3.0, GPL-2.0, AGPL-3.0, JSON, BSD-3-Clause
  1. package mpv5.utils.tables;
  2. //~--- JDK imports ------------------------------------------------------------
  3. import java.awt.*;
  4. import java.awt.datatransfer.*;
  5. import java.awt.event.*;
  6. import java.util.*;
  7. import javax.swing.*;
  8. /**
  9. * @author http://www.javaworld.com/javaworld/javatips/jw-javatip77.html
  10. * ExcelAdapter enables Copy-Paste Clipboard functionality on JTables.
  11. * The clipboard data format used by the adapter is compatible with
  12. * the clipboard format used by Excel. This provides for clipboard
  13. * interoperability between enabled JTables and Excel.
  14. */
  15. public class ExcelAdapter implements ActionListener {
  16. private JTable jTable1;
  17. private String rowstring, value;
  18. private StringSelection stsel;
  19. private Clipboard system;
  20. /**
  21. * The Excel Adapter is constructed with a
  22. * JTable on which it enables Copy-Paste and acts
  23. * as a Clipboard listener.
  24. */
  25. public ExcelAdapter(JTable myJTable) {
  26. jTable1 = myJTable;
  27. KeyStroke copy = KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK, false);
  28. // Identifying the copy KeyStroke user can modify this
  29. // to copy on some other Key combination.
  30. KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK, false);
  31. // Identifying the Paste KeyStroke user can modify this
  32. // to copy on some other Key combination.
  33. jTable1.registerKeyboardAction(this, "Copy", copy, JComponent.WHEN_FOCUSED);
  34. jTable1.registerKeyboardAction(this, "Paste", paste, JComponent.WHEN_FOCUSED);
  35. system = Toolkit.getDefaultToolkit().getSystemClipboard();
  36. }
  37. /**
  38. * Public Accessor methods for the Table on which this adapter acts.
  39. */
  40. public JTable getJTable() {
  41. return jTable1;
  42. }
  43. public void setJTable(JTable jTable1) {
  44. this.jTable1 = jTable1;
  45. }
  46. /**
  47. * This method is activated on the Keystrokes we are listening to
  48. * in this implementation. Here it listens for Copy and Paste ActionCommands.
  49. * Selections comprising non-adjacent cells result in invalid selection and
  50. * then copy action cannot be performed.
  51. * Paste is done by aligning the upper left corner of the selection with the
  52. * 1st element in the current selection of the JTable.
  53. */
  54. public void actionPerformed(ActionEvent e) {
  55. if (e.getActionCommand().compareTo("Copy") == 0) {
  56. StringBuffer sbf = new StringBuffer();
  57. // Check to ensure we have selected only a contiguous block of
  58. // cells
  59. int numcols = jTable1.getSelectedColumnCount();
  60. int numrows = jTable1.getSelectedRowCount();
  61. int[] rowsselected = jTable1.getSelectedRows();
  62. int[] colsselected = jTable1.getSelectedColumns();
  63. if (!(((numrows - 1 == rowsselected[rowsselected.length - 1] - rowsselected[0])
  64. && (numrows == rowsselected.length)) && ((numcols - 1
  65. == colsselected[colsselected.length - 1] - colsselected[0]) && (numcols
  66. == colsselected.length)))) {
  67. JOptionPane.showMessageDialog(null, "Invalid Copy Selection", "Invalid Copy Selection",
  68. JOptionPane.ERROR_MESSAGE);
  69. return;
  70. }
  71. for (int i = 0; i < numrows; i++) {
  72. for (int j = 0; j < numcols; j++) {
  73. String s = String.valueOf(jTable1.getValueAt(rowsselected[i],
  74. colsselected[j])).replaceAll("\\<.*?\\>", "");
  75. if (s.equals("null")) {
  76. s = "";
  77. }
  78. sbf.append(s);
  79. if (j < numcols - 1) {
  80. sbf.append("\t");
  81. }
  82. }
  83. sbf.append("\n");
  84. }
  85. stsel = new StringSelection(sbf.toString());
  86. system = Toolkit.getDefaultToolkit().getSystemClipboard();
  87. system.setContents(stsel, stsel);
  88. }
  89. if (e.getActionCommand().compareTo("Paste") == 0) {
  90. // System.out.println("Trying to Paste");
  91. int startRow = (jTable1.getSelectedRows())[0];
  92. int startCol = (jTable1.getSelectedColumns())[0];
  93. try {
  94. String trstring = (String) (system.getContents(this).getTransferData(DataFlavor.stringFlavor));
  95. // System.out.println("String is:" + trstring);
  96. StringTokenizer st1 = new StringTokenizer(trstring, "\n");
  97. for (int i = 0; st1.hasMoreTokens(); i++) {
  98. rowstring = st1.nextToken();
  99. StringTokenizer st2 = new StringTokenizer(rowstring, "\t");
  100. for (int j = 0; st2.hasMoreTokens(); j++) {
  101. value = (String) st2.nextToken();
  102. if ((startRow + i < jTable1.getRowCount()) && (startCol + j < jTable1.getColumnCount())) {
  103. jTable1.setValueAt(value, startRow + i, startCol + j);
  104. }
  105. // System.out.println("Putting " + value + "atrow=" + startRow + i + "column=" + startCol + j);
  106. }
  107. }
  108. } catch (Exception ex) {
  109. ex.printStackTrace();
  110. }
  111. }
  112. }
  113. }
  114. //~ Formatted by Jindent --- http://www.jindent.com