1 /*
   2  * Copyright (c) 2016, 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 /*
  26   @test
  27   @bug 8150176
  28   @ignore 8150176
  29   @summary Check if correct resolution variant is used for tray icon.
  30   @author a.stepanov
  31   @run applet/manual=yesno MultiResolutionTrayIconTest.html
  32 */
  33 
  34 
  35 import java.applet.Applet;
  36 import java.awt.*;
  37 import java.awt.event.*;
  38 import java.awt.image.*;
  39 
  40 
  41 public class MultiResolutionTrayIconTest extends Applet {
  42 
  43     private SystemTray tray;
  44     private TrayIcon   icon, iconMRI;
  45 
  46     public void init() { this.setLayout(new BorderLayout()); }
  47 
  48     public void start() {
  49 
  50         boolean trayIsSupported = SystemTray.isSupported();
  51         Button b = new Button("Start");
  52         if (trayIsSupported) {
  53 
  54             prepareIcons();
  55             b.addActionListener(new ActionListener() {
  56                 @Override
  57                 public void actionPerformed(ActionEvent e) { doTest(); }
  58             });
  59         } else {
  60              b.setLabel("not supported");
  61              b.setEnabled(false);
  62              System.out.println("system tray is not supported");
  63         }
  64         add(b, BorderLayout.CENTER);
  65 
  66         validate();
  67         setVisible(true);
  68     }
  69 
  70     private BufferedImage generateImage(int w, int h, Color c) {
  71 
  72         BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  73         Graphics g = img.getGraphics();
  74         g.setColor(c);
  75         g.fillRect(0, 0, w, h);
  76         g.setColor(Color.WHITE);
  77         int r = (Math.min(w, h) >= 8) ? 3 : 1;
  78         g.fillRect(r, r, w - 2 * r, h - 2 * r);
  79         return img;
  80     }
  81 
  82     private void prepareIcons() {
  83 
  84         tray = SystemTray.getSystemTray();
  85         Dimension d = tray.getTrayIconSize();
  86         int w = d.width, h = d.height;
  87 
  88         BufferedImage img = generateImage(w, h, Color.BLUE);
  89         // use wrong icon size for "nok"
  90         BufferedImage nok = generateImage(w / 2 + 2, h / 2 + 2, Color.RED);
  91         BaseMultiResolutionImage mri =
  92             new BaseMultiResolutionImage(new BufferedImage[] {nok, img});
  93         icon    = new TrayIcon(img);
  94         iconMRI = new TrayIcon(mri);
  95     }
  96 
  97     private void doTest() {
  98 
  99         if (tray.getTrayIcons().length > 0) { return; } // icons were added already
 100         try {
 101             tray.add(icon);
 102             tray.add(iconMRI);
 103         } catch (Exception e) {
 104             throw new RuntimeException(e);
 105         }
 106     }
 107 
 108     public void stop() {
 109 
 110         // check for null, just in case
 111         if (tray != null) {
 112             tray.remove(icon);
 113             tray.remove(iconMRI);
 114         }
 115     }
 116 }