This implements the missing AccessibleJButton methods. This is more or
less a verbatim copy of the same methods in AccessibleJLabel. I also
completed the implementation of getCharacterAttribute() which seems to
have been forgotten in the middle of the method.

2006-10-16  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/AbstractButton.java
        (AccessibleJButton.getAfterIndex): Implemented.
        (AccessibleJButton.getAtIndex): Implemented.
        (AccessibleJButton.getBeforeIndex): Implemented.
        (AccessibleJButton.getCharacterAttribute): Completed incomplete
        method implementation.

/Roman

Index: javax/swing/AbstractButton.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/AbstractButton.java,v
retrieving revision 1.68
diff -u -1 -5 -r1.68 AbstractButton.java
--- javax/swing/AbstractButton.java	19 Sep 2006 14:26:43 -0000	1.68
+++ javax/swing/AbstractButton.java	16 Oct 2006 12:23:09 -0000
@@ -25,32 +25,30 @@
 
 As a special exception, the copyright holders of this library give you
 permission to link this library with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on this library.  If you modify this library, you may extend
 this exception to your version of the library, but you are not
 obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version. */
 
 package javax.swing;
 
-import gnu.classpath.NotImplementedException;
-
 import java.awt.Component;
 import java.awt.Graphics;
 import java.awt.Image;
 import java.awt.Insets;
 import java.awt.ItemSelectable;
 import java.awt.LayoutManager;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.Shape;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.awt.event.ItemEvent;
 import java.awt.event.ItemListener;
 import java.awt.image.ImageObserver;
 import java.beans.PropertyChangeEvent;
@@ -62,31 +60,34 @@
 import javax.accessibility.AccessibleAction;
 import javax.accessibility.AccessibleContext;
 import javax.accessibility.AccessibleIcon;
 import javax.accessibility.AccessibleRelation;
 import javax.accessibility.AccessibleRelationSet;
 import javax.accessibility.AccessibleState;
 import javax.accessibility.AccessibleStateSet;
 import javax.accessibility.AccessibleText;
 import javax.accessibility.AccessibleValue;
 import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 import javax.swing.plaf.ButtonUI;
 import javax.swing.plaf.basic.BasicHTML;
 import javax.swing.text.AttributeSet;
 import javax.swing.text.BadLocationException;
+import javax.swing.text.Document;
+import javax.swing.text.Element;
 import javax.swing.text.Position;
+import javax.swing.text.StyledDocument;
 import javax.swing.text.View;
 
 
 /**
  * Provides an abstract implementation of common button behaviour,
  * data model and look &amp; feel.
  *
  * <p>This class is supposed to serve as a base class for
  * several kinds of buttons with similar but non-identical semantics:
  * toggle buttons (radio buttons and checkboxes), simple push buttons,
  * menu items, etc.</p>
  *
  * <p>Buttons have many properties, some of which are stored in this class
  * while others are delegated to the button's model. The following properties
  * are available:</p>
@@ -792,64 +793,176 @@
           charCount = getAccessibleName().length();
         }
       return charCount;
     }
 
     /**
      * This always returns <code>-1</code> since there is no caret in a button.
      *
      * @return <code>-1</code> since there is no caret in a button
      */
     public int getCaretPosition()
     {
       return -1;
     }
 
