001: package com.xoetrope.carousel.testpilot;
002:
003: import java.util.Collections;
004: import java.util.HashMap;
005: import java.util.Map;
006: import java.util.StringTokenizer;
007:
008: import java.awt.AWTException;
009: import java.awt.Component;
010: import java.awt.Container;
011: import java.awt.Frame;
012: import java.awt.GraphicsDevice;
013: import java.awt.Robot;
014: import java.awt.Window;
015: import javax.swing.SwingUtilities;
016:
017: import net.xoetrope.swing.XButton;
018: import net.xoetrope.xui.XPage;
019: import java.awt.GraphicsConfiguration;
020: import java.util.Hashtable;
021:
022: /**
023: * Support for common scripting operations
024: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
025: * the GNU Public License (GPL), please see license.txt for more details. If
026: * you make commercial use of this software you must purchase a commercial
027: * license from Xoetrope.</p>
028: * <p> $Revision: 1.2 $</p>
029: */
030: public class ScriptPage extends XPage {
031: protected Object mainClassObject;
032: protected XButton btnStart, btnBack;
033: private static Hashtable baseNames;
034:
035: /** Hashmap of GraphicDevice's to Robot's. **/
036: protected static Map robots = Collections
037: .synchronizedMap(new HashMap());
038:
039: public ScriptPage() {
040: if (baseNames == null)
041: baseNames = new Hashtable();
042: }
043:
044: /**
045: * Set the instance of the class whose events are being recorded.
046: * @param obj
047: */
048: public void setMainClassObject(Object obj) {
049: if (mainClassObject != obj)
050: btnStart.setEnabled(obj != null);
051: mainClassObject = obj;
052: }
053:
054: /**
055: * Get a reference to a component for recording in the script
056: * @param srcComp the component object
057: * @return the name/reference
058: */
059: public String getComponentReference(Component srcComp) {
060: String name = "";
061: while (!(srcComp instanceof Frame)) {
062: String compName = srcComp.getName();
063: Container parent = srcComp.getParent();
064: if ((compName != null) && (compName.length() > 0)
065: && !usesConstructedName(srcComp, compName)) {
066: name = "[" + compName + "]" + name;
067: } else {
068: Component[] children = parent.getComponents();
069: for (int i = 0; i < children.length; i++) {
070: if (children[i] == srcComp) {
071: name = "[" + Integer.toString(i) + "]" + name;
072: break;
073: }
074: }
075: }
076: srcComp = parent;
077: }
078:
079: return name;
080: }
081:
082: /**
083: * Get the referenced component
084: * @param compName the component name
085: * @return the component object
086: */
087: public Component getReferenceComponent(String compName) {
088: Component comp;
089: Component parent = (Component) mainClassObject;
090: if ((compName == null) || (compName.length() == 0))
091: return parent;
092:
093: XPage page;
094: StringTokenizer st = new StringTokenizer(compName, "]");
095: while (st.hasMoreTokens()) {
096: String token = st.nextToken();
097: token = token.substring(1);
098: int compIdx = -1;
099: try {
100: compIdx = Integer.parseInt(token);
101: } catch (NumberFormatException ex) {
102: }
103: if (compIdx >= 0)
104: parent = ((Container) parent).getComponent(compIdx);
105: else {
106: // Try looking the component up by name
107: int numComponents = ((Container) parent)
108: .getComponentCount();
109: for (int i = 0; i < numComponents; i++) {
110: if (((Container) parent).getComponent(i).getName()
111: .equals(token)) {
112: parent = ((Container) parent).getComponent(i);
113: break;
114: }
115: }
116: }
117: }
118: return parent;
119: }
120:
121: private boolean usesConstructedName(Component srcComp,
122: String compName) {
123: try {
124: String baseName = (String) baseNames.get(srcComp.getClass()
125: .getName());
126: int len = 0;
127: if (baseName == null) {
128: Component newComp = (Component) srcComp.getClass()
129: .newInstance();
130: baseName = newComp.getName();
131: if (baseName == null)
132: return false;
133: while (Character.isDigit(baseName
134: .charAt(len = (baseName.length() - 1)))) {
135: baseName = baseName.substring(0, len);
136: }
137: baseNames.put(srcComp.getClass().getName(), baseName);
138: }
139: while (Character.isDigit(compName.charAt(len = (compName
140: .length() - 1)))) {
141: compName = compName.substring(0, len);
142:
143: }
144: return compName.equals(baseName);
145: } catch (InstantiationException ex) {
146: return true;
147: } catch (IllegalAccessException ex) {
148: return true;
149: }
150: }
151:
152: /** Return a Robot for the given Component. Makes sure it's aimed
153: * at the correct screen. **/
154: public RobotWrapper getRobot(Component c)
155: throws IllegalStateException, AWTException {
156: Window w;
157: if (c instanceof Window)
158: w = (Window) c;
159: else
160: w = SwingUtilities.windowForComponent(c);
161: if (w == null)
162: throw new IllegalStateException(
163: "Window not found for Component: " + c);
164:
165: GraphicsConfiguration gc = w.getGraphicsConfiguration();
166: GraphicsDevice gd = gc.getDevice();
167: if (!robots.containsKey(gd)) {
168: Robot r = new Robot(gd);
169: r.setAutoDelay(0);
170: r.setAutoWaitForIdle(true);
171: robots.put(gd, new RobotWrapper(r, gc.getBounds().x));
172: }
173:
174: return (RobotWrapper) robots.get(gd);
175: }
176: }
|