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

  1. /*
  2.  * @(#)Cursor.java    1.13 98/03/18
  3.  *
  4.  * Copyright 1996, 1997 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. package java.awt;
  15.  
  16. import java.awt.AWTException;
  17. import java.awt.Point;
  18. import java.awt.Toolkit;
  19.  
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22.  
  23. import java.util.Enumeration;
  24. import java.util.Hashtable;
  25. import java.util.Properties;
  26. import java.util.StringTokenizer;
  27.  
  28. import java.security.AccessController;
  29.  
  30. /**
  31.  * A class to encapsulate the bitmap representation of the mouse cursor.
  32.  *
  33.  * @see Component#setCursor
  34.  * @version     1.13, 03/18/98
  35.  * @author     Amy Fowler
  36.  */
  37. public class Cursor implements java.io.Serializable {
  38.  
  39.     /**
  40.      * The default cursor type (gets set if no cursor is defined).
  41.      */
  42.     public static final int    DEFAULT_CURSOR           = 0;
  43.  
  44.     /**
  45.      * The crosshair cursor type.
  46.      */
  47.     public static final int    CROSSHAIR_CURSOR         = 1;
  48.  
  49.     /**
  50.      * The text cursor type.
  51.      */
  52.     public static final int    TEXT_CURSOR              = 2;
  53.  
  54.     /**
  55.      * The wait cursor type.
  56.      */
  57.     public static final int    WAIT_CURSOR             = 3;
  58.  
  59.     /**
  60.      * The south-west-resize cursor type.
  61.      */
  62.     public static final int    SW_RESIZE_CURSOR         = 4;
  63.  
  64.     /**
  65.      * The south-east-resize cursor type.
  66.      */
  67.     public static final int    SE_RESIZE_CURSOR         = 5;
  68.  
  69.     /**
  70.      * The north-west-resize cursor type.
  71.      */
  72.     public static final int    NW_RESIZE_CURSOR        = 6;
  73.  
  74.     /**
  75.      * The north-east-resize cursor type.
  76.      */
  77.     public static final int    NE_RESIZE_CURSOR         = 7;
  78.  
  79.     /**
  80.      * The north-resize cursor type.
  81.      */
  82.     public static final int    N_RESIZE_CURSOR         = 8;
  83.  
  84.     /**
  85.      * The south-resize cursor type.
  86.      */
  87.     public static final int    S_RESIZE_CURSOR         = 9;
  88.  
  89.     /**
  90.      * The west-resize cursor type.
  91.      */
  92.     public static final int    W_RESIZE_CURSOR             = 10;
  93.  
  94.     /**
  95.      * The east-resize cursor type.
  96.      */
  97.     public static final int    E_RESIZE_CURSOR            = 11;
  98.  
  99.     /**
  100.      * The hand cursor type.
  101.      */
  102.     public static final int    HAND_CURSOR            = 12;
  103.  
  104.     /**
  105.      * The move cursor type.
  106.      */
  107.     public static final int    MOVE_CURSOR            = 13;
  108.  
  109.     /**
  110.      * The platform default drag cursor type.
  111.      */
  112.     public static final int    DEFAULT_DRAG_CURSOR        = 14;
  113.  
  114.     /**
  115.      * The platform default no drop cursor type.
  116.      */
  117.     public static final int    DEFAULT_NODROP_CURSOR        = 15;
  118.  
  119.     /**
  120.      * The  platform default drop cursor type.
  121.      */
  122.     public static final int    DEFAULT_DROP_CURSOR        = 16;
  123.  
  124.     protected static Cursor predefined[] = new Cursor[17];
  125.  
  126.     /* Localization names and default values */
  127.     static final String[][] cursorProperties = {
  128.         { "AWT.DefaultCursor", "Default Cursor" },
  129.         { "AWT.CrosshairCursor", "Crosshair Cursor" },
  130.         { "AWT.TextCursor", "Text Cursor" },
  131.         { "AWT.WaitCursor", "Wait Cursor" },
  132.         { "AWT.SWResizeCursor", "Southwest Resize Cursor" },
  133.         { "AWT.SEResizeCursor", "Southeast Resize Cursor" },
  134.         { "AWT.NWResizeCursor", "Northwest Resize Cursor" },
  135.         { "AWT.NEResizeCursor", "Northeast Resize Cursor" },
  136.         { "AWT.NResizeCursor", "North Resize Cursor" },
  137.         { "AWT.SResizeCursor", "South Resize Cursor" },
  138.         { "AWT.WResizeCursor", "West Resize Cursor" },
  139.         { "AWT.EResizeCursor", "East Resize Cursor" },
  140.         { "AWT.HandCursor", "Hand Cursor" },
  141.         { "AWT.MoveCursor", "Move Cursor" },
  142.         { "AWT.DefaultDragCursor", "Default Drag Cursor" },
  143.         { "AWT.DefaultNoDropCursor", "Default No Drop Cursor" },
  144.         { "AWT.DefautlDropCursor", "Default Drop Cursor" }
  145.     };
  146.  
  147.     int type = DEFAULT_CURSOR;
  148.  
  149.     /**
  150.      * The type associated with all custom cursors.
  151.      */
  152.     public static final int    CUSTOM_CURSOR            = -1;
  153.  
  154.     /*
  155.      * hashtable, filesystem dir prefix, filename, and properties for custom cursors support
  156.      */
  157.  
  158.     private static final Hashtable  systemCustomCursors         = new Hashtable(1);
  159.     private static final String systemCustomCursorDirPrefix = initCursorDir();
  160.  
  161.     private static String initCursorDir() {
  162.     try {
  163.         java.security.AccessController.beginPrivileged();
  164.         return System.getProperty("java.home") + 
  165.         File.separator + "lib" + File.separator + "images" +
  166.         File.separator + "cursors" + File.separator;
  167.     } finally{
  168.         java.security.AccessController.endPrivileged();
  169.     }
  170.     }
  171.  
  172.     private static final String     systemCustomCursorPropertiesFile = systemCustomCursorDirPrefix + "cursors.properties";
  173.  
  174.     private static       Properties systemCustomCursorProperties = null;
  175.  
  176.     private static final String CursorDotPrefix  = "Cursor.";
  177.     private static final String DotFileSuffix    = ".File";
  178.     private static final String DotHotspotSuffix = ".HotSpot";
  179.     private static final String DotNameSuffix    = ".Name";
  180.  
  181.      /*
  182.       * JDK 1.1 serialVersionUID 
  183.       */
  184.     private static final long serialVersionUID = 8028237497568985504L;
  185.  
  186.     protected String name;
  187.  
  188.     /**
  189.      * Returns a cursor object with the specified predefined type.
  190.      * @param type the type of predefined cursor
  191.      */
  192.     static public Cursor getPredefinedCursor(int type) {
  193.     if (type < Cursor.DEFAULT_CURSOR || type > Cursor.DEFAULT_DROP_CURSOR) {
  194.         throw new IllegalArgumentException("illegal cursor type");
  195.     }
  196.     if (predefined[type] == null) {
  197.         predefined[type] = new Cursor(type);
  198.     }
  199.     return predefined[type];
  200.     }
  201.  
  202.     /**
  203.      * @return the system specific custom Cursor named
  204.      *
  205.      * Cursor names are, for example: "Invalid.16x16"
  206.      */
  207.  
  208.     static public Cursor getSystemCustomCursor(String name) throws AWTException {
  209.     Cursor cursor = (Cursor)systemCustomCursors.get(name);
  210.  
  211.     if (cursor == null) {
  212.         synchronized(systemCustomCursors) {
  213.         if (systemCustomCursorProperties == null)
  214.             loadSystemCustomCursorProperties();
  215.         }
  216.  
  217.         String prefix = CursorDotPrefix + name;
  218.         String key    = prefix + DotFileSuffix;
  219.  
  220.         if (!systemCustomCursorProperties.containsKey(key)) return null;
  221.  
  222.         String fileName  = systemCustomCursorProperties.getProperty(key);
  223.  
  224.         String localized = (String)systemCustomCursorProperties.getProperty(prefix + DotNameSuffix);
  225.  
  226.         if (localized == null) localized = name;
  227.  
  228.         String hotspot = (String)systemCustomCursorProperties.getProperty(prefix + DotHotspotSuffix);
  229.  
  230.         if (hotspot == null)
  231.             throw new AWTException("no hotspot property defined for cursor: " + name);
  232.  
  233.         StringTokenizer st = new StringTokenizer(hotspot, ",");
  234.  
  235.         if (st.countTokens() != 2)
  236.             throw new AWTException("failed to parse hotspot property for cursor: " + name);
  237.  
  238.         int x = 0;
  239.         int y = 0;
  240.  
  241.         try {
  242.         x = Integer.parseInt(st.nextToken());
  243.         y = Integer.parseInt(st.nextToken());
  244.         } catch (NumberFormatException nfe) {
  245.             throw new AWTException("failed to parse hotspot property for cursor: " + name);
  246.         }
  247.  
  248.         try {
  249.             Toolkit toolkit = Toolkit.getDefaultToolkit();
  250.  
  251.         Image   image   = toolkit.getImage(systemCustomCursorDirPrefix + fileName);
  252.  
  253.         cursor = toolkit.createCustomCursor(image, new Point(x,y), localized);
  254.         } catch (Exception e) {
  255.         throw new AWTException("Exception: " + e.getClass() + " " + e.getMessage() + " occurred while creating cursor " + name);
  256.         }
  257.  
  258.         systemCustomCursors.put(name, cursor);
  259.     }
  260.  
  261.     return cursor;
  262.     }
  263.  
  264.     /**
  265.      * Return the system default cursor.
  266.      */
  267.     static public Cursor getDefaultCursor() {
  268.         return getPredefinedCursor(Cursor.DEFAULT_CURSOR);
  269.     }
  270.  
  271.     /**
  272.      * Creates a new cursor object with the specified type.
  273.      * @param type the type of cursor
  274.      */
  275.     public Cursor(int type) {
  276.     if (type < Cursor.DEFAULT_CURSOR || type > Cursor.DEFAULT_DROP_CURSOR) {
  277.         throw new IllegalArgumentException("illegal cursor type");
  278.     }
  279.     this.type = type;
  280.  
  281.         // Lookup localized name.
  282.         name = Toolkit.getDefaultToolkit().getProperty(cursorProperties[type][0],
  283.                                                        cursorProperties[type][1]);
  284.     }
  285.  
  286.     /**
  287.      * Creates a new custom cursor object with the specified name.<p>
  288.      * Note:  this constructor should only be used by AWT implementations
  289.      * as part of their support for custom cursors.  Applications should
  290.      * use Toolkit.createCustomCursor().
  291.      * @param name the user-visible name of the cursor.
  292.      * @see java.awt.Toolkit#createCustomCursor
  293.      */
  294.     protected Cursor(String name) {
  295.         this.type = Cursor.CUSTOM_CURSOR;
  296.         this.name = name;
  297.     }
  298.  
  299.     /**
  300.      * Returns the type for this cursor.
  301.      */
  302.     public int getType() {
  303.     return type;
  304.     }    
  305.  
  306.     /**
  307.      * Returns the name of this cursor.
  308.      * @return    a localized description of this cursor.
  309.      * @since     JDK1.2
  310.      */
  311.     public String getName() {
  312.     return name;
  313.     }    
  314.  
  315.     /**
  316.      * Returns a string representation of this cursor.
  317.      * @return    a string representation of this cursor.
  318.      * @since     JDK1.2
  319.      */
  320.     public String toString() {
  321.     return getClass().getName() + "[" + getName() + "]";
  322.     }
  323.  
  324.     /*
  325.      * load the cursor.properties file
  326.      */
  327.  
  328.     private static void loadSystemCustomCursorProperties() throws AWTException {
  329.     synchronized(systemCustomCursors) {
  330.         systemCustomCursorProperties = new Properties();
  331.  
  332.             try {
  333.             java.security.AccessController.beginPrivileged();
  334.     
  335.              try {
  336.                 FileInputStream fis = new FileInputStream(systemCustomCursorPropertiesFile);
  337.  
  338.                 systemCustomCursorProperties.load(fis);
  339.  
  340.                 fis.close();
  341.             } catch (Exception e) {
  342.             systemCustomCursorProperties = null;
  343.  
  344.                throw new AWTException("Exception: " + e.getClass() + " " + e.getMessage() + " occurred while loading: " + systemCustomCursorPropertiesFile);
  345.  
  346.         }
  347.         } finally {
  348.             java.security.AccessController.endPrivileged();
  349.         }
  350.         }
  351.     }
  352. }
  353.