home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-02-11 | 18.6 KB | 528 lines |
- /*
- * @version @(#)ifc.java 1.8 98/05/15
- *
- * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the confidential and proprietary information of Sun
- * Microsystems, Inc. ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Sun.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- */
-
- package locators;
- import suntest.javastar.lib.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import java.lang.reflect.*;
- import locators.utils.IFC_Info;
-
- /**
- * <ul>
- * <li>Non Component Locator for IFC toolkit
- * <li>Copyright (C) 1997 Sun Microsystem Inc.
- * <li>Netscape and IFC are registered trademarks of Netscape Corporation
- * </ul>
- * <p> The ifc class implements a nonComponent Locators solution for
- * Netscape's ifc
- * toolkit. When used in conjunction with JavaStar the locator allows
- * JavaStar tests to operate in the ifc object level. This version
- * of the locators support index, captions, and non hiearchical recording.
- * Index recording creates the tags by determining the index of a component
- * within its class. For examples a TextField is the 2nd textfield in the
- * Container based upon its creation order. A Caption of an ifc component
- * is when an object is of a certain class that contains a caption. If
- * the caption is unique within the container instead of index JavaStar
- * will use the caption. Non hiearchical recording occurs when a component
- * has a unique caption within the entire application. In this case the
- * hiearchy does not have to be recorded. When recording a test captions
- * take precedence over index. If a component has a unique caption throughout
- * the application, the non-hiearchical tag is used. When coding a script
- * manually any of these approaches may be utilized.
- *
- * @author Dan Schaffer
- * @version @(#)ifc.java 1.8 0
- */
- public class ifc implements JSNonComponentLocator {
- /** the characters used to represent index separator */
- public final static String INDEXSEP="%";
- /** the characters used to represent caption separator */
- public final static String CAPTIONSEP="^";
-
- /**
- * Returns the non-component object and a string representation that
- * is recreating by getNamedObjectData. IFC does not support .getName.
- * The possible tags are index based or caption based. If the View
- * (IFC component) has a .title() method the view title will be used
- * otherwise the index will be used.
- * @param comp The AWT based Component or frame of IFC top level frame
- * in IFC its a Foundation Panel
- * @param e The AWTEvent, extract x,y coords
- * @return JSNCLData object is (String name,Point(widgets loc),Object)
- * @see ifc#getNamedObjectData
- */
- public JSNCLData findObject(Component c, AWTEvent e){
- if((c instanceof netscape.application.FoundationPanel)==false)
- return null;
- int x = ((MouseEvent)e).getX();
- int y = ((MouseEvent)e).getY();
- netscape.application.View v = getRoot(c);
- netscape.application.View vm = v.viewForMouse(x,y);
- vm=walkUpUntilEnabled(vm);
- Vector vTree=buildTree(v);
- IFC_Info nc=findView(vTree,vm);
- if (nc==null)
- return null;
- String name=nc.theTag;
- return new JSNCLData(name,locate(vm),vm);
- }
-
-
- /**
- * Returns the non-component object after getting the actual View from
- * the string description passed in. Reconstructs the view from the
- * text parameter.
- *
- * @param c The AWT based Component in IFC its a FoundationPanel
- * @param name The name of the object
- * @return JSNCLData object is (String name,Point(widgets loc),Object)
- * @see ifc#findObject
- */
- public JSNCLData getNamedObjectData(Component c, String name){
- if((c instanceof netscape.application.FoundationPanel)==false)
- return null;
- netscape.application.View v=getRoot(c);
-
- // build entire tree
- Vector vTree=buildTree(v);
- // look in tree for the entry with name
- IFC_Info nc=findTag(vTree,name);
- if (isEnabled(nc.view)==false || isVisible(nc.view)==false)
- return null;
- // get the actual coordinates, return actual ifc component
- return new JSNCLData(name,locate(nc.view),nc.view);
- }
-
- /* Moves up the hiearchy until a component is enabled.
- * @param v the object
- * @returns View the first enabled object which could be and typically is
- * the parameter v.
- */
- public static netscape.application.View walkUpUntilEnabled(netscape.application.View v) {
- boolean enabled=false;
- while(enabled==false) {
- enabled=isEnabled(v);
- if (enabled==false) {
- v=v.superview();
- if (v==null)
- return null;
- }
- }
- return v;
- }
- /**
- * determines whether a component is visible. Reflection proved to be
- * too slow. The method checks to see the parameter is an instance of
- * one the classes that defines isVisible. Returns true if class is
- * visible or if the class does not define visible.
- * @param v the ifc view to check
- * @returns boolean whether the object is visible or not
- */
- public static boolean isVisible(netscape.application.View v) {
- boolean visible=true;
- while (v!=null) {
- if (v instanceof netscape.application.InternalWindow)
- visible=((netscape.application.InternalWindow)v).isVisible();
- else {
- if (v instanceof netscape.application.RootView)
- visible=((netscape.application.RootView)v).isVisible();
- else {
- if (v instanceof netscape.application.MenuView)
- visible=((netscape.application.MenuView)v).isVisible();
- }
- }
- if (visible==false)
- return false;
- v=v.superview();
- }
- return visible;
- }
- /**
- * determines whether a component is enabled. Reflection proved to be
- * too slow. The method checks to see the parameter is an instance of
- * one the classes that defines isEnabled. Returns true if class is
- * enabled or if the class does not define isEnabled.
- * @param v the ifc view to check
- * @returns boolean whether the object is enabled or not
- */
- public static boolean isEnabled(netscape.application.View v) {
- boolean enabled=true;
- if (v instanceof netscape.application.Button)
- enabled=((netscape.application.Button)v).isEnabled();
- else {
- if (v instanceof netscape.application.DragWell)
- enabled=((netscape.application.DragWell)v).isEnabled();
- else {
- if (v instanceof netscape.application.ListView)
- enabled=((netscape.application.ListView)v).isEnabled();
- else {
- if (v instanceof netscape.application.ScrollBar)
- enabled=((netscape.application.ScrollBar)v).isEnabled();
- else {
- if (v instanceof netscape.application.Slider)
- enabled=((netscape.application.Slider)v).isEnabled();
- }
- }
- }
- }
- return enabled;
- }
- /**
- * gets the class name of the object.
- * @param o the object
- * @returns the class name as a string
- */
- public static String getClassName(Object o){
- String cl = o.getClass().getName();
- return cl;
- }
- /**
- * gets the class name of the object in a short version. The package
- * name is removed. All text after the "." is returned
- * @param o the object
- * @returns the class name as a string
- */
- public static String getClassNameShort(Object o) {
- String cl = o.getClass().getName();
- return cl.substring(1+cl.lastIndexOf('.'));
- }
-
- /**
- * find the x,y location of the view relative to the Foundation Panel
- * get the coordinates of ancestry to determine absolute location
- * The methods walks up the hiearchy added each components relative
- * locations to calculate the global coordinate within the top level
- * ifc component.
- * @param rv the view to start with
- * @returns the Point
- */
- public static Point locate(netscape.application.View rv){
- Point p=new Point(0,0);
- while (rv!=null) {
- p=new Point(p.x+rv.x(),p.y+rv.y());
- rv=rv.superview();
- }
- return p;
- }
- /**
- * Searches the widget for a child widget matching the class. Returns
- * the widget when find the child widget with the correct index
- * @param w the ifc component
- * @param cname the matching class name
- * @param index the index of the classname to find
- * @returns View the object matching the criteria
- */
- public static netscape.application.View findIndexView(netscape.application.View w,String cname,int index) {
- int iNum=0;
- netscape.util.Vector v=w.subviews();
- for (int i=0;i<v.size();i++){
- if (getClassNameShort(v.elementAt(i)).equals(cname))
- iNum++;
- if (iNum>index)
- return (netscape.application.View)v.elementAt(i);
- }
- return null;
- }
- /**
- * Creates a string representation of the widget. For each level in
- * the widget hiearchy get the index of the component and the class.
- * for each level create <classname>%<index>. The "." separates each
- * widget.
- * @param w the View of the object
- * @returns String the string representation of the tag
- */
- public static String buildTag(netscape.application.View w) {
- int index;
- String sRet="",s;
- // starting at top get the widget class,index append to tag and walk
- // down to top level where w.parent is null
- // if can't find a component is the parent list return null
- while (w!=null) {
- s=getClassNameShort(w);
- if (w.superview()!=null) {
- index=getViewIndex(w);
- if (index!= -1)
- s+=INDEXSEP+index;
- }
- if (sRet=="")
- sRet=s;
- else
- sRet=s+"."+sRet;
- w=w.superview();
- }
- return sRet;
- }
- /**
- * gets the index of a component by counting the number of object with
- * the same class name that the parent contains. The index count of
- * the number of objects created by the current component of the same
- * class +1.
- * @param w the object
- * @returns int the index of the object
- */
- public static int getViewIndex(netscape.application.View w) {
- netscape.application.View container=w.superview();
- int index=0;
- if (container==null)
- return -1;
- netscape.util.Vector v=container.subviews();
- for (int i=0;i<v.size();i++) {
- if (v.elementAt(i)==w)
- return index;
- if (v.elementAt(i).getClass()==w.getClass())
- index++;
- }
- return -1;
- }
-
- /** Checks to see if the view has a title method. Reflection
- * proved to be too slow. The method checks if the object is
- * an instance of a class that defines the title() method. If
- * not the method returns null otherwise returns the title
- * or caption of the view.
- * @param w the object
- * @returns String the caption of null if one is not available
- */
- public static String getViewCaption(netscape.application.View w) {
- String sRet=null;
- String theClass;
- if (w instanceof netscape.application.Button)
- sRet=((netscape.application.Button)w).title();
- else {
- if (w instanceof netscape.application.Label)
- sRet=((netscape.application.Label)w).title();
- else {
- if (w instanceof netscape.application.ContainerView)
- sRet=((netscape.application.ContainerView)w).title();
- else {
- if (w instanceof netscape.application.InternalWindow)
- sRet=((netscape.application.InternalWindow)w).title();
- }
- }
- }
- if (sRet==null || sRet=="")
- return null;
- return sRet;
- }
- /**
- * returns the top level ifc component based upon a FoundationPanel.
- * The parameter is a Component that is casted into a FoundationPanel.
- * @param c the top level Component (FoundationPanel)
- * @returns the top level view
- */
- public static netscape.application.View getRoot(Component c) {
- netscape.application.FoundationPanel fp=(netscape.application.FoundationPanel)c;
- return (netscape.application.View)fp.rootView();
- }
- /**
- * returns the top level ifc component based upon a view
- * @param w one of the children
- * @returns the top level view
- */
- public static netscape.application.View getRoot(netscape.application.View w) {
- netscape.application.View wlast=w;
- if (w==null)
- return null;
- return w.rootView();
- }
- /** searches through the widget tree for widget with the tag (String)
- * returns the IFC_Info datatype
- * @param v the widget tree
- * @param s the search tag
- * @returns IFC_Info the data structure containing the desired component
- * information
- */
- public static IFC_Info findTag(Vector v,String s) {
- IFC_Info wi;
- if (s.startsWith("root")) {
- if (s.indexOf(".")== -1)
- return null;
- s="RootView"+s.substring(s.indexOf("."));
- }
- for (int i=0;i<v.size();i++) {
- wi=(IFC_Info)v.elementAt(i);
- if (wi.theTag.equals(s) || (wi.parent+"."+wi.className+INDEXSEP+wi.index).equals(s) || (wi.caption!=null && (wi.parent+"."+wi.className+CAPTIONSEP+wi.caption+CAPTIONSEP).equals(s))) {
- return wi;
- }
- }
- return null;
- }
- /**
- * searches through widget tree for a view
- * @param v the widget tree
- * @param w the view to search for
- * @returns IFC_Info the data structure containing the desired component
- * information
- */
- public static IFC_Info findView(Vector v,netscape.application.View w) {
- IFC_Info wi;
- for (int i=0;i<v.size();i++) {
- wi=(IFC_Info)v.elementAt(i);
- if (wi.view==w)
- return wi;
- }
- return null;
- }
- /**
- * creates a Vector of the entire component hierarchy. Each ifc view
- * is added to the Vector. Inside the data structure contains all the
- * information necessary to calculate whether the component is unique
- */
- public static Vector buildTree(netscape.application.View vRoot) {
- int index,i;
- Hashtable vIntName=new Hashtable();
- Hashtable vCaption=new Hashtable();
- Vector vRet=new Vector();
- Vector vCon=new Vector();
- IFC_Info w,wi;
- Hashtable classTable=new Hashtable();
- vCon.addElement(new IFC_Info(vRoot,null));
- while (vCon.isEmpty()==false) {
- w=(IFC_Info)vCon.firstElement();
- vCon.removeElementAt(0);
-
- w.className=getClassNameShort(w.view);
- w.index=getViewIndex(w.view);
- w.caption=getViewCaption(w.view);
- if (w.caption!=null) {
- w.localTagName=w.className+CAPTIONSEP+w.caption+CAPTIONSEP;
- w.fullTagName=w.localTagName;
- if (w.parent!=null) {
- w.fullTagName=w.parent+"."+w.fullTagName;
- }
- }
- else {
- w.localTagName=null;
- if (w.index != -1) {
- w.fullTagName=w.className+INDEXSEP+w.index;
- } else {
- w.fullTagName=w.className;
- }
- if (w.parent!=null) {
- w.fullTagName=w.parent+"."+w.fullTagName;
- }
- }
- if (w.caption!=null) {
- if (vCaption.get(w.localTagName)!=null) {
- i=((Integer)vCaption.get(w.localTagName)).intValue();
- wi=(IFC_Info)vRet.elementAt(i);
- wi.localTagName=null;
- wi.setTheTag();
- vRet.setElementAt(wi,i);
- w.localTagName=null;
- } else {
- vCaption.put(w.localTagName,new Integer(vRet.size()));
- vCaption.put(w.fullTagName,new Integer(vRet.size()));
- }
- if (w.localTagName==null) {
- if (vCaption.get(w.fullTagName)!=null) {
- i=((Integer)vCaption.get(w.fullTagName)).intValue();
- wi=(IFC_Info)vRet.elementAt(i);
- wi.fullTagName=wi.className+INDEXSEP+wi.index;
- if (wi.parent!=null) {
- wi.fullTagName=wi.parent+"."+wi.fullTagName;
- }
- wi.setTheTag();
- vRet.setElementAt(wi,i);
- w.fullTagName=w.className+INDEXSEP+w.index;
- if (w.parent!=null) {
- w.fullTagName=w.parent+"."+w.fullTagName;
- }
- }
- }
- }
- w.setTheTag();
- vRet.addElement(w);
- netscape.util.Vector vList=w.view.subviews();
- for (i=0;i<vList.size();i++)
- vCon.addElement(new IFC_Info((netscape.application.View)vList.elementAt(i),w.fullTagName));
- }
- return vRet;
- }
- /**
- * removes a marker such as '%' '^' from beginning and end of string
- * also if the marker exists in the string unescapes it
- * @param tag the source string
- * @param marker the string to remove
- * @returns String the source with all markers removed
- */
- public static String removeMarkers(String tag,String marker) {
- if (tag.startsWith(marker))
- tag=tag.substring(1);
- if (tag.endsWith(marker))
- tag=tag.substring(0,tag.length()-1);
- tag=replaceString(tag,marker+marker,marker);
- return tag;
- }
- /** adds a marker such as '%' '^' to beginning and end of string
- * also if the marker exists in the string makes it "%%" to escape
- * the marker
- * @param tag the source string
- * @marker the marker to add
- * @returns String the source string with markers added on either end and
- * all occurrences of the marker is replaced with two markers to escape
- * the string
- */
- public static String addMarkers(String tag,String marker) {
- tag=replaceString(tag,marker,marker+marker);
- tag=marker+tag+marker;
- return tag;
- }
- /**
- * replaces all occurrences of replace with replaceWith
- * @param s the source string
- * @param replace the string to replace
- * @param replaceWith the string to replace with
- * @returns String the source string with all occurrences of string replaced
- */
- public static String replaceString(String s,String replace,String replaceWith) {
- int index=0;
- for (index=s.indexOf(replace,index);index!= -1;) {
- s=s.substring(0,index)+replaceWith+s.substring(index+replace.length());
- index=index-replace.length()+replaceWith.length()+1;
- index=s.indexOf(replace,index);
- }
- return s;
- }
- /** Used for Debugging purpose. Outputs to stdout the entire tree.
- * this is really going to hurt performance but is useful for debugging
- * purpose.
- * @param vWid the widget tree to print out
- */
- public static void printTree(netscape.application.FoundationPanel fp) {
- Vector vWid=buildTree(getRoot(fp));
- printTree(vWid);
- }
- /** Used for Debugging purpose. Outputs to stdout the entire tree.
- * this is really going to hurt performance but is useful for debugging
- * purpose.
- * @param vWid the widget tree to print out
- */
- public static void printTree(Vector vWid) {
- IFC_Info wi;
- for (int i=0;i<vWid.size();i++) {
- wi=(IFC_Info)vWid.elementAt(i);
- System.out.println(wi);
- }
- }
-
- }
-