Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/com/apple/eawt/Application.java
41153 views
1
/*
2
* Copyright (c) 2011, 2021, 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 com.apple.eawt;
27
28
import java.awt.Image;
29
import java.awt.PopupMenu;
30
import java.awt.Toolkit;
31
import java.awt.Window;
32
import java.awt.desktop.AboutHandler;
33
import java.awt.desktop.AppForegroundListener;
34
import java.awt.desktop.AppHiddenListener;
35
import java.awt.desktop.AppReopenedListener;
36
import java.awt.desktop.OpenFilesEvent;
37
import java.awt.desktop.OpenFilesHandler;
38
import java.awt.desktop.OpenURIEvent;
39
import java.awt.desktop.OpenURIHandler;
40
import java.awt.desktop.PreferencesHandler;
41
import java.awt.desktop.PrintFilesEvent;
42
import java.awt.desktop.PrintFilesHandler;
43
import java.awt.desktop.QuitHandler;
44
import java.awt.desktop.QuitResponse;
45
import java.awt.desktop.QuitStrategy;
46
import java.awt.desktop.ScreenSleepListener;
47
import java.awt.desktop.SystemEventListener;
48
import java.awt.desktop.SystemSleepListener;
49
import java.awt.desktop.UserSessionListener;
50
import java.beans.Beans;
51
52
import javax.swing.JMenuBar;
53
54
import sun.awt.AWTAccessor;
55
import sun.lwawt.LWWindowPeer;
56
import sun.lwawt.macosx.CPlatformWindow;
57
58
/**
59
* The {@code Application} class allows you to integrate your Java application with the native Mac OS X environment.
60
* You can provide your Mac OS X users a greatly enhanced experience by implementing a few basic handlers for standard system events.
61
*
62
* For example:
63
* <ul>
64
* <li>Open an about dialog when a user chooses About from the application menu.</li>
65
* <li>Open a preferences window when the users chooses Preferences from the application menu.</li>
66
* <li>Create a new document when the user clicks on your Dock icon, and no windows are open.</li>
67
* <li>Open a document that the user double-clicked on in the Finder.</li>
68
* <li>Open a custom URL scheme when a user clicks on link in a web browser.</li>
69
* <li>Reconnect to network services after the system has awoke from sleep.</li>
70
* <li>Cleanly shutdown your application when the user chooses Quit from the application menu, Dock icon, or types Command-Q.</li>
71
* <li>Cancel shutdown/logout if the user has unsaved changes in your application.</li>
72
* </ul>
73
*
74
* @since 1.4
75
*/
76
public class Application {
77
private static native void nativeInitializeApplicationDelegate();
78
79
static Application sApplication = null;
80
81
static {
82
checkSecurity();
83
Toolkit.getDefaultToolkit(); // Start AppKit
84
if (!Beans.isDesignTime()) {
85
nativeInitializeApplicationDelegate();
86
}
87
88
sApplication = new Application();
89
}
90
91
private static void checkSecurity() {
92
@SuppressWarnings("removal")
93
final SecurityManager security = System.getSecurityManager();
94
if (security == null) return;
95
security.checkPermission(new RuntimePermission("canProcessApplicationEvents"));
96
}
97
98
/**
99
* @return the singleton representing this Mac OS X Application
100
*
101
* @since 1.4
102
*/
103
public static Application getApplication() {
104
checkSecurity();
105
return sApplication;
106
}
107
108
// some singletons, since they get called back into from native
109
final _AppEventHandler eventHandler = _AppEventHandler.getInstance();
110
final _AppMenuBarHandler menuBarHandler = _AppMenuBarHandler.getInstance();
111
final _AppDockIconHandler iconHandler = new _AppDockIconHandler();
112
113
/**
114
* Creates an Application instance. Should only be used in JavaBean environments.
115
* @deprecated use {@link #getApplication()}
116
*
117
* @since 1.4
118
*/
119
@Deprecated
120
public Application() {
121
checkSecurity();
122
}
123
124
/**
125
* Adds sub-types of {@link SystemEventListener} to listen for notifications from the native Mac OS X system.
126
*
127
* @see AppForegroundListener
128
* @see AppHiddenListener
129
* @see AppReopenedListener
130
* @see ScreenSleepListener
131
* @see SystemSleepListener
132
* @see UserSessionListener
133
*
134
* @param listener
135
* @since Java for Mac OS X 10.6 Update 3
136
* @since Java for Mac OS X 10.5 Update 8
137
*/
138
public void addAppEventListener(final SystemEventListener listener) {
139
eventHandler.addListener(listener);
140
}
141
142
/**
143
* Removes sub-types of {@link SystemEventListener} from listening for notifications from the native Mac OS X system.
144
*
145
* @see AppForegroundListener
146
* @see AppHiddenListener
147
* @see AppReopenedListener
148
* @see ScreenSleepListener
149
* @see SystemSleepListener
150
* @see UserSessionListener
151
*
152
* @param listener
153
* @since Java for Mac OS X 10.6 Update 3
154
* @since Java for Mac OS X 10.5 Update 8
155
*/
156
public void removeAppEventListener(final SystemEventListener listener) {
157
eventHandler.removeListener(listener);
158
}
159
160
/**
161
* Installs a handler to show a custom About window for your application.
162
*
163
* Setting the {@link AboutHandler} to {@code null} reverts it to the default Cocoa About window.
164
*
165
* @param aboutHandler the handler to respond to the {@link AboutHandler#handleAbout} message
166
* @since Java for Mac OS X 10.6 Update 3
167
* @since Java for Mac OS X 10.5 Update 8
168
*/
169
public void setAboutHandler(final AboutHandler aboutHandler) {
170
eventHandler.aboutDispatcher.setHandler(aboutHandler);
171
}
172
173
/**
174
* Installs a handler to create the Preferences menu item in your application's app menu.
175
*
176
* Setting the {@link PreferencesHandler} to {@code null} will remove the Preferences item from the app menu.
177
*
178
* @param preferencesHandler
179
* @since Java for Mac OS X 10.6 Update 3
180
* @since Java for Mac OS X 10.5 Update 8
181
*/
182
public void setPreferencesHandler(final PreferencesHandler preferencesHandler) {
183
eventHandler.preferencesDispatcher.setHandler(preferencesHandler);
184
}
185
186
/**
187
* Installs the handler which is notified when the application is asked to open a list of files.
188
* The {@link OpenFilesHandler#openFiles(OpenFilesEvent)} notifications are only sent if the Java app is a bundled application, with a {@code CFBundleDocumentTypes} array present in it's Info.plist.
189
* See the <a href="http://developer.apple.com/mac/library/documentation/General/Reference/InfoPlistKeyReference">Info.plist Key Reference</a> for more information about adding a {@code CFBundleDocumentTypes} key to your app's Info.plist.
190
*
191
* @param openFileHandler
192
* @since Java for Mac OS X 10.6 Update 3
193
* @since Java for Mac OS X 10.5 Update 8
194
*/
195
public void setOpenFileHandler(final OpenFilesHandler openFileHandler) {
196
eventHandler.openFilesDispatcher.setHandler(openFileHandler);
197
}
198
199
/**
200
* Installs the handler which is notified when the application is asked to print a list of files.
201
* The {@link PrintFilesHandler#printFiles(PrintFilesEvent)} notifications are only sent if the Java app is a bundled application, with a {@code CFBundleDocumentTypes} array present in it's Info.plist.
202
* See the <a href="http://developer.apple.com/mac/library/documentation/General/Reference/InfoPlistKeyReference">Info.plist Key Reference</a> for more information about adding a {@code CFBundleDocumentTypes} key to your app's Info.plist.
203
*
204
* @param printFileHandler
205
* @since Java for Mac OS X 10.6 Update 3
206
* @since Java for Mac OS X 10.5 Update 8
207
*/
208
public void setPrintFileHandler(final PrintFilesHandler printFileHandler) {
209
eventHandler.printFilesDispatcher.setHandler(printFileHandler);
210
}
211
212
/**
213
* Installs the handler which is notified when the application is asked to open a URL.
214
* The {@link OpenURIHandler#openURI(OpenURIEvent)} notifications are only sent if the Java app is a bundled application, with a {@code CFBundleURLTypes} array present in it's Info.plist.
215
* See the <a href="http://developer.apple.com/mac/library/documentation/General/Reference/InfoPlistKeyReference">Info.plist Key Reference</a> for more information about adding a {@code CFBundleURLTypes} key to your app's Info.plist.
216
*
217
* Setting the handler to {@code null} causes all {@link OpenURIHandler#openURI(OpenURIEvent)} requests to be enqueued until another handler is set.
218
*
219
* @param openURIHandler
220
* @since Java for Mac OS X 10.6 Update 3
221
* @since Java for Mac OS X 10.5 Update 8
222
*/
223
public void setOpenURIHandler(final OpenURIHandler openURIHandler) {
224
eventHandler.openURIDispatcher.setHandler(openURIHandler);
225
}
226
227
/**
228
* Installs the handler which determines if the application should quit.
229
* The handler is passed a one-shot {@link QuitResponse} which can cancel or proceed with the quit.
230
* Setting the handler to {@code null} causes all quit requests to directly perform the default {@link QuitStrategy}.
231
*
232
* @param quitHandler the handler that is called when the application is asked to quit
233
* @since Java for Mac OS X 10.6 Update 3
234
* @since Java for Mac OS X 10.5 Update 8
235
*/
236
public void setQuitHandler(final QuitHandler quitHandler) {
237
eventHandler.quitDispatcher.setHandler(quitHandler);
238
}
239
240
/**
241
* Sets the default strategy used to quit this application. The default is calling SYSTEM_EXIT_0.
242
*
243
* The quit strategy can also be set with the "apple.eawt.quitStrategy" system property.
244
*
245
* @param strategy the way this application should be shutdown
246
* @since Java for Mac OS X 10.6 Update 3
247
* @since Java for Mac OS X 10.5 Update 8
248
*/
249
public void setQuitStrategy(final QuitStrategy strategy) {
250
eventHandler.setDefaultQuitStrategy(strategy);
251
}
252
253
/**
254
* Enables this application to be suddenly terminated.
255
*
256
* Call this method to indicate your application's state is saved, and requires no notification to be terminated.
257
* Letting your application remain terminatable improves the user experience by avoiding re-paging in your application when it's asked to quit.
258
*
259
* <b>Note: enabling sudden termination will allow your application to be quit without notifying your QuitHandler, or running any shutdown hooks.</b>
260
* User initiated Cmd-Q, logout, restart, or shutdown requests will effectively "kill -KILL" your application.
261
*
262
* This call has no effect on Mac OS X versions prior to 10.6.
263
*
264
* @see <a href="http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSProcessInfo_Class">NSProcessInfo class references</a> for more information about Sudden Termination.
265
* @see Application#disableSuddenTermination()
266
*
267
* @since Java for Mac OS X 10.6 Update 3
268
* @since Java for Mac OS X 10.5 Update 8
269
*/
270
public void enableSuddenTermination() {
271
_AppMiscHandlers.enableSuddenTermination();
272
}
273
274
/**
275
* Prevents this application from being suddenly terminated.
276
*
277
* Call this method to indicate that your application has unsaved state, and may not be terminated without notification.
278
*
279
* This call has no effect on Mac OS X versions prior to 10.6.
280
*
281
* @see <a href="http://developer.apple.com/mac/library/documentation/cocoa/reference/foundation/Classes/NSProcessInfo_Class">NSProcessInfo class references</a> for more information about Sudden Termination.
282
* @see Application#enableSuddenTermination()
283
*
284
* @since Java for Mac OS X 10.6 Update 3
285
* @since Java for Mac OS X 10.5 Update 8
286
*/
287
public void disableSuddenTermination() {
288
_AppMiscHandlers.disableSuddenTermination();
289
}
290
291
/**
292
* Requests this application to move to the foreground.
293
*
294
* @param allWindows if all windows of this application should be moved to the foreground, or only the foremost one
295
*
296
* @since Java for Mac OS X 10.6 Update 1
297
* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5
298
*/
299
public void requestForeground(final boolean allWindows) {
300
_AppMiscHandlers.requestActivation(allWindows);
301
}
302
303
/**
304
* Requests user attention to this application (usually through bouncing the Dock icon). Critical
305
* requests will continue to bounce the Dock icon until the app is activated. An already active
306
* application requesting attention does nothing.
307
*
308
* @param critical if this is an important request
309
*
310
* @since Java for Mac OS X 10.6 Update 1
311
* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5
312
*/
313
public void requestUserAttention(final boolean critical) {
314
_AppMiscHandlers.requestUserAttention(critical);
315
}
316
317
/**
318
* Opens the native help viewer application if a Help Book has been added to the
319
* application bundler and registered in the Info.plist with CFBundleHelpBookFolder.
320
*
321
* See http://developer.apple.com/qa/qa2001/qa1022.html for more information.
322
*
323
* @since Java for Mac OS X 10.5 - 1.5
324
* @since Java for Mac OS X 10.5 Update 1 - 1.6
325
*/
326
public void openHelpViewer() {
327
_AppMiscHandlers.openHelpViewer();
328
}
329
330
/**
331
* Attaches the contents of the provided PopupMenu to the application's Dock icon.
332
*
333
* @param menu the PopupMenu to attach to this application's Dock icon
334
*
335
* @since Java for Mac OS X 10.5 - 1.5
336
* @since Java for Mac OS X 10.5 Update 1 - 1.6
337
*/
338
public void setDockMenu(final PopupMenu menu) {
339
iconHandler.setDockMenu(menu);
340
}
341
342
/**
343
* @return the PopupMenu used to add items to this application's Dock icon
344
*
345
* @since Java for Mac OS X 10.5 - 1.5
346
* @since Java for Mac OS X 10.5 Update 1 - 1.6
347
*/
348
public PopupMenu getDockMenu() {
349
return iconHandler.getDockMenu();
350
}
351
352
/**
353
* Changes this application's Dock icon to the provided image.
354
*
355
* @param image
356
*
357
* @since Java for Mac OS X 10.5 - 1.5
358
* @since Java for Mac OS X 10.5 Update 1 - 1.6
359
*/
360
public void setDockIconImage(final Image image) {
361
iconHandler.setDockIconImage(image);
362
}
363
364
/**
365
* Obtains an image of this application's Dock icon.
366
*
367
* @return an image of this application's Dock icon
368
*
369
* @since Java for Mac OS X 10.5 - 1.5
370
* @since Java for Mac OS X 10.5 Update 1 - 1.6
371
*/
372
public Image getDockIconImage() {
373
return iconHandler.getDockIconImage();
374
}
375
376
/**
377
* Affixes a small system provided badge to this application's Dock icon. Usually a number.
378
*
379
* @param badge textual label to affix to the Dock icon
380
*
381
* @since Java for Mac OS X 10.5 - 1.5
382
* @since Java for Mac OS X 10.5 Update 1 - 1.6
383
*/
384
public void setDockIconBadge(final String badge) {
385
iconHandler.setDockIconBadge(badge);
386
}
387
388
/**
389
* Displays a progress bar to this application's Dock icon.
390
* Acceptable values are from 0 to 100, any other disables progress indication.
391
*
392
* @param value progress value
393
*/
394
public void setDockIconProgress(final int value) {
395
iconHandler.setDockIconProgress(value);
396
}
397
398
/**
399
* Sets the default menu bar to use when there are no active frames.
400
* Only used when the system property "apple.laf.useScreenMenuBar" is "true", and
401
* the Aqua Look and Feel is active.
402
*
403
* @param menuBar to use when no other frames are active
404
*
405
* @since Java for Mac OS X 10.6 Update 1
406
* @since Java for Mac OS X 10.5 Update 6 - 1.6, 1.5
407
*/
408
public void setDefaultMenuBar(final JMenuBar menuBar) {
409
menuBarHandler.setDefaultMenuBar(menuBar);
410
}
411
412
/**
413
* Requests that a {@link Window} should animate into or out of full screen mode.
414
* Only {@link Window}s marked as full screenable by {@link FullScreenUtilities#setWindowCanFullScreen(Window, boolean)} can be toggled.
415
*
416
* @param window to animate into or out of full screen mode
417
*
418
* @since Java for Mac OS X 10.7 Update 1
419
*/
420
public void requestToggleFullScreen(final Window window) {
421
final Object peer = AWTAccessor.getComponentAccessor().getPeer(window);
422
if (!(peer instanceof LWWindowPeer)) return;
423
Object platformWindow = ((LWWindowPeer) peer).getPlatformWindow();
424
if (!(platformWindow instanceof CPlatformWindow)) return;
425
((CPlatformWindow)platformWindow).toggleFullScreen();
426
}
427
428
}
429
430