home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / StringSelection.java < prev    next >
Text File  |  1997-05-20  |  3KB  |  86 lines

  1. /*
  2.  * @(#)StringSelection.java    1.4 97/02/12
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.awt.datatransfer;
  24.  
  25. import java.io.*;
  26.  
  27. /**
  28.  * A class which implements the capability required to transfer a
  29.  * simple java String in plain text format.
  30.  */
  31. public class StringSelection implements Transferable, ClipboardOwner {
  32.  
  33.     final static int STRING = 0;
  34.     final static int PLAIN_TEXT = 1;
  35.  
  36.     DataFlavor flavors[] = {DataFlavor.stringFlavor, DataFlavor.plainTextFlavor};
  37.  
  38.     private String data;
  39.                            
  40.     /**
  41.      * Creates a transferable object capable of transferring the
  42.      * specified string in plain text format.
  43.      */
  44.     public StringSelection(String data) {
  45.         this.data = data;
  46.     }
  47.  
  48.     /**
  49.      * Returns the array of flavors in which it can provide the data.
  50.      */
  51.     public synchronized DataFlavor[] getTransferDataFlavors() {
  52.     return flavors;
  53.     }
  54.  
  55.     /**
  56.      * Returns whether the requested flavor is supported by this object.
  57.      * @param flavor the requested flavor for the data
  58.      */
  59.     public boolean isDataFlavorSupported(DataFlavor flavor) {
  60.     return (flavor.equals(flavors[STRING]) || flavor.equals(flavors[PLAIN_TEXT]));
  61.     }
  62.  
  63.     /**
  64.      * If the data was requested in the "java.lang.String" flavor, return the
  65.      * String representing the selection.
  66.      *
  67.      * @param flavor the requested flavor for the data
  68.      * @exception UnsupportedFlavorException if the requested data flavor is
  69.      *              not supported in the "<code>java.lang.String</code>" flavor.
  70.      */
  71.     public synchronized Object getTransferData(DataFlavor flavor) 
  72.             throws UnsupportedFlavorException, IOException {
  73.     if (flavor.equals(flavors[STRING])) {
  74.         return (Object)data;
  75.     } else if (flavor.equals(flavors[PLAIN_TEXT])) {
  76.         return new StringReader(data);
  77.     } else {
  78.         throw new UnsupportedFlavorException(flavor);
  79.     }
  80.     }
  81.  
  82.     public void lostOwnership(Clipboard clipboard, Transferable contents) {
  83.     }
  84. }
  85.     
  86.