Hi,

I implemented the last missing methods in javax.swing.text.Utilities.
These should then be used by the text components to place the caret when
moving up or down using the arrow keys.

I had to add an abstract method to View, and an implementation of this to
all existing View implementations. Unfortunatly this introduces a couple
of new stubs, since I didn't want to bother implementing them all at
once (it's a difficult method). I have inserted a TODO tag and let them
throw an AssertionError, so we get clear results when using these
methods.

2005-11-02  Roman Kennke  <[EMAIL PROTECTED]>

        * javax/swing/text/Utilities.java
        (getPositionAbove): New utility method.
        (getPositionBelow): New utility method.
        (getParagraphElement): Special case for StyledDocuments.
        * javax/swing/text/View.java
        (getNextVisualPositionFrom): New abstract method.
        * javax/swing/text/ComponentView.java
        (getNextVisualPositionFrom): New method.
        * javax/swing/text/CompositeView.java
        (getNextVisualPositionFrom): New method.
        * javax/swing/text/FlowView.java
        (LogicalView.getNextVisualPositionFrom): New method.
        * javax/swing/text/GlyphView.java
        (getNextVisualPositionFrom): New method.
        * javax/swing/text/IconView.java
        (getNextVisualPositionFrom): New method.
        * javax/swing/text/PlainView.java
        (getNextVisualPositionFrom): New method.
        * javax/swing/text/WrappedPlainView.java
        (WrappedLine.getNextVisualPositionFrom): New method.

/Roman
Index: javax/swing/text/ComponentView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/ComponentView.java,v
retrieving revision 1.11
diff -u -r1.11 ComponentView.java
--- javax/swing/text/ComponentView.java	19 Oct 2005 14:57:30 -0000	1.11
+++ javax/swing/text/ComponentView.java	3 Nov 2005 14:08:26 -0000
@@ -41,6 +41,8 @@
 import java.awt.Graphics;
 import java.awt.Shape;
 
+import javax.swing.SwingConstants;
+
 /**
  * A [EMAIL PROTECTED] View} implementation that is able to render arbitrary
  * [EMAIL PROTECTED] Component}s. This uses the attribute
@@ -175,5 +177,35 @@
   {
     // FIXME: Implement this properly.
     return 0;
+  }
+
+  /**
+   * Returns the document position that is (visually) nearest to the given
+   * document position <code>pos</code> in the given direction <code>d</code>.
+   *
+   * @param c the text component
+   * @param pos the document position
+   * @param b the bias for <code>pos</code>
+   * @param d the direction, must be either [EMAIL PROTECTED] SwingConstants#NORTH},
+   *        [EMAIL PROTECTED] SwingConstants#SOUTH}, [EMAIL PROTECTED] SwingConstants#WEST} or
+   *        [EMAIL PROTECTED] SwingConstants#EAST}
+   * @param biasRet an array of [EMAIL PROTECTED] Position.Bias} that can hold at least
+   *        one element, which is filled with the bias of the return position
+   *        on method exit
+   *
+   * @return the document position that is (visually) nearest to the given
+   *         document position <code>pos</code> in the given direction
+   *         <code>d</code>
+   *
+   * @throws BadLocationException if <code>pos</code> is not a valid offset in
+   *         the document model
+   */
+  public int getNextVisualPositionFrom(JTextComponent c, int pos,
+                                       Position.Bias b, int d,
+                                       Position.Bias[] biasRet)
+    throws BadLocationException
+  {
+    // TODO: Implement this properly.
+    throw new AssertionError("Not implemented yet.");
   }
 }
Index: javax/swing/text/CompositeView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/CompositeView.java,v
retrieving revision 1.9
diff -u -r1.9 CompositeView.java
--- javax/swing/text/CompositeView.java	25 Oct 2005 18:54:17 -0000	1.9
+++ javax/swing/text/CompositeView.java	3 Nov 2005 14:08:26 -0000
@@ -658,4 +658,34 @@
   {
     return false;
   }
