001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Pavel Dolgov, Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import java.awt.event.InputEvent;
023: import java.awt.image.BufferedImage;
024: import java.lang.reflect.InvocationTargetException;
025:
026: import org.apache.harmony.awt.internal.nls.Messages;
027: import org.apache.harmony.awt.wtk.NativeRobot;
028:
029: public class Robot {
030:
031: private int autoDelay;
032: private boolean autoWaitForIdle;
033:
034: private final NativeRobot nativeRobot;
035:
036: public Robot() throws AWTException {
037: this (GraphicsEnvironment.getLocalGraphicsEnvironment()
038: .getDefaultScreenDevice());
039: }
040:
041: public Robot(GraphicsDevice screen) throws AWTException {
042: Toolkit.checkHeadless();
043: if ((screen == null)
044: || (screen.getType() != GraphicsDevice.TYPE_RASTER_SCREEN)) {
045: // awt.129=Not a screen device
046: throw new IllegalArgumentException(Messages
047: .getString("awt.129")); //$NON-NLS-1$
048: }
049: SecurityManager sm = System.getSecurityManager();
050: if (sm != null) {
051: sm.checkPermission(new AWTPermission("createRobot")); //$NON-NLS-1$
052: }
053: // create(or get) native robot instance
054: // for the specified screen
055: Toolkit tk = Toolkit.getDefaultToolkit();
056: nativeRobot = tk.getWTK().getNativeRobot(screen);
057: }
058:
059: @Override
060: public String toString() {
061: /* The format is based on 1.5 release behavior
062: * which can be revealed by the following code:
063: * System.out.println(new Robot());
064: */
065:
066: return getClass().getName() + "[" + "autoDelay = " + autoDelay + //$NON-NLS-1$ //$NON-NLS-2$
067: ", autoWaitForIdle = " + autoWaitForIdle + "]"; //$NON-NLS-1$ //$NON-NLS-2$
068: }
069:
070: public BufferedImage createScreenCapture(Rectangle screenRect) {
071: SecurityManager sm = System.getSecurityManager();
072: if (sm != null) {
073: sm.checkPermission(new AWTPermission("readDisplayPixels")); //$NON-NLS-1$
074: }
075: if (screenRect.isEmpty()) {
076: // awt.13D=Rectangle width and height must be > 0
077: throw new IllegalArgumentException(Messages
078: .getString("awt.13D")); //$NON-NLS-1$
079: }
080:
081: return nativeRobot.createScreenCapture(screenRect);
082: }
083:
084: public void delay(int ms) {
085: checkDelay(ms);
086: try {
087: Thread.sleep(ms);
088: } catch (InterruptedException e) {
089: }
090: }
091:
092: public int getAutoDelay() {
093: return autoDelay;
094: }
095:
096: public Color getPixelColor(int x, int y) {
097: return nativeRobot.getPixel(x, y);
098: }
099:
100: public boolean isAutoWaitForIdle() {
101: return autoWaitForIdle;
102: }
103:
104: public void keyPress(int keycode) {
105: nativeRobot.keyEvent(keycode, true);
106: doWait();
107: }
108:
109: public void keyRelease(int keycode) {
110: nativeRobot.keyEvent(keycode, false);
111: doWait();
112: }
113:
114: public void mouseMove(int x, int y) {
115: nativeRobot.mouseMove(x, y);
116: doWait();
117: }
118:
119: public void mousePress(int buttons) {
120: checkButtons(buttons);
121: nativeRobot.mouseButton(buttons, true);
122: doWait();
123: }
124:
125: public void mouseRelease(int buttons) {
126: checkButtons(buttons);
127: nativeRobot.mouseButton(buttons, false);
128: doWait();
129: }
130:
131: public void mouseWheel(int wheelAmt) {
132: nativeRobot.mouseWheel(wheelAmt);
133: doWait();
134: }
135:
136: public void setAutoDelay(int ms) {
137: checkDelay(ms);
138: autoDelay = ms;
139: }
140:
141: public void setAutoWaitForIdle(boolean isOn) {
142: autoWaitForIdle = isOn;
143: }
144:
145: public void waitForIdle() {
146: if (EventQueue.isDispatchThread()) {
147: // awt.13E=Cannot call method from the event dispatcher thread
148: throw new IllegalThreadStateException(Messages
149: .getString("awt.13E")); //$NON-NLS-1$
150: }
151: try {
152: EventQueue.invokeAndWait(new Runnable() {
153:
154: public void run() {
155: // just do nothing
156: }
157:
158: });
159: } catch (InterruptedException e) {
160: e.printStackTrace();
161: } catch (InvocationTargetException e) {
162: e.printStackTrace();
163: }
164: }
165:
166: private void checkDelay(int ms) {
167: if ((ms < 0) || (ms > 60000)) {
168: // awt.13F=Delay must be to 0 to 60,000ms
169: throw new IllegalArgumentException(Messages
170: .getString("awt.13F")); //$NON-NLS-1$
171: }
172: }
173:
174: private void checkButtons(int buttons) {
175: int mask = (InputEvent.BUTTON1_MASK | InputEvent.BUTTON2_MASK | InputEvent.BUTTON3_MASK);
176: if ((buttons & mask) != buttons) {
177: // awt.140=Invalid combination of button flags
178: throw new IllegalArgumentException(Messages
179: .getString("awt.140")); //$NON-NLS-1$
180: }
181: }
182:
183: private void doWait() {
184: // first wait for idle if necessary:
185: if (isAutoWaitForIdle()) {
186: waitForIdle();
187: }
188: // now sleep if autoDelay is > 0
189: int delay = getAutoDelay();
190: if (delay > 0) {
191: delay(delay);
192: }
193: }
194: }
|