1 package net.suberic.pooka.gui.dnd; 17 18 import java.awt.datatransfer.Clipboard ; 19 import java.awt.datatransfer.DataFlavor ; 20 import java.awt.datatransfer.Transferable ; 21 import java.awt.event.InputEvent ; 22 import java.lang.reflect.InvocationTargetException ; 23 import java.lang.reflect.Method ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.swing.Icon ; 29 import javax.swing.JComponent ; 30 import javax.swing.TransferHandler ; 31 32 33 64 public class MultipleTransferHandler extends TransferHandler { 65 66 private List transferHandlers = new ArrayList (); 67 68 private TransferHandler dragSourceHandler; 69 70 74 public void addTransferHandler(TransferHandler handler) { 75 if (!transferHandlers.contains(handler)) { 76 transferHandlers.add(handler); 77 } 78 if (dragSourceHandler == null) { 79 dragSourceHandler = handler; 80 } 81 } 82 83 87 public void removeTransferHandler(TransferHandler handler) { 88 transferHandlers.remove(handler); 89 if (handler == dragSourceHandler) { 90 if (transferHandlers.size() > 0) { 91 dragSourceHandler = (TransferHandler ) transferHandlers.get(0); 92 } else { 93 dragSourceHandler = null; 94 } 95 } 96 } 97 98 102 public List getTransferHandlers() { 103 return (List ) ((ArrayList ) transferHandlers).clone(); 104 } 105 106 111 public void setDragSourceTransferHandler(TransferHandler newDragHandler) { 112 dragSourceHandler = newDragHandler; 113 } 114 115 119 public TransferHandler getDragSourceTransferHandler() { 120 if (dragSourceHandler == null) { 121 if (transferHandlers.size() == 0) { 122 throw new IllegalStateException ("The multiple transfer handler must have at least one TransferHandler."); 123 } 124 dragSourceHandler = (TransferHandler ) transferHandlers.get(0); 125 } 126 return dragSourceHandler; 127 } 128 129 131 132 public boolean canImport(JComponent comp, DataFlavor [] transferFlavors) { 133 boolean canImport = false; 134 for (Iterator iterator = transferHandlers.iterator(); (iterator.hasNext()) && (!canImport);) { 135 TransferHandler handler = (TransferHandler ) iterator.next(); 136 canImport = handler.canImport(comp, transferFlavors); 137 } 138 return canImport; 139 } 140 141 142 public boolean importData(JComponent comp, Transferable t) { 143 boolean wasImported = false; 144 145 for (Iterator iterator = transferHandlers.iterator(); (iterator.hasNext()) && (!wasImported);) { 146 TransferHandler handler = (TransferHandler ) iterator.next(); 147 if (handler.canImport(comp, t.getTransferDataFlavors())) { 148 wasImported = handler.importData(comp, t); 149 } 150 } 151 return wasImported; 152 } 153 154 156 157 public int getSourceActions(JComponent c) { 158 return getDragSourceTransferHandler().getSourceActions(c); 159 } 160 161 162 public void exportAsDrag(JComponent comp, InputEvent e, int action) { 163 getDragSourceTransferHandler().exportAsDrag(comp, e, action); 164 } 165 166 167 public void exportToClipboard(JComponent comp, Clipboard clip, int action) { 168 getDragSourceTransferHandler().exportToClipboard(comp, clip, action); 169 } 170 171 172 public Icon getVisualRepresentation(Transferable t) { 173 return getDragSourceTransferHandler().getVisualRepresentation(t); 174 } 175 176 177 protected Transferable createTransferable(JComponent c) { 178 return (Transferable ) ignoreProtectedAccess("createTransferable", 179 new Class [] {JComponent .class}, 180 new Object []{c}); 181 } 182 183 184 protected void exportDone(JComponent source, Transferable data, int action) { 185 ignoreProtectedAccess("exportDone", 186 new Class [] {JComponent .class, Transferable .class, int.class}, 187 new Object []{source, data, new Integer (action)}); 188 } 189 190 205 private Object ignoreProtectedAccess(String methodName, Class [] methodArguments, Object [] arguments) { 206 Object object = null; 207 try { 208 Method method = TransferHandler .class.getDeclaredMethod(methodName, methodArguments); 209 method.setAccessible(true); 210 object = (Transferable ) method.invoke(getDragSourceTransferHandler(), arguments); 211 } catch (SecurityException se) { 212 } catch (NoSuchMethodException nsme) { 213 } catch (IllegalArgumentException iae) { 214 } catch (IllegalAccessException iace) { 215 } catch (InvocationTargetException ite) { 216 } 217 return object; 218 } 219 } 220 | Popular Tags |