/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2007-2009 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at LICENSE.html or
* http://www.sun.com/cddl.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this License Header
* Notice in each file.
*
* If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s): Alexandre (Shura) Iline. (shurymury@gmail.com)
*
* The Original Software is the Jemmy library.
* The Initial Developer of the Original Software is Alexandre Iline.
* All Rights Reserved.
*
*/
package org.jemmy.awt;
import java.awt.Component;
import java.awt.Container;
import java.awt.Window;
import java.util.ArrayList;
import java.util.List;
import org.jemmy.control.DefaultWrapper;
import org.jemmy.control.Wrap;
import org.jemmy.control.Wrapper;
import org.jemmy.env.Environment;
import org.jemmy.image.AWTImageFactory;
import org.jemmy.image.AWTRobotCapturer;
import org.jemmy.lookup.ControlHierarchy;
import org.jemmy.lookup.ParentImpl;
import org.jemmy.input.AWTRobotInputFactory;
/**
*
* @author shura
*/
public class AWT extends ParentImpl<Component> {
private static AWT AWT_ROOT = null;
private ControlHierarchy hierarchy = null;
private Wrapper awtWrapper = null;
static {
Environment.getEnvironment().setPropertyIfNotSet(Wrap.INPUT_FACTORY_PROPERTY, AWTRobotInputFactory.class.getName());
Environment.getEnvironment().setPropertyIfNotSet(Wrap.IMAGE_FACTORY_PROPERTY, AWTImageFactory.class.getName());
Environment.getEnvironment().setExecutor(new AWTExecutor());
Environment.getEnvironment().setImageCapturer(new AWTRobotCapturer());
}
/**
*
* @param env
*/
private AWT(Environment env, ControlHierarchy hierarchy, Wrapper wrapper) {
super(env, Component.class, hierarchy, wrapper);
this.hierarchy = hierarchy;
awtWrapper = wrapper;
}
public static AWT getAWT() {
if (AWT_ROOT == null) {
Environment env = new Environment(Environment.getEnvironment());
ControlHierarchy hierarchy = new AWTHierarchy();
DefaultWrapper wrapper = new DefaultWrapper(env);
wrapper.addAnnotated(AWTComponentOperator.class, FrameOperator.class,
SwingText.class, SwingLabel.class, SwingScroll.class, JCheckBoxOperator.class,
AbstractButtonOperator.class);
AWT_ROOT = new AWT(env, hierarchy, wrapper);
}
return AWT_ROOT;
}
/**
*
* @return
*/
public ControlHierarchy getHierarchy() {
return hierarchy;
}
public Wrapper getWrapper() {
return awtWrapper;
}
static class AWTHierarchy implements ControlHierarchy {
public List getChildren(Object subParent) {
ArrayList<Component> result = new ArrayList<Component>();
if (subParent instanceof Container) {
for (Component c : Container.class.cast(subParent).getComponents()) {
if (c.isShowing()) {
result.add(c);
}
}
}
if (subParent instanceof Window) {
for (Component c : Window.class.cast(subParent).getOwnedWindows()) {
if (c.isShowing()) {
result.add(c);
}
}
}
return result;
}
public List<?> getControls() {
ArrayList<Component> result = new ArrayList<Component>();
for (Component c : Window.getOwnerlessWindows()) {
if (c.isShowing()) {
result.add(c);
}
}
return result;
}
public Object getParent(Object child) {
if (child instanceof Component) {
return Component.class.cast(child).getParent();
}
return null;
}
}
}