FileDocCategorySizeDatePackage
Cursor.javaAPI DocAndroid 1.5 API12436Wed May 06 22:41:54 BST 2009java.awt

Cursor

public class Cursor extends Object implements Serializable
The Cursor class represents the bitmap of the mouse cursor.
since
Android 1.0

Fields Summary
private static final long
serialVersionUID
The Constant serialVersionUID.
public static final int
DEFAULT_CURSOR
The Constant DEFAULT_CURSOR indicates the default cursor type.
public static final int
CROSSHAIR_CURSOR
The Constant CROSSHAIR_CURSOR cursor type.
public static final int
TEXT_CURSOR
The Constant TEXT_CURSOR cursor type.
public static final int
WAIT_CURSOR
The Constant WAIT_CURSOR cursor type.
public static final int
SW_RESIZE_CURSOR
The Constant SW_RESIZE_CURSOR cursor type.
public static final int
SE_RESIZE_CURSOR
The Constant SE_RESIZE_CURSOR cursor type.
public static final int
NW_RESIZE_CURSOR
The Constant NW_RESIZE_CURSOR cursor type.
public static final int
NE_RESIZE_CURSOR
The Constant NE_RESIZE_CURSOR cursor type.
public static final int
N_RESIZE_CURSOR
The Constant N_RESIZE_CURSOR cursor type.
public static final int
S_RESIZE_CURSOR
The Constant S_RESIZE_CURSOR cursor type.
public static final int
W_RESIZE_CURSOR
The Constant W_RESIZE_CURSOR cursor type.
public static final int
E_RESIZE_CURSOR
The Constant E_RESIZE_CURSOR cursor type.
public static final int
HAND_CURSOR
The Constant HAND_CURSOR cursor type.
public static final int
MOVE_CURSOR
The Constant MOVE_CURSOR cursor type.
static Map
systemCustomCursors
A mapping from names to system custom cursors.
static Properties
cursorProps
The cursor props.
static final String[]
predefinedNames
The Constant predefinedNames.
protected static Cursor[]
predefined
The predefined set of cursors.
public static final int
CUSTOM_CURSOR
The Constant CUSTOM_CURSOR is associated with all custom cursor types. (Those which are not predefined)
protected String
name
The name of the cursor.
private final int
type
The type of the cursor, chosen from the list of cursor type constants.
private transient org.apache.harmony.awt.wtk.NativeCursor
nativeCursor
The native cursor.
private Point
hotSpot
The exact point on the cursor image that indicates which point the cursor is selecting (pointing to). The coordinates are given with respect the origin of the Image (its upper left corner).
private Image
image
The image to draw on the screen representing the cursor.
Constructors Summary
protected Cursor(String name)
Instantiates a new cursor with the specified name.

param
name the name of cursor.


                                   
       
        this(name, null, new Point());
    
public Cursor(int type)
Instantiates a new cursor of the specified type.

param
type the type of cursor.

        checkType(type);
        this.type = type;
        if ((type >= 0) && (type < predefinedNames.length)) {
            name = predefinedNames[type] + " Cursor"; //$NON-NLS-1$
        }
    
Cursor(String name, Image img, Point hotSpot)
Instantiates a new cursor.

param
name the name.
param
img the img.
param
hotSpot the hot spot.

        this.name = name;
        type = CUSTOM_CURSOR;
        this.hotSpot = hotSpot;
        image = img;
    
Methods Summary
static voidcheckType(int type)
Check type.

param
type the type.

        // can't use predefined array here because it may not have been
        // initialized yet
        if ((type < 0) || (type >= predefinedNames.length)) {
            // awt.143=illegal cursor type
            throw new IllegalArgumentException(Messages.getString("awt.143")); //$NON-NLS-1$
        }
    
protected voidfinalize()
Finalize method overrides the finalize method from Object class.

throws
Throwable if the native cursor is not null and throws a Throwable when destroyed.

        if (nativeCursor != null) {
            nativeCursor.destroyCursor();
        }
    
public static java.awt.CursorgetDefaultCursor()
Gets the default cursor.

return
the default cursor.

        return getPredefinedCursor(DEFAULT_CURSOR);
    
public java.lang.StringgetName()
Gets the name of the cursor.

return
the name of the cursor.

        return name;
    
org.apache.harmony.awt.wtk.NativeCursorgetNativeCursor()
Gets the native cursor.

