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
|
/*
* Copyright (c) 2016, 2021, 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.peer.TaskbarPeer;
import sun.awt.SunToolkit;
/**
* The {@code Taskbar} class allows a Java application to interact with
* the system task area (taskbar, Dock, etc.).
*
* <p>
* There are a variety of interactions depending on the current platform such as
* displaying progress of some task, appending user-specified menu to the application
* icon context menu, etc.
*
* @implNote Linux support is currently limited to Unity. However to make these
* features work on Unity, the app should be run from a .desktop file with
* specified {@code java.desktop.appName} system property set to this .desktop
* file name:
* {@code Exec=java -Djava.desktop.appName=MyApp.desktop -jar /path/to/myapp.jar}
* see <a href="https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles">
* https://help.ubuntu.com/community/UnityLaunchersAndDesktopFiles</a>
*
* @since 9
*/
public class Taskbar {
/**
* List of provided features. Each platform supports a different
* set of features. You may use the {@link Taskbar#isSupported}
* method to determine if the given feature is supported by the
* current platform.
*/
public static enum Feature {
/**
* Represents a textual icon badge feature.
* @see #setIconBadge(java.lang.String)
*/
ICON_BADGE_TEXT,
/**
* Represents a numerical icon badge feature.
* @see #setIconBadge(java.lang.String)
*/
ICON_BADGE_NUMBER,
/**
* Represents a graphical icon badge feature for a window.
* @see #setWindowIconBadge(java.awt.Window, java.awt.Image)
*/
ICON_BADGE_IMAGE_WINDOW,
/**
* Represents an icon feature.
* @see #setIconImage(java.awt.Image)
*/
ICON_IMAGE,
/**
* Represents a menu feature.
* @see #setMenu(java.awt.PopupMenu)
* @see #getMenu()
*/
MENU,
/**
* Represents a progress state feature for a specified window.
* @see #setWindowProgressState(java.awt.Window, State)
*/
PROGRESS_STATE_WINDOW,
/**
* Represents a progress value feature.
* @see #setProgressValue(int)
*/
PROGRESS_VALUE,
/**
* Represents a progress value feature for a specified window.
* @see #setWindowProgressValue(java.awt.Window, int)
*/
PROGRESS_VALUE_WINDOW,
/**
* Represents a user attention request feature.
* @see #requestUserAttention(boolean, boolean)
*/
USER_ATTENTION,
/**
* Represents a user attention request feature for a specified window.
* @see #requestWindowUserAttention(java.awt.Window)
*/
USER_ATTENTION_WINDOW
}
/**
* Kinds of available window progress states.
*
* @see #setWindowProgressState(java.awt.Window, java.awt.Taskbar.State)
*/
public static enum State {
/**
* Stops displaying the progress.
*/
OFF,
/**
* The progress indicator displays with normal color and determinate
* mode.
*/
NORMAL,
/**
* Shows progress as paused, progress can be resumed by the user.
* Switches to the determinate display.
*/
PAUSED,
/**
* The progress indicator displays activity without specifying what
* proportion of the progress is complete.
*/
INDETERMINATE,
/**
* Shows that an error has occurred. Switches to the determinate
* display.
*/
ERROR
}
private TaskbarPeer peer;
/**
* Tests whether a {@code Feature} is supported on the current platform.
* @param feature the specified {@link Feature}
* @return true if the specified feature is supported on the current platform
*/
public boolean isSupported(Feature feature) {
return peer.isSupported(feature);
}
/**
* Checks if the feature type is supported.
*
* @param featureType the action type in question
* @throws UnsupportedOperationException if the specified action type is not
* supported on the current platform
*/
private void checkFeatureSupport(Feature featureType){
if (!isSupported(featureType)) {
throw new UnsupportedOperationException("The " + featureType.name()
+ " feature is not supported on the current platform!");
}
}
/**
* Calls to the security manager's {@code checkPermission} method with
* an {@code RuntimePermission("canProcessApplicationEvents")} permissions.
*/
private void checkEventsProcessingPermission(){
@SuppressWarnings("removal")
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
sm.checkPermission(new RuntimePermission(
"canProcessApplicationEvents"));
}
}
private Taskbar() {
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
if (defaultToolkit instanceof SunToolkit) {
peer = ((SunToolkit) defaultToolkit).createTaskbarPeer(this);
}
}
/**
* Returns the {@code Taskbar} instance of the current
* taskbar context. On some platforms the Taskbar API may not be
* supported; use the {@link #isTaskbarSupported} method to
* determine if the current taskbar is supported.
* @return the Taskbar instance
* @throws HeadlessException if {@link
* GraphicsEnvironment#isHeadless()} returns {@code true}
* @throws UnsupportedOperationException if this class is not
* supported on the current platform
* @see #isTaskbarSupported()
* @see java.awt.GraphicsEnvironment#isHeadless
*/
public static synchronized Taskbar getTaskbar(){
if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
if (!Taskbar.isTaskbarSupported()) {
throw new UnsupportedOperationException("Taskbar API is not " +
"supported on the current platform");
}
sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
Taskbar taskbar = (Taskbar)context.get(Taskbar.class);
if (taskbar == null) {
taskbar = new Taskbar();
context.put(Taskbar.class, taskbar);
}
return taskbar;
}
/**
* Tests whether this class is supported on the current platform.
* If it's supported, use {@link #getTaskbar()} to retrieve an
* instance.
*
* @return {@code true} if this class is supported on the
* current platform; {@code false} otherwise
* @see #getTaskbar()
*/
public static boolean isTaskbarSupported(){
Toolkit defaultToolkit = Toolkit.getDefaultToolkit();
if (defaultToolkit instanceof SunToolkit) {
return ((SunToolkit)defaultToolkit).isTaskbarSupported();
}
return false;
}
/**
* Requests user attention to this application.
*
* Depending on the platform, this may be visually indicated by a bouncing
* or flashing icon in the task area. It may have no effect on an already active
* application.
*
* On some platforms (e.g. Mac OS) this effect may disappear upon app activation
* and cannot be dismissed by setting {@code enabled} to false.
* Other platforms may require an additional call
* {@link #requestUserAttention} to dismiss this request
* with {@code enabled} parameter set to false.
*
* @param enabled disables this request if false
* @param critical if this is an important request
* @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 Taskbar.Feature#USER_ATTENTION} feature
*/
public void requestUserAttention(final boolean enabled, final boolean critical) {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.USER_ATTENTION);
peer.requestUserAttention(enabled, critical);
}
/**
* Requests user attention to the specified window.
*
* Has no effect if a window representation is not displayable in
* the task area. Whether it is displayable is dependent on all
* of window type, platform, and implementation.
*
* @param w window
* @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 Taskbar.Feature#USER_ATTENTION_WINDOW} feature
*/
public void requestWindowUserAttention(Window w) {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.USER_ATTENTION_WINDOW);
peer.requestWindowUserAttention(w);
}
/**
* Attaches the contents of the provided PopupMenu to the application icon
* in the task area.
*
* @param menu the PopupMenu to attach to this application
* @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 Taskbar.Feature#MENU} feature
*/
public void setMenu(final PopupMenu menu) {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.MENU);
peer.setMenu(menu);
}
/**
* Gets PopupMenu used to add items to this application's icon in system task area.
*
* @return the PopupMenu
* @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 Taskbar.Feature#MENU} feature
*/
public PopupMenu getMenu() {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.MENU);
return peer.getMenu();
}
/**
* Changes this application's icon to the provided image.
*
* @param image to change
* @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 Taskbar.Feature#ICON_IMAGE} feature
*/
public void setIconImage(final Image image) {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.ICON_IMAGE);
peer.setIconImage(image);
}
/**
* Obtains an image of this application's icon.
*
* @return an image of this application's icon
* @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 Taskbar.Feature#ICON_IMAGE} feature
*/
public Image getIconImage() {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.ICON_IMAGE);
return peer.getIconImage();
}
/**
* Affixes a small system-provided badge to this application's icon.
* Usually a number.
*
* Some platforms do not support string values and accept only integer
* values. In this case, pass an integer represented as a string as parameter.
* This can be tested by {@code Feature.ICON_BADGE_TEXT} and
* {@code Feature.ICON_BADGE_NUMBER}.
*
* Passing {@code null} as parameter hides the badge.
* @param badge label to affix to the icon
* @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 Taskbar.Feature#ICON_BADGE_NUMBER}
* or {@link Taskbar.Feature#ICON_BADGE_TEXT} feature
*/
public void setIconBadge(final String badge) {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.ICON_BADGE_NUMBER);
peer.setIconBadge(badge);
}
/**
* Affixes a small badge to this application's icon in the task area
* for the specified window.
* It may be disabled by system settings.
*
* Has no effect if a window representation is not displayable in
* the task area. Whether it is displayable is dependent on all
* of window type, platform, and implementation.
*
* @param w window to update
* @param badge image to affix to the icon
* @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 Taskbar.Feature#ICON_BADGE_IMAGE_WINDOW} feature
*/
public void setWindowIconBadge(Window w, final Image badge) {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.ICON_BADGE_IMAGE_WINDOW);
if (w != null) {
peer.setWindowIconBadge(w, badge);
}
}
/**
* Affixes a small system-provided progress bar to this application's icon.
*
* @param value from 0 to 100, other to disable progress indication
* @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 Taskbar.Feature#PROGRESS_VALUE} feature
*/
public void setProgressValue(int value) {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.PROGRESS_VALUE);
peer.setProgressValue(value);
}
/**
* Displays a determinate progress bar in the task area for the specified
* window.
*
* Has no effect if a window representation is not displayable in
* the task area. Whether it is displayable is dependent on all
* of window type, platform, and implementation.
*
* <br>
* The visual behavior is platform and {@link State} dependent.
* <br>
* This call cancels the {@link State#INDETERMINATE INDETERMINATE} state
* of the window.
* <br>
* Note that when multiple windows is grouped in the task area
* the behavior is platform specific.
*
* @param w window to update
* @param value from 0 to 100, other to switch to {@link State#OFF} state
* and disable progress indication
* @see #setWindowProgressState(java.awt.Window, State)
* @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 Taskbar.Feature#PROGRESS_VALUE_WINDOW} feature
*/
public void setWindowProgressValue(Window w, int value) {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.PROGRESS_VALUE_WINDOW);
if (w != null) {
peer.setWindowProgressValue(w, value);
}
}
/**
* Sets a progress state for a specified window.
*
* Has no effect if a window representation is not displayable in
* the task area. Whether it is displayable is dependent on all
* of window type, platform, and implementation.
* <br>
* Each state displays a progress in a platform-dependent way.
* <br>
* Note than switching from {@link State#INDETERMINATE INDETERMINATE} state
* to any of determinate states may reset value set by
* {@link #setWindowProgressValue(java.awt.Window, int) setWindowProgressValue}
*
* @param w window
* @param state to change to
* @see State#OFF
* @see State#NORMAL
* @see State#PAUSED
* @see State#ERROR
* @see State#INDETERMINATE
* @see #setWindowProgressValue(java.awt.Window, int)
* @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 Taskbar.Feature#PROGRESS_STATE_WINDOW} feature
*/
public void setWindowProgressState(Window w, State state) {
checkEventsProcessingPermission();
checkFeatureSupport(Feature.PROGRESS_STATE_WINDOW);
if (w != null) {
peer.setWindowProgressState(w, state);
}
}
}
|