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

Print this page




 146     };
 147 
 148     /**
 149      * The chosen cursor type initially set to
 150      * the <code>DEFAULT_CURSOR</code>.
 151      *
 152      * @serial
 153      * @see #getType()
 154      */
 155     int type = DEFAULT_CURSOR;
 156 
 157     /**
 158      * The type associated with all custom cursors.
 159      */
 160     public static final int     CUSTOM_CURSOR                   = -1;
 161 
 162     /*
 163      * hashtable, filesystem dir prefix, filename, and properties for custom cursors support
 164      */
 165 
 166     private static final Hashtable  systemCustomCursors         = new Hashtable(1);
 167     private static final String systemCustomCursorDirPrefix = initCursorDir();
 168 
 169     private static String initCursorDir() {
 170         String jhome =  (String) java.security.AccessController.doPrivileged(
 171                new sun.security.action.GetPropertyAction("java.home"));
 172         return jhome +
 173             File.separator + "lib" + File.separator + "images" +
 174             File.separator + "cursors" + File.separator;
 175     }
 176 
 177     private static final String     systemCustomCursorPropertiesFile = systemCustomCursorDirPrefix + "cursors.properties";
 178 
 179     private static       Properties systemCustomCursorProperties = null;
 180 
 181     private static final String CursorDotPrefix  = "Cursor.";
 182     private static final String DotFileSuffix    = ".File";
 183     private static final String DotHotspotSuffix = ".HotSpot";
 184     private static final String DotNameSuffix    = ".Name";
 185 
 186     /*
 187      * JDK 1.1 serialVersionUID
 188      */
 189     private static final long serialVersionUID = 8028237497568985504L;
 190 


 281         }
 282         // fill 'predefined' array for backwards compatibility.
 283         if (predefined[type] == null) {
 284             predefined[type] = c;
 285         }
 286         return c;
 287     }
 288 
 289     /**
 290      * Returns a system-specific custom cursor object matching the
 291      * specified name.  Cursor names are, for example: "Invalid.16x16"
 292      *
 293      * @param name a string describing the desired system-specific custom cursor
 294      * @return the system specific custom cursor named
 295      * @exception HeadlessException if
 296      * <code>GraphicsEnvironment.isHeadless</code> returns true
 297      */
 298     static public Cursor getSystemCustomCursor(final String name)
 299         throws AWTException, HeadlessException {
 300         GraphicsEnvironment.checkHeadless();
 301         Cursor cursor = (Cursor)systemCustomCursors.get(name);
 302 
 303         if (cursor == null) {
 304             synchronized(systemCustomCursors) {
 305                 if (systemCustomCursorProperties == null)
 306                     loadSystemCustomCursorProperties();
 307             }
 308 
 309             String prefix = CursorDotPrefix + name;
 310             String key    = prefix + DotFileSuffix;
 311 
 312             if (!systemCustomCursorProperties.containsKey(key)) {
 313                 if (log.isLoggable(PlatformLogger.FINER)) {
 314                     log.finer("Cursor.getSystemCustomCursor(" + name + ") returned null");
 315                 }
 316                 return null;
 317             }
 318 
 319             final String fileName =
 320                 systemCustomCursorProperties.getProperty(key);
 321 
 322             String localized = (String)systemCustomCursorProperties.getProperty(prefix + DotNameSuffix);
 323 
 324             if (localized == null) localized = name;
 325 
 326             String hotspot = (String)systemCustomCursorProperties.getProperty(prefix + DotHotspotSuffix);
 327 
 328             if (hotspot == null)
 329                 throw new AWTException("no hotspot property defined for cursor: " + name);
 330 
 331             StringTokenizer st = new StringTokenizer(hotspot, ",");
 332 
 333             if (st.countTokens() != 2)
 334                 throw new AWTException("failed to parse hotspot property for cursor: " + name);
 335 
 336             int x = 0;
 337             int y = 0;
 338 
 339             try {
 340                 x = Integer.parseInt(st.nextToken());
 341                 y = Integer.parseInt(st.nextToken());
 342             } catch (NumberFormatException nfe) {
 343                 throw new AWTException("failed to parse hotspot property for cursor: " + name);
 344             }
 345 
 346             try {
 347                 final int fx = x;
 348                 final int fy = y;
 349                 final String flocalized = localized;
 350 
 351                 cursor = (Cursor) java.security.AccessController.doPrivileged(
 352                     new java.security.PrivilegedExceptionAction() {
 353                     public Object run() throws Exception {
 354                         Toolkit toolkit = Toolkit.getDefaultToolkit();
 355                         Image image = toolkit.getImage(
 356                            systemCustomCursorDirPrefix + fileName);
 357                         return toolkit.createCustomCursor(
 358                                     image, new Point(fx,fy), flocalized);
 359                     }
 360                 });
 361             } catch (Exception e) {
 362                 throw new AWTException(
 363                     "Exception: " + e.getClass() + " " + e.getMessage() +
 364                     " occurred while creating cursor " + name);
 365             }
 366 
 367             if (cursor == null) {
 368                 if (log.isLoggable(PlatformLogger.FINER)) {
 369                     log.finer("Cursor.getSystemCustomCursor(" + name + ") returned null");
 370                 }
 371             } else {
 372                 systemCustomCursors.put(name, cursor);
 373             }


 430         return name;
 431     }
 432 
 433     /**
 434      * Returns a string representation of this cursor.
 435      * @return    a string representation of this cursor.
 436      * @since     1.2
 437      */
 438     public String toString() {
 439         return getClass().getName() + "[" + getName() + "]";
 440     }
 441 
 442     /*
 443      * load the cursor.properties file
 444      */
 445     private static void loadSystemCustomCursorProperties() throws AWTException {
 446         synchronized(systemCustomCursors) {
 447             systemCustomCursorProperties = new Properties();
 448 
 449             try {
 450                 AccessController.doPrivileged(
 451                       new java.security.PrivilegedExceptionAction() {
 452                     public Object run() throws Exception {
 453                         FileInputStream fis = null;
 454                         try {
 455                             fis = new FileInputStream(
 456                                            systemCustomCursorPropertiesFile);
 457                             systemCustomCursorProperties.load(fis);
 458                         } finally {
 459                             if (fis != null)
 460                                 fis.close();
 461                         }
 462                         return null;
 463                     }
 464                 });
 465             } catch (Exception e) {
 466                 systemCustomCursorProperties = null;
 467                  throw new AWTException("Exception: " + e.getClass() + " " +
 468                    e.getMessage() + " occurred while loading: " +
 469                                         systemCustomCursorPropertiesFile);
 470             }
 471         }


 146     };
 147 
 148     /**
 149      * The chosen cursor type initially set to
 150      * the <code>DEFAULT_CURSOR</code>.
 151      *
 152      * @serial
 153      * @see #getType()
 154      */
 155     int type = DEFAULT_CURSOR;
 156 
 157     /**
 158      * The type associated with all custom cursors.
 159      */
 160     public static final int     CUSTOM_CURSOR                   = -1;
 161 
 162     /*
 163      * hashtable, filesystem dir prefix, filename, and properties for custom cursors support
 164      */
 165 
 166     private static final Hashtable<String,Cursor> systemCustomCursors = new Hashtable<>(1);
 167     private static final String systemCustomCursorDirPrefix = initCursorDir();
 168 
 169     private static String initCursorDir() {
 170         String jhome = java.security.AccessController.doPrivileged(
 171                new sun.security.action.GetPropertyAction("java.home"));
 172         return jhome +
 173             File.separator + "lib" + File.separator + "images" +
 174             File.separator + "cursors" + File.separator;
 175     }
 176 
 177     private static final String     systemCustomCursorPropertiesFile = systemCustomCursorDirPrefix + "cursors.properties";
 178 
 179     private static       Properties systemCustomCursorProperties = null;
 180 
 181     private static final String CursorDotPrefix  = "Cursor.";
 182     private static final String DotFileSuffix    = ".File";
 183     private static final String DotHotspotSuffix = ".HotSpot";
 184     private static final String DotNameSuffix    = ".Name";
 185 
 186     /*
 187      * JDK 1.1 serialVersionUID
 188      */
 189     private static final long serialVersionUID = 8028237497568985504L;
 190 


 281         }
 282         // fill 'predefined' array for backwards compatibility.
 283         if (predefined[type] == null) {
 284             predefined[type] = c;
 285         }
 286         return c;
 287     }
 288 
 289     /**
 290      * Returns a system-specific custom cursor object matching the
 291      * specified name.  Cursor names are, for example: "Invalid.16x16"
 292      *
 293      * @param name a string describing the desired system-specific custom cursor
 294      * @return the system specific custom cursor named
 295      * @exception HeadlessException if
 296      * <code>GraphicsEnvironment.isHeadless</code> returns true
 297      */
 298     static public Cursor getSystemCustomCursor(final String name)
 299         throws AWTException, HeadlessException {
 300         GraphicsEnvironment.checkHeadless();
 301         Cursor cursor = systemCustomCursors.get(name);
 302 
 303         if (cursor == null) {
 304             synchronized(systemCustomCursors) {
 305                 if (systemCustomCursorProperties == null)
 306                     loadSystemCustomCursorProperties();
 307             }
 308 
 309             String prefix = CursorDotPrefix + name;
 310             String key    = prefix + DotFileSuffix;
 311 
 312             if (!systemCustomCursorProperties.containsKey(key)) {
 313                 if (log.isLoggable(PlatformLogger.FINER)) {
 314                     log.finer("Cursor.getSystemCustomCursor(" + name + ") returned null");
 315                 }
 316                 return null;
 317             }
 318 
 319             final String fileName =
 320                 systemCustomCursorProperties.getProperty(key);
 321 
 322             String localized = systemCustomCursorProperties.getProperty(prefix + DotNameSuffix);
 323 
 324             if (localized == null) localized = name;
 325 
 326             String hotspot = systemCustomCursorProperties.getProperty(prefix + DotHotspotSuffix);
 327 
 328             if (hotspot == null)
 329                 throw new AWTException("no hotspot property defined for cursor: " + name);
 330 
 331             StringTokenizer st = new StringTokenizer(hotspot, ",");
 332 
 333             if (st.countTokens() != 2)
 334                 throw new AWTException("failed to parse hotspot property for cursor: " + name);
 335 
 336             int x = 0;
 337             int y = 0;
 338 
 339             try {
 340                 x = Integer.parseInt(st.nextToken());
 341                 y = Integer.parseInt(st.nextToken());
 342             } catch (NumberFormatException nfe) {
 343                 throw new AWTException("failed to parse hotspot property for cursor: " + name);
 344             }
 345 
 346             try {
 347                 final int fx = x;
 348                 final int fy = y;
 349                 final String flocalized = localized;
 350 
 351                 cursor = java.security.AccessController.<Cursor>doPrivileged(
 352                     new java.security.PrivilegedExceptionAction<Cursor>() {
 353                     public Cursor run() throws Exception {
 354                         Toolkit toolkit = Toolkit.getDefaultToolkit();
 355                         Image image = toolkit.getImage(
 356                            systemCustomCursorDirPrefix + fileName);
 357                         return toolkit.createCustomCursor(
 358                                     image, new Point(fx,fy), flocalized);
 359                     }
 360                 });
 361             } catch (Exception e) {
 362                 throw new AWTException(
 363                     "Exception: " + e.getClass() + " " + e.getMessage() +
 364                     " occurred while creating cursor " + name);
 365             }
 366 
 367             if (cursor == null) {
 368                 if (log.isLoggable(PlatformLogger.FINER)) {
 369                     log.finer("Cursor.getSystemCustomCursor(" + name + ") returned null");
 370                 }
 371             } else {
 372                 systemCustomCursors.put(name, cursor);
 373             }


 430         return name;
 431     }
 432 
 433     /**
 434      * Returns a string representation of this cursor.
 435      * @return    a string representation of this cursor.
 436      * @since     1.2
 437      */
 438     public String toString() {
 439         return getClass().getName() + "[" + getName() + "]";
 440     }
 441 
 442     /*
 443      * load the cursor.properties file
 444      */
 445     private static void loadSystemCustomCursorProperties() throws AWTException {
 446         synchronized(systemCustomCursors) {
 447             systemCustomCursorProperties = new Properties();
 448 
 449             try {
 450                 AccessController.<Object>doPrivileged(
 451                       new java.security.PrivilegedExceptionAction<Object>() {
 452                     public Object run() throws Exception {
 453                         FileInputStream fis = null;
 454                         try {
 455                             fis = new FileInputStream(
 456                                            systemCustomCursorPropertiesFile);
 457                             systemCustomCursorProperties.load(fis);
 458                         } finally {
 459                             if (fis != null)
 460                                 fis.close();
 461                         }
 462                         return null;
 463                     }
 464                 });
 465             } catch (Exception e) {
 466                 systemCustomCursorProperties = null;
 467                  throw new AWTException("Exception: " + e.getClass() + " " +
 468                    e.getMessage() + " occurred while loading: " +
 469                                         systemCustomCursorPropertiesFile);
 470             }
 471         }