Writing Non-Component Locators: Exercise


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:

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.

Setting up the Exercise

The tutorial directory for this example is:

The template for the JList Locator is:

Copy this to the NCL directory:

Write the NCL

  1. Use any editor to complete the NCL. You may use the JavaStar editor.
    Follow the steps in "Anatomy of a JSNonComponentLocator", seen eariler in this lesson. Use the solution at "Solution", shown later in this lesson, as a guide.

  2. Compile the NCL.

Test the NCL

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:

  1. Start JavaStar.

  2. Build a project.

    1. Name it WriteNCL.jpr and click Save As. Save it in the <Javastar>/tutorial/WriteNCL directory.

    2. Click on the App tab. Select the Applet radio button. Browse to locate this HTML file and Click Open.
        <JavaStar>/examples/SwingSet/Application/SwingSetApplet.html
        

    3. Click on the Mapping tab.
      Select the NCLs WNCL and TheListLoc from this directory:
        <JavaStar>/tutorial/writeNCL/NCLs
        

      Select the text map TheTextMap from this directory:
        <JavaStar>/tutorial/writeNCL/NCLs
        

      Click on the Test tab.
      Set the Tests, Test Path and JST Path directory to:
        <JavaStar>/tutorial/writeNCL/Tests
        

      Set the test results directory to:
        <JavaStar>/tutorial/writeNCL/TestResults
        

    4. Click the Save button to save your work.

  3. Click Create a Test Script.
    You'll see the "Record/Playback" window. Wait a bit for the applet to load.

  4. Click Record

  5. Name the script tryList, and click OK.

  6. From the SwingSet applet, click on the tabbed pane, ListBox.
    You'll see a list of food items appear.

  7. Click on the Burgers item in the list.

  8. Click on the Fries item in the list.

  9. Return to the "Record/Playback" window. Click Edit.
    You will see the script. Check to ensure that the non-component locator was in place for these items. You should see:

  10. Click Continue to close the editor.

  11. Click Stop to compile the script.

  12. Click Playback to play the script back. It should find all components.

Solution




Send feedback to JavaStar-feedback@suntest.com
Copyright © 1998 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, CA 94303. All rights reserved.