-    public String getAtIndex(int value0, int value1)
-      throws NotImplementedException
-    {
-      return null; // TODO
+    /**
+     * Returns the character, word or sentence at the specified index. The
+     * <code>part</code> parameter determines what is returned, the character,
+     * word or sentence after the index.
+     *
+     * @param part one of [EMAIL PROTECTED] AccessibleText#CHARACTER},
+     *             [EMAIL PROTECTED] AccessibleText#WORD} or
+     *             [EMAIL PROTECTED] AccessibleText#SENTENCE}, specifying what is returned
+     * @param index the index
+     *
+     * @return the character, word or sentence after <code>index</code>
+     */
+    public String getAtIndex(int part, int index)
+    {
+      String result = "";
+      int startIndex = -1;
+      int endIndex = -1;
+      switch(part)
+        {
+        case AccessibleText.CHARACTER:
+          result = String.valueOf(text.charAt(index));
+          break;
+        case AccessibleText.WORD:
+          startIndex = text.lastIndexOf(' ', index);
+          endIndex = text.indexOf(' ', startIndex + 1);
+          if (endIndex == -1)
+            endIndex = startIndex + 1;
+          result = text.substring(startIndex + 1, endIndex);
+          break;
+        case AccessibleText.SENTENCE:
+        default:
+          startIndex = text.lastIndexOf('.', index);
+          endIndex = text.indexOf('.', startIndex + 1);
+          if (endIndex == -1)
+            endIndex = startIndex + 1;
+          result = text.substring(startIndex + 1, endIndex);
+          break;
+        }
+      return result;
     }
 
-    public String getAfterIndex(int value0, int value1)
-      throws NotImplementedException
-    {
-      return null; // TODO
+    /**
+     * Returns the character, word or sentence after the specified index. The
+     * <code>part</code> parameter determines what is returned, the character,
+     * word or sentence after the index.
+     *
+     * @param part one of [EMAIL PROTECTED] AccessibleText#CHARACTER},
+     *             [EMAIL PROTECTED] AccessibleText#WORD} or
+     *             [EMAIL PROTECTED] AccessibleText#SENTENCE}, specifying what is returned
+     * @param index the index
+     *
+     * @return the character, word or sentence after <code>index</code>
+     */
+    public String getAfterIndex(int part, int index)
+    {
+      String result = "";
+      int startIndex = -1;
+      int endIndex = -1;
+      switch(part)
+        {
+        case AccessibleText.CHARACTER:
+          result = String.valueOf(text.charAt(index + 1));
+          break;
+        case AccessibleText.WORD:
+          startIndex = text.indexOf(' ', index);
+          endIndex = text.indexOf(' ', startIndex + 1);
+          if (endIndex == -1)
+            endIndex = startIndex + 1;
+          result = text.substring(startIndex + 1, endIndex);
+          break;
+        case AccessibleText.SENTENCE:
+        default:
+          startIndex = text.indexOf('.', index);
+          endIndex = text.indexOf('.', startIndex + 1);
+          if (endIndex == -1)
+            endIndex = startIndex + 1;
+          result = text.substring(startIndex + 1, endIndex);
+          break;
+        }
+      return result;
     }
 
-    public String getBeforeIndex(int value0, int value1)
-      throws NotImplementedException
-    {
-      return null; // TODO
+    /**
+     * Returns the character, word or sentence before the specified index. The
+     * <code>part</code> parameter determines what is returned, the character,
+     * word or sentence before the index.
+     *
+     * @param part one of [EMAIL PROTECTED] AccessibleText#CHARACTER},
+     *             [EMAIL PROTECTED] AccessibleText#WORD} or
+     *             [EMAIL PROTECTED] AccessibleText#SENTENCE}, specifying what is returned
+     * @param index the index
+     *
+     * @return the character, word or sentence before <code>index</code>
+     */
+    public String getBeforeIndex(int part, int index)
+    {
+      String result = "";
+      int startIndex = -1;
+      int endIndex = -1;
+      switch(part)
+        {
+        case AccessibleText.CHARACTER:
+          result = String.valueOf(text.charAt(index - 1));
+          break;
+        case AccessibleText.WORD:
+          endIndex = text.lastIndexOf(' ', index);
+          if (endIndex == -1)
+            endIndex = 0;
+          startIndex = text.lastIndexOf(' ', endIndex - 1);
+          result = text.substring(startIndex + 1, endIndex);
+          break;
+        case AccessibleText.SENTENCE:
+        default:
+          endIndex = text.lastIndexOf('.', index);
+          if (endIndex == -1)
+            endIndex = 0;
+          startIndex = text.lastIndexOf('.', endIndex - 1);
+          result = text.substring(startIndex + 1, endIndex);
+          break;
+        }
+      return result;
     }
 
     /**
      * Returns the text attribute for the character at the specified character
      * index.
      *
      * @param i the character index
      *
      * @return the character attributes for the specified character or
      *         <code>null</code> if the character has no attributes
      */
     public AttributeSet getCharacterAttribute(int i)
     {
       AttributeSet atts = null;
       View view = (View) getClientProperty(BasicHTML.propertyKey); 
       if (view != null)
         {
-          
+          Document doc = view.getDocument();
+          if (doc instanceof StyledDocument)
+            {
+              StyledDocument sDoc = (StyledDocument) doc;
+              Element charEl = sDoc.getCharacterElement(i);
+              if (charEl != null)
+                atts = charEl.getAttributes();
+            }
         }
       return atts;
     }
 
     /**
      * This always returns <code>-1</code> since
      * button labels can't be selected.
      *
      * @return <code>-1</code>, button labels can't be selected
      */
     public int getSelectionStart()
     {
       return -1;
     }
 

Reply via email to