home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 25 / IOPROG_25.ISO / SOFT / JavaS / javastar-eval.exe / data1.cab / Program_Files / contrib / locators / ifcSimple.java < prev   
Encoding:
Java Source  |  1999-02-11  |  9.6 KB  |  277 lines

  1. /*
  2.  *  @version             @(#)ifcSimple.java    1.4 98/05/15
  3.  *
  4.  * Copyright (c) 1997-1998 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package locators;
  21. import suntest.javastar.lib.*;
  22. import java.awt.AWTEvent;
  23. import java.awt.event.*;
  24. import java.awt.Component;
  25. import java.awt.Point;
  26.  
  27. /**
  28. * <ul>
  29. * <li>Non Component Locator for IFC toolkit simple version
  30. * <li>Copyright (C) 1997 Sun Microsystem Inc.
  31. * <li>Netscape and IFC are registered trademarks of Netscape Corporation
  32. * </ul>
  33. * <p> The ifcSimple class implements a nonComponent Locators solution for
  34. * Netscape's ifc
  35. * toolkit.  When used in conjunction with JavaStar the locator allows
  36. * JavaStar tests to operate in the ifc object level.  This version
  37. * of the locator is a smaller and faster version of the locator but
  38. * supports only full tags with hierarchy and indexes.
  39. *
  40. * @author     Dan Schaffer
  41. * @version    @(#)ifcSimple.java    1.4 0
  42. */
  43. public class ifcSimple implements JSNonComponentLocator {
  44.  
  45.   /**
  46.    * Based upon the top level ifc component that is a FoundationPanel
  47.    * and the event that occurred, the method creates a string tag that
  48.    * represents the ifc component.  During recording of a test javastar
  49.    * calls this method when the ifc locator is selected.  The location
  50.    * of the ifc component is returned so javastar can translate the
  51.    * offset based upon the ifc component instead of the global coordinates.
  52.    * the ifc component is also returned as part of the JSNCLData data
  53.    * structure.
  54.    * @param c the Component where the event occurred.  If it is not a
  55.    *    FoundationPanel null is returned.
  56.    * @param e the event that occurred.  from the event the x,y location
  57.    *    of the event is used.
  58.    * @return JSNCLData the nonComponent datatype containing a string
  59.    *    tag for the component, the x,y location within of ifc component,
  60.    *    and the actual ifc component.
  61.    * @see ifcSimple#getNamedObjectData
  62.    */
  63.   public JSNCLData findObject(Component c, AWTEvent e){
  64.     if(c instanceof netscape.application.FoundationPanel){
  65.       int x = ((MouseEvent)e).getX();
  66.       int y = ((MouseEvent)e).getY();
  67.       netscape.application.RootView rv = ((netscape.application.FoundationPanel)c).rootView();
  68.       netscape.application.View vm = rv.viewForMouse(x,y);
  69.       vm=walkUpUntilEnabled(vm);
  70.       String name = computePath(vm,rv);
  71.       return new JSNCLData(name,locate(vm),vm);
  72.     }
  73.     return null;
  74.   }
  75.  
  76.   /**
  77.    * Based upon the top level ifc component which is a FoundationPanel
  78.    * the method converts the tag into the actual ifc component.  If the
  79.    * component is not found of disabled null is returned and javastar will
  80.    * throw a GUINotFoundException.  The ifc component, its location
  81.    * within the FoundationPanel, and the tag are returned in the JSNCLData
  82.    * data structure.  This method is called by javastar during the playback
  83.    * of a script that calls the nonComponent api.
  84.    * @param c the top level ifc component, it is a FoundationPanel
  85.    * @param wname the tag name of the ifc object
  86.    * @returns JSNCLData the tag, the x,y location of the object, and the
  87.    *    ifc object are returned in this data structure.  If the object
  88.    *    is currently not present, not visible, or not enabled null is
  89.    *    returned and javastar will throw a GUINotFoundException if the
  90.    *    timeout occurs.
  91.    */
  92.   public JSNCLData getNamedObjectData(Component c, String wname){
  93.     if(c instanceof netscape.application.FoundationPanel){
  94.       netscape.application.RootView rv = ((netscape.application.FoundationPanel)c).rootView();
  95.       int x = rv.x();
  96.       int y = rv.y();
  97.       netscape.application.View cur = rv;
  98.       if(!wname.startsWith("RootView") && !wname.startsWith("root")){
  99.     return null;
  100.       }
  101.       String s = wname;
  102.       int i = s.indexOf('.');
  103.       while(i!=-1){
  104.     s = s.substring(1+i);
  105.     int j = s.indexOf('.');
  106.     String sub = s;
  107.     if(j!=-1){
  108.       sub = s.substring(0,j);
  109.     }
  110.     cur = step(cur,sub);
  111.     if(cur==null)return null;
  112.     x += cur.x();
  113.     y += cur.y();
  114.     i = j;
  115.       }
  116.       if (isEnabled(cur)==false)
  117.     return null;
  118.       return new JSNCLData(wname,new Point(x,y),cur);
  119.     }
  120.     return null;
  121.   }
  122.  
  123.   /**
  124.    * From the parent (des) to a child (anc), the method constructs a string
  125.    * representation of the hierarchy.  At each level the index of the class of
  126.    * the component is used with the name of the class.  Each level of hiearchy
  127.    * is separated with a ".".  The entire string is returned.
  128.    * @param des the parent ifc View
  129.    * @param anc the child ifc View
  130.    * @returns String the representation of the object as a string
  131.    */
  132.   public static String computePath(netscape.application.View des, netscape.application.View anc){
  133.     netscape.application.View par;
  134.     netscape.util.Vector v;
  135.     Object o=null;
  136.     String osc,sc,sRet="";
  137.     int ct;
  138.     while (des!=anc) {
  139.       sc=simpClass(des);
  140.       par=des.superview();
  141.       v=par.subviews();
  142.       ct=0;
  143.       osc="";
  144.       for (int i=0;i<v.size();i++) {
  145.     o=v.elementAt(i);
  146.     osc=simpClass(o);
  147.     if (o==des)
  148.       break;
  149.     if (osc.equals(sc))
  150.       ct++;
  151.       }
  152.       if (o!=des)
  153.     return null;
  154.       if (sRet.equals(""))
  155.     sRet=osc+"%"+ct;
  156.       else
  157.     sRet=osc+"%"+ct+"."+sRet;
  158.       des=par;
  159.     }
  160.     if (des==anc) {
  161.       if (sRet.equals(""))
  162.     sRet="RootView";
  163.       else
  164.     sRet="RootView"+"."+sRet;
  165.     }
  166.     return sRet;
  167.   }
  168.   /**
  169.    * Given a tag and a top level View, the method walks through the component
  170.    * hiearchy and finds the View represented by the string tag.  If at any
  171.    * level the component is not found null is returned.
  172.    * @param p the top level view
  173.    * @param s the tag
  174.    * @returns View the object represented by the tag or null if it cannot be
  175.    *     found.
  176.    */
  177.   public static netscape.application.View step(netscape.application.View p, String s){
  178.     int tot = 0;
  179.     if (s.indexOf("%")!= -1) {
  180.       tot=new Integer(s.substring(s.lastIndexOf("%")+1)).intValue();
  181.     }
  182.     int ct = 0;
  183.     String cn = s.substring(0,s.lastIndexOf("%"));
  184.     netscape.util.Vector v = p.subviews();
  185.     for(int k=0;k<v.size();k++){
  186.       Object o = v.elementAt(k);
  187.       if(o instanceof netscape.application.View){
  188.     String osc = simpClass(o);
  189.     if(osc.equals(cn)){
  190.       if(ct==tot)
  191.         return ((netscape.application.View)o);
  192.       ct++;
  193.     }
  194.       }
  195.     }
  196.     return null;
  197.   }
  198.   /**
  199.    * creates the simple class name.  All package names are removed.
  200.    * @param o the ifc object
  201.    * @returns String representation of the class name
  202.    */
  203.   public static String simpClass(Object o){
  204.     String cl = o.getClass().getName();
  205.     return cl.substring(1+cl.lastIndexOf('.'));
  206.   }
  207.  
  208.   /**
  209.    * calcuates the location of a view.  The method walks up the component
  210.    * hierarchy calculating the location at each component and returns
  211.    * the global coordinates of the view with respect to its top level
  212.    * ifc component.
  213.    * @param rv the object
  214.    * @returns Point the global location of the object relative to its top
  215.    *    level ifc component
  216.    */
  217.   public static Point locate(netscape.application.View rv){
  218.     Point p=new Point(0,0);
  219.     while (rv!=null) {
  220.       p=new Point(p.x+rv.x(),p.y+rv.y());
  221.       rv=rv.superview();
  222.     }
  223.     return p;
  224.   }
  225.  
  226.   /**
  227.    * isEnabled calculates whether the ifc component is enabled or not.
  228.    * Only certain ifc classes implement the isEnabled method.  Reflection
  229.    * proved to be too slow so the method checks if the object is an
  230.    * instance of any of the defined classes.  Returns true if isEnabled
  231.    * returns true or if the class does not defined isEnabled.  Returns
  232.    * false in the class has an isEnabled method and its returns false.
  233.    * @param v the view
  234.    * @returns boolean whether the object is enabled of not
  235.    */
  236.   public static boolean isEnabled(netscape.application.View v) {
  237.     boolean enabled=true;
  238.     if (v instanceof netscape.application.Button)
  239.       enabled=((netscape.application.Button)v).isEnabled();
  240.     else {
  241.       if (v instanceof netscape.application.DragWell)
  242.     enabled=((netscape.application.DragWell)v).isEnabled();
  243.       else {
  244.     if (v instanceof netscape.application.ListView)
  245.       enabled=((netscape.application.ListView)v).isEnabled();
  246.     else {
  247.       if (v instanceof netscape.application.ScrollBar)
  248.         enabled=((netscape.application.ScrollBar)v).isEnabled();
  249.       else {
  250.         if (v instanceof netscape.application.Slider)
  251.           enabled=((netscape.application.Slider)v).isEnabled();
  252.       }
  253.     }
  254.       }
  255.     }
  256.     return enabled;
  257.   }
  258.   /** Moves up the hiearchy until a component is enabled.
  259.    * @param v the object
  260.    * @returns View the first enabled object which could be and typically is
  261.    *    the parameter v.
  262.    */
  263.   public static netscape.application.View walkUpUntilEnabled(netscape.application.View v) {
  264.     boolean enabled=false;
  265.     while(enabled==false) {
  266.       enabled=isEnabled(v);
  267.       if (enabled==false) {
  268.     v=v.superview();
  269.     if (v==null)
  270.       return null;
  271.       }
  272.     }
  273.     return v;
  274.   }
  275.  
  276. }
  277.