[gui] Add AccessibleAWTTextComponent

Jerry Quinn jlquinn@optonline.net
Sun Nov 7 16:45:00 GMT 2004


There's some stuff here dealing with attributes and property changes that I'm 
not sure about, but I think the rest should be OK.

2004-11-07  Jerry Quinn  <jlquinn@optonline.net>

	* java/awt/TextComponent.java (AccessibleAWTTextComponent):
	Implement.
	(getIndexAtPoint, getCharacterBounds): New methods.

Index: TextComponent.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/awt/TextComponent.java,v
retrieving revision 1.11.8.2
diff -u -r1.11.8.2 TextComponent.java
--- TextComponent.java	24 Jun 2004 05:30:16 -0000	1.11.8.2
+++ TextComponent.java	7 Nov 2004 16:42:49 -0000
@@ -42,8 +42,16 @@
  import java.awt.event.TextListener;
  import java.awt.peer.TextComponentPeer;
  import java.io.Serializable;
+import java.text.BreakIterator;
  import java.util.EventListener;

+import javax.accessibility.Accessible;
+import javax.accessibility.AccessibleRole;
+import javax.accessibility.AccessibleState;
+import javax.accessibility.AccessibleStateSet;
+import javax.accessibility.AccessibleText;
+import javax.swing.text.AttributeSet;
+
  /**
    * This class provides common functionality for widgets than
    * contain text.
@@ -51,7 +59,7 @@
    * @author Aaron M. Renn (arenn@urbanophile.com)
    */
  public class TextComponent extends Component
