KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > Cursor


1 /*
2  * @(#)Cursor.java 1.43 06/10/23
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package java.awt;
8
9 import java.awt.AWTException JavaDoc;
10 import java.awt.Point JavaDoc;
11 import java.awt.Toolkit JavaDoc;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15
16 import java.util.Enumeration JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.util.StringTokenizer JavaDoc;
20
21 import java.security.AccessController JavaDoc;
22
23 import sun.awt.DebugHelper;
24
25 /**
26  * A class to encapsulate the bitmap representation of the mouse cursor.
27  *
28  * @see Component#setCursor
29  * @version 1.43, 10/23/06
30  * @author Amy Fowler
31  */

32 public class Cursor implements java.io.Serializable JavaDoc {
33
34     /**
35      * The default cursor type (gets set if no cursor is defined).
36      */

37     public static final int DEFAULT_CURSOR = 0;
38
39     /**
40      * The crosshair cursor type.
41      */

42     public static final int CROSSHAIR_CURSOR = 1;
43
44     /**
45      * The text cursor type.
46      */

47     public static final int TEXT_CURSOR = 2;
48
49     /**
50      * The wait cursor type.
51      */

52     public static final int WAIT_CURSOR = 3;
53
54     /**
55      * The south-west-resize cursor type.
56      */

57     public static final int SW_RESIZE_CURSOR = 4;
58
59     /**
60      * The south-east-resize cursor type.
61      */

62     public static final int SE_RESIZE_CURSOR = 5;
63
64     /**
65      * The north-west-resize cursor type.
66      */

67     public static final int NW_RESIZE_CURSOR = 6;
68
69     /**
70      * The north-east-resize cursor type.
71      */

72     public static final int NE_RESIZE_CURSOR = 7;
73
74     /**
75      * The north-resize cursor type.
76      */

77     public static final int N_RESIZE_CURSOR = 8;
78
79     /**
80      * The south-resize cursor type.
81      */

82     public static final int S_RESIZE_CURSOR = 9;
83
84     /**
85      * The west-resize cursor type.
86      */

87     public static final int W_RESIZE_CURSOR = 10;
88
89     /**
90      * The east-resize cursor type.
91      */

92     public static final int E_RESIZE_CURSOR = 11;
93
94     /**
95      * The hand cursor type.
96      */

97     public static final int HAND_CURSOR = 12;
98
99     /**
100      * The move cursor type.
101      */

102     public static final int MOVE_CURSOR = 13;
103
104     protected static Cursor JavaDoc predefined[] = new Cursor JavaDoc[14];
105
106     /* Localization names and default values */
107     static final String JavaDoc[][] 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     /**
125      * The chosen cursor type initially set to
126      * the <code>DEFAULT_CURSOR</code>.
127      *
128      * @serial
129      * @see #getType()
130      */

131     int type = DEFAULT_CURSOR;
132
133     /**
134      * The type associated with all custom cursors.
135      */

136     public static final int CUSTOM_CURSOR = -1;
137
138     /*
139      * hashtable, filesystem dir prefix, filename, and properties for custom cursors support
140      */

141
142     private static final Hashtable JavaDoc systemCustomCursors = new Hashtable JavaDoc(1);
143     private static final String JavaDoc systemCustomCursorDirPrefix = initCursorDir();
144
145     private static String JavaDoc initCursorDir() {
146     String JavaDoc jhome = (String JavaDoc) 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 JavaDoc systemCustomCursorPropertiesFile = systemCustomCursorDirPrefix + "cursors.properties";
154
155     private static Properties JavaDoc systemCustomCursorProperties = null;
156
157     private static final String JavaDoc CursorDotPrefix = "Cursor.";
158     private static final String JavaDoc DotFileSuffix = ".File";
159     private static final String JavaDoc DotHotspotSuffix = ".HotSpot";
160     private static final String JavaDoc DotNameSuffix = ".Name";
161
162     /*
163      * JDK 1.1 serialVersionUID
164      */

165     private static final long serialVersionUID = 8028237497568985504L;
166
167     private static final DebugHelper dbg = DebugHelper.create(Cursor JavaDoc.class);
168
169     static {
170         /* ensure that the necessary native libraries are loaded */
171     Toolkit.loadLibraries();
172         if (!GraphicsEnvironment.isHeadless()) {
173             initIDs();
174         }
175     }
176
177     /**
178      * Initialize JNI field and method IDs for fields that may be
179      * accessed from C.
180      */

181     private static native void initIDs();
182
183     /**
184      * Hook into native data.
185      */

186     private transient long pData;
187
188     private transient Object JavaDoc anchor = new Object JavaDoc();
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                 // anchor is null after deserialization
202
if (anchor == null) {
203                     anchor = new Object JavaDoc();
204                 }
205                 disposer = new CursorDisposer();
206                 sun.java2d.Disposer.addRecord(anchor, disposer);
207             }
208             disposer.pData = pData;
209         }
210     }
211
212     /**
213      * The user-visible name of the cursor.
214      *
215      * @serial
216      * @see #getName()
217      */

218     protected String JavaDoc name;
219
220     /**
221      * Returns a cursor object with the specified predefined type.
222      *
223      * @param type the type of predefined cursor
224      * @return the specified predefined cursor
225      * @throws IllegalArgumentException if the specified cursor type is
226      * invalid
227      */

