1 package com.explosion.datastream.exql.gui.table; 2 3 import java.awt.datatransfer.DataFlavor; 4 import java.awt.datatransfer.Transferable; 5 import java.awt.datatransfer.UnsupportedFlavorException; 6 import java.io.IOException; 7 import java.io.Serializable; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import com.explosion.datastream.exql.gui.table.editandrender.EditorAndRenderFactory; 12 import com.explosion.expfmodules.rdbmsconn.dbom.DBEntityColumn; 13 14 15 public class RowsTransferable implements Transferable, Serializable { 16 17 private static final long serialVersionUID = 1; 18 19 private List columns = new ArrayList(); 20 private List rows = new ArrayList(); 21 22 public RowsTransferable(DsProTable table) 23 { 24 boolean allColumnsAdded = false; 25 for (int i=0;i<table.getSelectedRows().length;i++) 26 { 27 int rowId = table.getSelectedRows()[i]; 28 List row = new ArrayList(); 29 for (int t=0;t<table.getColumnCount();t++) 30 { 31 Object object = table.getValueAt(rowId,t); 32 DBEntityColumn entityColumn = table.getDBEntityColumn(t); 33 row.add(object); 34 if (!allColumnsAdded) 35 columns.add(entityColumn); 36 } 37 rows.add(row); 38 allColumnsAdded = true; 39 } 40 } 41 42 43 44 /*** 45 * Returns a boolean indicating whether the data in the target column is compatible with the 46 * type in the numbered column 47 * @param target 48 * @param columnNumber 49 * @return 50 */ 51 public boolean isCompatibleType(DBEntityColumn target, int columnNumber) 52 { 53 return target.getType() == getColumn(columnNumber).getType(); 54 } 55 56 /*** 57 * Returns the DBentity column for this number 58 * @param columnNumber 59 * @return 60 */ 61 public DBEntityColumn getColumn(int columnNumber) 62 { 63 if (columns.size()<=columnNumber) 64 return (DBEntityColumn) columns.get(columnNumber); 65 else 66 return null; 67 } 68 69 /*** 70 * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor) 71 */ 72 public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException { 73 if (flavor.getRepresentationClass().equals(DsProTableDataFlavor.class)) 74 { 75 return this; 76 } 77 else if (flavor.isFlavorTextType()) 78 { 79 return this.toString(); 80 } 81 else 82 { 83 return this.toString(); 84 } 85 } 86 87 /*** 88 * Returns the rows of data 89 * @return 90 */ 91 public List getRows() 92 { 93 return rows; 94 } 95 96 public int getColumnCount() 97 { 98 return this.columns.size(); 99 } 100 101 /*** 102 * @see java.awt.datatransfer.Transferable#getTransferDataFlavors() 103 */ 104 public DataFlavor[] getTransferDataFlavors() { 105 return new DataFlavor[] {DsProTableDataFlavor.dsproTableDataFlavor()}; 106 } 107 108 /*** 109 * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor) 110 */ 111 public boolean isDataFlavorSupported(DataFlavor flavor) { 112 if (flavor.getRepresentationClass().equals(DsProTableDataFlavor.class)) 113 return true; 114 else if (flavor.isFlavorTextType()) 115 return true; 116 else 117 return false; 118 } 119 120 /*** 121 * Outputs the results as a String 122 * @see java.lang.Object#toString() 123 */ 124 public String toString() 125 { 126 StringBuffer buf = new StringBuffer(); 127 for (int i=0;i<rows.size();i++) 128 { 129 List values = (ArrayList) rows.get(i); 130 for (int t=0;t<columns.size();t++) 131 { 132 Object object = values.get(t); 133 DBEntityColumn entityColumn = (DBEntityColumn) columns.get(t); 134 buf.append(object == null ? "NULL\t" : EditorAndRenderFactory.renderValueForColumn(object,entityColumn) + "\t"); 135 } 136 buf.append(System.getProperty("line.separator")); 137 } 138 return buf.toString(); 139 } 140 } 141 142 143 144