1 /*
   2  * Copyright (c) 2013, 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 
  25 import test.java.awt.regtesthelpers.Sysout;
  26 
  27 import java.applet.Applet;
  28 import java.awt.*;
  29 import java.awt.image.BufferedImage;
  30 
  31 public class AddPopupAfterShowTest extends Applet {
  32     @Override
  33     public void init() {
  34         if (!SystemTray.isSupported()) {
  35             Sysout.createDialogWithInstructions(new String[]{
  36                     "Press PASS, the System Tray is not supported"});
  37             return;
  38         }
  39 
  40 
  41         String[] instructions = {
  42                 "1) The red circle icon was added to the system tray.",
  43                 "2) Check that a popup menu is opened when the icon is clicked.",
  44                 "3) If true the test is passed, otherwise failed."};
  45         Sysout.createDialogWithInstructions(instructions);
  46     }
  47 
  48     @Override
  49     public void start() {
  50         setSize(200, 200);
  51         show();
  52 
  53         createSystemTrayIcon();
  54     }
  55 
  56     private static void createSystemTrayIcon() {
  57         final TrayIcon trayIcon = new TrayIcon(createTrayIconImage());
  58         trayIcon.setImageAutoSize(true);
  59 
  60         try {
  61             // Add tray icon to system tray *before* adding popup menu to demonstrate buggy behaviour
  62             SystemTray.getSystemTray().add(trayIcon);
  63             trayIcon.setPopupMenu(createTrayIconPopupMenu());
  64         } catch (final AWTException awte) {
  65             awte.printStackTrace();
  66         }
  67     }
  68 
  69     private static Image createTrayIconImage() {
  70         /**
  71          * Create a small image of a red circle to use as the icon for the tray icon
  72          */
  73         int trayIconImageSize = 32;
  74         final BufferedImage trayImage = new BufferedImage(trayIconImageSize, trayIconImageSize, BufferedImage.TYPE_INT_ARGB);
  75         final Graphics2D trayImageGraphics = (Graphics2D) trayImage.getGraphics();
  76 
  77         trayImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  78 
  79         trayImageGraphics.setColor(new Color(255, 255, 255, 0));
  80         trayImageGraphics.fillRect(0, 0, trayImage.getWidth(), trayImage.getHeight());
  81 
  82         trayImageGraphics.setColor(Color.red);
  83 
  84         int trayIconImageInset = 4;
  85         trayImageGraphics.fillOval(trayIconImageInset,
  86                 trayIconImageInset,
  87                 trayImage.getWidth() - 2 * trayIconImageInset,
  88                 trayImage.getHeight() - 2 * trayIconImageInset);
  89 
  90         trayImageGraphics.setColor(Color.darkGray);
  91 
  92         trayImageGraphics.drawOval(trayIconImageInset,
  93                 trayIconImageInset,
  94                 trayImage.getWidth() - 2 * trayIconImageInset,
  95                 trayImage.getHeight() - 2 * trayIconImageInset);
  96 
  97         return trayImage;
  98     }
  99 
 100     private static PopupMenu createTrayIconPopupMenu() {
 101         final PopupMenu trayIconPopupMenu = new PopupMenu();
 102         final MenuItem popupMenuItem = new MenuItem("TEST PASSED!");
 103         trayIconPopupMenu.add(popupMenuItem);
 104         return trayIconPopupMenu;
 105     }
 106 }