228     static public Cursor JavaDoc getPredefinedCursor(int type) {
229     if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
230         throw new IllegalArgumentException JavaDoc("illegal cursor type");
231     }
232     if (predefined[type] == null) {
233         predefined[type] = new Cursor JavaDoc(type);
234     }
235     return predefined[type];
236     }
237
238     /**
239      * Returns a system-specific custom cursor object matching the
240      * specified name. Cursor names are, for example: "Invalid.16x16"
241      *
242      * @param name a string describing the desired system-specific custom cursor
243      * @return the system specific custom cursor named
244      * @exception HeadlessException if
245      * <code>GraphicsEnvironment.isHeadless</code> returns true
246      */

247     static public Cursor JavaDoc getSystemCustomCursor(final String JavaDoc name)
248     throws AWTException JavaDoc, HeadlessException JavaDoc {
249         GraphicsEnvironment.checkHeadless();
250     Cursor JavaDoc cursor = (Cursor JavaDoc)systemCustomCursors.get(name);
251
252     if (cursor == null) {
253         synchronized(systemCustomCursors) {
254         if (systemCustomCursorProperties == null)
255             loadSystemCustomCursorProperties();
256         }
257
258         String JavaDoc prefix = CursorDotPrefix + name;
259         String JavaDoc 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 JavaDoc fileName =
269         systemCustomCursorProperties.getProperty(key);
270
271         String JavaDoc localized = (String JavaDoc)systemCustomCursorProperties.getProperty(prefix + DotNameSuffix);
272
273         if (localized == null) localized = name;
274
275         String JavaDoc hotspot = (String JavaDoc)systemCustomCursorProperties.getProperty(prefix + DotHotspotSuffix);
276
277         if (hotspot == null)
278             throw new AWTException JavaDoc("no hotspot property defined for cursor: " + name);
279
280         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(hotspot, ",");
281
282         if (st.countTokens() != 2)
283             throw new AWTException JavaDoc("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 JavaDoc nfe) {
292             throw new AWTException JavaDoc("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 JavaDoc flocalized = localized;
299
300                 cursor = (Cursor JavaDoc) java.security.AccessController.doPrivileged(
301             new java.security.PrivilegedExceptionAction JavaDoc() {
302             public Object JavaDoc run() throws Exception JavaDoc {
303             Toolkit JavaDoc toolkit = Toolkit.getDefaultToolkit();
304             Image JavaDoc image = toolkit.getImage(
305                systemCustomCursorDirPrefix + fileName);
306             return toolkit.createCustomCursor(
307                     image, new Point JavaDoc(fx,fy), flocalized);
308             }
309         });
310         } catch (Exception JavaDoc e) {
311         throw new AWTException JavaDoc(
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     /**
329      * Return the system default cursor.
330      */

331     static public Cursor JavaDoc getDefaultCursor() {
332         return getPredefinedCursor(Cursor.DEFAULT_CURSOR);
333     }
334
335     /**
336      * Creates a new cursor object with the specified type.
337      * @param type the type of cursor
338      * @throws IllegalArgumentException if the specified cursor type
339      * is invalid
340      */

341     public Cursor(int type) {
342     if (type < Cursor.DEFAULT_CURSOR || type > Cursor.MOVE_CURSOR) {
343         throw new IllegalArgumentException JavaDoc("illegal cursor type");
344     }
345     this.type = type;
346
347         // Lookup localized name.
348
name = Toolkit.getProperty(cursorProperties[type][0],
349                                    cursorProperties[type][1]);
350     }
351
352     /**
353      * Creates a new custom cursor object with the specified name.<p>
354      * Note: this constructor should only be used by AWT implementations
355      * as part of their support for custom cursors. Applications should
356      * use Toolkit.createCustomCursor().
357      * @param name the user-visible name of the cursor.
358      * @see java.awt.Toolkit#createCustomCursor
359      */

360     protected Cursor(String JavaDoc name) {
361         this.type = Cursor.CUSTOM_CURSOR;
362         this.name = name;
363     }
364
365     /**
366      * Returns the type for this cursor.
367      */

368     public int getType() {
369     return type;
370     }
371
372     /**
373      * Returns the name of this cursor.
374      * @return a localized description of this cursor.
375      * @since 1.2
376      */

377     public String JavaDoc getName() {
378     return name;
379     }
380
381     /**
382      * Returns a string representation of this cursor.
383      * @return a string representation of this cursor.
384      * @since 1.2
385      */

386     public String JavaDoc toString() {
387     return getClass().getName() + "[" + getName() + "]";
388     }
389
390     /*
391      * load the cursor.properties file
392      */

393     private static void loadSystemCustomCursorProperties() throws AWTException JavaDoc {
394     synchronized(systemCustomCursors) {
395         systemCustomCursorProperties = new Properties JavaDoc();
396
397             try {
398             AccessController.doPrivileged(
399                       new java.security.PrivilegedExceptionAction JavaDoc() {
400             public Object JavaDoc run() throws Exception JavaDoc {
401             FileInputStream JavaDoc fis = null;
402             try {
403                 fis = new FileInputStream JavaDoc(
404                        systemCustomCursorPropertiesFile);
405                 systemCustomCursorProperties.load(fis);
406             } finally {
407                 if (fis != null)
408                 fis.close();
409             }
410             return null;
411             }
412         });
413         } catch (Exception JavaDoc e) {
414         systemCustomCursorProperties = null;
415          throw new AWTException JavaDoc("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