This section steps through the locators designed for the JClass toolkit (from the KL Group). Unlike the Bongo toolkit, the JClass toolkit does not use a common base class, and so it needs a locator for each base class.
The three classes that make up the KL Group locators are:
JCOutliner
is the locator for objects that extend JCOutlinerComponent
. MultiColumn
is the locator for objects that extend JCMultiColumnListComponent. klgroup
is actually a delegating class for all objects from the JClass toolkit--it determines whether the object in question is a JCOutliner
or a JCMultiColumnListComponent
, then calls the appropriate locator. This locator does not cover the entire KL Group widget set; it serves as an example for two types of widgets within the JClass toolkit.
Here you'll step through JCOutliner
and klgroup in detail. MultiColumn
is a variation on JCOutliner
and is not shown in this chapter, but you can find the source in the \javastar\contrib\locator\klgroup
directory and examine it on your own.
The JCOutliner
locator, designed for the KL Group JClass toolkit, provides an example of a NCL that's a little complicated. The toolkit does not use a common class, so this locator is specific to one of the base classes (JCOutliner
). The tutorial includes it as an example because all toolkits are different, and showing an example of a simple, straightforward NCL might not help you in creating a more challenging NCL.
Note - For an example of an NCL for a toolkit where all classes extend a common object, see the Bongo NCL located in the \contrib\locators
directory.
The header for the JCOutliner
locator imports several libraries:
/* ***************************************************
** @(#)JCOutliner.java 1.2 0
**
** Non Component Locator for KL Group JClass toolkit
**
** Copyright (C) 1997 Sun Microsystems Inc.
**
** ***************************************************
*/
package locators.klgroup;
import suntest.javastar.lib.*;
import java.awt.*;
import java.awt.event.*;
import jclass.bwt.JCOutlinerComponent;
import jclass.bwt.JCOutlinerNode;
import jclass.bwt.JCOutlinerNodeStyle;
import jclass.util.JCVector;
public class JCOutliner implements JSNonComponentLocator {
At minimum, your NCL needs to import:
java.awt
and java.awt.event
libraries
The class declaration is also included above--it implements the JavaStar API's JSNonComponentLocator
class.
The first method defined in this NCL is findObject(), the method JavaStar uses when recording. This method needs to return a reference within a JSNCLData object that JavaStar can use to insert into the test code.
public JSNCLData findObject(Component c, AWTEvent ev) {
JCOutlinerComponent jz;
if (c instanceof JCOutlinerComponent)
jz = (JCOutlinerComponent)c;
else
return null;
int x=((MouseEvent)ev).getX();
int y=((MouseEvent)ev).getY();
Event nev = new Event (c, Event.ACTION_EVENT, null);
nev.x = ((MouseEvent)ev).getX();
nev.y = ((MouseEvent)ev).getY();
JCOutlinerNode jnode = null;
jnode = jz.eventToNode (nev, false);
if (c.getName() != null) {
return new JSNCLData(jnode.getLabelString()+","+nev.x,
new Point(nev.x,nev.y), jz);
}
return null;
}
The findObject()
method:
M
akes the assumption that the object needed is a JCOutlinerComponent
. If it is, the code formally casts the Component passed in to be a JCOutlinerComponent
object. If the Component passed in is not a JCOutlinerComponent
, the method returns null.
JCOutlinerNode
and calls JCOutliner.eventToNode
, passing in the using the old event type. In the KL Group J Class structure, JCOutliner
objects contain JCOutlinerNodes
. You determine where the event occurred by calling eventToNode()
and assigning the result to the node.
JSNCLData
object. JSNCLData
takes a name, a point, and an object reference. findObject() supplies these as:
Next, the code defines the playback side of the locator by implementing the getNamedObjectData
method. The name passed as an argument is the name variable from the JSNCLData
object, as created by the findObject()
method, so the format is expected as whatever was defined in findObject()
.
The task of getNamedObjectData()
is to turn this information back into a component that JavaStar can then act upon.
public JSNCLData getNamedObjectData(Component c, String name) {
JCOutlinerComponent jz;
if (c instanceof JCOutlinerComponent)
jz = (JCOutlinerComponent)c;
else
return null;
int offsetx=0, offsety=0;
offsetx = new Integer
(name.substring(name.indexOf(",")+1)).intValue();
name = name.substring(0, name.indexOf(","));
JCVector visiblenodes = jz.getVisibleNodes ();
JCOutlinerNode jcnode = null;
int stdincr = jz.getNodeHeight() + jz.getSpacing() + 2;
for (int i=0;i<visiblenodes.size();i++) {
jcnode = (JCOutlinerNode)visiblenodes.elementAt(i);
offsety += stdincr;
if (jcnode.getLabelString().equals(name))
break;
}
return new JSNCLData(name,new Point(offsetx,offsety),c);
}
}
The getNamedObject()
method:
JCOutlinerComponent
(just as findObject()
did).
findObject()
appended to the name, and assign this to the x offset.
JCOutlinerComponent
and initialize a node, in preparation to traverse the list.
stdincr
) that equals the height and spacing of the JCOutlinerComponent
, plus two. The value of two was arrived at through testing the locator; you might find a different way to determine the exact spacing.
getNamedObjectData()
.
JSNCLData
object containing:
As mentioned before, the klgroup
class is a delegating class. JavaStar calls either klgroup.findObject
or klgroup.getNamedObject
. The methods then determine which component time is being passed in, and calls the same method in either JCOutliner
or MultiColumn
.
/* ***************************************************
** @(#)klgroup.java 1.2 0
**
** Non Component Locator for KL Group JClass toolkit
** Only covers JCMultiColumnListComponent and
** JCOutlinerComponent (and those only weakly)
**
** Copyright (C) 1997 Sun Microsystems Inc.
**
** ***************************************************
*/
package locators.klgroup;
import suntest.javastar.lib.*;
import java.awt.*;
import java.awt.event.*;
import jclass.bwt.*;
public class klgroup implements JSNonComponentLocator {
public JSNCLData findObject(Component c, AWTEvent ev) {
// check the class, call that class's findObject
if (c instanceof JCMultiColumnListComponent) {
MultiColumn mc = new MultiColumn();
return mc.findObject (c, ev);
} else if (c instanceof JCOutlinerComponent ) {
JCOutliner jc = new JCOutliner ();
return jc.findObject (c, ev);
}
return null;
}
For klgroup, findObject()
:
t
. If it is, the code instantiates a MultiColumn
object and calls the objects findObject()
method.
t
, the code checks to see if it is a JCOutlinerComponent
. If so, it instantiates a JCOutliner
and calls the objects findObject()
method.
t
nor a JCMultiColumnListComponent
, findObject()
returns null
.
public JSNCLData getNamedObjectData(Component c, String name) {
if (c instanceof JCMultiColumnListComponent) {
MultiColumn mc = new MultiColumn();
return mc.getNamedObjectData (c, name);
} else if (c instanceof JCOutlinerComponent) {
JCOutliner jc = new JCOutliner ();
return jc.getNamedObjectData (c, name);
}
return null;
}
}
This method works the same as klgroup.findObject(), except that it calls the getNamedObjectData() methods of the classes it instantiates.
Send feedback to
JavaStar-feedback@suntest.com
Copyright © 1998
Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, CA 94303.
All rights reserved.