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;
328 }
|
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 }
315 }
316
317 return desktop;
318 }
319
320 /**
321 * Tests whether this class is supported on the current platform.
322 * If it's supported, use {@link #getDesktop()} to retrieve an
323 * instance.
324 *
325 * @return {@code true} if this class is supported on the
326 * current platform; {@code false} otherwise
327 * @see #getDesktop()
328 */
329 public static boolean isDesktopSupported(){
330 Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
331 if (defaultToolkit instanceof SunToolkit) {
332 return ((SunToolkit)defaultToolkit).isDesktopSupported();
333 }
334 return false;
335 }
|