49
50 if (System.getProperty("os.name").startsWith("Win")) {
51 try {
52 // sun.awt.windows.WTrayIconPeer
53 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
54 Dimension iconSize = icon.getSize();
55
56 int width = (int) iconSize.getWidth();
57 int height = (int) iconSize.getHeight();
58
59 // Some previously created icons may not be removed
60 // from tray until mouse move on it. So we glide
61 // through the whole tray bar.
62 robot.glide((int) screenSize.getWidth(), (int) (screenSize.getHeight()-15), 0, (int) (screenSize.getHeight() - 15), 1, 2);
63
64 BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));
65
66 for (int x = (int) (screenSize.getWidth()-width); x > 0; x--) {
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))) {
69 return new Point(x+5, y+5);
70 }
71 }
72 }
73 } catch (Exception e) {
74 e.printStackTrace();
75 return null;
76 }
77 } else if (System.getProperty("os.name").startsWith("Mac")) {
78 Point2D point2d;
79 try {
80 // sun.lwawt.macosx.CTrayIcon
81 Field f_peer = getField( java.awt.TrayIcon.class, "peer");
82
83 Object peer = f_peer.get(icon);
84 Method m_getModel = peer.getClass().getDeclaredMethod(
85 "getModel");
86 m_getModel.setAccessible(true);
87 long model = (Long) (m_getModel.invoke(peer, new Object[]{}));
88 Method m_getLocation = peer.getClass().getDeclaredMethod(
89 "nativeGetIconLocation", new Class[]{Long.TYPE});
90 m_getLocation.setAccessible(true);
91 point2d = (Point2D)m_getLocation.invoke(peer, new Object[]{model});
92 Point po = new Point((int)(point2d.getX()), (int)(point2d.getY()));
93 po.translate(10, -5);
94 return po;
95 }catch(Exception e) {
96 e.printStackTrace();
97 return null;
98 }
99 } else {
100 try {
101 // sun.awt.X11.XTrayIconPeer
102 Field f_peer = getField(java.awt.TrayIcon.class, "peer");
103
104 Object peer = f_peer.get(icon);
105 Method m_getLOS = peer.getClass().getDeclaredMethod(
106 "getLocationOnScreen", new Class[]{});
107 m_getLOS.setAccessible(true);
108 Point point = (Point)m_getLOS.invoke(peer, new Object[]{});
109 point.translate(5, 5);
110 return point;
111 } catch (Exception e) {
112 e.printStackTrace();
113 return null;
114 }
115 }
116 return null;
117 }
118
119 static Field getField(final Class clz, final String fieldName) {
120 Field res = null;
121 try {
122 res = (Field)AccessController.doPrivileged((PrivilegedExceptionAction) () -> {
123 Field f = clz.getDeclaredField(fieldName);
124 f.setAccessible(true);
125 return f;
126 });
127 } catch (PrivilegedActionException ex) {
128 ex.printStackTrace();
129 }
151 robot.mouseRelease(InputEvent.BUTTON1_MASK);
152 robot.delay(50);
153 robot.mousePress(InputEvent.BUTTON1_MASK);
154 robot.delay(50);
155 robot.mouseRelease(InputEvent.BUTTON1_MASK);
156 }
157 }
158
159 // Method for skipping some OS specific cases
160 static boolean skip(int button) {
161 if (System.getProperty("os.name").toLowerCase().startsWith("win")){
162 if (button == InputEvent.BUTTON1_MASK){
163 // See JDK-6827035
164 return true;
165 }
166 } else if (System.getProperty("os.name").toLowerCase().contains("os x")){
167 // See JDK-7153700
168 return true;
169 }
170 return false;
171 }
172 }
|
49
50 if (System.getProperty("os.name").startsWith("Win")) {
51 try {
52 // sun.awt.windows.WTrayIconPeer
53 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
54 Dimension iconSize = icon.getSize();
55
56 int width = (int) iconSize.getWidth();
57 int height = (int) iconSize.getHeight();
58
59 // Some previously created icons may not be removed
60 // from tray until mouse move on it. So we glide
61 // through the whole tray bar.
62 robot.glide((int) screenSize.getWidth(), (int) (screenSize.getHeight()-15), 0, (int) (screenSize.getHeight() - 15), 1, 2);
63
64 BufferedImage screen = robot.createScreenCapture(new Rectangle(screenSize));
65
66 for (int x = (int) (screenSize.getWidth()-width); x > 0; x--) {
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))) {
69 Point point = new Point(x + 5, y + 5);
70 System.out.println("Icon location " + point);
71 return point;
72 }
73 }
74 }
75 } catch (Exception e) {
76 e.printStackTrace();
77 return null;
78 }
79 } else if (System.getProperty("os.name").startsWith("Mac")) {
80 Point2D point2d;
81 try {
82 // sun.lwawt.macosx.CTrayIcon
83 Field f_peer = getField( java.awt.TrayIcon.class, "peer");
84
85 Object peer = f_peer.get(icon);
86 Method m_getModel = peer.getClass().getDeclaredMethod(
87 "getModel");
88 m_getModel.setAccessible(true);
89 long model = (Long) (m_getModel.invoke(peer, new Object[]{}));
90 Method m_getLocation = peer.getClass().getDeclaredMethod(
91 "nativeGetIconLocation", new Class[]{Long.TYPE});
92 m_getLocation.setAccessible(true);
93 point2d = (Point2D)m_getLocation.invoke(peer, new Object[]{model});
94 Point po = new Point((int)(point2d.getX()), (int)(point2d.getY()));
95 po.translate(10, -5);
96 System.out.println("Icon location " + po);
97 return po;
98 }catch(Exception e) {
99 e.printStackTrace();
100 return null;
101 }
102 } else {
103 try {
104 // sun.awt.X11.XTrayIconPeer
105 Field f_peer = getField(java.awt.TrayIcon.class, "peer");
106
107 SystemTrayIconHelper.openTrayIfNeeded(robot);
108
109 Object peer = f_peer.get(icon);
110 Method m_getLOS = peer.getClass().getDeclaredMethod(
111 "getLocationOnScreen", new Class[]{});
112 m_getLOS.setAccessible(true);
113 Point point = (Point)m_getLOS.invoke(peer, new Object[]{});
114 point.translate(5, 5);
115 System.out.println("Icon location " + point);
116 return point;
117 } catch (Exception e) {
118 e.printStackTrace();
119 return null;
120 }
121 }
122 return null;
123 }
124
125 static Field getField(final Class clz, final String fieldName) {
126 Field res = null;
127 try {
128 res = (Field)AccessController.doPrivileged((PrivilegedExceptionAction) () -> {
129 Field f = clz.getDeclaredField(fieldName);
130 f.setAccessible(true);
131 return f;
132 });
133 } catch (PrivilegedActionException ex) {
134 ex.printStackTrace();
135 }
157 robot.mouseRelease(InputEvent.BUTTON1_MASK);
158 robot.delay(50);
159 robot.mousePress(InputEvent.BUTTON1_MASK);
160 robot.delay(50);
161 robot.mouseRelease(InputEvent.BUTTON1_MASK);
162 }
163 }
164
165 // Method for skipping some OS specific cases
166 static boolean skip(int button) {
167 if (System.getProperty("os.name").toLowerCase().startsWith("win")){
168 if (button == InputEvent.BUTTON1_MASK){
169 // See JDK-6827035
170 return true;
171 }
172 } else if (System.getProperty("os.name").toLowerCase().contains("os x")){
173 // See JDK-7153700
174 return true;
175 }
176 return false;
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 }
212 }
|