src/java.desktop/share/classes/java/awt/Cursor.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -22,22 +22,21 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt;
-import java.io.File;
-import java.io.FileInputStream;
-
import java.beans.ConstructorProperties;
+import java.io.InputStream;
+import java.net.URL;
+import java.security.AccessController;
+import java.security.PrivilegedExceptionAction;
import java.util.Hashtable;
import java.util.Properties;
import java.util.StringTokenizer;
-import java.security.AccessController;
-
-import sun.util.logging.PlatformLogger;
import sun.awt.AWTAccessor;
+import sun.util.logging.PlatformLogger;
/**
* A class to encapsulate the bitmap representation of the mouse cursor.
*
* @see Component#setCursor
@@ -158,25 +157,16 @@
* The type associated with all custom cursors.
*/
public static final int CUSTOM_CURSOR = -1;
/*
- * hashtable, filesystem dir prefix, filename, and properties for custom cursors support
+ * hashtable, resource prefix, filename, and properties for custom cursors
+ * support
*/
-
private static final Hashtable<String,Cursor> systemCustomCursors = new Hashtable<>(1);
- private static final String systemCustomCursorDirPrefix = initCursorDir();
-
- private static String initCursorDir() {
- String jhome = java.security.AccessController.doPrivileged(
- new sun.security.action.GetPropertyAction("java.home"));
- return jhome +
- File.separator + "lib" + File.separator + "images" +
- File.separator + "cursors" + File.separator;
- }
-
- private static final String systemCustomCursorPropertiesFile = systemCustomCursorDirPrefix + "cursors.properties";
+ private static final String resourcePrefix = "/sun/awt/resources/cursors/";
+ private static final String propertiesFile = resourcePrefix + "cursors.properties";
private static Properties systemCustomCursorProperties = null;
private static final String CursorDotPrefix = "Cursor.";
private static final String DotFileSuffix = ".File";
@@ -318,13 +308,12 @@
}
final String fileName =
systemCustomCursorProperties.getProperty(key);
- String localized = systemCustomCursorProperties.getProperty(prefix + DotNameSuffix);
-
- if (localized == null) localized = name;
+ final String localized = systemCustomCursorProperties.getProperty(
+ prefix + DotNameSuffix, name);
String hotspot = systemCustomCursorProperties.getProperty(prefix + DotHotspotSuffix);
if (hotspot == null)
throw new AWTException("no hotspot property defined for cursor: " + name);
@@ -332,34 +321,28 @@
StringTokenizer st = new StringTokenizer(hotspot, ",");
if (st.countTokens() != 2)
throw new AWTException("failed to parse hotspot property for cursor: " + name);
- int x = 0;
- int y = 0;
-
+ final Point hotPoint;
try {
- x = Integer.parseInt(st.nextToken());
- y = Integer.parseInt(st.nextToken());
+ hotPoint = new Point(Integer.parseInt(st.nextToken()),
+ Integer.parseInt(st.nextToken()));
} catch (NumberFormatException nfe) {
throw new AWTException("failed to parse hotspot property for cursor: " + name);
}
try {
- final int fx = x;
- final int fy = y;
- final String flocalized = localized;
-
- cursor = java.security.AccessController.<Cursor>doPrivileged(
- new java.security.PrivilegedExceptionAction<Cursor>() {
- public Cursor run() throws Exception {
- Toolkit toolkit = Toolkit.getDefaultToolkit();
- Image image = toolkit.getImage(
- systemCustomCursorDirPrefix + fileName);
- return toolkit.createCustomCursor(
- image, new Point(fx,fy), flocalized);
- }
+ final Toolkit toolkit = Toolkit.getDefaultToolkit();
+ final String file = resourcePrefix + fileName;
+
+ cursor = AccessController.doPrivileged(
+ (PrivilegedExceptionAction<Cursor>) () -> {
+ URL url = Cursor.class.getResource(file);
+ Image image = toolkit.getImage(url);
+ return toolkit.createCustomCursor(image, hotPoint,
+ localized);
});
} catch (Exception e) {
throw new AWTException(
"Exception: " + e.getClass() + " " + e.getMessage() +
" occurred while creating cursor " + name);
@@ -450,30 +433,28 @@
private static void loadSystemCustomCursorProperties() throws AWTException {
synchronized(systemCustomCursors) {
systemCustomCursorProperties = new Properties();
try {
- AccessController.<Object>doPrivileged(
- new java.security.PrivilegedExceptionAction<Object>() {
- public Object run() throws Exception {
- FileInputStream fis = null;
+ AccessController.doPrivileged(
+ (PrivilegedExceptionAction<Object>) () -> {
+ InputStream fis = null;
try {
- fis = new FileInputStream(
- systemCustomCursorPropertiesFile);
+ fis = Cursor.class.getResourceAsStream(
+ propertiesFile);
systemCustomCursorProperties.load(fis);
} finally {
if (fis != null)
fis.close();
}
return null;
- }
});
} catch (Exception e) {
systemCustomCursorProperties = null;
throw new AWTException("Exception: " + e.getClass() + " " +
e.getMessage() + " occurred while loading: " +
- systemCustomCursorPropertiesFile);
+ propertiesFile);
}
}
}
private native static void finalizeImpl(long pData);