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

  1. /*
  2.  *  @version             @(#)bongoSimple.java    1.5 98/05/15
  3.  *
  4.  * Copyright (c) 1997 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. import java.util.Vector;
  27. import java.util.Hashtable;
  28. import marimba.gui.*;
  29.  
  30. /**
  31. * <ul>
  32. * <li>Non Component Locator for the Bongo toolkit simple version
  33. * <li>Copyright (C) 1997 Sun Microsystem Inc.
  34. * <li>Marimba and Bongo are registered trademarks of Marimba, Inc
  35. * </ul>
  36. * <p> The bongoSimple class implements a nonComponent Locators solution for
  37. * Marimba's bongo
  38. * toolkit.  When used in conjunction with JavaStar the locator allows
  39. * JavaStar tests to operate in the bongo object level.  This version
  40. * of the locator is a smaller and faster version of the locator but
  41. * supports only full tags with hierarchy and indexes.
  42. *
  43. * @author     Dan Schaffer
  44. * @version    @(#)bongoSimple.java    1.5 0
  45. */
  46. public class bongoSimple implements JSNonComponentLocator {
  47.  
  48.   /**
  49.    * Returns the non-component object and a string representation that
  50.    * is recreating by getNamedObjectData.  For examples:
  51.    * "PopupWidget.Presentation%0.FolderWidget%0.PageWidget%1.CheckBoxWidget%1"
  52.    *
  53.    * @param    comp The AWT based Component or frame of Bongo, its a PlayerPanel
  54.    * @param    e The AWTEvent, extract x,y coords
  55.    * @return   JSNCLData object is (String name,Point(widgets loc),Object)
  56.    * @see bongo#getNamedObjectData
  57.    */
  58.   public JSNCLData findObject(Component comp, AWTEvent evt){
  59.     System.out.println("ok");
  60.     if (comp instanceof PlayerPanel) {
  61.     // get the x,y location of the event inside the PlayerPanel
  62.     // locateWidget returns the bongo Widget x,y
  63.       int x = ((MouseEvent)evt).getX();
  64.       int y = ((MouseEvent)evt).getY();
  65.       PlayerPanel ppRoot=(PlayerPanel)comp;
  66.       Widget w=ppRoot.locateWidget(x,y);
  67.       if (w instanceof PopupMenuItemWidget) {
  68.     PopupMenu popup=((PopupMenuItemWidget)w).getMenu();
  69.     if (popup!=null && popup.owner!=null)
  70.       w=popup.owner;
  71.       }
  72.       w=walkUpUntilEnabled(w);
  73.       String name=buildTag(w);
  74.       Point p=getLocation(w);
  75.       return new JSNCLData(name,p,w);
  76.     }
  77.     return null;
  78.   }
  79.  
  80.   /**
  81.    * Returns the non-component object after getting the actual Widget from
  82.    * the string description passed in.  Reconstructs the widget from the
  83.    * text parameter.
  84.    *
  85.    * @param    c The AWT based Component or frame of Bongo, its a PlayerPanel
  86.    * @param    name The name of the object
  87.    * @return   JSNCLData object is (String name,Point(widgets loc),Object)
  88.    * @see bongo#findObject
  89.    */
  90.   public JSNCLData getNamedObjectData(Component c, String name){
  91.     // If the component is not PlayerPanel fail
  92.     // its not a bongo widget
  93.     if(c instanceof PlayerPanel) {
  94.       PlayerPanel ppRoot=(PlayerPanel)c;
  95.       Widget wroot=getRoot(ppRoot);
  96.       Widget w=getWidget(wroot,name);
  97.       if (isEnabled(w)==false)
  98.     return null;
  99.       Point p=getLocation(w);
  100.       return new JSNCLData(name,p,w);
  101.     }
  102.     return null;
  103.   }
  104.  
  105.   /**
  106.    *  Creates the widget by starting at the base getting each component by index.
  107.   *  Parses the string by "." to get each component.  If any components fail
  108.   *  returns null and exception should be thrown by caller.
  109.   * @param wbase the root widget
  110.   * @param s the tag
  111.   * @returns Widget the widget represented by the tag or null if it is not
  112.   *    found or not visible
  113.   */
  114.   public static Widget getWidget(Widget wbase,String s) {
  115.     Widget w=wbase;
  116.     String sCurrent;
  117.     int index= -1;
  118.     // parse by "."
  119.     while (s.equals("")==false) {
  120.       if (s.indexOf(".")== -1) {
  121.     sCurrent=s;
  122.     s="";
  123.       } else {
  124.     sCurrent=s.substring(0,s.indexOf("."));
  125.     s=s.substring(s.indexOf(".")+1);
  126.       }
  127.       if (sCurrent.indexOf("%") != -1) {
  128.     index=new Integer(sCurrent.substring(sCurrent.indexOf("%")+1)).intValue();
  129.     sCurrent=sCurrent.substring(0,sCurrent.indexOf("%"));
  130.     w=findIndexWidget(w,sCurrent,index);
  131.     if (w==null)
  132.       return null;
  133.       }
  134.       if (w==null)
  135.     return null;
  136.     }
  137.     return w;
  138. }
  139.   /**
  140.   * returns the position of the widget in the PlayerFrame
  141.   * @param w the widget
  142.   * @returns Point the location of the widget relative to the PlayerFrame
  143.   */
  144.   public static Point getLocation(Widget w) {
  145.     int x,y;
  146.     Point p=new Point(0,0);
  147.     while (w!=null) {
  148.       x=w.x;
  149.       y=w.y;
  150.       if (x<0) x=0;
  151.       if (y<0) y=0;
  152.       p=new Point(p.x+w.x,p.y+w.y);
  153.       w=w.parent;
  154.     }
  155.     return p;
  156.   }
  157.   /**
  158.   *  Searches the widget for a child widget matching the class.  Returns
  159.   *  the widget when find the child widget with the correct index
  160.   * @param w the parent widget
  161.   * @param cname the class name
  162.   * @param index the index to search for
  163.   * @returns Widget the child widget or null if it does not exist
  164.   */
  165.   public static Widget findIndexWidget(Widget w,String cname,int index) {
  166.     Widget wRet=null;
  167.     int iNum=0;
  168.     for (int i=0;i<w.nwidgets;i++){
  169.       if (getWidgetClassName(w.widgets[i]).equals(cname))
  170.     iNum++;
  171.       if (iNum>index)
  172.     return w.widgets[i];
  173.     }
  174.     return wRet;
  175.   }
  176.   /**
  177.   * Creates a string representation of the widget.  For each level in
  178.   * the widget hiearchy get the index of the component and the class.
  179.   * For each level create <classname>%<index>.  The "." separates each
  180.   * widget.
  181.   * @param widget the widget to create the tag
  182.   * @returns String the tag representation of the widget
  183.   */
  184.   public static String buildTag(Widget w) {
  185.     int index;
  186.     String sRet="",s;
  187.     // starting at top get the widget class,index append to tag and walk
  188.     // down to top level where w.parent is null
  189.     // if can't find a component is the parent list return null
  190.     while (w!=null) {
  191.       s=getWidgetClassName(w);
  192.       if (w.parent!=null) {
  193.     index=getWidgetIndex(w);
  194.     if (index!= -1)
  195.       s+="%"+index;
  196.       }
  197.       if (sRet=="")
  198.     sRet=s;
  199.       else
  200.     sRet=s+"."+sRet;
  201.       w=w.parent;
  202.     }
  203.     return sRet;
  204.   }
  205.   /**
  206.   * returns the index of this widget from its parents widget list by class name.
  207.   * @param w the widget
  208.   * @returns its index relative to its parent and the class name
  209.   */
  210.   public static int getWidgetIndex(Widget w) {
  211.     Widget container=w.parent;
  212.     int index=0;
  213.     for (int i=0;i<container.nwidgets;i++) {
  214.       if (container.widgets[i]==w)
  215.     return index;
  216.       if (container.widgets[i].getClass()==w.getClass())
  217.     index++;
  218.     }
  219.     return -1;
  220.   }
  221.   /**
  222.   *  gets the class name from a widget. strips marimba.gui
  223.   *  do all widgets start with marimba.gui?
  224.   *  could also get the name before last "."
  225.   * @param o the widget
  226.   * @returns the class name without the package name
  227.   */
  228.   public static String getWidgetClassName(Object o){
  229.     String cl = o.getClass().getName();
  230.     cl = cl.substring(1+cl.lastIndexOf('.'));
  231.     // hackaround for Bongo returning script objects
  232.     if (cl.indexOf("_") != -1) {
  233.       cl=cl.substring(cl.indexOf("_")+1,cl.lastIndexOf("_"));
  234.     }
  235.     return cl;
  236.   }
  237.   /**
  238.   * get widget at 0,0 then walks up hiearchy tree
  239.   * @param ppRoot the PlayPanel object
  240.   * @returns Widget the top level widget
  241.   */
  242.   public static Widget getRoot(PlayerPanel ppRoot) {
  243.     Widget w;
  244.     w=ppRoot.locateWidget(0,0);
  245.     return getRoot(w);
  246.   }
  247.   /**
  248.   * walks up hiearchy 1tree to get top level widget
  249.   * theres not .getRoot method in Bongo from PlayerPanel
  250.   * @param w the widget
  251.   * @returns Widget the root Widget
  252.   */
  253.   public static Widget getRoot(Widget w) {
  254.     Widget wlast=w;
  255.     if (w==null)
  256.       return null;
  257.     while (w!=null) {
  258.       wlast=w;
  259.       w=w.parent;
  260.     }
  261.     return wlast;
  262.   }
  263.   /**
  264.    * walks up widget tree until find a widget that is enabled. If
  265.    * no widgets are enabled even at top level null is returned.
  266.    * @param v the widget to start at
  267.    * @returns the first widget that is enabled or null
  268.    */
  269.   private static Widget walkUpUntilEnabled(Widget v) {
  270.     boolean enabled=false;
  271.     while(enabled==false) {
  272.       enabled=isEnabled(v);
  273.       if (enabled==false) {
  274.     v=v.parent;
  275.     if (v==null)
  276.       return null;
  277.       }
  278.     }
  279.     return v;
  280.   }
  281.   /** returns whether the widget is visible.
  282.    * @param v the widget to test
  283.    * @returns boolean true if visible, false if not visible
  284.    */
  285.   private static boolean isVisible(Widget v) {
  286.     return v.isVisible();
  287.   }
  288.   /** returns whether the widget is enabled
  289.    * @param v the widget to test
  290.    * @returns true if widget is enabled, false if widget is disabled
  291.    */
  292.   private static boolean isEnabled(Widget v) {
  293.     return !v.isDisabled();
  294.   }
  295. }
  296.