forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktop.java
More file actions
1042 lines (973 loc) · 37.9 KB
/
Copy pathDesktop.java
File metadata and controls
1042 lines (973 loc) · 37.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2005, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.awt;
import java.awt.desktop.AboutEvent;
import java.awt.desktop.AboutHandler;
import java.awt.desktop.OpenFilesHandler;
import java.awt.desktop.OpenURIEvent;
import java.awt.desktop.OpenURIHandler;
import java.awt.desktop.PreferencesEvent;
import java.awt.desktop.PreferencesHandler;
import java.awt.desktop.PrintFilesHandler;
import java.awt.desktop.QuitHandler;
import java.awt.desktop.QuitStrategy;
import java.awt.desktop.SystemEventListener;
import java.awt.peer.DesktopPeer;
import java.io.File;
import java.io.FilePermission;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Objects;
import javax.swing.JMenuBar;
import sun.awt.SunToolkit;
import sun.security.util.SecurityConstants;
/**
* The {@code Desktop} class allows interact with various desktop capabilities.
*
* <p> Supported operations include:
* <ul>
* <li>launching the user-default browser to show a specified
* URI;</li>
* <li>launching the user-default mail client with an optional
* {@code mailto} URI;</li>
* <li>launching a registered application to open, edit or print a
* specified file.</li>
* </ul>
*
* <p> This class provides methods corresponding to these
* operations. The methods look for the associated application
* registered on the current platform, and launch it to handle a URI
* or file. If there is no associated application or the associated
* application fails to be launched, an exception is thrown.
*
* Please see {@link Desktop.Action} for the full list of supported operations
* and capabilities.
*
* <p> An application is registered to a URI or file type.
* The mechanism of registering, accessing, and
* launching the associated application is platform-dependent.
*
* <p> Each operation is an action type represented by the {@link
* Desktop.Action} class.
*
* <p> Note: when some action is invoked and the associated
* application is executed, it will be executed on the same system as
* the one on which the Java application was launched.
*
* @see Action
*
* @since 1.6
* @author Armin Chen
* @author George Zhang
*/
public class Desktop {
/**
* Represents an action type. Each platform supports a different
* set of actions. You may use the {@link Desktop#isSupported}
* method to determine if the given action is supported by the
* current platform.
* @see java.awt.Desktop#isSupported(java.awt.Desktop.Action)
* @since 1.6
*/
public static enum Action {
/**
* Represents an "open" action.
* @see Desktop#open(java.io.File)
*/
OPEN,
/**
* Represents an "edit" action.
* @see Desktop#edit(java.io.File)
*/
EDIT,
/**
* Represents a "print" action.
* @see Desktop#print(java.io.File)
*/
PRINT,
/**
* Represents a "mail" action.
* @see Desktop#mail()
* @see Desktop#mail(java.net.URI)
*/
MAIL,
/**
* Represents a "browse" action.
* @see Desktop#browse(java.net.URI)
*/
BROWSE,
/**
* Represents an AppForegroundListener
* @see java.awt.desktop.AppForegroundListener
* @since 9
*/
APP_EVENT_FOREGROUND,
/**
* Represents an AppHiddenListener
* @see java.awt.desktop.AppHiddenListener
* @since 9
*/
APP_EVENT_HIDDEN,
/**
* Represents an AppReopenedListener
* @see java.awt.desktop.AppReopenedListener
* @since 9
*/
APP_EVENT_REOPENED,
/**
* Represents a ScreenSleepListener
* @see java.awt.desktop.ScreenSleepListener
* @since 9
*/
APP_EVENT_SCREEN_SLEEP,
/**
* Represents a SystemSleepListener
* @see java.awt.desktop.SystemSleepListener
* @since 9
*/
APP_EVENT_SYSTEM_SLEEP,
/**
* Represents a UserSessionListener
* @see java.awt.desktop.UserSessionListener
* @since 9
*/
APP_EVENT_USER_SESSION,
/**
* Represents an AboutHandler
* @see #setAboutHandler(java.awt.desktop.AboutHandler)
* @since 9
*/
APP_ABOUT,
/**
* Represents a PreferencesHandler
* @see #setPreferencesHandler(java.awt.desktop.PreferencesHandler)
* @since 9
*/
APP_PREFERENCES,
/**
* Represents an OpenFilesHandler
* @see #setOpenFileHandler(java.awt.desktop.OpenFilesHandler)
* @since 9
*/
APP_OPEN_FILE,
/**
* Represents a PrintFilesHandler
* @see #setPrintFileHandler(java.awt.desktop.PrintFilesHandler)
* @since 9
*/
APP_PRINT_FILE,
/**
* Represents an OpenURIHandler
* @see #setOpenURIHandler(java.awt.desktop.OpenURIHandler)
* @since 9
*/
APP_OPEN_URI,
/**
* Represents a QuitHandler
* @see #setQuitHandler(java.awt.desktop.QuitHandler)
* @since 9
*/
APP_QUIT_HANDLER,
/**
* Represents a QuitStrategy
* @see #setQuitStrategy(java.awt.desktop.QuitStrategy)
* @since 9
*/
APP_QUIT_STRATEGY,
/**
* Represents a SuddenTermination
* @see #enableSuddenTermination()
* @since 9
*/
APP_SUDDEN_TERMINATION,
/**
* Represents a requestForeground
* @see #requestForeground(boolean)
* @since 9
*/
APP_REQUEST_FOREGROUND,
/**
* Represents a HelpViewer
* @see #openHelpViewer()
* @since 9
*/
APP_HELP_VIEWER,
/**
* Represents a menu bar
* @see #setDefaultMenuBar(javax.swing.JMenuBar)
* @since 9
*/
APP_MENU_BAR,
/**
* Represents a browse file directory
* @see #browseFileDirectory(java.io.File)
* @since 9
*/
BROWSE_FILE_DIR,
/**
* Represents a move to trash
* @see #moveToTrash(java.io.File)
* @since 9
*/
MOVE_TO_TRASH
};
private DesktopPeer peer;
/**
* Suppresses default constructor for noninstantiability.
*/
private Desktop() {
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
// same cast as in isDesktopSupported()
if (defaultToolkit instanceof SunToolkit) {
peer = ((SunToolkit) defaultToolkit).createDesktopPeer(this);
}
}
private void checkEventsProcessingPermission() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission(
"canProcessApplicationEvents"));
}
}
/**
* Returns the {@code Desktop} instance of the current
* desktop context. On some platforms the Desktop API may not be
* supported; use the {@link #isDesktopSupported} method to
* determine if the current desktop is supported.
* @return the Desktop instance
* @throws HeadlessException if {@link
* GraphicsEnvironment#isHeadless()} returns {@code true}
* @throws UnsupportedOperationException if this class is not
* supported on the current platform
* @see #isDesktopSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static synchronized Desktop getDesktop(){
if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
if (!Desktop.isDesktopSupported()) {
throw new UnsupportedOperationException("Desktop API is not " +
"supported on the current platform");
}
sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
Desktop desktop = (Desktop)context.get(Desktop.class);
if (desktop == null) {
desktop = new Desktop();
context.put(Desktop.class, desktop);
}
return desktop;
}
/**
* Tests whether this class is supported on the current platform.
* If it's supported, use {@link #getDesktop()} to retrieve an
* instance.
*
* @return {@code true} if this class is supported on the
* current platform; {@code false} otherwise
* @see #getDesktop()
*/
public static boolean isDesktopSupported(){
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
if (defaultToolkit instanceof SunToolkit) {
return ((SunToolkit)defaultToolkit).isDesktopSupported();
}
return false;
}
/**
* Tests whether an action is supported on the current platform.
*
* <p>Even when the platform supports an action, a file or URI may
* not have a registered application for the action. For example,
* most of the platforms support the {@link Desktop.Action#OPEN}
* action. But for a specific file, there may not be an
* application registered to open it. In this case, {@link
* #isSupported(Action)} may return {@code true}, but the corresponding
* action method will throw an {@link IOException}.
*
* @param action the specified {@link Action}
* @return {@code true} if the specified action is supported on
* the current platform; {@code false} otherwise
* @see Desktop.Action
*/
public boolean isSupported(Action action) {
return peer.isSupported(action);
}
/**
* Checks if the file is a valid file and readable.
*
* @throws SecurityException If a security manager exists and its
* {@link SecurityManager#checkRead(java.lang.String)} method
* denies read access to the file
* @throws NullPointerException if file is null
* @throws IllegalArgumentException if file doesn't exist
*/
private static void checkFileValidation(File file) {
if (!file.exists()) {
throw new IllegalArgumentException("The file: "
+ file.getPath() + " doesn't exist.");
}
}
/**
* Checks if the action type is supported.
*
* @param actionType the action type in question
* @throws UnsupportedOperationException if the specified action type is not
* supported on the current platform
*/
private void checkActionSupport(Action actionType){
if (!isSupported(actionType)) {
throw new UnsupportedOperationException("The " + actionType.name()
+ " action is not supported on the current platform!");
}
}
/**
* Calls to the security manager's {@code checkPermission} method with an
* {@code AWTPermission("showWindowWithoutWarningBanner")} permission. This
* permission is needed, because we cannot add a security warning icon to
* the windows of the external native application.
*/
private void checkAWTPermission() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new AWTPermission(
"showWindowWithoutWarningBanner"));
}
}
/**
* Launches the associated application to open the file.
*
* <p> If the specified file is a directory, the file manager of
* the current platform is launched to open it.
*
* @param file the file to be opened with the associated application
* @throws NullPointerException if {@code file} is {@code null}
* @throws IllegalArgumentException if the specified file doesn't
* exist
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#OPEN} action
* @throws IOException if the specified file has no associated
* application or the associated application fails to be launched
* @throws SecurityException if a security manager exists and its
* {@link java.lang.SecurityManager#checkRead(java.lang.String)}
* method denies read access to the file, or it denies the
* {@code AWTPermission("showWindowWithoutWarningBanner")}
* permission, or the calling thread is not allowed to create a
* subprocess
* @see java.awt.AWTPermission
*/
public void open(File file) throws IOException {
file = new File(file.getPath());
checkAWTPermission();
checkExec();
checkActionSupport(Action.OPEN);
checkFileValidation(file);
peer.open(file);
}
/**
* Launches the associated editor application and opens a file for
* editing.
*
* @param file the file to be opened for editing
* @throws NullPointerException if the specified file is {@code null}
* @throws IllegalArgumentException if the specified file doesn't
* exist
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#EDIT} action
* @throws IOException if the specified file has no associated
* editor, or the associated application fails to be launched
* @throws SecurityException if a security manager exists and its
* {@link java.lang.SecurityManager#checkRead(java.lang.String)}
* method denies read access to the file, or {@link
* java.lang.SecurityManager#checkWrite(java.lang.String)} method
* denies write access to the file, or it denies the
* {@code AWTPermission("showWindowWithoutWarningBanner")}
* permission, or the calling thread is not allowed to create a
* subprocess
* @see java.awt.AWTPermission
*/
public void edit(File file) throws IOException {
file = new File(file.getPath());
checkAWTPermission();
checkExec();
checkActionSupport(Action.EDIT);
file.canWrite();
checkFileValidation(file);
if (file.isDirectory()) {
throw new IOException(file.getPath() + " is a directory");
}
peer.edit(file);
}
/**
* Prints a file with the native desktop printing facility, using
* the associated application's print command.
*
* @param file the file to be printed
* @throws NullPointerException if the specified file is {@code
* null}
* @throws IllegalArgumentException if the specified file doesn't
* exist
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#PRINT} action
* @throws IOException if the specified file has no associated
* application that can be used to print it
* @throws SecurityException if a security manager exists and its
* {@link java.lang.SecurityManager#checkRead(java.lang.String)}
* method denies read access to the file, or its {@link
* java.lang.SecurityManager#checkPrintJobAccess()} method denies
* the permission to print the file, or the calling thread is not
* allowed to create a subprocess
*/
public void print(File file) throws IOException {
file = new File(file.getPath());
checkExec();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPrintJobAccess();
}
checkActionSupport(Action.PRINT);
checkFileValidation(file);
if (file.isDirectory()) {
throw new IOException(file.getPath() + " is a directory");
}
peer.print(file);
}
/**
* Launches the default browser to display a {@code URI}.
* If the default browser is not able to handle the specified
* {@code URI}, the application registered for handling
* {@code URIs} of the specified type is invoked. The application
* is determined from the protocol and path of the {@code URI}, as
* defined by the {@code URI} class.
*
* @param uri the URI to be displayed in the user default browser
* @throws NullPointerException if {@code uri} is {@code null}
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#BROWSE} action
* @throws IOException if the user default browser is not found,
* or it fails to be launched, or the default handler application
* failed to be launched
* @throws SecurityException if a security manager exists and it
* denies the
* {@code AWTPermission("showWindowWithoutWarningBanner")}
* permission, or the calling thread is not allowed to create a
* subprocess
* @see java.net.URI
* @see java.awt.AWTPermission
*/
public void browse(URI uri) throws IOException {
checkAWTPermission();
checkExec();
checkActionSupport(Action.BROWSE);
Objects.requireNonNull(uri);
peer.browse(uri);
}
/**
* Launches the mail composing window of the user default mail
* client.
*
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#MAIL} action
* @throws IOException if the user default mail client is not
* found, or it fails to be launched
* @throws SecurityException if a security manager exists and it
* denies the
* {@code AWTPermission("showWindowWithoutWarningBanner")}
* permission, or the calling thread is not allowed to create a
* subprocess
* @see java.awt.AWTPermission
*/
public void mail() throws IOException {
checkAWTPermission();
checkExec();
checkActionSupport(Action.MAIL);
URI mailtoURI = null;
try{
mailtoURI = new URI("mailto:?");
peer.mail(mailtoURI);
} catch (URISyntaxException e){
// won't reach here.
}
}
/**
* Launches the mail composing window of the user default mail
* client, filling the message fields specified by a {@code
* mailto:} URI.
*
* <p> A {@code mailto:} URI can specify message fields
* including <i>"to"</i>, <i>"cc"</i>, <i>"subject"</i>,
* <i>"body"</i>, etc. See <a
* href="http://www.ietf.org/rfc/rfc2368.txt">The mailto URL
* scheme (RFC 2368)</a> for the {@code mailto:} URI specification
* details.
*
* @param mailtoURI the specified {@code mailto:} URI
* @throws NullPointerException if the specified URI is {@code
* null}
* @throws IllegalArgumentException if the URI scheme is not
* {@code "mailto"}
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#MAIL} action
* @throws IOException if the user default mail client is not
* found or fails to be launched
* @throws SecurityException if a security manager exists and it
* denies the
* {@code AWTPermission("showWindowWithoutWarningBanner")}
* permission, or the calling thread is not allowed to create a
* subprocess
* @see java.net.URI
* @see java.awt.AWTPermission
*/
public void mail(URI mailtoURI) throws IOException {
checkAWTPermission();
checkExec();
checkActionSupport(Action.MAIL);
if (mailtoURI == null) throw new NullPointerException();
if (!"mailto".equalsIgnoreCase(mailtoURI.getScheme())) {
throw new IllegalArgumentException("URI scheme is not \"mailto\"");
}
peer.mail(mailtoURI);
}
private void checkExec() throws SecurityException {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new FilePermission("<<ALL FILES>>",
SecurityConstants.FILE_EXECUTE_ACTION));
}
}
private void checkRead() throws SecurityException {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new FilePermission("<<ALL FILES>>",
SecurityConstants.FILE_READ_ACTION));
}
}
private void checkQuitPermission() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkExit(0);
}
}
/**
* Adds sub-types of {@link SystemEventListener} to listen for notifications
* from the native system.
*
* Has no effect if SystemEventListener's sub-type is unsupported on the current
* platform.
*
* @param listener listener
*
* @throws SecurityException if a security manager exists and it
* denies the
* {@code RuntimePermission("canProcessApplicationEvents")}
* permission
*
* @see java.awt.desktop.AppForegroundListener
* @see java.awt.desktop.AppHiddenListener
* @see java.awt.desktop.AppReopenedListener
* @see java.awt.desktop.ScreenSleepListener
* @see java.awt.desktop.SystemSleepListener
* @see java.awt.desktop.UserSessionListener
* @since 9
*/
public void addAppEventListener(final SystemEventListener listener) {
checkEventsProcessingPermission();
peer.addAppEventListener(listener);
}
/**
* Removes sub-types of {@link SystemEventListener} to listen for notifications
* from the native system.
*
* Has no effect if SystemEventListener's sub-type is unsupported on the current
* platform.
*
* @param listener listener
*
* @throws SecurityException if a security manager exists and it
* denies the
* {@code RuntimePermission("canProcessApplicationEvents")}
* permission
*
* @see java.awt.desktop.AppForegroundListener
* @see java.awt.desktop.AppHiddenListener
* @see java.awt.desktop.AppReopenedListener
* @see java.awt.desktop.ScreenSleepListener
* @see java.awt.desktop.SystemSleepListener
* @see java.awt.desktop.UserSessionListener
* @since 9
*/
public void removeAppEventListener(final SystemEventListener listener) {
checkEventsProcessingPermission();
peer.removeAppEventListener(listener);
}
/**
* Installs a handler to show a custom About window for your application.
* <p>
* Setting the {@link java.awt.desktop.AboutHandler} to {@code null} reverts it to the
* default behavior.
*
* @param aboutHandler the handler to respond to the
* {@link java.awt.desktop.AboutHandler#handleAbout(AboutEvent)} message
*
* @throws SecurityException if a security manager exists and it
* denies the
* {@code RuntimePermission("canProcessApplicationEvents")}
* permission
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_ABOUT} action
*
* @since 9
*/
public void setAboutHandler(final AboutHandler aboutHandler) {
checkEventsProcessingPermission();
checkActionSupport(Action.APP_ABOUT);
peer.setAboutHandler(aboutHandler);
}
/**
* Installs a handler to show a custom Preferences window for your
* application.
* <p>
* Setting the {@link PreferencesHandler} to {@code null} reverts it to
* the default behavior
*
* @param preferencesHandler the handler to respond to the
* {@link PreferencesHandler#handlePreferences(PreferencesEvent)}
*
* @throws SecurityException if a security manager exists and it
* denies the
* {@code RuntimePermission("canProcessApplicationEvents")} permission
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_PREFERENCES} action
* @since 9
*/
public void setPreferencesHandler(final PreferencesHandler preferencesHandler) {
checkEventsProcessingPermission();
checkActionSupport(Action.APP_PREFERENCES);
peer.setPreferencesHandler(preferencesHandler);
}
/**
* Installs the handler which is notified when the application is asked to
* open a list of files.
*
* @implNote Please note that for macOS, notifications
* are only sent if the Java app is a bundled application,
* with a {@code CFBundleDocumentTypes} array present in its
* {@code Info.plist}. Check the
* <a href="https://developer.apple.com/documentation">
* Apple Developer Documentation</a> for more information about
* {@code Info.plist}.
*
* @param openFileHandler handler
*
* @throws SecurityException if a security manager exists and its
* {@link java.lang.SecurityManager#checkRead(java.lang.String)}
* method denies read access to the files, or it denies the
* {@code RuntimePermission("canProcessApplicationEvents")}
* permission, or the calling thread is not allowed to create a
* subprocess
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_OPEN_FILE} action
* @since 9
*/
public void setOpenFileHandler(final OpenFilesHandler openFileHandler) {
checkEventsProcessingPermission();
checkExec();
checkRead();
checkActionSupport(Action.APP_OPEN_FILE);
peer.setOpenFileHandler(openFileHandler);
}
/**
* Installs the handler which is notified when the application is asked to
* print a list of files.
*
* @implNote Please note that for macOS, notifications
* are only sent if the Java app is a bundled application,
* with a {@code CFBundleDocumentTypes} array present in its
* {@code Info.plist}. Check the
* <a href="https://developer.apple.com/documentation">
* Apple Developer Documentation</a> for more information about
* {@code Info.plist}.
*
* @param printFileHandler handler
* @throws SecurityException if a security manager exists and its
* {@link java.lang.SecurityManager#checkPrintJobAccess()} method denies
* the permission to print or it denies the
* {@code RuntimePermission("canProcessApplicationEvents")} permission
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_PRINT_FILE} action
* @since 9
*/
public void setPrintFileHandler(final PrintFilesHandler printFileHandler) {
checkEventsProcessingPermission();
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPrintJobAccess();
}
checkActionSupport(Action.APP_PRINT_FILE);
peer.setPrintFileHandler(printFileHandler);
}
/**
* Installs the handler which is notified when the application is asked to
* open a URL.
*
* Setting the handler to {@code null} causes all
* {@link OpenURIHandler#openURI(OpenURIEvent)} requests to be
* enqueued until another handler is set.
*
* @implNote Please note that for macOS, notifications
* are only sent if the Java app is a bundled application,
* with a {@code CFBundleDocumentTypes} array present in its
* {@code Info.plist}. Check the
* <a href="https://developer.apple.com/documentation">
* Apple Developer Documentation</a> for more information about
* {@code Info.plist}.
*
* @param openURIHandler handler
*
* {@code RuntimePermission("canProcessApplicationEvents")}
* permission, or the calling thread is not allowed to create a
* subprocess
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_OPEN_URI} action
* @since 9
*/
public void setOpenURIHandler(final OpenURIHandler openURIHandler) {
checkEventsProcessingPermission();
checkExec();
checkActionSupport(Action.APP_OPEN_URI);
peer.setOpenURIHandler(openURIHandler);
}
/**
* Installs the handler which determines if the application should quit. The
* handler is passed a one-shot {@link java.awt.desktop.QuitResponse} which can cancel or
* proceed with the quit. Setting the handler to {@code null} causes
* all quit requests to directly perform the default {@link QuitStrategy}.
*
* @param quitHandler the handler that is called when the application is
* asked to quit
*
* @throws SecurityException if a security manager exists and it
* will not allow the caller to invoke {@code System.exit} or it denies the
* {@code RuntimePermission("canProcessApplicationEvents")} permission
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_QUIT_HANDLER} action
* @since 9
*/
public void setQuitHandler(final QuitHandler quitHandler) {
checkEventsProcessingPermission();
checkQuitPermission();
checkActionSupport(Action.APP_QUIT_HANDLER);
peer.setQuitHandler(quitHandler);
}
/**
* Sets the default strategy used to quit this application. The default is
* calling SYSTEM_EXIT_0.
*
* @param strategy the way this application should be shutdown
*
* @throws SecurityException if a security manager exists and it
* will not allow the caller to invoke {@code System.exit} or it denies the
* {@code RuntimePermission("canProcessApplicationEvents")} permission
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_QUIT_STRATEGY} action
* @see QuitStrategy
* @since 9
*/
public void setQuitStrategy(final QuitStrategy strategy) {
checkEventsProcessingPermission();
checkQuitPermission();
checkActionSupport(Action.APP_QUIT_STRATEGY);
peer.setQuitStrategy(strategy);
}
/**
* Enables this application to be suddenly terminated.
*
* Call this method to indicate your application's state is saved, and
* requires no notification to be terminated. Letting your application
* remain terminatable improves the user experience by avoiding re-paging in
* your application when it's asked to quit.
*
* <b>Note: enabling sudden termination will allow your application to be
* quit without notifying your QuitHandler, or running any shutdown
* hooks.</b>
* E.g. user-initiated Cmd-Q, logout, restart, or shutdown requests will
* effectively "kill -KILL" your application.
*
* @throws SecurityException if a security manager exists and it
* will not allow the caller to invoke {@code System.exit} or it denies the
* {@code RuntimePermission("canProcessApplicationEvents")} permission
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_SUDDEN_TERMINATION} action
* @see #disableSuddenTermination()
* @since 9
*/
public void enableSuddenTermination() {
checkEventsProcessingPermission();
checkQuitPermission();
checkActionSupport(Action.APP_SUDDEN_TERMINATION);
peer.enableSuddenTermination();
}
/**
* Prevents this application from being suddenly terminated.
*
* Call this method to indicate that your application has unsaved state, and
* may not be terminated without notification.
*
* @throws SecurityException if a security manager exists and it
* will not allow the caller to invoke {@code System.exit} or it denies the
* {@code RuntimePermission("canProcessApplicationEvents")} permission
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_SUDDEN_TERMINATION} action
* @see #enableSuddenTermination()
* @since 9
*/
public void disableSuddenTermination() {
checkEventsProcessingPermission();
checkQuitPermission();
checkActionSupport(Action.APP_SUDDEN_TERMINATION);
peer.disableSuddenTermination();
}
/**
* Requests this application to move to the foreground.
*
* @param allWindows if all windows of this application should be moved to
* the foreground, or only the foremost one
* @throws SecurityException if a security manager exists and it denies the
* {@code RuntimePermission("canProcessApplicationEvents")} permission.
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_REQUEST_FOREGROUND} action
* @since 9
*/
public void requestForeground(final boolean allWindows) {
checkEventsProcessingPermission();
checkActionSupport(Action.APP_REQUEST_FOREGROUND);
peer.requestForeground(allWindows);
}
/**
* Opens the native help viewer application.
*
* @implNote Please note that for Mac OS, it opens the native help viewer
* application if a Help Book has been added to the application bundler
* and registered in the Info.plist with CFBundleHelpBookFolder
*
* @throws SecurityException if a security manager exists and it denies the
* {@code RuntimePermission("canProcessApplicationEvents")}
* permission, or it denies the
* {@code AWTPermission("showWindowWithoutWarningBanner")}
* permission, or the calling thread is not allowed to create a
* subprocess
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_HELP_VIEWER} action
* @since 9
*/
public void openHelpViewer() {
checkAWTPermission();
checkExec();
checkEventsProcessingPermission();
checkActionSupport(Action.APP_HELP_VIEWER);
peer.openHelpViewer();
}
/**
* Sets the default menu bar to use when there are no active frames.
*
* @param menuBar to use when no other frames are active
* @throws SecurityException if a security manager exists and it denies the
* {@code RuntimePermission("canProcessApplicationEvents")} permission.
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#APP_MENU_BAR} action
* @since 9
*/
public void setDefaultMenuBar(final JMenuBar menuBar) {
checkEventsProcessingPermission();
checkActionSupport(Action.APP_MENU_BAR);
if (menuBar != null) {
Container parent = menuBar.getParent();
if (parent != null) {
parent.remove(menuBar);
menuBar.updateUI();
}
}
peer.setDefaultMenuBar(menuBar);
}
/**
* Opens a folder containing the {@code file} and selects it
* in a default system file manager.
* @param file the file
* @throws SecurityException If a security manager exists and its
* {@link SecurityManager#checkRead(java.lang.String)} method
* denies read access to the file or to its parent, or it denies the
* {@code AWTPermission("showWindowWithoutWarningBanner")}
* permission, or the calling thread is not allowed to create a
* subprocess
* @throws UnsupportedOperationException if the current platform
* does not support the {@link Desktop.Action#BROWSE_FILE_DIR} action
* @throws NullPointerException if {@code file} is {@code null}
* @throws IllegalArgumentException if the specified file or its parent
* doesn't exist
* @since 9
*/
public void browseFileDirectory(File file) {