-  implements Serializable
+  implements Serializable, Accessible
  {

  /*
@@ -90,6 +98,219 @@
    */
  protected transient TextListener textListener;

+  protected class AccessibleAWTTextComponent
+    extends AccessibleAWTComponent
+    implements AccessibleText, TextListener
+  {
+    // Constructor
+    // Adds a listener for tracking caret changes
+    public AccessibleAWTTextComponent()
+    {
+      TextComponent.this.addTextListener(this);
+    }
+
+    public AccessibleRole getAccessibleRole()
+    {
+      return AccessibleRole.TEXT;
+    }
+
+    public AccessibleStateSet getAccessibleStateSet()
+    {
+      // TODO: Docs say PropertyChangeEvent will fire if this state changes.
+      // That means that the event has to fire when editable changes.
+      AccessibleStateSet ss = super.getAccessibleStateSet();
+      if (editable)
+        ss.add(AccessibleState.EDITABLE);
+      return ss;
+    }
+
+    public AccessibleText getAccessibleText()
+    {
+      return this;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getIndexAtPoint(java.awt.Point)
+     */
+    public int getIndexAtPoint(Point point)
+    {
+      return TextComponent.this.getIndexAtPoint(point);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getCharacterBounds(int)
+     */
+    public Rectangle getCharacterBounds(int index)
+    {
+      return TextComponent.this.getCharacterBounds(index);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getCharCount()
+     */
+    public int getCharCount()
+    {
+      return text.length();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getCaretPosition()
+     */
+    public int getCaretPosition()
+    {
+      return TextComponent.this.getCaretPosition();
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getAtIndex(int, int)
+     */
+    public String getAtIndex(int part, int index)
+    {
+      if (index < 0 || index >= text.length())
+        return null;
+      BreakIterator it = null;
+      switch (part)
+      {
+      	case CHARACTER:
+      	  return text.substring(index, index + 1);
+      	case WORD:
+      	  it = BreakIterator.getWordInstance();
+      	  break;
+      	case SENTENCE:
+      	  it = BreakIterator.getSentenceInstance();
+      	  break;
+      	default:
+      	  return null;
+      }
+  	  it.setText(text);
+  	  int start = index;
+  	  if (!it.isBoundary(index))
+  	    start = it.preceding(index);
+  	  int end = it.following(index);
+  	  if (end == -1)
+  	    return text.substring(index);
+  	  else
+  	    return text.substring(index, end);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getAfterIndex(int, int)
+     */
+    public String getAfterIndex(int part, int index) {
+      if (index < 0 || index >= text.length())
+        return null;
+      BreakIterator it = null;
+      switch (part)
+      {
+      	case CHARACTER:
+      	  return text.substring(index, index + 1);
+      	case WORD:
+      	  it = BreakIterator.getWordInstance();
+      	  break;
+      	case SENTENCE:
+      	  it = BreakIterator.getSentenceInstance();
+      	  break;
+      	default:
+      	  return null;
+      }
+  	  it.setText(text);
+  	  int start = index;
+  	  if (!it.isBoundary(index))
+  	    start = it.following(index);
+  	  // Make sure there was a complete unit.  I.e. if index is in the middle
+  	  // of a word, return null if there is no word after the that one.
+  	  if (start == -1)
+  	    return null;
+  	  int end = it.following(start);
+  	  if (end == -1)
+  	    return text.substring(index);
+  	  else
+  	    return text.substring(index, end);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getBeforeIndex(int, int)
+     */
+    public String getBeforeIndex(int part, int index)
+    {
+      if (index < 1 || index >= text.length())
+        return null;
+      BreakIterator it = null;
+      switch (part)
+      {
+      	case CHARACTER:
+      	  return text.substring(index - 1, index);
+      	case WORD:
+      	  it = BreakIterator.getWordInstance();
+      	  break;
+      	case SENTENCE:
+      	  it = BreakIterator.getSentenceInstance();
+      	  break;
+      	default:
+      	  return null;
+      }
+  	  it.setText(text);
+  	  int end = index;
+  	  if (!it.isBoundary(index))
+  	    end = it.preceding(index);
+  	  // Make sure there was a complete unit.  I.e. if index is in the middle
+  	  // of a word, return null if there is no word before that one.
+  	  if (end == -1)
+  	    return null;
+  	  int start = it.preceding(end);
+  	  if (start == -1)
+  	    return text.substring(0, end);
+  	  else
+  	    return text.substring(start, end);
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getCharacterAttribute(int)
+     */
+    public AttributeSet getCharacterAttribute(int index)
+    {
+      // FIXME: I suspect this really gets filled in by subclasses.
+      return null;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getSelectionStart()
+     */
+    public int getSelectionStart() {
+      // TODO Auto-generated method stub
+      return selectionStart;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getSelectionEnd()
+     */
+    public int getSelectionEnd()
+    {
+      return selectionEnd;
+    }
+
+    /* (non-Javadoc)
+     * @see javax.accessibility.AccessibleText#getSelectedText()
+     */
+    public String getSelectedText()
+    {
+      if (selectionEnd - selectionStart > 0)
+        return text.substring(selectionStart, selectionEnd);
+      else
+        return null;
+    }
+
+    /* (non-Javadoc)
+     * @see 
java.awt.event.TextListener#textValueChanged(java.awt.event.TextEvent)
+     */
+    public void textValueChanged(TextEvent event)
+    {
+      // TODO Auto-generated method stub
+
+    }
+
+  }
+
  /*************************************************************************/

  /*
@@ -468,5 +689,30 @@
    {
      return (TextListener[]) getListeners (TextListener.class);
    }
+
+  /*******************************/
+  // Provide AccessibleAWTTextComponent access to several peer functions that
+  // aren't publicly exposed.
+  private synchronized int
+  getIndexAtPoint(Point p)
+  {
+    TextComponentPeer tcp = (TextComponentPeer)getPeer();
+    if (tcp != null)
+      return tcp.getIndexAtPoint(p.x, p.y);
+    return -1;
+  }
+
+  private synchronized Rectangle
+  getCharacterBounds(int i)
+  {
+    TextComponentPeer tcp = (TextComponentPeer)getPeer();
+    if (tcp != null)
+      return tcp.getCharacterBounds(i);
+    return null;
+  }
+
+
+
+
  } // class TextComponent




More information about the Java-patches mailing list