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