net.sf.gluebooster.java.booster.basic.gui.awt.BoostedRobot Maven / Gradle / Ivy
package net.sf.gluebooster.java.booster.basic.gui.awt;
import java.awt.AWTException;
import java.awt.Component;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import net.sf.gluebooster.java.booster.essentials.logging.LogBooster;
import net.sf.gluebooster.java.booster.essentials.utils.MathBoostUtils;
import net.sf.gluebooster.java.booster.essentials.utils.ThreadBoostUtils;
import org.apache.commons.lang3.ObjectUtils;
/**
* A robot with additional configuration.
*
* @author CBauer
*
*/
public class BoostedRobot extends Robot {
/**
* Used for logging.
*/
private static final LogBooster LOG = new LogBooster(BoostedRobot.class);
/**
* The screen of this robot.
*/
private GraphicsDevice screen;
/**
* Should the mouse be moved incrementally to a target position (true) or be
* placed directly to the position (false).
*/
private boolean moveMouseIncrementally = true;
/**
* The delay between mouse movements in milliseconds.
*/
private int mouseMoveDelayInMillis = 5;
public BoostedRobot() throws AWTException {
this(GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice());
}
public BoostedRobot(GraphicsDevice screen) throws AWTException {
super(screen);
this.screen = screen;
}
/**
*
* @param template
* a template for the new instance
*/
public BoostedRobot(GraphicsDevice screen, BoostedRobot template)
throws AWTException {
this(screen);
setMoveMouseIncrementally(template.isMoveMouseIncrementally());
}
@Override
public synchronized void mouseMove(int x, int y) {
if (isMoveMouseIncrementally()) {
Point mouseLocation = MouseInfo.getPointerInfo().getLocation();
for (Point point : MathBoostUtils.getLinePointsBetween(
mouseLocation, new Point(x, y))) {
super.mouseMove(point.x, point.y);
ThreadBoostUtils.sleep(mouseMoveDelayInMillis);
}
} else {
super.mouseMove(x, y);
}
}
/**
* Considers multi-monitor situations. The original robot works with the
* coordinate system of the primary screen, unless specified otherwise.
* Adapted from
* http://stackoverflow.com/questions/2941324/how-do-i-set-the-position
* -of-the-mouse-in-java
*
* @param point
* based global screen coordinates
* @return boolean true if everything is ok
*/
public boolean mouseMove(Point point) {
// Search the devices for the one that draws the specified point.
for (GraphicsDevice device : GraphicsEnvironment
.getLocalGraphicsEnvironment().getScreenDevices()) {
GraphicsConfiguration[] configurations = device.getConfigurations();
for (GraphicsConfiguration config : configurations) {
Rectangle bounds = config.getBounds();
if (bounds.contains(point)) {
// Set point to screen coordinates.
Point b = bounds.getLocation();
Point s = new Point(point.x - b.x, point.y - b.y);
try {
Robot robot;
if (ObjectUtils.equals(device, screen))
robot = this;
else
robot = new BoostedRobot(device);
robot.mouseMove(s.x, s.y);
return true;
} catch (AWTException ex) {
LOG.warn(ex);
return false;
}
}
}
}
return false;
}
/**
* Move the mouse to the center of the component
*
* @param component
* the wanted component
* @return true if successful
*/
public boolean mouseMove(Component component) {
Point location = component.getLocationOnScreen();
location.translate(component.getWidth() / 2, component.getHeight() / 2);
return mouseMove(location);
}
/**
* Click with the mouse.
*/
public void mouseClick() {
mousePress(InputEvent.BUTTON1_DOWN_MASK);
mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
}
// public static void main(String[] ignored) {
// GraphicsDevice[] devices = GraphicsEnvironment
// .getLocalGraphicsEnvironment().getScreenDevices();
//
// LOG.info(devices);
// }
private boolean isMoveMouseIncrementally() {
return moveMouseIncrementally;
}
private void setMoveMouseIncrementally(boolean moveMouseIncrementally) {
this.moveMouseIncrementally = moveMouseIncrementally;
}
}