< prev index next >

src/java.desktop/share/classes/java/awt/Cursor.java

Print this page




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.beans.ConstructorProperties;
  28 import java.io.InputStream;
  29 import java.net.URL;
  30 import java.security.AccessController;

  31 import java.security.PrivilegedExceptionAction;
  32 import java.util.Hashtable;
  33 import java.util.Properties;
  34 import java.util.StringTokenizer;
  35 
  36 import sun.awt.AWTAccessor;
  37 import sun.util.logging.PlatformLogger;
  38 
  39 /**
  40  * A class to encapsulate the bitmap representation of the mouse cursor.
  41  *
  42  * @see Component#setCursor
  43  * @author      Amy Fowler
  44  */
  45 public class Cursor implements java.io.Serializable {
  46 
  47     /**
  48      * The default cursor type (gets set if no cursor is defined).
  49      */
  50     public static final int     DEFAULT_CURSOR                  = 0;


 244             disposer.pData = pData;
 245         }
 246     }
 247 
 248     /**
 249      * The user-visible name of the cursor.
 250      *
 251      * @serial
 252      * @see #getName()
 253      */
 254     protected String name;
 255 
 256     /**
 257      * Returns a cursor object with the specified predefined type.
 258      *
 259      * @param type the type of predefined cursor
 260      * @return the specified predefined cursor
 261      * @throws IllegalArgumentException if the specified cursor type is
 262      *         invalid
 263      */
 264     static public Cursor getPredefinedCursor(int type) {
 265         if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
 266             throw new IllegalArgumentException("illegal cursor type");
 267         }
 268         Cursor c = predefinedPrivate[type];
 269         if (c == null) {
 270             predefinedPrivate[type] = c = new Cursor(type);
 271         }
 272         // fill 'predefined' array for backwards compatibility.
 273         if (predefined[type] == null) {
 274             predefined[type] = c;
 275         }
 276         return c;
 277     }
 278 
 279     /**
 280      * Returns a system-specific custom cursor object matching the
 281      * specified name.  Cursor names are, for example: "Invalid.16x16"
 282      *
 283      * @param name a string describing the desired system-specific custom cursor
 284      * @return the system specific custom cursor named
 285      * @exception HeadlessException if
 286      * <code>GraphicsEnvironment.isHeadless</code> returns true
 287      * @exception AWTException in case of erroneous retrieving of the cursor
 288      */
 289     static public Cursor getSystemCustomCursor(final String name)
 290         throws AWTException, HeadlessException {
 291         GraphicsEnvironment.checkHeadless();
 292         Cursor cursor = systemCustomCursors.get(name);
 293 
 294         if (cursor == null) {
 295             synchronized(systemCustomCursors) {
 296                 if (systemCustomCursorProperties == null)
 297                     loadSystemCustomCursorProperties();
 298             }
 299 
 300             String prefix = CURSOR_DOT_PREFIX + name;
 301             String key    = prefix + DOT_FILE_SUFFIX;
 302 
 303             if (!systemCustomCursorProperties.containsKey(key)) {
 304                 if (log.isLoggable(PlatformLogger.Level.FINER)) {
 305                     log.finer("Cursor.getSystemCustomCursor(" + name + ") returned null");
 306                 }
 307                 return null;
 308             }
 309 


 313             final String localized = systemCustomCursorProperties.getProperty(
 314                     prefix + DOT_NAME_SUFFIX, name);
 315 
 316             String hotspot = systemCustomCursorProperties.getProperty(prefix + DOT_HOTSPOT_SUFFIX);
 317 
 318             if (hotspot == null)
 319                 throw new AWTException("no hotspot property defined for cursor: " + name);
 320 
 321             StringTokenizer st = new StringTokenizer(hotspot, ",");
 322 
 323             if (st.countTokens() != 2)
 324                 throw new AWTException("failed to parse hotspot property for cursor: " + name);
 325 
 326             final Point hotPoint;
 327             try {
 328                 hotPoint = new Point(Integer.parseInt(st.nextToken()),
 329                                      Integer.parseInt(st.nextToken()));
 330             } catch (NumberFormatException nfe) {
 331                 throw new AWTException("failed to parse hotspot property for cursor: " + name);
 332             }
 333 
 334             try {
 335                 final Toolkit toolkit = Toolkit.getDefaultToolkit();
 336                 final String file = RESOURCE_PREFIX + fileName;
 337 
 338                 cursor = AccessController.doPrivileged(
 339                         (PrivilegedExceptionAction<Cursor>) () -> {
 340                             URL url = Cursor.class.getResource(file);
 341                             Image image = toolkit.getImage(url);
 342                             return toolkit.createCustomCursor(image, hotPoint,
 343                                                               localized);
 344                         });



 345             } catch (Exception e) {
 346                 throw new AWTException(
 347                     "Exception: " + e.getClass() + " " + e.getMessage() +
 348                     " occurred while creating cursor " + name);
 349             }
 350 
 351             if (cursor == null) {
 352                 if (log.isLoggable(PlatformLogger.Level.FINER)) {
 353                     log.finer("Cursor.getSystemCustomCursor(" + name + ") returned null");
 354                 }
 355             } else {
 356                 systemCustomCursors.put(name, cursor);
 357             }
 358         }
 359 
 360         return cursor;
 361     }
 362 
 363     /**
 364      * Return the system default cursor.
 365      *
 366      * @return the default cursor
 367      */
 368     static public Cursor getDefaultCursor() {
 369         return getPredefinedCursor(Cursor.DEFAULT_CURSOR);
 370     }
 371 
 372     /**
 373      * Creates a new cursor object with the specified type.
 374      * @param type the type of cursor
 375      * @throws IllegalArgumentException if the specified cursor type
 376      * is invalid
 377      */
 378     @ConstructorProperties({"type"})
 379     public Cursor(int type) {
 380         if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
 381             throw new IllegalArgumentException("illegal cursor type");
 382         }
 383         this.type = type;
 384 
 385         // Lookup localized name.
 386         name = Toolkit.getProperty(cursorProperties[type][0],
 387                                    cursorProperties[type][1]);
 388     }




   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.awt;
  26 
  27 import java.beans.ConstructorProperties;
  28 import java.io.InputStream;

  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 import java.security.PrivilegedExceptionAction;
  32 import java.util.Hashtable;
  33 import java.util.Properties;
  34 import java.util.StringTokenizer;
  35 
  36 import sun.awt.AWTAccessor;
  37 import sun.util.logging.PlatformLogger;
  38 
  39 /**
  40  * A class to encapsulate the bitmap representation of the mouse cursor.
  41  *
  42  * @see Component#setCursor
  43  * @author      Amy Fowler
  44  */
  45 public class Cursor implements java.io.Serializable {
  46 
  47     /**
  48      * The default cursor type (gets set if no cursor is defined).
  49      */
  50     public static final int     DEFAULT_CURSOR                  = 0;


 244             disposer.pData = pData;
 245         }
 246     }
 247 
 248     /**
 249      * The user-visible name of the cursor.
 250      *
 251      * @serial
 252      * @see #getName()
 253      */
 254     protected String name;
 255 
 256     /**
 257      * Returns a cursor object with the specified predefined type.
 258      *
 259      * @param type the type of predefined cursor
 260      * @return the specified predefined cursor
 261      * @throws IllegalArgumentException if the specified cursor type is
 262      *         invalid
 263      */
 264     public static Cursor getPredefinedCursor(int type) {
 265         if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
 266             throw new IllegalArgumentException("illegal cursor type");
 267         }
 268         Cursor c = predefinedPrivate[type];
 269         if (c == null) {
 270             predefinedPrivate[type] = c = new Cursor(type);
 271         }
 272         // fill 'predefined' array for backwards compatibility.
 273         if (predefined[type] == null) {
 274             predefined[type] = c;
 275         }
 276         return c;
 277     }
 278 
 279     /**
 280      * Returns a system-specific custom cursor object matching the
 281      * specified name.  Cursor names are, for example: "Invalid.16x16"
 282      *
 283      * @param name a string describing the desired system-specific custom cursor
 284      * @return the system specific custom cursor named
 285      * @exception HeadlessException if
 286      * <code>GraphicsEnvironment.isHeadless</code> returns true
 287      * @exception AWTException in case of erroneous retrieving of the cursor
 288      */
 289     public static Cursor getSystemCustomCursor(final String name)
 290         throws AWTException, HeadlessException {
 291         GraphicsEnvironment.checkHeadless();
 292         Cursor cursor = systemCustomCursors.get(name);
 293 
 294         if (cursor == null) {
 295             synchronized(systemCustomCursors) {
 296                 if (systemCustomCursorProperties == null)
 297                     loadSystemCustomCursorProperties();
 298             }
 299 
 300             String prefix = CURSOR_DOT_PREFIX + name;
 301             String key    = prefix + DOT_FILE_SUFFIX;
 302 
 303             if (!systemCustomCursorProperties.containsKey(key)) {
 304                 if (log.isLoggable(PlatformLogger.Level.FINER)) {
 305                     log.finer("Cursor.getSystemCustomCursor(" + name + ") returned null");
 306                 }
 307                 return null;
 308             }
 309 


 313             final String localized = systemCustomCursorProperties.getProperty(
 314                     prefix + DOT_NAME_SUFFIX, name);
 315 
 316             String hotspot = systemCustomCursorProperties.getProperty(prefix + DOT_HOTSPOT_SUFFIX);
 317 
 318             if (hotspot == null)
 319                 throw new AWTException("no hotspot property defined for cursor: " + name);
 320 
 321             StringTokenizer st = new StringTokenizer(hotspot, ",");
 322 
 323             if (st.countTokens() != 2)
 324                 throw new AWTException("failed to parse hotspot property for cursor: " + name);
 325 
 326             final Point hotPoint;
 327             try {
 328                 hotPoint = new Point(Integer.parseInt(st.nextToken()),
 329                                      Integer.parseInt(st.nextToken()));
 330             } catch (NumberFormatException nfe) {
 331                 throw new AWTException("failed to parse hotspot property for cursor: " + name);
 332             }


 333             final Toolkit toolkit = Toolkit.getDefaultToolkit();
 334             final String file = RESOURCE_PREFIX + fileName;
 335             final InputStream in = AccessController.doPrivileged(
 336                     (PrivilegedAction<InputStream>) () -> {
 337                         return Cursor.class.getResourceAsStream(file);




 338                     });
 339             try (in) {
 340                 Image image = toolkit.createImage(in.readAllBytes());
 341                 cursor = toolkit.createCustomCursor(image, hotPoint, localized);
 342             } catch (Exception e) {
 343                 throw new AWTException(
 344                     "Exception: " + e.getClass() + " " + e.getMessage() +
 345                     " occurred while creating cursor " + name);
 346             }
 347 
 348             if (cursor == null) {
 349                 if (log.isLoggable(PlatformLogger.Level.FINER)) {
 350                     log.finer("Cursor.getSystemCustomCursor(" + name + ") returned null");
 351                 }
 352             } else {
 353                 systemCustomCursors.put(name, cursor);
 354             }
 355         }
 356 
 357         return cursor;
 358     }
 359 
 360     /**
 361      * Return the system default cursor.
 362      *
 363      * @return the default cursor
 364      */
 365     public static Cursor getDefaultCursor() {
 366         return getPredefinedCursor(Cursor.DEFAULT_CURSOR);
 367     }
 368 
 369     /**
 370      * Creates a new cursor object with the specified type.
 371      * @param type the type of cursor
 372      * @throws IllegalArgumentException if the specified cursor type
 373      * is invalid
 374      */
 375     @ConstructorProperties({"type"})
 376     public Cursor(int type) {
 377         if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
 378             throw new IllegalArgumentException("illegal cursor type");
 379         }
 380         this.type = type;
 381 
 382         // Lookup localized name.
 383         name = Toolkit.getProperty(cursorProperties[type][0],
 384                                    cursorProperties[type][1]);
 385     }


< prev index next >