1 7 package java.awt; 8 9 import java.awt.AWTException ; 10 import java.awt.Point ; 11 import java.awt.Toolkit ; 12 13 import java.io.File ; 14 import java.io.FileInputStream ; 15 16 import java.util.Enumeration ; 17 import java.util.Hashtable ; 18 import java.util.Properties ; 19 import java.util.StringTokenizer ; 20 21 import java.security.AccessController ; 22 23 import sun.awt.DebugHelper; 24 25 32 public class Cursor implements java.io.Serializable { 33 34 37 public static final int DEFAULT_CURSOR = 0; 38 39 42 public static final int CROSSHAIR_CURSOR = 1; 43 44 47 public static final int TEXT_CURSOR = 2; 48 49 52 public static final int WAIT_CURSOR = 3; 53 54 57 public static final int SW_RESIZE_CURSOR = 4; 58 59 62 public static final int SE_RESIZE_CURSOR = 5; 63 64 67 public static final int NW_RESIZE_CURSOR = 6; 68 69 72 public static final int NE_RESIZE_CURSOR = 7; 73 74 77 public static final int N_RESIZE_CURSOR = 8; 78 79 82 public static final int S_RESIZE_CURSOR = 9; 83 84 87 public static final int W_RESIZE_CURSOR = 10; 88 89 92 public static final int E_RESIZE_CURSOR = 11; 93 94 97 public static final int HAND_CURSOR = 12; 98 99 102 public static final int MOVE_CURSOR = 13; 103 104 protected static Cursor predefined[] = new Cursor [14]; 105 106 107 static final String [][] cursorProperties = { 108 { "AWT.DefaultCursor", "Default Cursor" }, 109 { "AWT.CrosshairCursor", "Crosshair Cursor" }, 110 { "AWT.TextCursor", "Text Cursor" }, 111 { "AWT.WaitCursor", "Wait Cursor" }, 112 { "AWT.SWResizeCursor", "Southwest Resize Cursor" }, 113 { "AWT.SEResizeCursor", "Southeast Resize Cursor" }, 114 { "AWT.NWResizeCursor", "Northwest Resize Cursor" }, 115 { "AWT.NEResizeCursor", "Northeast Resize Cursor" }, 116 { "AWT.NResizeCursor", "North Resize Cursor" }, 117 { "AWT.SResizeCursor", "South Resize Cursor" }, 118 { "AWT.WResizeCursor", "West Resize Cursor" }, 119 { "AWT.EResizeCursor", "East Resize Cursor" }, 120 { "AWT.HandCursor", "Hand Cursor" }, 121 { "AWT.MoveCursor", "Move Cursor" }, 122 }; 123 124 131 int type = DEFAULT_CURSOR; 132 133 136 public static final int CUSTOM_CURSOR = -1; 137 138 141 142 private static final Hashtable systemCustomCursors = new Hashtable (1); 143 private static final String systemCustomCursorDirPrefix = initCursorDir(); 144 145 private static String initCursorDir() { 146 String jhome = (String ) java.security.AccessController.doPrivileged( 147 new sun.security.action.GetPropertyAction("java.home")); 148 return jhome + 149 File.separator + "lib" + File.separator + "images" + 150 File.separator + "cursors" + File.separator; 151 } 152 153 private static final String systemCustomCursorPropertiesFile = systemCustomCursorDirPrefix + "cursors.properties"; 154 155 private static Properties systemCustomCursorProperties = null; 156 157 private static final String CursorDotPrefix = "Cursor."; 158 private static final String DotFileSuffix = ".File"; 159 private static final String DotHotspotSuffix = ".HotSpot"; 160 private static final String DotNameSuffix = ".Name"; 161 162 165 private static final long serialVersionUID = 8028237497568985504L; 166 167 private static final DebugHelper dbg = DebugHelper.create(Cursor .class); 168 169 static { 170 171 Toolkit.loadLibraries(); 172 if (!GraphicsEnvironment.isHeadless()) { 173 initIDs(); 174 } 175 } 176 177 181 private static native void initIDs(); 182 183 186 private transient long pData; 187 188 private transient Object anchor = new Object (); 189 190 static class CursorDisposer extends sun.java2d.DisposerRecord { 191 long pData; 192 public void dispose() { 193 finalizeImpl(pData); 194 } 195 } 196 transient CursorDisposer disposer; 197 private void setPData(long pData) { 198 this.pData = pData; 199 if (!GraphicsEnvironment.isHeadless() && pData != 0) { 200 if (disposer == null) { 201 if (anchor == null) { 203 anchor = new Object (); 204 } 205 disposer = new CursorDisposer(); 206 sun.java2d.Disposer.addRecord(anchor, disposer); 207 } 208 disposer.pData = pData; 209 } 210 } 211 212 218 protected String name; 219 220 228 static public Cursor getPredefinedCursor(int type) { 229 if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) { 230 throw new IllegalArgumentException ("illegal cursor type"); 231 } 232 if (predefined[type] == null) { 233 predefined[type] = new Cursor (type); 234 } 235 return predefined[type]; 236 } 237 238 247 static public Cursor getSystemCustomCursor(final String name) 248 throws AWTException , HeadlessException { 249 GraphicsEnvironment.checkHeadless(); 250 Cursor cursor = (Cursor )systemCustomCursors.get(name); 251 252 if (cursor == null) { 253 synchronized(systemCustomCursors) { 254 if (systemCustomCursorProperties == null) 255 loadSystemCustomCursorProperties(); 256 } 257 258 String prefix = CursorDotPrefix + name; 259 String key = prefix + DotFileSuffix; 260 261 if (!systemCustomCursorProperties.containsKey(key)) { 262 if (dbg.on) { 263 dbg.println("Cursor.getSystemCustomCursor(" + name + ") returned null"); 264 } 265 return null; 266 } 267 268 final String fileName = 269 systemCustomCursorProperties.getProperty(key); 270 271 String localized = (String )systemCustomCursorProperties.getProperty(prefix + DotNameSuffix); 272 273 if (localized == null) localized = name; 274 275 String hotspot = (String )systemCustomCursorProperties.getProperty(prefix + DotHotspotSuffix); 276 277 if (hotspot == null) 278 throw new AWTException ("no hotspot property defined for cursor: " + name); 279 280 StringTokenizer st = new StringTokenizer (hotspot, ","); 281 282 if (st.countTokens() != 2) 283 throw new AWTException ("failed to parse hotspot property for cursor: " + name); 284 285 int x = 0; 286 int y = 0; 287 288 try { 289 x = Integer.parseInt(st.nextToken()); 290 y = Integer.parseInt(st.nextToken()); 291 } catch (NumberFormatException nfe) { 292 throw new AWTException ("failed to parse hotspot property for cursor: " + name); 293 } 294 295 try { 296 final int fx = x; 297 final int fy = y; 298 final String flocalized = localized; 299 300 cursor = (Cursor ) java.security.AccessController.doPrivileged( 301 new java.security.PrivilegedExceptionAction () { 302 public Object run() throws Exception { 303 Toolkit toolkit = Toolkit.getDefaultToolkit(); 304 Image image = toolkit.getImage( 305 systemCustomCursorDirPrefix + fileName); 306 return toolkit.createCustomCursor( 307 image, new Point (fx,fy), flocalized); 308 } 309 }); 310 } catch (Exception e) { 311 throw new AWTException ( 312 "Exception: " + e.getClass() + " " + e.getMessage() + 313 " occurred while creating cursor " + name); 314 } 315 316 if (cursor == null) { 317 if (dbg.on) { 318 dbg.println("Cursor.getSystemCustomCursor(" + name + ") returned null"); 319 } 320 } else { 321 systemCustomCursors.put(name, cursor); 322 } 323 } 324 325 return cursor; 326 } 327 328 331 static public Cursor getDefaultCursor() { 332 return getPredefinedCursor(Cursor.DEFAULT_CURSOR); 333 } 334 335 341 public Cursor(int type) { 342 if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) { 343 throw new IllegalArgumentException ("illegal cursor type"); 344 } 345 this.type = type; 346 347 name = Toolkit.getProperty(cursorProperties[type][0], 349 cursorProperties[type][1]); 350 } 351 352 360 protected Cursor(String name) { 361 this.type = Cursor.CUSTOM_CURSOR; 362 this.name = name; 363 } 364 365 368 public int getType() { 369 return type; 370 } 371 372 377 public String getName() { 378 return name; 379 } 380 381 386 public String toString() { 387 return getClass().getName() + "[" + getName() + "]"; 388 } 389 390 393 private static void loadSystemCustomCursorProperties() throws AWTException { 394 synchronized(systemCustomCursors) { 395 systemCustomCursorProperties = new Properties (); 396 397 try { 398 AccessController.doPrivileged( 399 new java.security.PrivilegedExceptionAction () { 400 public Object run() throws Exception { 401 FileInputStream fis = null; 402 try { 403 fis = new FileInputStream ( 404 systemCustomCursorPropertiesFile); 405 systemCustomCursorProperties.load(fis); 406 } finally { 407 if (fis != null) 408 fis.close(); 409 } 410 return null; 411 } 412 }); 413 } catch (Exception e) { 414 systemCustomCursorProperties = null; 415 throw new AWTException ("Exception: " + e.getClass() + " " + 416 e.getMessage() + " occurred while loading: " + 417 systemCustomCursorPropertiesFile); 418 } 419 } 420 } 421 422 private native static void finalizeImpl(long pData); 423 } 424 425 | Popular Tags |