Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/Desktop.java
38829 views
1
/*
2
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.awt;
27
28
import java.io.File;
29
import java.io.IOException;
30
import java.net.URISyntaxException;
31
import java.net.URI;
32
import java.net.URL;
33
import java.net.MalformedURLException;
34
import java.awt.AWTPermission;
35
import java.awt.GraphicsEnvironment;
36
import java.awt.HeadlessException;
37
import java.awt.peer.DesktopPeer;
38
import sun.awt.SunToolkit;
39
import sun.awt.HeadlessToolkit;
40
import java.io.FilePermission;
41
import sun.security.util.SecurityConstants;
42
43
/**
44
* The {@code Desktop} class allows a Java application to launch
45
* associated applications registered on the native desktop to handle
46
* a {@link java.net.URI} or a file.
47
*
48
* <p> Supported operations include:
49
* <ul>
50
* <li>launching the user-default browser to show a specified
51
* URI;</li>
52
* <li>launching the user-default mail client with an optional
53
* {@code mailto} URI;</li>
54
* <li>launching a registered application to open, edit or print a
55
* specified file.</li>
56
* </ul>
57
*
58
* <p> This class provides methods corresponding to these
59
* operations. The methods look for the associated application
60
* registered on the current platform, and launch it to handle a URI
61
* or file. If there is no associated application or the associated
62
* application fails to be launched, an exception is thrown.
63
*
64
* <p> An application is registered to a URI or file type; for
65
* example, the {@code "sxi"} file extension is typically registered
66
* to StarOffice. The mechanism of registering, accessing, and
67
* launching the associated application is platform-dependent.
68
*
69
* <p> Each operation is an action type represented by the {@link
70
* Desktop.Action} class.
71
*
72
* <p> Note: when some action is invoked and the associated
73
* application is executed, it will be executed on the same system as
74
* the one on which the Java application was launched.
75
*
76
* @since 1.6
77
* @author Armin Chen
78
* @author George Zhang
79
*/
80
public class Desktop {
81
82
/**
83
* Represents an action type. Each platform supports a different
84
* set of actions. You may use the {@link Desktop#isSupported}
85
* method to determine if the given action is supported by the
86
* current platform.
87
* @see java.awt.Desktop#isSupported(java.awt.Desktop.Action)
88
* @since 1.6
89
*/
90
public static enum Action {
91
/**
92
* Represents an "open" action.
93
* @see Desktop#open(java.io.File)
94
*/
95
OPEN,
96
/**
97
* Represents an "edit" action.
98
* @see Desktop#edit(java.io.File)
99
*/
100
EDIT,
101
/**
102
* Represents a "print" action.
103
* @see Desktop#print(java.io.File)
104
*/
105
PRINT,
106
/**
107
* Represents a "mail" action.
108
* @see Desktop#mail()
109
* @see Desktop#mail(java.net.URI)
110
*/
111
MAIL,
112
/**
113
* Represents a "browse" action.
114
* @see Desktop#browse(java.net.URI)
115
*/
116
BROWSE
117
};
118
119
private DesktopPeer peer;
120
121
/**
122
* Suppresses default constructor for noninstantiability.
123
*/
124
private Desktop() {
125
peer = Toolkit.getDefaultToolkit().createDesktopPeer(this);
126
}
127
128
/**
129
* Returns the <code>Desktop</code> instance of the current
130
* browser context. On some platforms the Desktop API may not be
131
* supported; use the {@link #isDesktopSupported} method to
132
* determine if the current desktop is supported.
133
* @return the Desktop instance of the current browser context
134
* @throws HeadlessException if {@link
135
* GraphicsEnvironment#isHeadless()} returns {@code true}
136
* @throws UnsupportedOperationException if this class is not
137
* supported on the current platform
138
* @see #isDesktopSupported()
139
* @see java.awt.GraphicsEnvironment#isHeadless
140
*/
141
public static synchronized Desktop getDesktop(){
142
if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
143
if (!Desktop.isDesktopSupported()) {
144
throw new UnsupportedOperationException("Desktop API is not " +
145
"supported on the current platform");
146
}
147
148
sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
149
Desktop desktop = (Desktop)context.get(Desktop.class);
150
151
if (desktop == null) {
152
desktop = new Desktop();
153
context.put(Desktop.class, desktop);
154
}
155
156
return desktop;
157
}
158
159
/**
160
* Tests whether this class is supported on the current platform.
161
* If it's supported, use {@link #getDesktop()} to retrieve an
162
* instance.
163
*
164
* @return <code>true</code> if this class is supported on the
165
* current platform; <code>false</code> otherwise
166
* @see #getDesktop()
167
*/
168
public static boolean isDesktopSupported(){
169
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
170
if (defaultToolkit instanceof SunToolkit) {
171
return ((SunToolkit)defaultToolkit).isDesktopSupported();
172
}
173
return false;
174
}
175
176
/**
177
* Tests whether an action is supported on the current platform.
178
*
179
* <p>Even when the platform supports an action, a file or URI may
180
* not have a registered application for the action. For example,
181
* most of the platforms support the {@link Desktop.Action#OPEN}
182
* action. But for a specific file, there may not be an
183
* application registered to open it. In this case, {@link
184
* #isSupported} may return {@code true}, but the corresponding
185
* action method will throw an {@link IOException}.
186
*
187
* @param action the specified {@link Action}
188
* @return <code>true</code> if the specified action is supported on
189
* the current platform; <code>false</code> otherwise
190
* @see Desktop.Action
191
*/
192
public boolean isSupported(Action action) {
193
return peer.isSupported(action);
194
}
195
196
/**
197
* Checks if the file is a valid file and readable.
198
*
199
* @throws SecurityException If a security manager exists and its
200
* {@link SecurityManager#checkRead(java.lang.String)} method
201
* denies read access to the file
202
* @throws NullPointerException if file is null
203
* @throws IllegalArgumentException if file doesn't exist
204
*/
205
private static void checkFileValidation(File file) {
206
if (!file.exists()) {
207
throw new IllegalArgumentException("The file: "
208
+ file.getPath() + " doesn't exist.");
209
}
210
}
211
212
/**
213
* Checks if the action type is supported.
214
*
215
* @param actionType the action type in question
216
* @throws UnsupportedOperationException if the specified action type is not
217
* supported on the current platform
218
*/
219
private void checkActionSupport(Action actionType){
220
if (!isSupported(actionType)) {
221
throw new UnsupportedOperationException("The " + actionType.name()
222
+ " action is not supported on the current platform!");
223
}
224
}
225
226
227
/**
228
* Calls to the security manager's <code>checkPermission</code> method with
229
* an <code>AWTPermission("showWindowWithoutWarningBanner")</code>
230
* permission.
231
*/
232
private void checkAWTPermission(){
233
SecurityManager sm = System.getSecurityManager();
234
if (sm != null) {
235
sm.checkPermission(new AWTPermission(
236
"showWindowWithoutWarningBanner"));
237
}
238
}
239
240
/**
241
* Launches the associated application to open the file.
242
*
243
* <p> If the specified file is a directory, the file manager of
244
* the current platform is launched to open it.
245
*
246
* @param file the file to be opened with the associated application
247
* @throws NullPointerException if {@code file} is {@code null}
248
* @throws IllegalArgumentException if the specified file doesn't
249
* exist
250
* @throws UnsupportedOperationException if the current platform
251
* does not support the {@link Desktop.Action#OPEN} action
252
* @throws IOException if the specified file has no associated
253
* application or the associated application fails to be launched
254
* @throws SecurityException if a security manager exists and its
255
* {@link java.lang.SecurityManager#checkRead(java.lang.String)}
256
* method denies read access to the file, or it denies the
257
* <code>AWTPermission("showWindowWithoutWarningBanner")</code>
258
* permission, or the calling thread is not allowed to create a
259
* subprocess
260
* @see java.awt.AWTPermission
261
*/
262
public void open(File file) throws IOException {
263
file = new File(file.getPath());
264
checkAWTPermission();
265
checkExec();
266
checkActionSupport(Action.OPEN);
267
checkFileValidation(file);
268
269
peer.open(file);
270
}
271
272
/**
273
* Launches the associated editor application and opens a file for
274
* editing.
275
*
276
* @param file the file to be opened for editing
277
* @throws NullPointerException if the specified file is {@code null}
278
* @throws IllegalArgumentException if the specified file doesn't
279
* exist
280
* @throws UnsupportedOperationException if the current platform
281
* does not support the {@link Desktop.Action#EDIT} action
282
* @throws IOException if the specified file has no associated
283
* editor, or the associated application fails to be launched
284
* @throws SecurityException if a security manager exists and its
285
* {@link java.lang.SecurityManager#checkRead(java.lang.String)}
286
* method denies read access to the file, or {@link
287
* java.lang.SecurityManager#checkWrite(java.lang.String)} method
288
* denies write access to the file, or it denies the
289
* <code>AWTPermission("showWindowWithoutWarningBanner")</code>
290
* permission, or the calling thread is not allowed to create a
291
* subprocess
292
* @see java.awt.AWTPermission
293
*/
294
public void edit(File file) throws IOException {
295
file = new File(file.getPath());
296
checkAWTPermission();
297
checkExec();
298
checkActionSupport(Action.EDIT);
299
file.canWrite();
300
checkFileValidation(file);
301
302
peer.edit(file);
303
}
304
305
/**
306
* Prints a file with the native desktop printing facility, using
307
* the associated application's print command.
308
*
309
* @param file the file to be printed
310
* @throws NullPointerException if the specified file is {@code
311
* null}
312
* @throws IllegalArgumentException if the specified file doesn't
313
* exist
314
* @throws UnsupportedOperationException if the current platform
315
* does not support the {@link Desktop.Action#PRINT} action
316
* @throws IOException if the specified file has no associated
317
* application that can be used to print it
318
* @throws SecurityException if a security manager exists and its
319
* {@link java.lang.SecurityManager#checkRead(java.lang.String)}
320
* method denies read access to the file, or its {@link
321
* java.lang.SecurityManager#checkPrintJobAccess()} method denies
322
* the permission to print the file, or the calling thread is not
323
* allowed to create a subprocess
324
*/
325
public void print(File file) throws IOException {
326
file = new File(file.getPath());
327
checkExec();
328
SecurityManager sm = System.getSecurityManager();
329
if (sm != null) {
330
sm.checkPrintJobAccess();
331
}
332
checkActionSupport(Action.PRINT);
333
checkFileValidation(file);
334
335
peer.print(file);
336
}
337
338
/**
339
* Launches the default browser to display a {@code URI}.
340
* If the default browser is not able to handle the specified
341
* {@code URI}, the application registered for handling
342
* {@code URIs} of the specified type is invoked. The application
343
* is determined from the protocol and path of the {@code URI}, as
344
* defined by the {@code URI} class.
345
* <p>
346
* If the calling thread does not have the necessary permissions,
347
* and this is invoked from within an applet,
348
* {@code AppletContext.showDocument()} is used. Similarly, if the calling
349
* does not have the necessary permissions, and this is invoked from within
350
* a Java Web Started application, {@code BasicService.showDocument()}
351
* is used.
352
*
353
* @param uri the URI to be displayed in the user default browser
354
* @throws NullPointerException if {@code uri} is {@code null}
355
* @throws UnsupportedOperationException if the current platform
356
* does not support the {@link Desktop.Action#BROWSE} action
357
* @throws IOException if the user default browser is not found,
358
* or it fails to be launched, or the default handler application
359
* failed to be launched
360
* @throws SecurityException if a security manager exists and it
361
* denies the
362
* <code>AWTPermission("showWindowWithoutWarningBanner")</code>
363
* permission, or the calling thread is not allowed to create a
364
* subprocess; and not invoked from within an applet or Java Web Started
365
* application
366
* @throws IllegalArgumentException if the necessary permissions
367
* are not available and the URI can not be converted to a {@code URL}
368
* @see java.net.URI
369
* @see java.awt.AWTPermission
370
* @see java.applet.AppletContext
371
*/
372
public void browse(URI uri) throws IOException {
373
SecurityException securityException = null;
374
try {
375
checkAWTPermission();
376
checkExec();
377
} catch (SecurityException e) {
378
securityException = e;
379
}
380
checkActionSupport(Action.BROWSE);
381
if (uri == null) {
382
throw new NullPointerException();
383
}
384
if (securityException == null) {
385
peer.browse(uri);
386
return;
387
}
388
389
// Calling thread doesn't have necessary priviledges.
390
// Delegate to DesktopBrowse so that it can work in
391
// applet/webstart.
392
URL url = null;
393
try {
394
url = uri.toURL();
395
} catch (MalformedURLException e) {
396
throw new IllegalArgumentException("Unable to convert URI to URL", e);
397
}
398
sun.awt.DesktopBrowse db = sun.awt.DesktopBrowse.getInstance();
399
if (db == null) {
400
// Not in webstart/applet, throw the exception.
401
throw securityException;
402
}
403
db.browse(url);
404
}
405
406
/**
407
* Launches the mail composing window of the user default mail
408
* client.
409
*
410
* @throws UnsupportedOperationException if the current platform
411
* does not support the {@link Desktop.Action#MAIL} action
412
* @throws IOException if the user default mail client is not
413
* found, or it fails to be launched
414
* @throws SecurityException if a security manager exists and it
415
* denies the
416
* <code>AWTPermission("showWindowWithoutWarningBanner")</code>
417
* permission, or the calling thread is not allowed to create a
418
* subprocess
419
* @see java.awt.AWTPermission
420
*/
421
public void mail() throws IOException {
422
checkAWTPermission();
423
checkExec();
424
checkActionSupport(Action.MAIL);
425
URI mailtoURI = null;
426
try{
427
mailtoURI = new URI("mailto:?");
428
peer.mail(mailtoURI);
429
} catch (URISyntaxException e){
430
// won't reach here.
431
}
432
}
433
434
/**
435
* Launches the mail composing window of the user default mail
436
* client, filling the message fields specified by a {@code
437
* mailto:} URI.
438
*
439
* <p> A <code>mailto:</code> URI can specify message fields
440
* including <i>"to"</i>, <i>"cc"</i>, <i>"subject"</i>,
441
* <i>"body"</i>, etc. See <a
442
* href="http://www.ietf.org/rfc/rfc2368.txt">The mailto URL
443
* scheme (RFC 2368)</a> for the {@code mailto:} URI specification
444
* details.
445
*
446
* @param mailtoURI the specified {@code mailto:} URI
447
* @throws NullPointerException if the specified URI is {@code
448
* null}
449
* @throws IllegalArgumentException if the URI scheme is not
450
* <code>"mailto"</code>
451
* @throws UnsupportedOperationException if the current platform
452
* does not support the {@link Desktop.Action#MAIL} action
453
* @throws IOException if the user default mail client is not
454
* found or fails to be launched
455
* @throws SecurityException if a security manager exists and it
456
* denies the
457
* <code>AWTPermission("showWindowWithoutWarningBanner")</code>
458
* permission, or the calling thread is not allowed to create a
459
* subprocess
460
* @see java.net.URI
461
* @see java.awt.AWTPermission
462
*/
463
public void mail(URI mailtoURI) throws IOException {
464
checkAWTPermission();
465
checkExec();
466
checkActionSupport(Action.MAIL);
467
if (mailtoURI == null) throw new NullPointerException();
468
469
if (!"mailto".equalsIgnoreCase(mailtoURI.getScheme())) {
470
throw new IllegalArgumentException("URI scheme is not \"mailto\"");
471
}
472
473
peer.mail(mailtoURI);
474
}
475
476
private void checkExec() throws SecurityException {
477
SecurityManager sm = System.getSecurityManager();
478
if (sm != null) {
479
sm.checkPermission(new FilePermission("<<ALL FILES>>",
480
SecurityConstants.FILE_EXECUTE_ACTION));
481
}
482
}
483
}
484
485