If you would like to try to build an NCL as an exercise, try one for a JList
.
Before you code, you need to consider how you will name the item, locate its bounds, and obtain the component. Since we don't expect you to be a JList
expert, here's some help.
The name of the component will be its index in the list, a colon, and a text string. We have provided a method findName()
that returns that additional string given a JList
and an index to it. You can use the Parser
utility just as it was used in the JTree
locator. To find the index, use the locationToIndex()
method of JList
. You can find the JList
API in this HTML file:
<Javastar>/examples/SwingSet/Application/doc/api/
com.sun.java.swing.JList.html
Once you have the index, you can get the bounds by using the JList
method indexToLocation()
.
Finding the actual object to return is a bit complicated with a JList
, especially this example as it uses embedded images. We have provided a method getObject()
for you to use. It returns the object given a JList
and an index.
Given that information, and a template with the provided methods and comments, follow along with the previous example and construct a JList
NCL.
Here is the setup information.
The tutorial directory for this example is:
<Javastar>/tutorial/WriteNCL/
The template for the JList Locator is:
<Javastar>/tutorial/WriteNCL/NCLs/template/TheListLoc.java
Copy this to the NCL directory:
<Javastar>/tutorial/WriteNCL/NCLs/
You must have Swing 1.0.2 or higher installed and in your classpath. The product can be downloaded from the Java web site at:
http://java.sun.com
WriteNCL.jpr
and click Save As. Save it in the <Javastar>/tutorial/WriteNCL directory.
<JavaStar>/examples/SwingSet/Application/SwingSetApplet.html
<JavaStar>/tutorial/writeNCL/NCLs
TheTextMap
from this directory:
<JavaStar>/tutorial/writeNCL/NCLs
<JavaStar>/tutorial/writeNCL/Tests
<JavaStar>/tutorial/writeNCL/TestResults
tryList
, and click OK.getNonComponent("TheListLoc","0: Burger")
import suntest.javastar.lib.*;
import com.sun.java.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
/*
*
* Non Component Locator for JList in JFC Swing
*/
public class TheListLoc implements JSNonComponentLocator {
public JSNCLData findObject(Component c, AWTEvent e){
// ensure that this is a list and a mouse event
if(!(c instanceof JList) || !(e instanceof MouseEvent)){
return null;
}
// get the point from the mouse event
Point mousePoint = ((MouseEvent)e).getPoint();
// cast the component to a JList
JList theList = (JList)c;
// obtain the index based on the location
int theIndex = theList.locationToIndex(mousePoint);
// make sure the index exists
if(theIndex != -1){
// use a common method to tranlate an index entry in a list to NCLData
return getListData(theList,theIndex);
}
return null;
}
public JSNCLData getNamedObjectData(Component c, String wname){
// make sure this is a list
if (!(c instanceof JList)){
return null;
}
// cast it to a JList
JList theList = (JList)c;
// create a parser for this string
Parser P = new Parser(wname);
// make sure the string is valid
if (!P.Valid){
throw new BadRegularExpressionError(wname + " is not a valid format");
}
// get the index and the confirming name from the parser
int theIndex = P.Index;
String theConfirmingName = P.Value;
// get the model for the list
ListModel theListModel = theList.getModel();
// confirm that the index is within the list
int numberInList = theListModel.getSize();
if(theIndex<=-1){
return null;
}
if(theIndex>=numberInList){
return null;
}
if(theConfirmingName == null){
return getListData(theList, theIndex);
}
//assert that the index is within the model, and that there is a confirming name to check for
String name = Parser.cleanToString(theListModel.getElementAt(theIndex));
if(theConfirmingName.equals(findName(theList, theIndex))){
// use the common method to translate a list and an index to that list to NCLData
return getListData(theList,theIndex);
} else {
return null;
}
}
public static JSNCLData getListData(JList aList, int anIndex){
// get the model for the list
ListModel aListModel = aList.getModel();
// make sure the index is within the model size
if(anIndex >=aListModel.getSize()) return null;
// get the location of the index point
Point theNCPoint = aList.indexToLocation(anIndex);
// get the element for the object
Object theNCObject = getObject(aList,anIndex);
// get a clean string (no special characters) for this object
String theObjectName = findName(aList, anIndex);
// prepend the index and create the name
String theNCName = anIndex + ":" + theObjectName;
return new JSNCLData(theNCName, theNCPoint, theNCObject);
}
static Object getObject(JList aList,int anIndex) {
ListModel aListModel = aList.getModel();
Object theNCObject = aListModel.getElementAt(anIndex);
if (theNCObject instanceof Integer) {
ListCellRenderer renderer = aList.getCellRenderer();
if (renderer != null) {
Component comp = renderer.getListCellRendererComponent
(aList, theNCObject,
anIndex, aList.isSelectedIndex(anIndex),
aList.hasFocus());
if (comp != null) {
theNCObject = comp;
}
}
}
return theNCObject;
}
static String findName(JList L, int idx) {
ListModel M = L.getModel();
Object o = M.getElementAt(idx);
String name = null;
if (o instanceof ImageIcon) {
name = ((ImageIcon)o).getDescription();
} else if (o instanceof Integer) {
ListCellRenderer renderer = L.getCellRenderer();
if (renderer != null) {
Component comp = renderer.getListCellRendererComponent(L, o, idx, L.isSelectedIndex(idx), L.hasFocus());
if (comp != null) {
o = comp;
if (comp.getName() != null && comp.getName().length() > 0) {
name = comp.getName();
} else {
if (comp instanceof JLabel)
name = ((JLabel)comp).getText();
else if (comp instanceof Label)
name = ((Label)comp).getText();
}
}
}
}
return (name != null ? name : Parser.cleanToString(o));
}
}
Send feedback to
JavaStar-feedback@suntest.com
Copyright © 1998
Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, CA 94303.
All rights reserved.