+
+  /**
+   * Returns the document position that is (visually) nearest to the given
+   * document position <code>pos</code> in the given direction <code>d</code>.
+   *
+   * @param c the text component
+   * @param pos the document position
+   * @param b the bias for <code>pos</code>
+   * @param d the direction, must be either [EMAIL PROTECTED] SwingConstants#NORTH},
+   *        [EMAIL PROTECTED] SwingConstants#SOUTH}, [EMAIL PROTECTED] SwingConstants#WEST} or
+   *        [EMAIL PROTECTED] SwingConstants#EAST}
+   * @param biasRet an array of [EMAIL PROTECTED] Position.Bias} that can hold at least
+   *        one element, which is filled with the bias of the return position
+   *        on method exit
+   *
+   * @return the document position that is (visually) nearest to the given
+   *         document position <code>pos</code> in the given direction
+   *         <code>d</code>
+   *
+   * @throws BadLocationException if <code>pos</code> is not a valid offset in
+   *         the document model
+   */
+  public int getNextVisualPositionFrom(JTextComponent c, int pos,
+                                       Position.Bias b, int d,
+                                       Position.Bias[] biasRet)
+    throws BadLocationException
+  {
+    // TODO: Implement this properly.
+    throw new AssertionError("Not implemented yet.");
+  }
 }
Index: javax/swing/text/FlowView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/FlowView.java,v
retrieving revision 1.4
diff -u -r1.4 FlowView.java
--- javax/swing/text/FlowView.java	19 Oct 2005 14:57:31 -0000	1.4
+++ javax/swing/text/FlowView.java	3 Nov 2005 14:08:26 -0000
@@ -45,6 +45,7 @@
 import java.util.Iterator;
 import java.util.Vector;
 
+import javax.swing.SwingConstants;
 import javax.swing.event.DocumentEvent;
 
 /**
@@ -394,6 +395,37 @@
     {
       throw new AssertionError("This method must not be called in "
                                + "LogicalView.");
+    }
+
+    /**
+     * Returns the document position that is (visually) nearest to the given
+     * document position <code>pos</code> in the given direction <code>d</code>.
+     *
+     * @param c the text component
+     * @param pos the document position
+     * @param b the bias for <code>pos</code>
+     * @param d the direction, must be either [EMAIL PROTECTED] SwingConstants#NORTH},
+     *        [EMAIL PROTECTED] SwingConstants#SOUTH}, [EMAIL PROTECTED] SwingConstants#WEST} or
+     *        [EMAIL PROTECTED] SwingConstants#EAST}
+     * @param biasRet an array of [EMAIL PROTECTED] Position.Bias} that can hold at least
+     *        one element, which is filled with the bias of the return position
+     *        on method exit
+     *
+     * @return the document position that is (visually) nearest to the given
+     *         document position <code>pos</code> in the given direction
+     *         <code>d</code>
+     *
+     * @throws BadLocationException if <code>pos</code> is not a valid offset in
+     *         the document model
+     */
+    public int getNextVisualPositionFrom(JTextComponent c, int pos,
+                                         Position.Bias b, int d,
+                                         Position.Bias[] biasRet)
+      throws BadLocationException
+    {
+      assert false : "getNextVisualPositionFrom() must not be called in "
+        + "LogicalView";
+      return 0;
     }
   }
 
Index: javax/swing/text/GlyphView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/GlyphView.java,v
retrieving revision 1.10
diff -u -r1.10 GlyphView.java
--- javax/swing/text/GlyphView.java	30 Oct 2005 18:02:53 -0000	1.10
+++ javax/swing/text/GlyphView.java	3 Nov 2005 14:08:26 -0000
@@ -1060,4 +1060,34 @@
     return painter.getNextVisualPositionFrom(this, pos, bias, a, direction,
                                              biasRet);
   }
