1 7 8 package java.awt.dnd; 9 10 import java.awt.Component ; 11 12 import java.awt.datatransfer.DataFlavor ; 13 import java.awt.datatransfer.Transferable ; 14 import java.awt.datatransfer.UnsupportedFlavorException ; 15 16 import java.awt.dnd.peer.DropTargetContextPeer; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 21 import java.util.Arrays ; 22 import java.util.List ; 23 24 25 39 40 public class DropTargetContext implements Serializable { 41 42 private static final long serialVersionUID = -634158968993743371L; 43 44 50 51 DropTargetContext(DropTarget dt) { 52 super(); 53 54 dropTarget = dt; 55 } 56 57 63 64 public DropTarget getDropTarget() { return dropTarget; } 65 66 72 73 public Component getComponent() { return dropTarget.getComponent(); } 74 75 80 81 public void addNotify(DropTargetContextPeer dtcp) { 82 dropTargetContextPeer = dtcp; 83 } 84 85 88 89 public void removeNotify() { 90 dropTargetContextPeer = null; 91 transferable = null; 92 } 93 94 100 101 protected void setTargetActions(int actions) { 102 DropTargetContextPeer peer = getDropTargetContextPeer(); 103 if (peer != null) { 104 synchronized (peer) { 105 peer.setTargetActions(actions); 106 getDropTarget().doSetDefaultActions(actions); 107 } 108 } else { 109 getDropTarget().doSetDefaultActions(actions); 110 } 111 } 112 113 119 120 protected int getTargetActions() { 121 DropTargetContextPeer peer = getDropTargetContextPeer(); 122 return ((peer != null) 123 ? peer.getTargetActions() 124 : dropTarget.getDefaultActions() 125 ); 126 } 127 128 136 137 public void dropComplete(boolean success) throws InvalidDnDOperationException { 138 DropTargetContextPeer peer = getDropTargetContextPeer(); 139 if (peer != null) { 140 peer.dropComplete(success); 141 } 142 } 143 144 149 150 protected void acceptDrag(int dragOperation) { 151 DropTargetContextPeer peer = getDropTargetContextPeer(); 152 if (peer != null) { 153 peer.acceptDrag(dragOperation); 154 } 155 } 156 157 160 161 protected void rejectDrag() { 162 DropTargetContextPeer peer = getDropTargetContextPeer(); 163 if (peer != null) { 164 peer.rejectDrag(); 165 } 166 } 167 168 175 176 protected void acceptDrop(int dropOperation) { 177 DropTargetContextPeer peer = getDropTargetContextPeer(); 178 if (peer != null) { 179 peer.acceptDrop(dropOperation); 180 } 181 } 182 183 187 188 protected void rejectDrop() { 189 DropTargetContextPeer peer = getDropTargetContextPeer(); 190 if (peer != null) { 191 peer.rejectDrop(); 192 } 193 } 194 195 203 204 protected DataFlavor [] getCurrentDataFlavors() { 205 DropTargetContextPeer peer = getDropTargetContextPeer(); 206 return peer != null ? peer.getTransferDataFlavors() : new DataFlavor [0]; 207 } 208 209 217 218 protected List <DataFlavor > getCurrentDataFlavorsAsList() { 219 return Arrays.asList(getCurrentDataFlavors()); 220 } 221 222 231 232 protected boolean isDataFlavorSupported(DataFlavor df) { 233 return getCurrentDataFlavorsAsList().contains(df); 234 } 235 236 243 244 protected Transferable getTransferable() throws InvalidDnDOperationException { 245 DropTargetContextPeer peer = getDropTargetContextPeer(); 246 if (peer == null) { 247 throw new InvalidDnDOperationException (); 248 } else { 249 if (transferable == null) { 250 Transferable t = peer.getTransferable(); 251 boolean isLocal = peer.isTransferableJVMLocal(); 252 synchronized (this) { 253 if (transferable == null) { 254 transferable = createTransferableProxy(t, isLocal); 255 } 256 } 257 } 258 259 return transferable; 260 } 261 } 262 263 268 269 DropTargetContextPeer getDropTargetContextPeer() { 270 return dropTargetContextPeer; 271 } 272 273 282 protected Transferable createTransferableProxy(Transferable t, boolean local) { 283 return new TransferableProxy(t, local); 284 } 285 286 287 288 289 299 300 protected class TransferableProxy implements Transferable { 301 302 313 TransferableProxy(Transferable t, boolean local) { 314 proxy = new sun.awt.datatransfer.TransferableProxy(t, local); 315 transferable = t; 316 isLocal = local; 317 } 318 319 326 public DataFlavor [] getTransferDataFlavors() { 327 return proxy.getTransferDataFlavors(); 328 } 329 330 337 public boolean isDataFlavorSupported(DataFlavor flavor) { 338 return proxy.isDataFlavorSupported(flavor); 339 } 340 341 356 public Object getTransferData(DataFlavor df) 357 throws UnsupportedFlavorException , IOException 358 { 359 return proxy.getTransferData(df); 360 } 361 362 365 366 372 375 protected Transferable transferable; 376 377 382 protected boolean isLocal; 383 384 private sun.awt.datatransfer.TransferableProxy proxy; 385 } 386 387 388 389 392 393 398 private DropTarget dropTarget; 399 400 private transient DropTargetContextPeer dropTargetContextPeer; 401 402 private transient Transferable transferable; 403 } 404 | Popular Tags |