|
| 1 | +/* |
| 2 | + * Copyright (c) 2005, 2024, 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.awt.AWTException; |
| 25 | +import java.awt.BorderLayout; |
| 26 | +import java.awt.Button; |
| 27 | +import java.awt.Checkbox; |
| 28 | +import java.awt.CheckboxGroup; |
| 29 | +import java.awt.Choice; |
| 30 | +import java.awt.Color; |
| 31 | +import java.awt.Container; |
| 32 | +import java.awt.EventQueue; |
| 33 | +import java.awt.FlowLayout; |
| 34 | +import java.awt.Frame; |
| 35 | +import java.awt.Graphics2D; |
| 36 | +import java.awt.GridLayout; |
| 37 | +import java.awt.Image; |
| 38 | +import java.awt.Insets; |
| 39 | +import java.awt.Label; |
| 40 | +import java.awt.MenuItem; |
| 41 | +import java.awt.Panel; |
| 42 | +import java.awt.PopupMenu; |
| 43 | +import java.awt.RenderingHints; |
| 44 | +import java.awt.SystemTray; |
| 45 | +import java.awt.TextField; |
| 46 | +import java.awt.Toolkit; |
| 47 | +import java.awt.TrayIcon; |
| 48 | +import java.awt.TrayIcon.MessageType; |
| 49 | +import java.awt.event.ActionEvent; |
| 50 | +import java.awt.event.MouseAdapter; |
| 51 | +import java.awt.event.MouseEvent; |
| 52 | +import java.awt.event.MouseMotionAdapter; |
| 53 | +import java.awt.event.WindowAdapter; |
| 54 | +import java.awt.event.WindowEvent; |
| 55 | +import java.awt.image.BufferedImage; |
| 56 | +import java.beans.PropertyChangeEvent; |
| 57 | +import java.util.HashMap; |
| 58 | +import java.util.Map; |
| 59 | +import jtreg.SkippedException; |
| 60 | + |
| 61 | +/* |
| 62 | + * @test |
| 63 | + * @key headful |
| 64 | + * @bug 4310333 |
| 65 | + * @library /java/awt/regtesthelpers /test/lib |
| 66 | + * @build PassFailJFrame jtreg.SkippedException |
| 67 | + * @summary A unit test for TrayIcon RFE |
| 68 | + * @run main/manual TrayIconTest |
| 69 | + */ |
| 70 | + |
| 71 | +public class TrayIconTest { |
| 72 | + private static SystemTray tray; |
| 73 | + static Frame frame = new Frame("TrayIcon Test"); |
| 74 | + private static final String INSTRUCTIONS = """ |
| 75 | + The test frame contains CheckboxGroup of tray icons. |
| 76 | + A selected checkbox represents the TrayIcon (or null |
| 77 | + TrayIcon) whose functionality is currently tested. |
| 78 | +
|
| 79 | + If you are under Linux make sure your Application Panel has |
| 80 | + System Tray (on Gnome it is called Notification Area). |
| 81 | +
|
| 82 | + Perform all the cases (1-7) documented below. |
| 83 | +
|
| 84 | + CASE 1: Testing ADD/REMOVE/PropertyChange functionality. |
| 85 | + -------------------------------------------------------- |
| 86 | + 1. Select null TrayIcon and pressAdd button: |
| 87 | + - NullPointerException should be thrown. |
| 88 | + 2. Select some of the valid TrayIcons and press Add button: |
| 89 | + - The selected TrayIcon should appear in the SystemTray. |
| 90 | + - PropertyChangeEvent should be fired (the property is |
| 91 | + an array of TrayIcons added to the system tray). |
| 92 | + 3. Press Add button again: |
| 93 | + - IllegalArgumentException should be thrown. |
| 94 | + - No PropertyChangeEvent should be fired. |
| 95 | + 4. Press Remove button: |
| 96 | + - The TrayIcon should disappear from the SystemTray. |
| 97 | + - PropertyChangeEvent should be fired. |
| 98 | + 5. Press Remove button again: |
| 99 | + - It should have no effect. |
| 100 | + - No PropertyChangeEvent should be fired. |
| 101 | + 6. Add all the valid TrayIcons (by selecting everyone and pressing Add |
| 102 | + button): |
| 103 | + - All the TrayIcons should appear in the SystemTray. |
| 104 | + - PropertyChangeEvent should be fired on each adding. |
| 105 | + 7. Remove all the TrayIcons (again by selecting everyone and pressing |
| 106 | + Remove): |
| 107 | + - All the TrayIcons should disappear from the SystemTray. |
| 108 | + - PropertyChangeEvent should be fired on each removing. |
| 109 | + 8. Not for Windows! Remove the system tray (Notification Area) from |
| 110 | + the desktop. Try to add some valid TrayIcon: |
| 111 | + - AWTException should be thrown. |
| 112 | + - No PropertyChangeEvent should be fired. |
| 113 | + 9. Not for Windows! Add the system tray back to the desktop. Add all the |
| 114 | + valid TrayIcons: |
| 115 | + - All the TrayIcons should appear in the system tray. |
| 116 | + - PropertyChangeEvent should be fired on each adding. |
| 117 | + 11. Not for Windows! Remove the system tray from the desktop: |
| 118 | + - All the TrayIcons should disappear. |
| 119 | + - PropertyChangeEvent should be fired for each TrayIcon |
| 120 | + removal. |
| 121 | + - PropertyChangeEvent should be fired for SystemTray removal. |
| 122 | + 12. Add the system tray and go to the next step. |
| 123 | + - All the TrayIcons should appear again. |
| 124 | + - PropertyChangeEvent should be fired for SystemTray addition. |
| 125 | + - PropertyChangeEvent shouldn't be fired for TrayIcon removal. |
| 126 | +
|
| 127 | + CASE 2: Testing RESIZE functionality. |
| 128 | + ------------------------------------- |
| 129 | + 1. Select some of the TrayIcons and add it. Then press resize button: |
| 130 | + - The TrayIcon selected should be resized to fit the area it occupies. |
| 131 | + 2. Press resize button again: |
| 132 | + - The TrayIcon should be resized to the original size. |
| 133 | + 3. Repeat the 1-2 steps for other TrayIcons: |
| 134 | + - The TrayIcons should be resized appropriately. |
| 135 | +
|
| 136 | + CASE 3: Testing EVENTS functionality |
| 137 | + --------------------------------- |
| 138 | + 1. Select some of the TrayIcons and add it. Select MouseEvent from the |
| 139 | + group of checkboxes at the top-right of the test frame. |
| 140 | + Click on the TrayIcon in the SystemTray: |
| 141 | + - MOUSE_PRESSED MOUSE_RELEASED and MOUSE_CLICKED events should be |
| 142 | + generated. |
| 143 | + 2. Press mouse inside the TrayIcon dragging mouse and releasing it. |
| 144 | + - Make sure that MOUSE_CLICKED event is not triggered. |
| 145 | + 3. Click on the TrayIcon with different modification keys: |
| 146 | + - there should be appropriate modifiers in the events. |
| 147 | + 4. Keep clicking on the TrayIcon: |
| 148 | + - there should be correct absolute coordinates in the events. |
| 149 | + 5. Only for Windows! Focus the system tray using keyboard: |
| 150 | + - press WIN key once to bring up the start menu then press ESC once to |
| 151 | + close the menu the focus should be on the start button |
| 152 | + - press TAB key for several times until you focus on the system |
| 153 | + tray then use ARROW keys to move to the TrayIcon |
| 154 | + - press ENTER or SPACE should trigger ACTION_PERFORMED message |
| 155 | + make sure that mouse events are not triggered. |
| 156 | + 6. Select MouseMotionEvent checkbox. Move mouse over the TrayIcon: |
| 157 | + - MOUSE_MOVED event should be generated. It should contain |
| 158 | + correct coordinates. |
| 159 | + 7. Deselect both the checkboxes and then select AWTEventListener. |
| 160 | + Click on the TrayIcon and then move mouse over it: |
| 161 | + - Appropriate mouse events should be generated (catched by the |
| 162 | + AWTEventListener). |
| 163 | + 8. Deselect all the checkboxes and go to the following step. |
| 164 | +
|
| 165 | + CASE 4: Testing DISPLAY MESSAGE functionality. |
| 166 | + ---------------------------------------------- |
| 167 | + 1. Select some of the TrayIcons and add it. Then press Display message |
| 168 | + button: |
| 169 | + - A balloon message should appear near the TrayIcon. |
| 170 | + 2. After the message is displayed wait for some period: |
| 171 | + - The message window should be closed automatically. |
| 172 | + 3. Display the message again. Close it by pressing X in its top-right |
| 173 | + corner: |
| 174 | + - The message window should be closed immediately. |
| 175 | + 4. Display the message again. Click inside it: |
| 176 | + - The message should be closed an ACTION_PERFORMED event should be |
| 177 | + generated with correct information and an Ok dialog should appear. |
| 178 | + Close the dialog. |
| 179 | + 5. Select a message type from the Type choice and display the message |
| 180 | + again: |
| 181 | + - It should contain an icon appropriate to the message type selected |
| 182 | + or no icon if NONE is selected. |
| 183 | + 6. Change the content of the Message and Caption text fields and |
| 184 | + display the message: |
| 185 | + - The message content should be changed in the accordance with the text |
| 186 | + typed. |
| 187 | + 7. Not for Windows! Type some too long or too short text for the Caption |
| 188 | + and Message: |
| 189 | + - The message should process the text correctly. The long text should |
| 190 | + be cut. |
| 191 | + 8. Not for Windows! Type null in the Message text field and display |
| 192 | + the message: |
| 193 | + - The message body should contain no text. |
| 194 | + 9. Type null in the Caption text field and display the message: |
| 195 | + - The message caption should contain no text. |
| 196 | + 10. Type null in the both Message and Caption fields and display |
| 197 | + the message: |
| 198 | + - NullPointerException should be generated and no message should be |
| 199 | + displayed. |
| 200 | + 11. Try to hide the taskbar. Click Display message for several times. |
| 201 | + Then restore the taskbar. Click on the TrayIcon: |
| 202 | + - No message should appear. |
| 203 | + Try to display the message once more: |
| 204 | + - It should appear appropriately. |
| 205 | + 12. Try to display the message for other TrayIcons: |
| 206 | + - The messages should be displayed appropriately. |
| 207 | +
|
| 208 | + CASE 5: Testing POPUP MENU functionality. |
| 209 | + ----------------------------------------- |
| 210 | + 1. Add some TrayIcon to the system tray. Press Set button in the |
| 211 | + Popup menu test area. Trigger the popup menu for the TrayIcon with |
| 212 | + the mouse: |
| 213 | + - A popup menu should appear. Make sure it behaves properly. |
| 214 | + - Make sure the 'duke.gif' image is animated while the popup menu is shown. |
| 215 | + 2. Press Remove button for the popup menu and try to trigger it again: |
| 216 | + - No popup menu should appear. |
| 217 | + 3. Perform 1-2 steps for other TrayIcons: |
| 218 | + - Make sure the popup menu behaves properly. |
| 219 | + 4. Add more than one TrayIcons to the system tray. Press Set button in |
| 220 | + the PopupMenu test area for some of the TrayIcon added. Trigger |
| 221 | + the popup menu for this TrayIcon: |
| 222 | + - A popup menu should appear properly. |
| 223 | + 5. Try to set the popup menu to the same TrayIcon again: |
| 224 | + - It should have no effect |
| 225 | + 6. Try to set the popup menu for other TrayIcons you've added to the system |
| 226 | + tray: |
| 227 | + - for each one IllegalArgumentException should be thrown. |
| 228 | +
|
| 229 | + CASE 6: Testing TOOLTIP functionality. |
| 230 | + -------------------------------------- |
| 231 | + 1. Type something in the Tooltip text field and press Set button. |
| 232 | + Then move mouse cursor over the TrayIcon and wait for a second: |
| 233 | + - A tooltip should appear containing the text typed. |
| 234 | + 2. Show a tooltip again and keep your mouse over the TrayIcon for some period: |
| 235 | + - The tooltip should disappear automatically. |
| 236 | + 3. Show a tooltip again and leave the TrayIcon: |
| 237 | + - The tooltip should disappear immediately. |
| 238 | + 4. Type null in the Tooltip field and press set then move your |
| 239 | + mouse to the SystemTray: |
| 240 | + - The tooltip shouldn't appear. |
| 241 | + 5. Type something too long in the Tooltip field and show the tooltip: |
| 242 | + - The tooltip text should be cut. |
| 243 | +
|
| 244 | + CASE 7: Testing ACTION functionality. |
| 245 | + ------------------------------------- |
| 246 | + 1. Add some TrayIcon to the system tray. Double click it with the left mouse |
| 247 | + button: |
| 248 | + - An ACTION_PERFORMED event should be generated. |
| 249 | + 2. Double click the TrayIcon with the left mouse button several times: |
| 250 | + - Several ACTION_PERFORMED events should be generated |
| 251 | + - Make sure that the time-stamp of each event ('when' field) is increased. |
| 252 | +
|
| 253 | + If all the above cases work as expected Press PASS else FAIL. |
| 254 | + """; |
| 255 | + |
| 256 | + public static void main(String[] args) throws Exception { |
| 257 | + if (!SystemTray.isSupported()) { |
| 258 | + throw new SkippedException("Test not applicable as" |
| 259 | + + " System Tray not supported"); |
| 260 | + } |
| 261 | + try { |
| 262 | + PassFailJFrame.builder() |
| 263 | + .title("TrayIconTest Instructions") |
| 264 | + .instructions(INSTRUCTIONS) |
| 265 | + .columns(50) |
| 266 | + .rows(40) |
| 267 | + .testUI(TrayIconTest::createAndShowUI) |
| 268 | + .logArea(10) |
| 269 | + .build() |
| 270 | + .awaitAndCheck(); |
| 271 | + |
| 272 | + } finally { |
| 273 | + EventQueue.invokeAndWait(() -> { |
| 274 | + if (tray != null) { |
| 275 | + //Remove any remaining tray icons before ending the test. |
| 276 | + TrayIcon[] icons = tray.getTrayIcons(); |
| 277 | + for (TrayIcon icon : icons) { |
| 278 | + tray.remove(icon); |
| 279 | + } |
| 280 | + } |
| 281 | + }); |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + private static Frame createAndShowUI() { |
| 286 | + final TrayIconControl ctrl = new TrayIconControl(); |
| 287 | + frame.setLayout(new BorderLayout()); |
| 288 | + frame.add(ctrl.cont, BorderLayout.CENTER); |
| 289 | + frame.setBackground(Color.LIGHT_GRAY); |
| 290 | + |
| 291 | + frame.addWindowListener(new WindowAdapter() { |
| 292 | + public void windowClosing(WindowEvent e) { |
| 293 | + ctrl.dispose(); |
| 294 | + } |
| 295 | + }); |
| 296 | + |
| 297 | + frame.pack(); |
| 298 | + return frame; |
| 299 | + } |
| 300 | + |
| 301 | + private static class TrayIconControl { |
| 302 | + final String RED_ICON = "RED ICON"; |
| 303 | + final String BLUE_ICON = "BLUE ICON"; |
| 304 | + final String GREEN_ICON = "GREEN ICON"; |
| 305 | + |
| 306 | + CheckboxGroup cbg = new CheckboxGroup(); |
| 307 | + Button addButton = new PackedButton(" Add "); |
| 308 | + Button remButton = new PackedButton("Remove"); |
| 309 | + Button resizeButton = new PackedButton("Resize"); |
| 310 | + Button balloonButton = new PackedButton("Display message"); |
| 311 | + Choice balloonChoice = new Choice(); |
| 312 | + String[] balloonTypes = new String[] { "ERROR", "WARNING", "INFO", "NONE" }; |
| 313 | + |
| 314 | + TextField balloonText = new TextField( |
| 315 | + "A TrayIcon can generate various MouseEvents and" |
| 316 | + + " supports adding corresponding listeners to receive" |
| 317 | + + " notification of these events. TrayIcon processes" |
| 318 | + + " some of the events by itself. For example," |
| 319 | + + " by default, when the right-mouse click", 70); |
| 320 | + TextField balloonCaption = new TextField("TrayIcon", 70); |
| 321 | + |
| 322 | + MessageType[] typeArr = new MessageType[] { MessageType.ERROR, MessageType.WARNING, |
| 323 | + MessageType.INFO, MessageType.NONE }; |
| 324 | + Checkbox mouseListenerCbox = new Checkbox("MouseEvent"); |
| 325 | + Checkbox motionListenerCbox = new Checkbox(" MouseMotionEvent"); |
| 326 | + Checkbox awtListenerCbox = new Checkbox(" AWTEventListener"); |
| 327 | + TextField tipText = new TextField("TrayIcon", 50); |
| 328 | + Button tipButton = new PackedButton("Set"); |
| 329 | + Button setPopupButton = new PackedButton("Set"); |
| 330 | + Button remPopupButton = new PackedButton("Remove"); |
| 331 | + |
| 332 | + PopupMenu popupMenu = new PopupMenu(); |
| 333 | + |
| 334 | + Map<String, TrayIcon> resToObjMap = new HashMap<>(); |
| 335 | + |
| 336 | + Container cont = new Container(); |
| 337 | + |
| 338 | + TrayIconControl() { |
| 339 | + Toolkit.getDefaultToolkit().addAWTEventListener(e -> { |
| 340 | + if (e.getSource() instanceof TrayIcon && awtListenerCbox.getState()) { |
| 341 | + PassFailJFrame.log(e.toString()); |
| 342 | + } |
| 343 | + }, MouseEvent.MOUSE_EVENT_MASK | MouseEvent.MOUSE_MOTION_EVENT_MASK | |
| 344 | + ActionEvent.ACTION_EVENT_MASK); |
| 345 | + |
| 346 | + cont.setLayout(new GridLayout(4, 1)); |
| 347 | + |
| 348 | + Container raw1 = new Container(); |
| 349 | + raw1.setLayout(new GridLayout(1, 4)); |
| 350 | + cont.add(raw1); |
| 351 | + |
| 352 | + InsetsPanel cbgPanel = new InsetsPanel(); |
| 353 | + cbgPanel.setLayout(new GridLayout(4, 1)); |
| 354 | + Checkbox nullCbox = new Checkbox("null", cbg, true); |
| 355 | + Checkbox redCbox = new Checkbox(RED_ICON, cbg, false); |
| 356 | + Checkbox blueCbox = new Checkbox(BLUE_ICON, cbg, false); |
| 357 | + Checkbox greenCbox = new Checkbox(GREEN_ICON, cbg, false); |
| 358 | + cbgPanel.add(nullCbox); |
| 359 | + cbgPanel.add(redCbox); |
| 360 | + cbgPanel.add(blueCbox); |
| 361 | + cbgPanel.add(greenCbox); |
| 362 | + cbgPanel.addTo(raw1); |
| 363 | + |
| 364 | + InsetsPanel addremPanel = new InsetsPanel(); |
| 365 | + addremPanel.setLayout(new BorderLayout()); |
| 366 | + addremPanel.add(addButton.getParent(), BorderLayout.NORTH); |
| 367 | + addremPanel.add(remButton.getParent(), BorderLayout.SOUTH); |
| 368 | + addremPanel.addTo(raw1); |
| 369 | + |
| 370 | + InsetsPanel resizePanel = new InsetsPanel(); |
| 371 | + resizePanel.add(resizeButton); |
| 372 | + resizePanel.addTo(raw1); |
| 373 | + |
| 374 | + InsetsPanel lstPanel = new InsetsPanel(); |
| 375 | + lstPanel.setLayout(new GridLayout(3, 1)); |
| 376 | + lstPanel.add(mouseListenerCbox); |
| 377 | + lstPanel.add(motionListenerCbox); |
| 378 | + lstPanel.add(awtListenerCbox); |
| 379 | + lstPanel.addTo(raw1); |
| 380 | + |
| 381 | + Container raw2 = new Container(); |
| 382 | + raw2.setLayout(new BorderLayout()); |
| 383 | + cont.add(raw2); |
| 384 | + |
| 385 | + InsetsPanel balloonPanel = new InsetsPanel(); |
| 386 | + balloonPanel.setLayout(new BorderLayout()); |
| 387 | + balloonPanel.add(balloonButton.getParent(), BorderLayout.NORTH); |
| 388 | + Container bc = new Container(); |
| 389 | + bc.setLayout(new FlowLayout()); |
| 390 | + bc.add(new Label(" Type:")); |
| 391 | + bc.add(balloonChoice); |
| 392 | + balloonPanel.add(bc, BorderLayout.SOUTH); |
| 393 | + balloonPanel.addTo(raw2, BorderLayout.WEST); |
| 394 | + |
| 395 | + InsetsPanel blnTextPanel = new InsetsPanel(); |
| 396 | + blnTextPanel.setLayout(new GridLayout(2, 2)); |
| 397 | + Container c1 = new Panel(); |
| 398 | + c1.setLayout(new FlowLayout()); |
| 399 | + blnTextPanel.add(c1); |
| 400 | + c1.add(new Label("Message:")); |
| 401 | + c1.add(balloonText); |
| 402 | + |
| 403 | + Container c2 = new Panel(); |
| 404 | + c2.setLayout(new FlowLayout()); |
| 405 | + blnTextPanel.add(c2); |
| 406 | + c2.add(new Label("Caption:")); |
| 407 | + c2.add(balloonCaption); |
| 408 | + blnTextPanel.addTo(raw2, BorderLayout.CENTER); |
| 409 | + |
| 410 | + |
| 411 | + Container raw3 = new Container(); |
| 412 | + raw3.setLayout(new BorderLayout()); |
| 413 | + cont.add(raw3); |
| 414 | + |
| 415 | + InsetsPanel popupPanel = new InsetsPanel(); |
| 416 | + popupPanel.setLayout(new FlowLayout()); |
| 417 | + popupPanel.add(new Label("Popup menu:")); |
| 418 | + popupPanel.add(setPopupButton); |
| 419 | + popupPanel.add(remPopupButton); |
| 420 | + popupPanel.addTo(raw3); |
| 421 | + |
| 422 | + |
| 423 | + Container raw4 = new Container(); |
| 424 | + raw4.setLayout(new BorderLayout()); |
| 425 | + cont.add(raw4); |
| 426 | + |
| 427 | + InsetsPanel tipPanel = new InsetsPanel(); |
| 428 | + tipPanel.setLayout(new FlowLayout()); |
| 429 | + tipPanel.add(new Label("Tooltip:")); |
| 430 | + tipPanel.add(tipText); |
| 431 | + tipPanel.add(tipButton); |
| 432 | + tipPanel.addTo(raw4); |
| 433 | + |
| 434 | + addButton.addActionListener(e -> { |
| 435 | + try { |
| 436 | + tray.add(getCurIcon()); |
| 437 | + } catch (NullPointerException npe) { |
| 438 | + if (npe.getMessage() == null) { |
| 439 | + PassFailJFrame.log("Probably wrong path to the images."); |
| 440 | + throw npe; // if wrong images path was set |
| 441 | + } |
| 442 | + PassFailJFrame.log(npe.toString()); |
| 443 | + } catch (IllegalArgumentException iae) { |
| 444 | + PassFailJFrame.log(iae.toString()); |
| 445 | + } catch (AWTException ise) { |
| 446 | + PassFailJFrame.log(ise.toString()); |
| 447 | + } |
| 448 | + }); |
| 449 | + remButton.addActionListener(e -> tray.remove(getCurIcon())); |
| 450 | + |
| 451 | + resizeButton.addActionListener( |
| 452 | + e -> getCurIcon().setImageAutoSize(!getCurIcon().isImageAutoSize())); |
| 453 | + |
| 454 | + balloonButton.addActionListener(e -> { |
| 455 | + String text = null, caption = null; |
| 456 | + if (balloonText.getText().compareToIgnoreCase("null") != 0) { |
| 457 | + text = balloonText.getText(); |
| 458 | + } |
| 459 | + if (balloonCaption.getText().compareToIgnoreCase("null") != 0) { |
| 460 | + caption = balloonCaption.getText(); |
| 461 | + } |
| 462 | + try { |
| 463 | + getCurIcon().displayMessage(caption, text, typeArr[balloonChoice.getSelectedIndex()]); |
| 464 | + } catch (NullPointerException npe) { |
| 465 | + PassFailJFrame.log(npe.toString()); |
| 466 | + } |
| 467 | + }); |
| 468 | + |
| 469 | + tipButton.addActionListener(e -> { |
| 470 | + String tip = null; |
| 471 | + if (tipText.getText().compareToIgnoreCase("null") != 0) { |
| 472 | + tip = tipText.getText(); |
| 473 | + } |
| 474 | + getCurIcon().setToolTip(tip); |
| 475 | + }); |
| 476 | + |
| 477 | + setPopupButton.addActionListener(e -> { |
| 478 | + try { |
| 479 | + getCurIcon().setPopupMenu(popupMenu); |
| 480 | + } catch (IllegalArgumentException iae) { |
| 481 | + PassFailJFrame.log(iae.toString()); |
| 482 | + } |
| 483 | + }); |
| 484 | + |
| 485 | + remPopupButton.addActionListener(e -> getCurIcon().setPopupMenu(null)); |
| 486 | + for (String s: balloonTypes) { |
| 487 | + balloonChoice.add(s); |
| 488 | + } |
| 489 | + |
| 490 | + init(); |
| 491 | + } |
| 492 | + |
| 493 | + void init() { |
| 494 | + tray = SystemTray.getSystemTray(); |
| 495 | + tray.addPropertyChangeListener("trayIcons", |
| 496 | + e -> printPropertyChangeEvent(e)); |
| 497 | + |
| 498 | + tray.addPropertyChangeListener("systemTray", |
| 499 | + e -> printPropertyChangeEvent(e)); |
| 500 | + |
| 501 | + configureTrayIcon(RED_ICON); |
| 502 | + configureTrayIcon(BLUE_ICON); |
| 503 | + configureTrayIcon(GREEN_ICON); |
| 504 | + |
| 505 | + for (String s: balloonTypes) { |
| 506 | + popupMenu.add(new MenuItem(s)); |
| 507 | + } |
| 508 | + } |
| 509 | + |
| 510 | + void printPropertyChangeEvent(PropertyChangeEvent e) { |
| 511 | + String name = e.getPropertyName(); |
| 512 | + Object oldValue = e.getOldValue(); |
| 513 | + Object newValue = e.getNewValue(); |
| 514 | + |
| 515 | + PassFailJFrame.log("PropertyChangeEvent[name=" + name |
| 516 | + + ",oldValue=" + oldValue + ",newValue=" + newValue + "]"); |
| 517 | + } |
| 518 | + |
| 519 | + void configureTrayIcon(String icon) { |
| 520 | + Color color = Color.WHITE; |
| 521 | + switch (icon) { |
| 522 | + case "RED ICON" -> color = Color.RED; |
| 523 | + case "BLUE ICON" -> color = Color.BLUE; |
| 524 | + case "GREEN ICON" -> color = Color.GREEN; |
| 525 | + } |
| 526 | + Image image = createIcon(color); |
| 527 | + TrayIcon trayIcon = new TrayIcon(image); |
| 528 | + |
| 529 | + trayIcon.addMouseListener(new MouseAdapter() { |
| 530 | + public void mousePressed(MouseEvent e) { |
| 531 | + if (mouseListenerCbox.getState()) |
| 532 | + PassFailJFrame.log(e.toString()); |
| 533 | + } |
| 534 | + public void mouseReleased(MouseEvent e) { |
| 535 | + if (mouseListenerCbox.getState()) |
| 536 | + PassFailJFrame.log(e.toString()); |
| 537 | + } |
| 538 | + public void mouseClicked(MouseEvent e) { |
| 539 | + if (mouseListenerCbox.getState()) |
| 540 | + PassFailJFrame.log(e.toString()); |
| 541 | + } |
| 542 | + }); |
| 543 | + trayIcon.addMouseMotionListener(new MouseMotionAdapter() { |
| 544 | + public void mouseMoved(MouseEvent e) { |
| 545 | + if (motionListenerCbox.getState()) |
| 546 | + PassFailJFrame.log(e.toString()); |
| 547 | + } |
| 548 | + }); |
| 549 | + trayIcon.addActionListener(e -> PassFailJFrame.log(e.toString())); |
| 550 | + |
| 551 | + resToObjMap.remove(icon); |
| 552 | + resToObjMap.put(icon, trayIcon); |
| 553 | + } |
| 554 | + |
| 555 | + String getCurImgName() { |
| 556 | + return cbg.getSelectedCheckbox().getLabel(); |
| 557 | + } |
| 558 | + |
| 559 | + TrayIcon getCurIcon() { |
| 560 | + return resToObjMap.get(getCurImgName()); |
| 561 | + } |
| 562 | + |
| 563 | + public void dispose() { |
| 564 | + tray.remove(getCurIcon()); |
| 565 | + } |
| 566 | + |
| 567 | + private static Image createIcon(Color color) { |
| 568 | + BufferedImage image = new BufferedImage(16, 16, |
| 569 | + BufferedImage.TYPE_INT_ARGB); |
| 570 | + Graphics2D g = image.createGraphics(); |
| 571 | + g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, |
| 572 | + RenderingHints.VALUE_TEXT_ANTIALIAS_ON); |
| 573 | + g.setColor(color); |
| 574 | + g.fillRect(0, 0, 16, 16); |
| 575 | + g.dispose(); |
| 576 | + return image; |
| 577 | + } |
| 578 | + |
| 579 | + } |
| 580 | + |
| 581 | + private static class InsetsPanel extends Panel { |
| 582 | + Container parent = new Container() { |
| 583 | + public Insets getInsets() { |
| 584 | + return new Insets(2, 2, 2, 2); |
| 585 | + } |
| 586 | + }; |
| 587 | + |
| 588 | + InsetsPanel() { |
| 589 | + parent.setLayout(new BorderLayout()); |
| 590 | + setBackground(new Color(240, 240, 240)); |
| 591 | + } |
| 592 | + |
| 593 | + void addTo(Container c) { |
| 594 | + parent.add(this); |
| 595 | + c.add(parent); |
| 596 | + } |
| 597 | + |
| 598 | + void addTo(Container c, String pos) { |
| 599 | + parent.add(this); |
| 600 | + c.add(parent, pos); |
| 601 | + } |
| 602 | + } |
| 603 | + |
| 604 | + private static class PackedButton extends Button { |
| 605 | + Container parent = new Container(); |
| 606 | + PackedButton(String l) { |
| 607 | + super(l); |
| 608 | + parent.setLayout(new FlowLayout()); |
| 609 | + parent.add(this); |
| 610 | + } |
| 611 | + } |
| 612 | +} |
| 613 | + |
0 commit comments