This implements two missing methods in AccessibleJLabel. This is
accompanied by Mauve tests to show that this is OK.

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

        * javax/swing/JLabel.java
        (AccessibleJLabel.getIndexAtPoint): Implemented.
        (AccessibleJLabel.getCharacterBounds): Implemented.
        (AccessibleJLabel.getTextRectangle): New helper method.

/Roman

Index: javax/swing/JLabel.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JLabel.java,v
retrieving revision 1.41
diff -u -1 -5 -r1.41 JLabel.java
--- javax/swing/JLabel.java	28 Jun 2006 12:58:40 -0000	1.41
+++ javax/swing/JLabel.java	16 Oct 2006 11:48:18 -0000
@@ -26,48 +26,53 @@
 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.Font;
+import java.awt.FontMetrics;
 import java.awt.Image;
+import java.awt.Insets;
 import java.awt.Point;
 import java.awt.Rectangle;
+import java.awt.Shape;
 import java.awt.event.KeyEvent;
 import java.beans.PropertyChangeEvent;
 
 import javax.accessibility.Accessible;
 import javax.accessibility.AccessibleContext;
 import javax.accessibility.AccessibleExtendedComponent;
 import javax.accessibility.AccessibleRole;
 import javax.accessibility.AccessibleText;
 import javax.swing.plaf.LabelUI;
+import javax.swing.plaf.basic.BasicHTML;
 import javax.swing.text.AttributeSet;
+import javax.swing.text.BadLocationException;
+import javax.swing.text.Position;
 import javax.swing.text.SimpleAttributeSet;
+import javax.swing.text.View;
 
 /**
  * A component that displays a static text message and/or an icon.
  */
 public class JLabel extends JComponent implements Accessible, SwingConstants
 {
 
   /**
    * Provides the accessibility features for the <code>JLabel</code>
    * component.
    */
   protected class AccessibleJLabel
     extends JComponent.AccessibleJComponent
     implements AccessibleText, AccessibleExtendedComponent
   {
@@ -291,50 +296,97 @@
     public int getCharCount()
     {
       // FIXME: Query HTML view for HTML labels.
       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)
-      throws NotImplementedException
     {
-      // FIXME: Implement this correctly.
-      return new Rectangle();
+      Rectangle bounds = null;
+      View view = (View) getClientProperty(BasicHTML.propertyKey);
+      if (view != null)
+        {
+          Rectangle textR = getTextRectangle();
+          try
+            {
+              Shape s = view.modelToView(index, textR, Position.Bias.Forward);
+              bounds = s.getBounds();
+            }
+          catch (BadLocationException ex)
+            {
+              // Can't return something reasonable in this case.
+            }
+        }
+      return bounds;
+    }
+
+    /**
+     * Returns the rectangle inside the JLabel, in which the actual text is
+     * rendered. This method has been adopted from the Mauve testcase
+     * gnu.testlet.javax.swing.JLabel.AccessibleJLabel.getCharacterBounds.
+     *
+     * @return the rectangle inside the JLabel, in which the actual text is
+     *         rendered
+     */
+    private Rectangle getTextRectangle()
+    {
+      JLabel l = JLabel.this;
+      Rectangle textR = new Rectangle();
+      Rectangle iconR = new Rectangle();
+      Insets i = l.getInsets();
+      int w = l.getWidth();
+      int h = l.getHeight();
+      Rectangle viewR = new Rectangle(i.left, i.top, w - i.left - i.right,
+                                      h - i.top - i.bottom);
+      FontMetrics fm = l.getFontMetrics(l.getFont());
+      SwingUtilities.layoutCompoundLabel(l, fm, l.getText(), l.getIcon(),
+                                         l.getVerticalAlignment(),
+                                         l.getHorizontalAlignment(),
+                                         l.getVerticalTextPosition(),
+                                         l.getHorizontalTextPosition(),
+                                         viewR, iconR, textR,
+                                         l.getIconTextGap());
+      return textR;
     }
 
     /**
      * 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)
-      throws NotImplementedException
     {
-      // FIXME: Implement this correctly.
-      return 0;
+      int index = -1;
+      View view = (View) getClientProperty(BasicHTML.propertyKey);
+      if (view != null)
+        {
+          Rectangle r = getTextRectangle();
+          index = view.viewToModel(point.x, point.y, r, new Position.Bias[0]);
+        }
+      return index;
     }
   }
 
   private static final long serialVersionUID = 5496508283662221534L;
 
   static final String LABEL_PROPERTY = "labeledBy";
 
   /**
    * The Component the label will give focus to when its mnemonic is
    * activated.
    */
   protected Component labelFor;
 
   /** The label's text. */
   transient String text;

Reply via email to