+
+  /**
+   * Returns the document position that is (visually) nearest to the given
+   * document position <code>pos</code> in the given direction <code>d</code>.
+   *
+   * @param c the text component
+   * @param pos the document position
+   * @param b the bias for <code>pos</code>
+   * @param d the direction, must be either [EMAIL PROTECTED] SwingConstants#NORTH},
+   *        [EMAIL PROTECTED] SwingConstants#SOUTH}, [EMAIL PROTECTED] SwingConstants#WEST} or
+   *        [EMAIL PROTECTED] SwingConstants#EAST}
+   * @param biasRet an array of [EMAIL PROTECTED] Position.Bias} that can hold at least
+   *        one element, which is filled with the bias of the return position
+   *        on method exit
+   *
+   * @return the document position that is (visually) nearest to the given
+   *         document position <code>pos</code> in the given direction
+   *         <code>d</code>
+   *
+   * @throws BadLocationException if <code>pos</code> is not a valid offset in
+   *         the document model
+   */
+  public int getNextVisualPositionFrom(JTextComponent c, int pos,
+                                       Position.Bias b, int d,
+                                       Position.Bias[] biasRet)
+    throws BadLocationException
+  {
+    // TODO: Implement this properly.
+    throw new AssertionError("Not implemented yet.");
+  }
 }
Index: javax/swing/text/IconView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/IconView.java,v
retrieving revision 1.4
diff -u -r1.4 IconView.java
--- javax/swing/text/IconView.java	22 Aug 2005 11:35:23 -0000	1.4
+++ javax/swing/text/IconView.java	3 Nov 2005 14:08:26 -0000
@@ -41,6 +41,8 @@
 import java.awt.Graphics;
 import java.awt.Shape;
 
+import javax.swing.SwingConstants;
+
 // TODO: Implement this class.
 public class IconView
   extends View
@@ -124,5 +126,35 @@
   {
     // FIXME: not implemented
     return 0;
+  }
+
+  /**
+   * Returns the document position that is (visually) nearest to the given
+   * document position <code>pos</code> in the given direction <code>d</code>.
+   *
+   * @param c the text component
+   * @param pos the document position
+   * @param b the bias for <code>pos</code>
+   * @param d the direction, must be either [EMAIL PROTECTED] SwingConstants#NORTH},
+   *        [EMAIL PROTECTED] SwingConstants#SOUTH}, [EMAIL PROTECTED] SwingConstants#WEST} or
+   *        [EMAIL PROTECTED] SwingConstants#EAST}
+   * @param biasRet an array of [EMAIL PROTECTED] Position.Bias} that can hold at least
+   *        one element, which is filled with the bias of the return position
+   *        on method exit
+   *
+   * @return the document position that is (visually) nearest to the given
+   *         document position <code>pos</code> in the given direction
+   *         <code>d</code>
+   *
+   * @throws BadLocationException if <code>pos</code> is not a valid offset in
+   *         the document model
+   */
+  public int getNextVisualPositionFrom(JTextComponent c, int pos,
+                                       Position.Bias b, int d,
+                                       Position.Bias[] biasRet)
+    throws BadLocationException
+  {
+    // TODO: Implement this properly.
+    throw new AssertionError("Not implemented yet.");
   }
 }
Index: javax/swing/text/PlainView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/PlainView.java,v
retrieving revision 1.29
diff -u -r1.29 PlainView.java
--- javax/swing/text/PlainView.java	30 Oct 2005 22:03:50 -0000	1.29
+++ javax/swing/text/PlainView.java	3 Nov 2005 14:08:26 -0000
@@ -46,6 +46,7 @@
 import java.awt.Rectangle;
 import java.awt.Shape;
 
+import javax.swing.SwingConstants;
 import javax.swing.event.DocumentEvent;
 import javax.swing.event.DocumentEvent.ElementChange;
 
@@ -528,6 +529,36 @@
     if (lineBuffer == null)
       lineBuffer = new Segment();
     return lineBuffer;
