home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 July & August / Pcwk78a98.iso / Internet / Javadraw / DATA.Z / DataFlavor.java < prev    next >
Text File  |  1997-08-30  |  7KB  |  215 lines

  1. /*
  2.  * @(#)DataFlavor.java    1.4 97/03/10
  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.InputStream;
  26.  
  27. /**
  28.  * Each instance represents the opaque concept of a data format as would
  29.  * appear on a clipboard, during drag and drop, or in a file system.
  30.  *
  31.  * @version     1.4, 03/10/97
  32.  * @author    Blake Sullivan
  33.  */
  34. public class DataFlavor {
  35.  
  36.    /**
  37.     * The DataFlavor representing a Java Unicode String class, where:
  38.     * <p>
  39.     * representationClass = java.lang.String<br>
  40.     * mimeType            = "application/x-java-serialized-object"        
  41.     * <p> 
  42.     */
  43.     public static DataFlavor stringFlavor;
  44.  
  45.    /**
  46.     * The DataFlavor representing plain text with unicode encoding, where:
  47.     * <p>
  48.     * representationClass = InputStream<br>
  49.     * mimeType            = "text/plain; charset=unicode"        
  50.     * <p> 
  51.     */
  52.     public static DataFlavor plainTextFlavor; 
  53.         
  54.     static final String serializedObjectMimeType = "application/x-java-serialized-object";
  55.     static Class ioInputStreamClass;
  56.  
  57.     static {
  58.     try {
  59.         stringFlavor = new DataFlavor(Class.forName("java.lang.String"), "Unicode String");
  60.         plainTextFlavor = new DataFlavor("text/plain; charset=unicode", "Plain Text");
  61.         ioInputStreamClass = Class.forName("java.io.InputStream");
  62.     } catch (ClassNotFoundException e) {
  63.  
  64.     }
  65.     } 
  66.  
  67.     /* placeholder for caching any platform-specific data for flavor */
  68.     int atom;      
  69.   
  70.    /**
  71.     * Mime type for this DataFlavor.  (See RFC 1521 for an explanation
  72.     * of Mime types)  The type name is stored internally in the following
  73.     * cannonical order to make comparisons simpler
  74.     *   1. type, subtype, and parameter names are converted to lowercase
  75.     *
  76.     *   2. parameters are ordered by parameter name
  77.     *
  78.     *   3. character set parameter names are converted to lowercase (they
  79.     *      are the exception to the rule that parameter names should be
  80.     *      case sensitive
  81.     *
  82.     *   4. White space is compressed
  83.     *
  84.     */
  85.     private String mimeType;
  86.     
  87.     /** Human-presentable name for this DataFlavor. Localizable. **/
  88.     private String humanPresentableName;  
  89.   
  90.     /** Java class of objects this DataFlavor represents **/
  91.     private Class representationClass;
  92.   
  93.    /**
  94.     * Construct a DataFlavor that represents a Java class
  95.     * <p>
  96.     * The returned DataFlavor will have the following characteristics
  97.     * <p>
  98.     * representationClass = representationClass<br>
  99.     * mimeType            = application/x-java-serialized-object        
  100.     * <p>
  101.     * @param representationClass the class used to transfer data in this flavor
  102.     * @param humanPresentableName the human-readible string used to identify this flavor
  103.     */
  104.     public DataFlavor(Class representationClass, String humanPresentableName) {
  105.     this.mimeType = serializedObjectMimeType;
  106.     this.representationClass = representationClass;
  107.           this.humanPresentableName = humanPresentableName;
  108.     }
  109.  
  110.    /**
  111.     * Construct a DataFlavor that represents a MimeType
  112.     * <p>
  113.     * The returned DataFlavor will have the following characteristics:
  114.     * <p>
  115.     * If the mimeType is
  116.     * "application/x-java-serialized-object; class=<representation class>",
  117.     * the result is the same as calling
  118.     * new DataFlavor(Class:forName(<representation class>) as above
  119.     * <p>
  120.     * otherwise:
  121.     * <p>
  122.     * representationClass = InputStream<br>
  123.     * mimeType            = mimeType         
  124.     * <p>
  125.     * @param mimeType the string used to identify the MIME type for this flavor
  126.     * @param humanPresentableName the human-readible string used to identify this flavor
  127.     */
  128.     public DataFlavor(String mimeType, String humanPresentableName) {
  129.     this.mimeType = mimeType;
  130.     this.representationClass = ioInputStreamClass;
  131.           this.humanPresentableName = humanPresentableName;
  132.     }       
  133.   
  134.    /**
  135.     * Returns the MIME type string for this DataFlavor
  136.     */ 
  137.     public String getMimeType() {
  138.         return mimeType;
  139.     }
  140.  
  141.    /**
  142.     *  Returns the Class which objects supporting this DataFlavor
  143.     *  will return when this DataFlavor is requested.
  144.     */
  145.     public Class getRepresentationClass() {
  146.         return representationClass;
  147.     }
  148.   
  149.    /**
  150.     * Returns the human presentable name for the data foramt that this
  151.     * DataFlavor represents.  This name would be localized for different
  152.     * countries
  153.     */
  154.     public String getHumanPresentableName() {
  155.         return humanPresentableName;
  156.     }
  157.   
  158.    /**
  159.     * Sets the human presentable name for the data format that this
  160.     * DataFlavor represents. This name would be localized for different
  161.     * countries
  162.     */
  163.     public void setHumanPresentableName(String humanPresentableName) {
  164.         humanPresentableName = humanPresentableName;
  165.     }
  166.  
  167.     public boolean equals(DataFlavor dataFlavor) {
  168.     return (isMimeTypeEqual(dataFlavor) &&
  169.          dataFlavor.getRepresentationClass() == representationClass);
  170.     }
  171.   
  172.     /**
  173.      * Is the string representation of the MIME type passed in equivalent
  174.      * to the MIME type of this DataFlavor.  This may involve adding default
  175.      * attributes for some MIME types (like adding charset=US-ASCII to
  176.      * text/plain MIME types that have no charset parameter specified)
  177.      */
  178.     public boolean isMimeTypeEqual(String mimeType) {
  179.     // This is too simplistic
  180.     return mimeType.equals(this.mimeType);
  181.     }
  182.   
  183.     /**
  184.      * Convenience function equivalent to calling:
  185.      * isMimeTypeEqual(dataFlavor.getMimeType());
  186.      */
  187.     public final boolean isMimeTypeEqual(DataFlavor dataFlavor) {
  188.     return isMimeTypeEqual(dataFlavor.getMimeType());
  189.     }
  190.     
  191.    /**
  192.     * Called on DataFlavor for every MIME Type parameter to allow DataFlavor
  193.     * subclasses to handle special parameters like the text/plain charset
  194.     * parameters, whose values are case insensitive.  (MIME type parameter
  195.     * values are supposed to be case sensitive.
  196.     * <p>
  197.     * This method is called for each parameter name/value pair and should
  198.     * return the normalized representation of the parameterValue
  199.     */
  200.     protected String normalizeMimeTypeParameter(String parameterName, String parameterValue) {
  201.     return parameterName+"="+parameterValue;    
  202.     }
  203.   
  204.    /**
  205.     * Called for each MIME type string to give DataFlavor subtypes the
  206.     * opportunity to change how the normalization of MIME types is accomplished.
  207.     * One possible use would be to add default parameter/value pairs in cases
  208.     * where none are present in the MIME type string passed in
  209.     */
  210.     protected String normalizeMimeType(String mimeType) {
  211.     return mimeType;    
  212.     }
  213. }
  214.  
  215.