1 package SnowMailClient.view; 2 3 6 7 import snow.utils.gui.VectorTransferable; 8 9 import java.util.*; 10 import java.io.*; 11 import java.awt.*; 12 import java.awt.event.*; 13 import java.awt.datatransfer.*; 14 import java.awt.dnd.*; 15 import javax.swing.*; 16 import javax.swing.table.*; 17 import javax.swing.tree.*; 18 19 23 public final class MailDnDTransferHandler extends TransferHandler 24 { 25 final String id; 26 final FolderView transferHandlerSource; 27 28 Object source = null; 29 int[] indices = null; 30 31 public MailDnDTransferHandler(String id, FolderView transferHandlerSource) 32 { 33 this.transferHandlerSource = transferHandlerSource; 34 this.id = id; 35 } 36 37 39 @SuppressWarnings ("unchecked") 40 public boolean importData(JComponent c, Transferable t) 41 { 42 System.out.println("Import data on "+c); 43 44 if(c instanceof JTable) return false; 45 46 Vector<Object > transferedVector = null; 47 if (!canImport(c, t.getTransferDataFlavors())) 48 { 49 return false; 50 } 51 52 try 53 { 54 55 if (hasLocalVectorFlavor(t.getTransferDataFlavors())) 56 { 57 transferedVector = (Vector) t.getTransferData(VectorTransferable.localVectorFlavor); 58 } 59 else if (hasSerialVectorFlavor(t.getTransferDataFlavors())) 60 { 61 transferedVector = (Vector) t.getTransferData(VectorTransferable.serialVectorFlavor); 62 } 63 else 64 { 65 return false; 66 } 67 68 } catch (UnsupportedFlavorException ufe) { 69 System.out.println("importData: unsupported data flavor"); 70 return false; 71 } catch (IOException ioe) { 72 System.out.println("importData: I/O exception"); 73 return false; 74 } 75 76 77 if(c instanceof JTree) 78 { 79 return importData_to_JTree((JTree) c, transferedVector); 80 } 81 82 return false; 83 } 84 85 private boolean importData_to_JTree(JTree target, Vector<Object > transferedVector) 86 { 87 System.out.println("Import data to JTree"); 88 System.out.println("model = "+target.getModel().getClass().getName()); 89 90 System.out.println(""+this.transferHandlerSource.getSelectedMessages().length+" messages"); 91 92 98 99 return true; 100 } 101 102 103 104 106 protected final void exportDone(JComponent c, Transferable data, int action) 107 { 108 109 if ((action == MOVE) && (indices != null)) 110 { 111 if(source instanceof JTable) 112 { 113 JTable table = (JTable) source; 114 } 115 else if(source instanceof JTree) 116 { 117 JTree tree = (JTree) source; 118 } 119 else 120 { 121 System.out.println("not recognized source "+source); 122 } 123 124 } 125 indices = null; 126 } 127 128 private boolean hasLocalVectorFlavor(DataFlavor[] flavors) { 129 if (VectorTransferable.localVectorFlavor == null) { 130 return false; 131 } 132 133 for (int i = 0; i < flavors.length; i++) { 134 if (flavors[i].equals(VectorTransferable.localVectorFlavor)) { 135 return true; 136 } 137 } 138 return false; 139 } 140 141 private boolean hasSerialVectorFlavor(DataFlavor[] flavors) { 142 if (VectorTransferable.serialVectorFlavor == null) { 143 return false; 144 } 145 146 for (int i = 0; i < flavors.length; i++) 147 { 148 if (flavors[i].equals(VectorTransferable.serialVectorFlavor)) 149 { 150 return true; 151 } 152 } 153 return false; 154 } 155 156 158 public boolean canImport(JComponent c, DataFlavor[] flavors) 159 { 160 if(c instanceof JTable) return false; 162 if (hasLocalVectorFlavor(flavors)) { return true; } 163 if (hasSerialVectorFlavor(flavors)) { return true; } 164 return false; 165 } 166 167 169 protected Transferable createTransferable(JComponent c) 170 { 171 if (c instanceof JTable) 172 { 173 JTable table = (JTable) c; 174 source = table; 175 indices = table.getSelectedRows(); 176 if(indices.length==0) return null; 177 178 Vector<Object > v = new Vector<Object >(); 179 v.addElement(id); 180 v.addElement("JTable"); 181 v.addElement("selectedRows"); 182 v.addElement(indices); 183 v.addElement(this); return new VectorTransferable(v); 185 } 186 187 else if (c instanceof JTree) 188 { 189 JTree tree = (JTree) c; 190 194 Vector<Object > v = new Vector<Object >(); 195 v.addElement(id); 196 v.addElement("JTree"); 197 v.addElement("selectionRows"); 198 v.addElement(tree.getSelectionRows()); 199 return new VectorTransferable(v); 200 } 201 else 202 { 203 204 return null; 205 } 206 } 207 208 public int getSourceActions(JComponent c) 209 { 210 return COPY_OR_MOVE; 211 } 212 213 214 217 public static void main( String [] arguments ) 218 { 219 } 222 } | Popular Tags |