+  }
+
+  /**
+   * Returns the document position that is (visually) nearest to the given
+   * document position <code>pos</code> in the given direction <code>d</code>.
+   *
+   * @param c the text component
+   * @param pos the document position
+   * @param b the bias for <code>pos</code>
+   * @param d the direction, must be either [EMAIL PROTECTED] SwingConstants#NORTH},
+   *        [EMAIL PROTECTED] SwingConstants#SOUTH}, [EMAIL PROTECTED] SwingConstants#WEST} or
+   *        [EMAIL PROTECTED] SwingConstants#EAST}
+   * @param biasRet an array of [EMAIL PROTECTED] Position.Bias} that can hold at least
+   *        one element, which is filled with the bias of the return position
+   *        on method exit
+   *
+   * @return the document position that is (visually) nearest to the given
+   *         document position <code>pos</code> in the given direction
+   *         <code>d</code>
+   *
+   * @throws BadLocationException if <code>pos</code> is not a valid offset in
+   *         the document model
+   */
+  public int getNextVisualPositionFrom(JTextComponent c, int pos,
+                                       Position.Bias b, int d,
+                                       Position.Bias[] biasRet)
+    throws BadLocationException
+  {
+    // TODO: Implement this properly.
+    throw new AssertionError("Not implemented yet.");
   }
 }
 
Index: javax/swing/text/Utilities.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/Utilities.java,v
retrieving revision 1.17
diff -u -r1.17 Utilities.java
--- javax/swing/text/Utilities.java	3 Nov 2005 11:19:29 -0000	1.17
+++ javax/swing/text/Utilities.java	3 Nov 2005 14:08:26 -0000
@@ -40,8 +40,12 @@
 
 import java.awt.FontMetrics;
 import java.awt.Graphics;
+import java.awt.Point;
+import java.awt.Rectangle;
 import java.text.BreakIterator;
 
+import javax.swing.SwingConstants;
+
 /**
  * A set of utilities to deal with text. This is used by several other classes
  * inside this package.
@@ -532,10 +536,73 @@
    *
    * @return the paragraph element at <code>offset</code>
    */
-  public static Element getParagraphElement(JTextComponent c, int offset)
+  public static final Element getParagraphElement(JTextComponent c, int offset)
+  {
+    Document doc = c.getDocument();
+    Element par = null;
+    if (doc instanceof StyledDocument)
+      {
+        StyledDocument styledDoc = (StyledDocument) doc;
+        par = styledDoc.getParagraphElement(offset);
+      }
+    else
+      {
+        Element root = c.getDocument().getDefaultRootElement();
+        int parIndex = root.getElementIndex(offset);
+        par = root.getElement(parIndex);
+      }
+    return par;
+  }
+
+  /**
+   * Returns the document position that is closest above to the specified x
+   * coordinate in the row containing <code>offset</code>.
+   *
+   * @param c the text component
+   * @param offset the offset
+   * @param x the x coordinate
+   *
+   * @return  the document position that is closest above to the specified x
+   *          coordinate in the row containing <code>offset</code>
+   *
+   * @throws BadLocationException if <code>offset</code> is not a valid offset
+   */
+  public static final int getPositionAbove(JTextComponent c, int offset, int x)
+    throws BadLocationException
   {
-    Element root = c.getDocument().getDefaultRootElement();
-    int parIndex = root.getElementIndex(offset);
-    return root.getElement(parIndex);
+    View rootView = c.getUI().getRootView(c);
+    Rectangle r = c.modelToView(offset);
+    int offs = c.viewToModel(new Point(x, r.y));
+    int pos = rootView.getNextVisualPositionFrom(c, offs,
+                                                 Position.Bias.Forward,
+                                                 SwingConstants.NORTH,
+                                                 new Position.Bias[1]);
+    return pos;
+  }
+
+  /**
+   * Returns the document position that is closest below to the specified x
+   * coordinate in the row containing <code>offset</code>.
+   *
+   * @param c the text component
+   * @param offset the offset
+   * @param x the x coordinate
+   *
+   * @return  the document position that is closest above to the specified x
+   *          coordinate in the row containing <code>offset</code>
+   *
+   * @throws BadLocationException if <code>offset</code> is not a valid offset
+   */
+  public static final int getPositionBelow(JTextComponent c, int offset, int x)
+    throws BadLocationException
+  {
+    View rootView = c.getUI().getRootView(c);
+    Rectangle r = c.modelToView(offset);
+    int offs = c.viewToModel(new Point(x, r.y));
+    int pos = rootView.getNextVisualPositionFrom(c, offs,
+                                                 Position.Bias.Forward,
+                                                 SwingConstants.SOUTH,
+                                                 new Position.Bias[1]);
+    return pos;
   }
 }
