classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: Accessibility for JButton and JLabel


From: Roman Kennke
Subject: [cp-patches] FYI: Accessibility for JButton and JLabel
Date: Wed, 20 Jul 2005 14:36:37 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050317)

I added accessibility support for JButton and JLabel.

2005-07-20  Roman Kennke  <address@hidden>

       * javax/swing/JButton.java
       (AccessibleJButton): Added accessibility support for JButton.
       (getAccessibleContext): Implemented to return an AccessibleJButton.
       * javax/swing/JLabel.java
       (AccessibleJLabel): Added accessibility support for JLabel.
       (getAccessibleContext): Implemented to return an AccessibleJLabel.

/Roman

Index: javax/swing/JButton.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JButton.java,v
retrieving revision 1.18
diff -u -r1.18 JButton.java
--- javax/swing/JButton.java    15 Jul 2005 11:41:06 -0000      1.18
+++ javax/swing/JButton.java    20 Jul 2005 12:30:44 -0000
@@ -39,6 +39,7 @@
 
 import javax.accessibility.Accessible;
 import javax.accessibility.AccessibleContext;
+import javax.accessibility.AccessibleRole;
 import javax.swing.plaf.ButtonUI;
 
 
@@ -50,10 +51,32 @@
 public class JButton extends AbstractButton
   implements Accessible
 {
+
+  /**
+   * Accessibility support for JButtons.
+   */
+  protected class AccessibleJButton
+    extends AbstractButton.AccessibleAbstractButton
+  {
+    /**
+     * Returns the accessible role that this component represents.
+     * This is address@hidden AccessibleRole.PUSH_BUTTON} for 
<code>JButton</code>s.
+     *
+     * @return the accessible role that this component represents
+     */
+    public AccessibleRole getAccessibleRole()
+    {
+      return AccessibleRole.PUSH_BUTTON;
+    }
+  }
+
   private static final long serialVersionUID = -1907255238954382202L;
   boolean def;
   boolean is_def;
 
+  /** The AccessibleContext for this JButton. */
+  AccessibleJButton accessibleContext;
+
   public JButton()
   {
     this(null, null);
@@ -96,8 +119,9 @@
 
   public AccessibleContext getAccessibleContext()
   {
-    // Gets the AccessibleContext associated with this JButton. 
-    return null;
+    if (accessibleContext == null)
+      accessibleContext = new AccessibleJButton();
+    return accessibleContext;
   }
 
   public String getUIClassID()
Index: javax/swing/JLabel.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JLabel.java,v
retrieving revision 1.23
diff -u -r1.23 JLabel.java
--- javax/swing/JLabel.java     5 Jul 2005 12:14:43 -0000       1.23
+++ javax/swing/JLabel.java     20 Jul 2005 12:30:44 -0000
@@ -41,17 +41,257 @@
 import java.awt.Component;
 import java.awt.Font;
 import java.awt.Image;
+import java.awt.Point;
+import java.awt.Rectangle;
 import java.awt.event.KeyEvent;
 
 import javax.accessibility.Accessible;
 import javax.accessibility.AccessibleContext;
+import javax.accessibility.AccessibleExtendedComponent;
+import javax.accessibility.AccessibleText;
 import javax.swing.plaf.LabelUI;
+import javax.swing.text.AttributeSet;
+import javax.swing.text.SimpleAttributeSet;
 
 /**
  * A swing widget that displays a text message and/or an icon.
  */
 public class JLabel extends JComponent implements Accessible, SwingConstants
 {
+
+  /**
+   * Accessibility support for JLabel.
+   */
+  protected class AccessibleJLabel
+    extends JComponent.AccessibleJComponent
+    implements AccessibleText, AccessibleExtendedComponent
+  {
+    /**
+     * Returns the selected text. This is an empty string since JLabels
+     * are not selectable.
+     *
+     * @return the selected text
+     */
+    public String getSelectedText()
+    {
+      // We return "" here since JLabel's text is not selectable.
+      return "";
+    }
+
+    /**
+     * Returns the start index of the selected text.
+     *
+     * @return the start index of the selected text
+     */
+    public int getSelectionStart()
+    {
+      // TODO: Figure out what should be returned here, because JLabels don't
+      // allow selection. I guess -1 for now.
+      return -1;
+    }
+
+    /**
+     * Returns the end index of the selected text.
+     *
+     * @return the end index of the selected text
+     */
+    public int getSelectionEnd()
+    {
+      // TODO: Figure out what should be returned here, because JLabels don't
+      // allow selection. I guess -1 for now.
+      return -1;
+    }
+
+    /**
+     * Returns an address@hidden AttributeSet} that reflects the text 
attributes of
+     * the specified character. We return an empty
+     * <code>AttributeSet</code> here, because JLabels don't support text
+     * attributes (at least not yet).
+     *
+     * @param index the index of the character
+     *
+     * @return an address@hidden AttributeSet} that reflects the text 
attributes of
+     *         the specified character
+     */
+    public AttributeSet getCharacterAttribute(int index)
+    {
+      return new SimpleAttributeSet();
+    }
+
+    /**
+     * 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 address@hidden AccessibleText.CHARACTER},
+     *             address@hidden AccessibleText.WORD} or
+     *             address@hidden 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;
+    }
+
+    /**
+     * 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 address@hidden AccessibleText.CHARACTER},
+     *             address@hidden AccessibleText.WORD} or
+     *             address@hidden 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;
+    }
+
+    /**
+     * 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 address@hidden AccessibleText.CHARACTER},
+     *             address@hidden AccessibleText.WORD} or
+     *             address@hidden 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 caret position. This method returns -1 because JLabel don't
+     * have a caret.
+     *
+     * @return the caret position
+     */
+    public int getCaretPosition()
+    {
+      return -1;
+    }
+
+    /**
+     * Returns the number of characters that are displayed by the JLabel.
+     *
+     * @return the number of characters that are displayed by the JLabel
+     */
+    public int getCharCount()
+    {
+      return text.length();
+    }
+
+    /**
+     * Returns the bounding box of the character at the specified index.
+     *
+     * @param index the index of the character that we return the
+     *        bounds for
+     *
+     * @return the bounding box of the character at the specified index
+     */
+    public Rectangle getCharacterBounds(int index)
+    {
+      // FIXME: Implement this correctly.
+      return new Rectangle();
+    }
+
+    /**
+     * Returns the index of the character that is located at the specified
+     * point.
+     *
+     * @param point the location that we lookup the character for
+     *
+     * @return the index of the character that is located at the specified
+     *         point
+     */
+    public int getIndexAtPoint(Point point)
+    {
+      // FIXME: Implement this correctly.
+      return 0;
+    }
+  }
+
   /** DOCUMENT ME! */
   private static final long serialVersionUID = 5496508283662221534L;
 
@@ -62,7 +302,7 @@
   protected Component labelFor;
 
   /** The label's text. */
-  private transient String text;
+  transient String text;
 
   /** Where the label will be positioned horizontally. */
   private transient int horizontalAlignment = LEADING;
@@ -91,6 +331,9 @@
   /** The gap between the icon and the text. */
   private transient int iconTextGap = 4;
 
+  /** The accessible context for this JLabel. */
+  private AccessibleJLabel accessibleContext;
+
   /**
    * Creates a new vertically centered, horizontally on the leading edge
    * JLabel object with text and no icon.
@@ -642,6 +885,8 @@
    */
   public AccessibleContext getAccessibleContext()
   {
-    return null;
+    if (accessibleContext == null)
+      accessibleContext = new AccessibleJLabel();
+    return accessibleContext;
   }
 }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]