25.5 Classes Added in SDK 1.3 and 1.4


The 1.3 and 1.4 SDKs both augmented the Accessibility package. Three interfaces in particular stand out: AccessibleIcon, AccessibleEditableText, and AccessibleTable.

25.5.1 The AccessibleIcon Interface

Added in the 1.3 release, the AccessibleIcon interface allows assistive technologies to get information about any icons on a component (such as a button or a label). Notice that the getAccessibleIcon( ) method from the AccessibleContext class returns an array of icons. While Swing labels and buttons do not support multiple icons, custom components can freely do so and still provide useful information to assistive technologies.

25.5.1.1 Properties

Table 25-12 lists the three descriptive properties for AccessibleIcon. accessibleIcon-Description is the most useful property and can be set through the accessible context as it is on other components.

Table 25-12. AccessibleIcon properties

Property

Data type

get

is

set

Default value

accessibleIconDescription

String

·

 

·

 

accessibleIconHeight

int

·

     

accessibleIconWidth

int

·

     

25.5.2 The AccessibleEditableText Interface

While the AccessibleText interface has always been part of Swing, an editable representation for text did not exist. With the release of the 1.4 SDK, AccessibleEditableText fills that gap.

25.5.2.1 Property

As an extension of the AccessibleText interface, most of the properties for AccessibleEditableText are inherited. One writable property to alter the contents of the text component, textContents, was added. It is shown in Table 25-13.

Table 25-13. AccessibleEditableText property

Property

Data type

get

is

set

Default value

textContents

String

   

·

 

25.5.2.2 Text manipulation methods

The remaining AccessibleEditableText methods allow assistive technologies to manipulate text in the editable component:

public void cut(int startIndex, int endIndex)

This method cuts text in the component (between the given indices) and places it on the system clipboard.

public void delete(int startIndex, int endIndex)

Similar to the cut( ) method, but the text is simply cleared it is not sent to the clipboard.

public String getTextRange(int startIndex, int endIndex)

This method returns the text between the given indices.

public void insertTextAtIndex(int index, String s)

Insert the text in s at the point given by index. Similar to the paste( ) method below, but the string to insert is not retrieved from the clipboard.

public void paste(int startIndex)

Insert the text on the clipboard at the point given by startIndex.

public void replaceText(int startIndex, int endIndex, String s)

This method replaces the text between startIndex and endIndex with the text in s. Note that there is no variation for this method that takes the text directly from the clipboard.

public void selectText(int startIndex, int endIndex)

This method can be used to programmatically select the text between the startIndex and endIndex.

public void setAttributes(int startIndex, int endIndex, AttributeSet as)

This method can be used to programmatically alter the attributes of a range of text between startIndex and endIndex.

25.5.3 The AccessibleTable Interface

The 1.3 SDK also introduced an accessible interface for tables. This interface allows assistive technologies access to descriptive information on the table and its various parts (rows, columns, and cells).

25.5.3.1 Properties

The properties in Table 25-14 describe just about every part of a table you would expect to have access to. Some of the less obvious properties include accessibleAt , which returns the Accessible object at a given row and column. The accessibleCaption property is the table caption which is not necessarily the same as the summary. The extent properties (accessibleColumnExtentAt and accessibleRowExtentAt) provide information on how many columns or rows, respectively, the Accessible object occupies. For example, an accessible spreadsheet might have an entry that occupies two or more "straddled" cells.

Table 25-14. AccessibleHyperlink properties

Property

Data type

get

is

set

Default value

accessibleAtii

Accessible

·

     

accessibleCaption

Accessible

·

 

·

 

accessibleColumnCount

int

·

     

accessibleColumnDescriptioni

Accessible

·

 

·

 

accessibleColumnExtentAtii

int

·

     

accessibleColumnHeader

AccessibleTable

·

 

·

 

accessibleColumnSelectedi

boolean

 

·

   

accessibleRowCount

int

·

     

accessibleRowDescriptioni

Accessible

·

 

·

 

accessibleRowExtentAtii

int

·

     

accessibleRowHeader

AccessibleTable

·

 

·

 

accessibleRowSelectedi

boolean

 

·

   

accessibleSelectedii

boolean

 

·

   

accessibleSummary

Accessible

·

 

·

 

selectedAccessibleColumns

int[]

·

     

selectedAccessibleRows

int[]

·

     

iindexed, iidouble-indexed (row, column)

25.5.4 Relations and Extended Information

