home *** CD-ROM | disk | FTP | other *** search
Java Source | 1999-02-11 | 9.3 KB | 296 lines |
- /*
- * @version @(#)bongoSimple.java 1.5 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.AWTEvent;
- import java.awt.event.*;
- import java.awt.Component;
- import java.awt.Point;
- import java.util.Vector;
- import java.util.Hashtable;
- import marimba.gui.*;
-
- /**
- * <ul>
- * <li>Non Component Locator for the Bongo toolkit simple version
- * <li>Copyright (C) 1997 Sun Microsystem Inc.
- * <li>Marimba and Bongo are registered trademarks of Marimba, Inc
- * </ul>
- * <p> The bongoSimple class implements a nonComponent Locators solution for
- * Marimba's bongo
- * toolkit. When used in conjunction with JavaStar the locator allows
- * JavaStar tests to operate in the bongo 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 @(#)bongoSimple.java 1.5 0
- */
- public class bongoSimple implements JSNonComponentLocator {
-
- /**
- * Returns the non-component object and a string representation that
- * is recreating by getNamedObjectData. For examples:
- * "PopupWidget.Presentation%0.FolderWidget%0.PageWidget%1.CheckBoxWidget%1"
- *
- * @param comp The AWT based Component or frame of Bongo, its a PlayerPanel
- * @param e The AWTEvent, extract x,y coords
- * @return JSNCLData object is (String name,Point(widgets loc),Object)
- * @see bongo#getNamedObjectData
- */
- public JSNCLData findObject(Component comp, AWTEvent evt){
- System.out.println("ok");
- if (comp instanceof PlayerPanel) {
- // get the x,y location of the event inside the PlayerPanel
- // locateWidget returns the bongo Widget x,y
- int x = ((MouseEvent)evt).getX();
- int y = ((MouseEvent)evt).getY();
- PlayerPanel ppRoot=(PlayerPanel)comp;
- Widget w=ppRoot.locateWidget(x,y);
- if (w instanceof PopupMenuItemWidget) {
- PopupMenu popup=((PopupMenuItemWidget)w).getMenu();
- if (popup!=null && popup.owner!=null)
- w=popup.owner;
- }
- w=walkUpUntilEnabled(w);
- String name=buildTag(w);
- Point p=getLocation(w);
- return new JSNCLData(name,p,w);
- }
- return null;
- }
-
- /**
- * Returns the non-component object after getting the actual Widget from
- * the string description passed in. Reconstructs the widget from the
- * text parameter.
- *
- * @param c The AWT based Component or frame of Bongo, its a PlayerPanel
- * @param name The name of the object
- * @return JSNCLData object is (String name,Point(widgets loc),Object)
- * @see bongo#findObject
- */
- public JSNCLData getNamedObjectData(Component c, String name){
- // If the component is not PlayerPanel fail
- // its not a bongo widget
- if(c instanceof PlayerPanel) {
- PlayerPanel ppRoot=(PlayerPanel)c;
- Widget wroot=getRoot(ppRoot);
- Widget w=getWidget(wroot,name);
- if (isEnabled(w)==false)
- return null;
- Point p=getLocation(w);
- return new JSNCLData(name,p,w);
- }
- return null;
- }
-
- /**
- * Creates the widget by starting at the base getting each component by index.
- * Parses the string by "." to get each component. If any components fail
- * returns null and exception should be thrown by caller.
- * @param wbase the root widget
- * @param s the tag
- * @returns Widget the widget represented by the tag or null if it is not
- * found or not visible
- */
- public static Widget getWidget(Widget wbase,String s) {
- Widget w=wbase;
- String sCurrent;
- int index= -1;
- // parse by "."
- while (s.equals("")==false) {
- if (s.indexOf(".")== -1) {
- sCurrent=s;
- s="";
- } else {
- sCurrent=s.substring(0,s.indexOf("."));
- s=s.substring(s.indexOf(".")+1);
- }
- if (sCurrent.indexOf("%") != -1) {
- index=new Integer(sCurrent.substring(sCurrent.indexOf("%")+1)).intValue();
- sCurrent=sCurrent.substring(0,sCurrent.indexOf("%"));
- w=findIndexWidget(w,sCurrent,index);
- if (w==null)
- return null;
- }
- if (w==null)
- return null;
- }
- return w;
- }
- /**
- * returns the position of the widget in the PlayerFrame
- * @param w the widget
- * @returns Point the location of the widget relative to the PlayerFrame
- */
- public static Point getLocation(Widget w) {
- int x,y;
- Point p=new Point(0,0);
- while (w!=null) {
- x=w.x;
- y=w.y;
- if (x<0) x=0;
- if (y<0) y=0;
- p=new Point(p.x+w.x,p.y+w.y);
- w=w.parent;
- }
- 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 parent widget
- * @param cname the class name
- * @param index the index to search for
- * @returns Widget the child widget or null if it does not exist
- */
- public static Widget findIndexWidget(Widget w,String cname,int index) {
- Widget wRet=null;
- int iNum=0;
- for (int i=0;i<w.nwidgets;i++){
- if (getWidgetClassName(w.widgets[i]).equals(cname))
- iNum++;
- if (iNum>index)
- return w.widgets[i];
- }
- return wRet;
- }
- /**
- * 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 widget the widget to create the tag
- * @returns String the tag representation of the widget
- */
- public static String buildTag(Widget 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=getWidgetClassName(w);
- if (w.parent!=null) {
- index=getWidgetIndex(w);
- if (index!= -1)
- s+="%"+index;
- }
- if (sRet=="")
- sRet=s;
- else
- sRet=s+"."+sRet;
- w=w.parent;
- }
- return sRet;
- }
- /**
- * returns the index of this widget from its parents widget list by class name.
- * @param w the widget
- * @returns its index relative to its parent and the class name
- */
- public static int getWidgetIndex(Widget w) {
- Widget container=w.parent;
- int index=0;
- for (int i=0;i<container.nwidgets;i++) {
- if (container.widgets[i]==w)
- return index;
- if (container.widgets[i].getClass()==w.getClass())
- index++;
- }
- return -1;
- }
- /**
- * gets the class name from a widget. strips marimba.gui
- * do all widgets start with marimba.gui?
- * could also get the name before last "."
- * @param o the widget
- * @returns the class name without the package name
- */
- public static String getWidgetClassName(Object o){
- String cl = o.getClass().getName();
- cl = cl.substring(1+cl.lastIndexOf('.'));
- // hackaround for Bongo returning script objects
- if (cl.indexOf("_") != -1) {
- cl=cl.substring(cl.indexOf("_")+1,cl.lastIndexOf("_"));
- }
- return cl;
- }
- /**
- * get widget at 0,0 then walks up hiearchy tree
- * @param ppRoot the PlayPanel object
- * @returns Widget the top level widget
- */
- public static Widget getRoot(PlayerPanel ppRoot) {
- Widget w;
- w=ppRoot.locateWidget(0,0);
- return getRoot(w);
- }
- /**
- * walks up hiearchy 1tree to get top level widget
- * theres not .getRoot method in Bongo from PlayerPanel
- * @param w the widget
- * @returns Widget the root Widget
- */
- public static Widget getRoot(Widget w) {
- Widget wlast=w;
- if (w==null)
- return null;
- while (w!=null) {
- wlast=w;
- w=w.parent;
- }
- return wlast;
- }
- /**
- * walks up widget tree until find a widget that is enabled. If
- * no widgets are enabled even at top level null is returned.
- * @param v the widget to start at
- * @returns the first widget that is enabled or null
- */
- private static Widget walkUpUntilEnabled(Widget v) {
- boolean enabled=false;
- while(enabled==false) {
- enabled=isEnabled(v);
- if (enabled==false) {
- v=v.parent;
- if (v==null)
- return null;
- }
- }
- return v;
- }
- /** returns whether the widget is visible.
- * @param v the widget to test
- * @returns boolean true if visible, false if not visible
- */
- private static boolean isVisible(Widget v) {
- return v.isVisible();
- }
- /** returns whether the widget is enabled
- * @param v the widget to test
- * @returns true if widget is enabled, false if widget is disabled
- */
- private static boolean isEnabled(Widget v) {
- return !v.isDisabled();
- }
- }
-