home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-02-11 | 9.6 KB | 277 lines |
- /*
- * @version @(#)ifcSimple.java 1.4 98/05/15
- *
- * Copyright (c) 1997-1998 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.AWTEvent;
- import java.awt.event.*;
- import java.awt.Component;
- import java.awt.Point;
-
- /**
- * <ul>
- * <li>Non Component Locator for IFC toolkit simple version
- * <li>Copyright (C) 1997 Sun Microsystem Inc.
- * <li>Netscape and IFC are registered trademarks of Netscape Corporation
- * </ul>
- * <p> The ifcSimple 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 locator is a smaller and faster version of the locator but
- * supports only full tags with hierarchy and indexes.
- *
- * @author Dan Schaffer
- * @version @(#)ifcSimple.java 1.4 0
- */
- public class ifcSimple implements JSNonComponentLocator {
-
- /**
- * Based upon the top level ifc component that is a FoundationPanel
- * and the event that occurred, the method creates a string tag that
- * represents the ifc component. During recording of a test javastar
- * calls this method when the ifc locator is selected. The location
- * of the ifc component is returned so javastar can translate the
- * offset based upon the ifc component instead of the global coordinates.
- * the ifc component is also returned as part of the JSNCLData data
- * structure.
- * @param c the Component where the event occurred. If it is not a
- * FoundationPanel null is returned.
- * @param e the event that occurred. from the event the x,y location
- * of the event is used.
- * @return JSNCLData the nonComponent datatype containing a string
- * tag for the component, the x,y location within of ifc component,
- * and the actual ifc component.
- * @see ifcSimple#getNamedObjectData
- */
- public JSNCLData findObject(Component c, AWTEvent e){
- if(c instanceof netscape.application.FoundationPanel){
- int x = ((MouseEvent)e).getX();
- int y = ((MouseEvent)e).getY();
- netscape.application.RootView rv = ((netscape.application.FoundationPanel)c).rootView();
- netscape.application.View vm = rv.viewForMouse(x,y);
- vm=walkUpUntilEnabled(vm);
- String name = computePath(vm,rv);
- return new JSNCLData(name,locate(vm),vm);
- }
- return null;
- }
-
- /**
- * Based upon the top level ifc component which is a FoundationPanel
- * the method converts the tag into the actual ifc component. If the
- * component is not found of disabled null is returned and javastar will
- * throw a GUINotFoundException. The ifc component, its location
- * within the FoundationPanel, and the tag are returned in the JSNCLData
- * data structure. This method is called by javastar during the playback
- * of a script that calls the nonComponent api.
- * @param c the top level ifc component, it is a FoundationPanel
- * @param wname the tag name of the ifc object
- * @returns JSNCLData the tag, the x,y location of the object, and the
- * ifc object are returned in this data structure. If the object
- * is currently not present, not visible, or not enabled null is
- * returned and javastar will throw a GUINotFoundException if the
- * timeout occurs.
- */
- public JSNCLData getNamedObjectData(Component c, String wname){
- if(c instanceof netscape.application.FoundationPanel){
- netscape.application.RootView rv = ((netscape.application.FoundationPanel)c).rootView();
- int x = rv.x();
- int y = rv.y();
- netscape.application.View cur = rv;
- if(!wname.startsWith("RootView") && !wname.startsWith("root")){
- return null;
- }
- String s = wname;
- int i = s.indexOf('.');
- while(i!=-1){
- s = s.substring(1+i);
- int j = s.indexOf('.');
- String sub = s;
- if(j!=-1){
- sub = s.substring(0,j);
- }
- cur = step(cur,sub);
- if(cur==null)return null;
- x += cur.x();
- y += cur.y();
- i = j;
- }
- if (isEnabled(cur)==false)
- return null;
- return new JSNCLData(wname,new Point(x,y),cur);
- }
- return null;
- }
-
- /**
- * From the parent (des) to a child (anc), the method constructs a string
- * representation of the hierarchy. At each level the index of the class of
- * the component is used with the name of the class. Each level of hiearchy
- * is separated with a ".". The entire string is returned.
- * @param des the parent ifc View
- * @param anc the child ifc View
- * @returns String the representation of the object as a string
- */
- public static String computePath(netscape.application.View des, netscape.application.View anc){
- netscape.application.View par;
- netscape.util.Vector v;
- Object o=null;
- String osc,sc,sRet="";
- int ct;
- while (des!=anc) {
- sc=simpClass(des);
- par=des.superview();
- v=par.subviews();
- ct=0;
- osc="";
- for (int i=0;i<v.size();i++) {
- o=v.elementAt(i);
- osc=simpClass(o);
- if (o==des)
- break;
- if (osc.equals(sc))
- ct++;
- }
- if (o!=des)
- return null;
- if (sRet.equals(""))
- sRet=osc+"%"+ct;
- else
- sRet=osc+"%"+ct+"."+sRet;
- des=par;
- }
- if (des==anc) {
- if (sRet.equals(""))
- sRet="RootView";
- else
- sRet="RootView"+"."+sRet;
- }
- return sRet;
- }
- /**
- * Given a tag and a top level View, the method walks through the component
- * hiearchy and finds the View represented by the string tag. If at any
- * level the component is not found null is returned.
- * @param p the top level view
- * @param s the tag
- * @returns View the object represented by the tag or null if it cannot be
- * found.
- */
- public static netscape.application.View step(netscape.application.View p, String s){
- int tot = 0;
- if (s.indexOf("%")!= -1) {
- tot=new Integer(s.substring(s.lastIndexOf("%")+1)).intValue();
- }
- int ct = 0;
- String cn = s.substring(0,s.lastIndexOf("%"));
- netscape.util.Vector v = p.subviews();
- for(int k=0;k<v.size();k++){
- Object o = v.elementAt(k);
- if(o instanceof netscape.application.View){
- String osc = simpClass(o);
- if(osc.equals(cn)){
- if(ct==tot)
- return ((netscape.application.View)o);
- ct++;
- }
- }
- }
- return null;
- }
- /**
- * creates the simple class name. All package names are removed.
- * @param o the ifc object
- * @returns String representation of the class name
- */
- public static String simpClass(Object o){
- String cl = o.getClass().getName();
- return cl.substring(1+cl.lastIndexOf('.'));
- }
-
- /**
- * calcuates the location of a view. The method walks up the component
- * hierarchy calculating the location at each component and returns
- * the global coordinates of the view with respect to its top level
- * ifc component.
- * @param rv the object
- * @returns Point the global location of the object relative to its top
- * level ifc component
- */
- 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;
- }
-
- /**
- * isEnabled calculates whether the ifc component is enabled or not.
- * Only certain ifc classes implement the isEnabled method. Reflection
- * proved to be too slow so the method checks if the object is an
- * instance of any of the defined classes. Returns true if isEnabled
- * returns true or if the class does not defined isEnabled. Returns
- * false in the class has an isEnabled method and its returns false.
- * @param v the view
- * @returns boolean whether the object is enabled of 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;
- }
- /** 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;
- }
-
- }
-