jdk/src/share/classes/java/awt/Cursor.java
author anthony
Tue, 05 May 2009 17:56:31 +0400
changeset 3444 18840bd1c784
parent 2 90ce3da70b43
child 3445 39d00a6cac5e
permissions -rw-r--r--
6818787: It is possible to reposition the security icon too far from the border of the window on X11 Summary: The constraints for the position of the icon are moved to the shared code Reviewed-by: art, dcherepanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.AWTException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Point;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Toolkit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.FileInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.beans.ConstructorProperties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.Properties;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.logging.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * A class to encapsulate the bitmap representation of the mouse cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see Component#setCursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author      Amy Fowler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class Cursor implements java.io.Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * The default cursor type (gets set if no cursor is defined).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public static final int     DEFAULT_CURSOR                  = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * The crosshair cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int     CROSSHAIR_CURSOR                = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * The text cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static final int     TEXT_CURSOR                     = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * The wait cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public static final int     WAIT_CURSOR                     = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The south-west-resize cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public static final int     SW_RESIZE_CURSOR                = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * The south-east-resize cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final int     SE_RESIZE_CURSOR                = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The north-west-resize cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public static final int     NW_RESIZE_CURSOR                = 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The north-east-resize cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public static final int     NE_RESIZE_CURSOR                = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * The north-resize cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public static final int     N_RESIZE_CURSOR                 = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The south-resize cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public static final int     S_RESIZE_CURSOR                 = 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * The west-resize cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static final int     W_RESIZE_CURSOR                 = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * The east-resize cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public static final int     E_RESIZE_CURSOR                 = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The hand cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public static final int     HAND_CURSOR                     = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * The move cursor type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public static final int     MOVE_CURSOR                     = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    protected static Cursor predefined[] = new Cursor[14];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /* Localization names and default values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static final String[][] cursorProperties = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        { "AWT.DefaultCursor", "Default Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        { "AWT.CrosshairCursor", "Crosshair Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        { "AWT.TextCursor", "Text Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        { "AWT.WaitCursor", "Wait Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        { "AWT.SWResizeCursor", "Southwest Resize Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        { "AWT.SEResizeCursor", "Southeast Resize Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        { "AWT.NWResizeCursor", "Northwest Resize Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        { "AWT.NEResizeCursor", "Northeast Resize Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        { "AWT.NResizeCursor", "North Resize Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        { "AWT.SResizeCursor", "South Resize Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        { "AWT.WResizeCursor", "West Resize Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        { "AWT.EResizeCursor", "East Resize Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        { "AWT.HandCursor", "Hand Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        { "AWT.MoveCursor", "Move Cursor" },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * The chosen cursor type initially set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * the <code>DEFAULT_CURSOR</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see #getType()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    int type = DEFAULT_CURSOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * The type associated with all custom cursors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public static final int     CUSTOM_CURSOR                   = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * hashtable, filesystem dir prefix, filename, and properties for custom cursors support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private static final Hashtable  systemCustomCursors         = new Hashtable(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static final String systemCustomCursorDirPrefix = initCursorDir();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private static String initCursorDir() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        String jhome =  (String) java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
               new sun.security.action.GetPropertyAction("java.home"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return jhome +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            File.separator + "lib" + File.separator + "images" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            File.separator + "cursors" + File.separator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private static final String     systemCustomCursorPropertiesFile = systemCustomCursorDirPrefix + "cursors.properties";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private static       Properties systemCustomCursorProperties = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    private static final String CursorDotPrefix  = "Cursor.";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private static final String DotFileSuffix    = ".File";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static final String DotHotspotSuffix = ".HotSpot";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private static final String DotNameSuffix    = ".Name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private static final long serialVersionUID = 8028237497568985504L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    private static final Logger log = Logger.getLogger("java.awt.Cursor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (!GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Initialize JNI field and method IDs for fields that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * accessed from C.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * Hook into native data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private transient long pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private transient Object anchor = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    static class CursorDisposer implements sun.java2d.DisposerRecord {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        volatile long pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        public CursorDisposer(long pData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            this.pData = pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            if (pData != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                finalizeImpl(pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    transient CursorDisposer disposer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private void setPData(long pData) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.pData = pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        if (disposer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            disposer = new CursorDisposer(pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            // anchor is null after deserialization
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            if (anchor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                anchor = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            sun.java2d.Disposer.addRecord(anchor, disposer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            disposer.pData = pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * The user-visible name of the cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @see #getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    protected String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Returns a cursor object with the specified predefined type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @param type the type of predefined cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @return the specified predefined cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @throws IllegalArgumentException if the specified cursor type is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *         invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    static public Cursor getPredefinedCursor(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new IllegalArgumentException("illegal cursor type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (predefined[type] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            predefined[type] = new Cursor(type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return predefined[type];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Returns a system-specific custom cursor object matching the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * specified name.  Cursor names are, for example: "Invalid.16x16"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * @param name a string describing the desired system-specific custom cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return the system specific custom cursor named
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * <code>GraphicsEnvironment.isHeadless</code> returns true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    static public Cursor getSystemCustomCursor(final String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        throws AWTException, HeadlessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        GraphicsEnvironment.checkHeadless();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        Cursor cursor = (Cursor)systemCustomCursors.get(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        if (cursor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            synchronized(systemCustomCursors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                if (systemCustomCursorProperties == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    loadSystemCustomCursorProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            String prefix = CursorDotPrefix + name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            String key    = prefix + DotFileSuffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            if (!systemCustomCursorProperties.containsKey(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (log.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    log.log(Level.FINER, "Cursor.getSystemCustomCursor(" + name + ") returned null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            final String fileName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                systemCustomCursorProperties.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            String localized = (String)systemCustomCursorProperties.getProperty(prefix + DotNameSuffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (localized == null) localized = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            String hotspot = (String)systemCustomCursorProperties.getProperty(prefix + DotHotspotSuffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (hotspot == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                throw new AWTException("no hotspot property defined for cursor: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            StringTokenizer st = new StringTokenizer(hotspot, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (st.countTokens() != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                throw new AWTException("failed to parse hotspot property for cursor: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            int y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                x = Integer.parseInt(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                y = Integer.parseInt(st.nextToken());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            } catch (NumberFormatException nfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                throw new AWTException("failed to parse hotspot property for cursor: " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                final int fx = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                final int fy = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                final String flocalized = localized;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                cursor = (Cursor) java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    new java.security.PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    public Object run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        Toolkit toolkit = Toolkit.getDefaultToolkit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        Image image = toolkit.getImage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                           systemCustomCursorDirPrefix + fileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                        return toolkit.createCustomCursor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                    image, new Point(fx,fy), flocalized);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                throw new AWTException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    "Exception: " + e.getClass() + " " + e.getMessage() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    " occurred while creating cursor " + name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            if (cursor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                if (log.isLoggable(Level.FINER)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    log.log(Level.FINER, "Cursor.getSystemCustomCursor(" + name + ") returned null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                systemCustomCursors.put(name, cursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return cursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * Return the system default cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    static public Cursor getDefaultCursor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        return getPredefinedCursor(Cursor.DEFAULT_CURSOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Creates a new cursor object with the specified type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param type the type of cursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * @throws IllegalArgumentException if the specified cursor type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * is invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    @ConstructorProperties({"type"})
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    public Cursor(int type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            throw new IllegalArgumentException("illegal cursor type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        this.type = type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        // Lookup localized name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        name = Toolkit.getProperty(cursorProperties[type][0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                                   cursorProperties[type][1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * Creates a new custom cursor object with the specified name.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * Note:  this constructor should only be used by AWT implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * as part of their support for custom cursors.  Applications should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * use Toolkit.createCustomCursor().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @param name the user-visible name of the cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @see java.awt.Toolkit#createCustomCursor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    protected Cursor(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        this.type = Cursor.CUSTOM_CURSOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * Returns the type for this cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    public int getType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Returns the name of this cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @return    a localized description of this cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @since     1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Returns a string representation of this cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return    a string representation of this cursor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @since     1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return getClass().getName() + "[" + getName() + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * load the cursor.properties file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private static void loadSystemCustomCursorProperties() throws AWTException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        synchronized(systemCustomCursors) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            systemCustomCursorProperties = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                      new java.security.PrivilegedExceptionAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    public Object run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        FileInputStream fis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                            fis = new FileInputStream(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                           systemCustomCursorPropertiesFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                            systemCustomCursorProperties.load(fis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            if (fis != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                                fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                systemCustomCursorProperties = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                 throw new AWTException("Exception: " + e.getClass() + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                   e.getMessage() + " occurred while loading: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                        systemCustomCursorPropertiesFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private native static void finalizeImpl(long pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
}