jdk/test/java/awt/TrayIcon/SystemTrayIconHelper.java
changeset 34788 176e7a6529d4
parent 25137 43055e2deee9
child 36511 9d0388c6b336
equal deleted inserted replaced
34787:36167bbe1a9f 34788:176e7a6529d4
    64                 BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));
    64                 BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));
    65 
    65 
    66                 for (int x = (int) (screenSize.getWidth()-width); x > 0; x--) {
    66                 for (int x = (int) (screenSize.getWidth()-width); x > 0; x--) {
    67                     for (int y = (int) (screenSize.getHeight()-height); y > (screenSize.getHeight()-50); y--) {
    67                     for (int y = (int) (screenSize.getHeight()-height); y > (screenSize.getHeight()-50); y--) {
    68                         if (imagesEquals(((BufferedImage)icon.getImage()).getSubimage(0, 0, width, height), screen.getSubimage(x, y, width, height))) {
    68                         if (imagesEquals(((BufferedImage)icon.getImage()).getSubimage(0, 0, width, height), screen.getSubimage(x, y, width, height))) {
    69                             return new Point(x+5, y+5);
    69                             Point point = new Point(x + 5, y + 5);
       
    70                             System.out.println("Icon location " + point);
       
    71                             return point;
    70                         }
    72                         }
    71                     }
    73                     }
    72                 }
    74                 }
    73             } catch (Exception e) {
    75             } catch (Exception e) {
    74                 e.printStackTrace();
    76                 e.printStackTrace();
    89                         "nativeGetIconLocation", new Class[]{Long.TYPE});
    91                         "nativeGetIconLocation", new Class[]{Long.TYPE});
    90                 m_getLocation.setAccessible(true);
    92                 m_getLocation.setAccessible(true);
    91                 point2d = (Point2D)m_getLocation.invoke(peer, new Object[]{model});
    93                 point2d = (Point2D)m_getLocation.invoke(peer, new Object[]{model});
    92                 Point po = new Point((int)(point2d.getX()), (int)(point2d.getY()));
    94                 Point po = new Point((int)(point2d.getX()), (int)(point2d.getY()));
    93                 po.translate(10, -5);
    95                 po.translate(10, -5);
       
    96                 System.out.println("Icon location " + po);
    94                 return po;
    97                 return po;
    95             }catch(Exception e) {
    98             }catch(Exception e) {
    96                 e.printStackTrace();
    99                 e.printStackTrace();
    97                 return null;
   100                 return null;
    98             }
   101             }
    99         } else {
   102         } else {
   100             try {
   103             try {
   101                 // sun.awt.X11.XTrayIconPeer
   104                 // sun.awt.X11.XTrayIconPeer
   102                 Field f_peer = getField(java.awt.TrayIcon.class, "peer");
   105                 Field f_peer = getField(java.awt.TrayIcon.class, "peer");
       
   106 
       
   107                 SystemTrayIconHelper.openTrayIfNeeded(robot);
   103 
   108 
   104                 Object peer = f_peer.get(icon);
   109                 Object peer = f_peer.get(icon);
   105                 Method m_getLOS = peer.getClass().getDeclaredMethod(
   110                 Method m_getLOS = peer.getClass().getDeclaredMethod(
   106                         "getLocationOnScreen", new Class[]{});
   111                         "getLocationOnScreen", new Class[]{});
   107                 m_getLOS.setAccessible(true);
   112                 m_getLOS.setAccessible(true);
   108                 Point point = (Point)m_getLOS.invoke(peer, new Object[]{});
   113                 Point point = (Point)m_getLOS.invoke(peer, new Object[]{});
   109                 point.translate(5, 5);
   114                 point.translate(5, 5);
       
   115                 System.out.println("Icon location " + point);
   110                 return point;
   116                 return point;
   111             } catch (Exception e) {
   117             } catch (Exception e) {
   112                 e.printStackTrace();
   118                 e.printStackTrace();
   113                 return null;
   119                 return null;
   114             }
   120             }
   167             // See JDK-7153700
   173             // See JDK-7153700
   168             return true;
   174             return true;
   169         }
   175         }
   170         return false;
   176         return false;
   171     }
   177     }
       
   178 
       
   179     public static boolean openTrayIfNeeded(Robot robot) {
       
   180         String sysv = System.getProperty("os.version");
       
   181         System.out.println("System version is " + sysv);
       
   182         //Additional step to raise the system try in Gnome 3 in OEL 7
       
   183         if(isOel7()) {
       
   184             System.out.println("OEL 7 detected");
       
   185             GraphicsConfiguration gc = GraphicsEnvironment.
       
   186                     getLocalGraphicsEnvironment().getDefaultScreenDevice().
       
   187                     getDefaultConfiguration();
       
   188             Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
       
   189             if(insets.bottom > 0) {
       
   190                 Dimension screenSize = Toolkit.getDefaultToolkit()
       
   191                         .getScreenSize();
       
   192                 robot.mouseMove(screenSize.width - insets.bottom / 2,
       
   193                         screenSize.height - insets.bottom / 2);
       
   194                 robot.delay(50);
       
   195                 robot.mousePress(InputEvent.BUTTON1_MASK);
       
   196                 robot.delay(50);
       
   197                 robot.mouseRelease(InputEvent.BUTTON1_MASK);
       
   198                 robot.waitForIdle();
       
   199                 robot.delay(1000);
       
   200                 System.out.println("Tray is opened");
       
   201                 return true;
       
   202             }
       
   203         }
       
   204         return false;
       
   205     }
       
   206 
       
   207     public static boolean isOel7() {
       
   208         return System.getProperty("os.name").toLowerCase()
       
   209                 .contains("linux") && System.getProperty("os.version")
       
   210                 .toLowerCase().contains("el7");
       
   211     }
   172 }
   212 }