home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / dnd / DropTargetContext.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.2 KB  |  291 lines

  1. /*
  2.  * @(#)DropTargetContext.java    1.11 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt.dnd;
  16.  
  17. import java.awt.Component;
  18. import java.awt.datatransfer.DataFlavor;
  19. import java.awt.datatransfer.Transferable;
  20. import java.awt.datatransfer.UnsupportedFlavorException;
  21. import java.awt.Component;
  22.  
  23. import java.awt.dnd.DnDConstants;
  24. import java.awt.dnd.DropTarget;
  25. import java.awt.dnd.InvalidDnDOperationException;
  26.  
  27. import java.awt.dnd.peer.DropTargetContextPeer;
  28.  
  29. import java.io.ByteArrayInputStream;
  30. import java.io.ByteArrayOutputStream;
  31. import java.io.DataInputStream;
  32. import java.io.InputStream;
  33. import java.io.IOException;
  34. import java.io.ObjectInputStream;
  35. import java.io.ObjectOutputStream;
  36. import java.io.Serializable;
  37.  
  38. /**
  39.  * <p>
  40.  * A DropTargetContext is created whenever the logical cursor associated
  41.  * with a Drag and Drop operation coincides with the visibel geometry of
  42.  * a Component, with an associated DropTarget.
  43.  * The DropTargetContext provides the mechanism for a potential receiver
  44.  * of a Drop operation to both provide the end userr with the appropriate
  45.  * drag under feedback, but also to effect the subsequent data transfer
  46.  * if appropriate.
  47.  * </p>
  48.  *
  49.  * @version 1.8
  50.  * @since JDK1.2
  51.  *
  52.  */
  53.  
  54. public class DropTargetContext {
  55.  
  56.     /**
  57.      * Construct a DropTargetContext
  58.      */
  59.  
  60.     DropTargetContext(DropTarget dt) {
  61.     super();
  62.  
  63.     dropTarget = dt;
  64.     }
  65.  
  66.     /**
  67.      * @return the DropTarget associated with this Context
  68.      */
  69.  
  70.     public DropTarget getDropTarget() { return dropTarget; }
  71.  
  72.     /**
  73.      * @return the Component associated with this Context
  74.      */
  75.  
  76.     public Component getComponent() { return dropTarget.getComponent(); }
  77.  
  78.     /**
  79.      * called when associated with the DropTargetContextPeer
  80.      */
  81.  
  82.     public synchronized void addNotify(DropTargetContextPeer dtcp) {
  83.     dropTargetContextPeer = dtcp;
  84.     }
  85.  
  86.     /**
  87.      * called when disassociated with the DropTargetContextPeer
  88.      */
  89.  
  90.     public synchronized void removeNotify() {
  91.     dropTargetContextPeer = null;
  92.     transferable          = null;
  93.     }
  94.  
  95.     /**
  96.      * set the current actions acceptable to this DropTarget
  97.      */
  98.  
  99.     protected void setTargetActions(int actions) {
  100.     if (dropTargetContextPeer != null) dropTargetContextPeer.setTargetActions(actions);
  101.     }
  102.  
  103.     /**
  104.      * @return the current actions acceptable to this DropTarget
  105.      */
  106.  
  107.     protected int getTargetActions() {
  108.     return ((dropTargetContextPeer != null)
  109.             ? dropTargetContextPeer.getTargetActions() 
  110.             : dropTarget.getDefaultActions()
  111.     );
  112.     }
  113.  
  114.     /**
  115.      * signal that the drop is completed and if it was successful or not
  116.      */
  117.  
  118.     public void dropComplete(boolean success) throws InvalidDnDOperationException{
  119.     if (dropTargetContextPeer != null)
  120.         dropTargetContextPeer.dropComplete(success);
  121.     }
  122.  
  123.     /**
  124.      * accept the Drag
  125.      */
  126.  
  127.     protected void acceptDrag(int dragOperation) {
  128.     if (dropTargetContextPeer != null) dropTargetContextPeer.acceptDrag(dragOperation);
  129.     }
  130.  
  131.     /**
  132.      * reject the Drag
  133.      */
  134.  
  135.     protected void rejectDrag() {
  136.     if (dropTargetContextPeer != null) dropTargetContextPeer.rejectDrag();
  137.     }
  138.  
  139.     /**
  140.      * called to signal that the drop is acceptable using the specified operation.
  141.      * must be called during DropTargetListener.drop method invocation.
  142.      */
  143.  
  144.     protected void acceptDrop(int dropOperation) {
  145.     if (dropTargetContextPeer != null) dropTargetContextPeer.acceptDrop(dropOperation);
  146.     }
  147.  
  148.     /**
  149.      * called to signal that the drop is unacceptable.
  150.      * must be called during DropTargetListener.drop method invocation.
  151.      */
  152.  
  153.     protected void rejectDrop() {
  154.     if (dropTargetContextPeer != null) dropTargetContextPeer.rejectDrop();
  155.     }
  156.  
  157.     /**
  158.      * get the available DataFlavors of the Transferable operand of this operation
  159.      */
  160.  
  161.    protected DataFlavor[] getCurrentDataFlavors() {
  162.     return dropTargetContextPeer != null ? dropTargetContextPeer.getTransferDataFlavors() : new DataFlavor[0];
  163.    }
  164.  
  165.     /**
  166.      * get the Transferable (proxy) operand of this operation
  167.      */
  168.  
  169.     protected synchronized Transferable getTransferable() throws InvalidDnDOperationException {
  170.     if (dropTargetContextPeer == null) {
  171.         throw new InvalidDnDOperationException();
  172.     } else {
  173.         if (transferable == null) {
  174.         transferable = createTransferableProxy(dropTargetContextPeer.getTransferable(), dropTargetContextPeer.isTransferableJVMLocal());
  175.         }
  176.  
  177.         return transferable;
  178.     }
  179.     }
  180.  
  181.     /**
  182.      * @return the platform peer
  183.      */
  184.  
  185.     DropTargetContextPeer getDropTargetContextPeer() { 
  186.     return dropTargetContextPeer;
  187.     }
  188.  
  189.     /**
  190.      * subclasses may override this to supply their own Proxy
  191.      */
  192.  
  193.     protected Transferable createTransferableProxy(Transferable t, boolean local) {
  194.     return new TransferableProxy(t, local);
  195.     }
  196.  
  197. /****************************************************************************/
  198.  
  199.     /*
  200.      * private nested class to provide Remote Transferable proxy ...
  201.      */
  202.  
  203.     protected class TransferableProxy implements Transferable {
  204.  
  205.     /**
  206.          * construct the proxy
  207.          */
  208.  
  209.      TransferableProxy(Transferable t, boolean local) {
  210.         transferable = t;
  211.         isLocal     = local;
  212.     }
  213.  
  214.     /** 
  215.          * get the flavors
  216.          */
  217.  
  218.     public synchronized DataFlavor[] getTransferDataFlavors() {
  219.         return transferable.getTransferDataFlavors();
  220.     }
  221.  
  222.     /**
  223.          * check if a particular flavor is supported?
  224.          */
  225.  
  226.     public synchronized boolean isDataFlavorSupported(DataFlavor flavor) {
  227.         DataFlavor[] flavors = getTransferDataFlavors();
  228.     
  229.         if (flavors != null && flavors.length != 0) {
  230.         for (int i = 0; i < flavors.length; i++)
  231.             if (flavors[i].equals(flavor)) return true;
  232.         }
  233.  
  234.         return false;
  235.     }
  236.  
  237.     /**
  238.          * get the transfer data
  239.          */
  240.  
  241.     public synchronized Object getTransferData(DataFlavor df) throws UnsupportedFlavorException, IOException {
  242.         if (!isDataFlavorSupported(df)) 
  243.         throw new UnsupportedFlavorException(df);
  244.         else {
  245.         Object data;
  246.         InputStream is = null;
  247.  
  248.         try {
  249.             data = transferable.getTransferData(df);
  250.         } catch (Exception e) {
  251.             throw new IOException(e.getClass() + ":" + e.getMessage() + " caught while getting Data");
  252.         }
  253.  
  254.         if (isLocal && df.isMimeTypeSerializedObject()) {
  255.             ByteArrayOutputStream baos = new ByteArrayOutputStream();
  256.  
  257.             (new ObjectOutputStream(baos)).writeObject(data);
  258.         
  259.             ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  260.             try {
  261.                 data = (new ObjectInputStream(bais)).readObject();
  262.             } catch (ClassNotFoundException cnfe) {
  263.                 throw new IOException(cnfe.getMessage());
  264.             }
  265.         }
  266.     
  267.             return data;
  268.         }
  269.     }
  270.  
  271.     /*
  272.      * fields
  273.      */
  274.  
  275.     protected Transferable    transferable;
  276.     protected boolean    isLocal;
  277.     }
  278.  
  279. /****************************************************************************/
  280.  
  281.     /*
  282.      * fields
  283.      */
  284.  
  285.     private DropTarget              dropTarget;
  286.  
  287.     private DropTargetContextPeer dropTargetContextPeer;
  288.  
  289.     private Transferable          transferable;
  290. }
  291.