return
the native cursor.

        if (nativeCursor != null) {
            return nativeCursor;
        }
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        if (type != CUSTOM_CURSOR) {
            nativeCursor = toolkit.createNativeCursor(type);
        } else {
            nativeCursor = toolkit.createCustomNativeCursor(image, hotSpot, name);
        }
        return nativeCursor;
    
public static java.awt.CursorgetPredefinedCursor(int type)
Gets the predefined cursor with the specified type.

param
type the type of cursor.
return
the predefined cursor with the specified type.

        checkType(type);
        Cursor cursor = predefined[type];
        if (cursor == null) {
            cursor = new Cursor(type);
            predefined[type] = cursor;
        }
        return cursor;
    
public static java.awt.CursorgetSystemCustomCursor(java.lang.String name)
Gets the specified system custom cursor.

param
name the name of the desired system cursor.
return
the specific system cursor with the specified name.
throws
AWTException if the desired cursor has malformed data such as an incorrectly defined hot spot.
throws
HeadlessException if the isHeadless method of the GraphicsEnvironment returns true.

        Toolkit.checkHeadless();
        return getSystemCustomCursorFromMap(name);
    
private static java.awt.CursorgetSystemCustomCursorFromMap(java.lang.String name)
Gets the specified system custom cursor from the map of system custom cursors.

param
name the name of the desired cursor.
return
the desired system custom cursor from the map of system custom cursors.
throws
AWTException the AWT exception.

        loadCursorProps();
        if (systemCustomCursors == null) {
            systemCustomCursors = new HashMap<String, Cursor>();
        }
        Cursor cursor = systemCustomCursors.get(name);
        if (cursor != null) {
            return cursor;
        }
        // awt.141=failed to parse hotspot property for cursor:
        String exMsg = Messages.getString("awt.141") + name; //$NON-NLS-1$
        String nm = "Cursor." + name; //$NON-NLS-1$
        String nameStr = cursorProps.getProperty(nm + ".Name"); //$NON-NLS-1$
        String hotSpotStr = cursorProps.getProperty(nm + ".HotSpot"); //$NON-NLS-1$
        String fileStr = cursorProps.getProperty(nm + ".File"); //$NON-NLS-1$
        int idx = hotSpotStr.indexOf(',");
        if (idx < 0) {
            throw new AWTException(exMsg);
        }
        int x, y;
        try {
            x = new Integer(hotSpotStr.substring(0, idx)).intValue();
            y = new Integer(hotSpotStr.substring(idx + 1, hotSpotStr.length())).intValue();
        } catch (NumberFormatException nfe) {
            throw new AWTException(exMsg);
        }
        Image img = Toolkit.getDefaultToolkit().createImage(fileStr);
        cursor = new Cursor(nameStr, img, new Point(x, y));
        systemCustomCursors.put(name, cursor);

        return cursor;
    
public intgetType()
Gets the cursor type.

return
the cursor type.

        return type;
    
private static voidloadCursorProps()
Load cursor props.

throws
AWTException the AWT exception.

        if (cursorProps != null) {
            return;
        }
        String sep = File.separator;
        String cursorsDir = "lib" + sep + "images" + sep + "cursors"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        String cursorsAbsDir = System.getProperty("java.home") + sep + //$NON-NLS-1$
                cursorsDir;
        String cursorPropsFileName = "cursors.properties"; //$NON-NLS-1$
        String cursorPropsFullFileName = (cursorsAbsDir + sep + cursorPropsFileName);
        cursorProps = new Properties();
        try {
            cursorProps.load(new FileInputStream(new File(cursorPropsFullFileName)));
        } catch (FileNotFoundException e) {
            // awt.142=Exception: class {0} {1} occurred while loading: {2}
            throw new AWTException(Messages.getString("awt.142",//$NON-NLS-1$
                    new Object[] {
                            e.getClass(), e.getMessage(), cursorPropsFullFileName
                    }));
        } catch (IOException e) {
            throw new AWTException(e.getMessage());
        }

    
voidsetNativeCursor(org.apache.harmony.awt.wtk.NativeCursor nativeCursor)
Sets the native cursor.

param
nativeCursor the new native cursor.

        this.nativeCursor = nativeCursor;
    
public java.lang.StringtoString()
Returns the String representation of the cursor.

return
the String representation of the cursor.

        return getClass().getName() + "[" + name + "]"; //$NON-NLS-1$ //$NON-NLS-2$