00001 package edu.stanford.hci.r3.actions.types;
00002
00003 import java.awt.AWTException;
00004 import java.awt.Color;
00005 import java.awt.GraphicsDevice;
00006 import java.awt.GraphicsEnvironment;
00007 import java.awt.Rectangle;
00008 import java.awt.Robot;
00009 import java.awt.image.BufferedImage;
00010 import java.io.File;
00011 import java.util.ArrayList;
00012 import java.util.List;
00013
00014 import javax.swing.KeyStroke;
00015
00016 import edu.stanford.hci.r3.actions.R3Action;
00017 import edu.stanford.hci.r3.util.graphics.ImageUtils;
00018
00034 public class RobotAction implements R3Action {
00035
00039 public static enum MouseWheelDirection {
00040 ROLL_WHEEL_DOWN(1), ROLL_WHEEL_UP(-1);
00041
00042 private int value;
00043
00044 private MouseWheelDirection(int val) {
00045 value = val;
00046 }
00047
00048 public int getValue() {
00049 return value;
00050 }
00051 }
00052
00056 public static class RobotCommand {
00057 private Object[] arguments;
00058
00059 private RobotMethod method;
00060
00061 public RobotCommand(RobotMethod m, Object... args) {
00062 method = m;
00063 arguments = args;
00064 }
00065 }
00066
00070 public static enum RobotMethod {
00071 CREATE_SCREEN_CAPTURE("CreateScreenCapture"),
00072 DELAY("Delay"), GET_PIXEL_COLOR("GetPixelColor"), KEY_PRESS("KPress"), KEY_RELEASE("KRelease"), KEY_TYPE(
00073 "KType"), MOUSE_MOVE("MMove"), MOUSE_PRESS("MPress"), MOUSE_RELEASE("MRelease"), MOUSE_WHEEL(
00074 "MWheel"), SET_AUTO_DELAY("SADelay"), SET_AUTO_WAIT_FOR_IDLE("SAWaitForIdle"), WAIT_FOR_IDLE(
00075 "WaitForIdle");
00076
00077 private String command;
00078
00079 private RobotMethod(String commandString) {
00080 command = commandString;
00081 }
00082
00083 public String getCommand() {
00084 return command;
00085 }
00086 }
00087
00091 private List<RobotCommand> commandsToRun = new ArrayList<RobotCommand>();
00092
00096 private int deviceToControl = 0;
00097
00101 public RobotAction() {
00102
00103 }
00104
00109 public void createScreenCapture(Rectangle screenRect, File destFile) {
00110 commandsToRun.add(new RobotCommand(RobotMethod.CREATE_SCREEN_CAPTURE, screenRect, destFile));
00111 }
00112
00116 public void delay(int ms) {
00117 commandsToRun.add(new RobotCommand(RobotMethod.DELAY, ms));
00118 }
00119
00123 public int getNumCommands() {
00124 return commandsToRun.size();
00125 }
00126
00131 public void getPixelColor(int x, int y) {
00132 commandsToRun.add(new RobotCommand(RobotMethod.GET_PIXEL_COLOR, x, y));
00133 }
00134
00140 private Robot getRobot() {
00141 Robot r = null;
00142 final GraphicsDevice[] screenDevices = GraphicsEnvironment.getLocalGraphicsEnvironment()
00143 .getScreenDevices();
00144
00145 if (!(deviceToControl >= 0 && deviceToControl < screenDevices.length)) {
00146 deviceToControl = 0;
00147 }
00148
00149
00150 try {
00151 r = new Robot(screenDevices[deviceToControl]);
00152 } catch (AWTException e) {
00153 e.printStackTrace();
00154 }
00155 return r;
00156 }
00157
00163 public void invoke() {
00164
00165 final Robot rob = getRobot();
00166
00167
00168 for (RobotCommand command : commandsToRun) {
00169 Object[] arguments = command.arguments;
00170 RobotMethod method = command.method;
00171
00172
00173
00174 switch (method) {
00175 case CREATE_SCREEN_CAPTURE:
00176 BufferedImage image = rob.createScreenCapture((Rectangle) arguments[0]);
00177
00178 ImageUtils.writeImageToJPEG(image, 100, (File) arguments[1]);
00179
00180 break;
00181 case DELAY:
00182 rob.delay((Integer) arguments[0]);
00183 break;
00184 case GET_PIXEL_COLOR:
00185 final Color pixelColor = rob.getPixelColor((Integer) arguments[0],
00186 (Integer) arguments[1]);
00187 System.out.println("RobotAction :: Pixel Color at " + arguments[0] + "," + arguments[1]
00188 + " is " + pixelColor);
00189
00190 break;
00191 case KEY_PRESS:
00192 rob.keyPress((Integer) arguments[0]);
00193 break;
00194 case KEY_RELEASE:
00195 rob.keyRelease((Integer) arguments[0]);
00196 break;
00197 case MOUSE_MOVE:
00198
00199
00200 rob.mouseMove((Integer) arguments[0], (Integer) arguments[1]);
00201 break;
00202 case MOUSE_PRESS:
00203 rob.mousePress((Integer) arguments[0]);
00204 break;
00205 case MOUSE_RELEASE:
00206 rob.mouseRelease((Integer) arguments[0]);
00207 break;
00208 case MOUSE_WHEEL:
00209 rob.mouseWheel((Integer) arguments[0]);
00210 break;
00211 case SET_AUTO_DELAY:
00212 rob.setAutoDelay((Integer) arguments[0]);
00213 break;
00214 case SET_AUTO_WAIT_FOR_IDLE:
00215 rob.setAutoWaitForIdle((Boolean) arguments[0]);
00216 break;
00217 case WAIT_FOR_IDLE:
00218 rob.waitForIdle();
00219 break;
00220 }
00221
00222 }
00223 }
00224
00229 public void keyPress(int keycode) {
00230 commandsToRun.add(new RobotCommand(RobotMethod.KEY_PRESS, keycode));
00231 }
00232
00236 public void keyRelease(int keycode) {
00237 commandsToRun.add(new RobotCommand(RobotMethod.KEY_RELEASE, keycode));
00238 }
00239
00244 public void keyType(int keycode) {
00245 keyPress(keycode);
00246 keyRelease(keycode);
00247 }
00248
00255 public void mouseMove(int x, int y) {
00256 commandsToRun.add(new RobotCommand(RobotMethod.MOUSE_MOVE, x, y));
00257 }
00258
00264 public void mousePress(int buttons) {
00265 commandsToRun.add(new RobotCommand(RobotMethod.MOUSE_PRESS, buttons));
00266 }
00267
00272 public void mouseRelease(int buttons) {
00273 commandsToRun.add(new RobotCommand(RobotMethod.MOUSE_RELEASE, buttons));
00274 }
00275
00280 public void mouseWheel(int wheelAmt, MouseWheelDirection direction) {
00281 commandsToRun.add(new RobotCommand(RobotMethod.MOUSE_WHEEL, Math.abs(wheelAmt)
00282 * direction.getValue()));
00283 }
00284
00288 public void setAutoDelay(int ms) {
00289 commandsToRun.add(new RobotCommand(RobotMethod.SET_AUTO_DELAY, ms));
00290 }
00291
00295 public void setAutoWaitForIdle(boolean isOn) {
00296 commandsToRun.add(new RobotCommand(RobotMethod.SET_AUTO_WAIT_FOR_IDLE, isOn));
00297 }
00298
00302 public void setScreenDevice(int requestedDevice) {
00303 deviceToControl = requestedDevice;
00304 }
00305
00309 public void typeString(String text) {
00310 char[] cs = text.toCharArray();
00311 for (char key : cs) {
00312 keyType(KeyStroke.getKeyStroke(key).getKeyCode());
00313 }
00314 }
00315
00318 public void waitForIdle() {
00319 commandsToRun.add(new RobotCommand(RobotMethod.WAIT_FOR_IDLE));
00320 }
00321
00322 }