Index: javax/swing/text/View.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/View.java,v
retrieving revision 1.22
diff -u -r1.22 View.java
--- javax/swing/text/View.java	10 Oct 2005 19:30:26 -0000	1.22
+++ javax/swing/text/View.java	3 Nov 2005 14:08:27 -0000
@@ -594,4 +594,30 @@
     for (int i = 0; i < count; ++i)
       getView(i).dump(indent + 1);
   }
+
+  /**
+   * Returns the document position that is (visually) nearest to the given
+   * document position <code>pos</code> in the given direction <code>d</code>.
+   *
+   * @param c the text component
+   * @param pos the document position
+   * @param b the bias for <code>pos</code>
+   * @param d the direction, must be either [EMAIL PROTECTED] SwingConstants#NORTH},
+   *        [EMAIL PROTECTED] SwingConstants#SOUTH}, [EMAIL PROTECTED] SwingConstants#WEST} or
+   *        [EMAIL PROTECTED] SwingConstants#EAST}
+   * @param biasRet an array of [EMAIL PROTECTED] Position.Bias} that can hold at least
+   *        one element, which is filled with the bias of the return position
+   *        on method exit
+   *
+   * @return the document position that is (visually) nearest to the given
+   *         document position <code>pos</code> in the given direction
+   *         <code>d</code>
+   *
+   * @throws BadLocationException if <code>pos</code> is not a valid offset in
+   *         the document model
+   */
+  public abstract int getNextVisualPositionFrom(JTextComponent c, int pos,
+                                                Position.Bias b, int d,
+                                                Position.Bias[] biasRet)
+    throws BadLocationException;
 }
Index: javax/swing/text/WrappedPlainView.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/text/WrappedPlainView.java,v
retrieving revision 1.4
diff -u -r1.4 WrappedPlainView.java
--- javax/swing/text/WrappedPlainView.java	2 Nov 2005 19:07:01 -0000	1.4
+++ javax/swing/text/WrappedPlainView.java	3 Nov 2005 14:08:27 -0000
@@ -45,6 +45,7 @@
 import java.awt.Rectangle;
 import java.awt.Shape;
 
+import javax.swing.SwingConstants;
 import javax.swing.event.DocumentEvent;
 import javax.swing.text.Position.Bias;
 
@@ -544,5 +545,35 @@
             currLineStart = currLineEnd;
         }
     }    
+
+    /**
+     * Returns the document position that is (visually) nearest to the given
+     * document position <code>pos</code> in the given direction <code>d</code>.
+     *
+     * @param c the text component
+     * @param pos the document position
+     * @param b the bias for <code>pos</code>
+     * @param d the direction, must be either [EMAIL PROTECTED] SwingConstants#NORTH},
+     *        [EMAIL PROTECTED] SwingConstants#SOUTH}, [EMAIL PROTECTED] SwingConstants#WEST} or
+     *        [EMAIL PROTECTED] SwingConstants#EAST}
+     * @param biasRet an array of [EMAIL PROTECTED] Position.Bias} that can hold at least
+     *        one element, which is filled with the bias of the return position
+     *        on method exit
+     *
+     * @return the document position that is (visually) nearest to the given
+     *         document position <code>pos</code> in the given direction
+     *         <code>d</code>
+     *
+     * @throws BadLocationException if <code>pos</code> is not a valid offset in
+     *         the document model
+     */
+    public int getNextVisualPositionFrom(JTextComponent c, int pos,
+                                         Position.Bias b, int d,
+                                         Position.Bias[] biasRet)
+      throws BadLocationException
+    {
+      // TODO: Implement this properly.
+      throw new AssertionError("Not implemented yet.");
+    }
   }
 }
_______________________________________________
Classpath-patches mailing list
Classpath-patches@gnu.org
http://lists.gnu.org/mailman/listinfo/classpath-patches

Reply via email to