< prev index next >

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

Print this page




 262 
 263     /**
 264      * Suppresses default constructor for noninstantiability.
 265      */
 266     private Desktop() {
 267         Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
 268         // same cast as in isDesktopSupported()
 269         if (defaultToolkit instanceof SunToolkit) {
 270             peer = ((SunToolkit) defaultToolkit).createDesktopPeer(this);
 271         }
 272     }
 273 
 274     private void checkEventsProcessingPermission() {
 275         SecurityManager sm = System.getSecurityManager();
 276         if (sm != null) {
 277             sm.checkPermission(new RuntimePermission(
 278                     "canProcessApplicationEvents"));
 279         }
 280     }
 281 


 282     /**
 283      * Returns the {@code Desktop} instance of the current
 284      * desktop context. On some platforms the Desktop API may not be
 285      * supported; use the {@link #isDesktopSupported} method to
 286      * determine if the current desktop is supported.
 287      * @return the Desktop instance
 288      * @throws HeadlessException if {@link
 289      * GraphicsEnvironment#isHeadless()} returns {@code true}
 290      * @throws UnsupportedOperationException if this class is not
 291      * supported on the current platform
 292      * @see #isDesktopSupported()
 293      * @see java.awt.GraphicsEnvironment#isHeadless
 294      */
 295     public static synchronized Desktop getDesktop(){
 296         if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
 297         if (!Desktop.isDesktopSupported()) {
 298             throw new UnsupportedOperationException("Desktop API is not " +
 299                                                     "supported on the current platform");
 300         }
 301 
 302         sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
 303         Desktop desktop = (Desktop)context.get(Desktop.class);



 304 
 305         if (desktop == null) {
 306             desktop = new Desktop();

 307             context.put(Desktop.class, desktop);



 308         }
 309 
 310         return desktop;
 311     }
 312 
 313     /**
 314      * Tests whether this class is supported on the current platform.
 315      * If it's supported, use {@link #getDesktop()} to retrieve an
 316      * instance.
 317      *
 318      * @return {@code true} if this class is supported on the
 319      *         current platform; {@code false} otherwise
 320      * @see #getDesktop()
 321      */
 322     public static boolean isDesktopSupported(){
 323         Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
 324         if (defaultToolkit instanceof SunToolkit) {
 325             return ((SunToolkit)defaultToolkit).isDesktopSupported();
 326         }
 327         return false;




 262 
 263     /**
 264      * Suppresses default constructor for noninstantiability.
 265      */
 266     private Desktop() {
 267         Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
 268         // same cast as in isDesktopSupported()
 269         if (defaultToolkit instanceof SunToolkit) {
 270             peer = ((SunToolkit) defaultToolkit).createDesktopPeer(this);
 271         }
 272     }
 273 
 274     private void checkEventsProcessingPermission() {
 275         SecurityManager sm = System.getSecurityManager();
 276         if (sm != null) {
 277             sm.checkPermission(new RuntimePermission(
 278                     "canProcessApplicationEvents"));
 279         }
 280     }
 281 
 282     private static Desktop desktop;
 283 
 284     /**
 285      * Returns the {@code Desktop} instance of the current
 286      * desktop context. On some platforms the Desktop API may not be
 287      * supported; use the {@link #isDesktopSupported} method to
 288      * determine if the current desktop is supported.
 289      * @return the Desktop instance
 290      * @throws HeadlessException if {@link
 291      * GraphicsEnvironment#isHeadless()} returns {@code true}
 292      * @throws UnsupportedOperationException if this class is not
 293      * supported on the current platform
 294      * @see #isDesktopSupported()
 295      * @see java.awt.GraphicsEnvironment#isHeadless
 296      */
 297     public static synchronized Desktop getDesktop(){
 298         if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
 299         if (!Desktop.isDesktopSupported()) {
 300             throw new UnsupportedOperationException("Desktop API is not " +
 301                                                     "supported on the current platform");
 302         }
 303 
 304         sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
 305 
 306         Desktop desktop = (context == null)
 307                 ? Desktop.desktop
 308                 : (Desktop)context.get(Desktop.class);
 309 
 310         if (desktop == null) {
 311             desktop = new Desktop();
 312             if (context != null) {
 313                 context.put(Desktop.class, desktop);
 314             } else {
 315                 Desktop.desktop = desktop;
 316             }
 317         }
 318 
 319         return desktop;
 320     }
 321 
 322     /**
 323      * Tests whether this class is supported on the current platform.
 324      * If it's supported, use {@link #getDesktop()} to retrieve an
 325      * instance.
 326      *
 327      * @return {@code true} if this class is supported on the
 328      *         current platform; {@code false} otherwise
 329      * @see #getDesktop()
 330      */
 331     public static boolean isDesktopSupported(){
 332         Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
 333         if (defaultToolkit instanceof SunToolkit) {
 334             return ((SunToolkit)defaultToolkit).isDesktopSupported();
 335         }
 336         return false;


< prev index next >