Among the other accessibility pieces added in the 1.3 and 1.4 releases are the AccessibleRelation and AccessibleRelationSet classes. A new method in the AccessibleContext class gives you access to the relation set for an accessible component. A relation set is the collection of all relationships between this component and other components in the system. The AccessibleRelation class encodes these relationships. An AccessibleRelation can describe several relationships, including control (controller/controlled by), labeling (label for/labeled by), and membership.

Perhaps not surprisingly, the extended accessibility interfaces (AccessibleExtendedTable and AccessibleExtendedComponent) provide information above and beyond what's available through their parent interfaces, AccessibleTable and AccessibleComponent, respectively. The extended component interface describes information for onscreen components, including tooltip text, border text, and key bindings. The extended table information allows you to translate between row and column indices and a linear index of Accessible objects. For example, given a pair of row and column values, you can look up the Accessible object associated with that cell. Alternatively, you can take the index of a particular Accessible object and find out which row or column it is associated with.

The last big addition to the accessibility package is the AccessibleKeyBindings interface. This interface simply describes the key bindings (i.e., shortcuts and mnemonics) associated with a component. Again, this type of information is available in other formats from other places, but this interface gives assistive technologies a consistent view of the bindings as well as a consistent way of extracting the relevant parts.

25.5.5 Implementing AccessibleAction

Here is a short example that implements an AccessibleContext and the AccessibleAction interface for a simple AWT button:

// ActionExampleButton.java // import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.accessibility.*; public class ActionExampleButton extends Button      implements ActionListener, Accessible {      public ActionExampleButton( ) {          super("Press this Button");          addActionListener(this);      }      public AccessibleContext getAccessibleContext( ) {          return (new ActionAccessibleContext( ));      }      public void actionPerformed(ActionEvent e) {          System.out.println("The button was pressed!");      }      public void processActionEvent(ActionEvent e) {          super.processActionEvent(e);      }      // This class contains the accessible context for the component. Many of the      // abstract methods simply call the SwingUtilities class to get the job done;      // this is advised if you can get away with it. Otherwise, see the source code      // for SwingUtilities.      class ActionAccessibleContext extends AccessibleContext {         public ActionAccessibleContext( ) {             super( );             setAccessibleName("Button");             setAccessibleDescription("Press the Button");         }         public AccessibleRole getAccessibleRole( ) {             // Fill in whatever role you want here.             return (AccessibleRole.AWT_COMPONENT);         }         public AccessibleStateSet getAccessibleStateSet( ) {             return SwingUtilities.getAccessibleStateSet(ActionExample.this);         }         public int getAccessibleIndexInParent( ) {             return SwingUtilities.getAccessibleIndexInParent(ActionExample.this);         }         public int getAccessibleChildrenCount( ) {             return SwingUtilities.getAccessibleChildrenCount(ActionExample.this);         }         public Accessible getAccessibleChild(int i) {             return SwingUtilities.getAccessibleChild(ActionExample.this, i);         }         public Locale getLocale( ) {             // Ask the component what its locale is.             return ActionExample.this.getLocale( );         }         public AccessibleAction getAccessibleAction( ) {             return new AccessAction( );         }      }        // This class implements the AccessibleAction interface. Essentially, there      // is only one action, which is the equivalent of clicking on the button.      class AccessAction implements AccessibleAction {         final int NUMBER_OF_ACTIONS = 1;         final String DESCRIPTION = "Presses the button";         public int getAccessibleActionCount( ) {             return NUMBER_OF_ACTIONS;         }         public String getAccessibleActionDescription(int i) {             if (i == 0)                 return (DESCRIPTION);             else                 return null;         }         public boolean doAccessibleAction(int i) {             if (i == 0) {                 // Simulate clicking on a button.                 ActionExample.this.processActionEvent(new ActionEvent(this,                      ActionEvent.ACTION_PERFORMED,                       ActionExample.this.getActionCommand( )));                 return true;              } else                 return false;         }      }      public static void main(String s[]) {         ActionExampleButton example = new ActionExampleButton( );                  JFrame frame = new JFrame("AccessibleAction Example");         frame.setDefaultCloseOperation(EXIT_ON_CLOSE);         frame.getContentPane( ).add(example, BorderLayout.CENTER);         frame.setSize(100, 100);          frame.setVisible(true);     } } 

The result is somewhat anticlimactic: the example creates a simple button on the screen, and nothing else. If you click on the button, the following output is displayed:

The button was pressed!

However, it is important to know that an assistive technology could incorporate itself into the virtual machine and find out the button's state and description and even cause the button to be clicked. In order to understand more about how this can occur, we need to talk about the accessibility utility classes.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net