OSDN Git Service

2006-08-14 Mark Wielaard <mark@klomp.org>
[pf3gnuchains/gcc-fork.git] / libjava / classpath / javax / swing / text / JTextComponent.java
index 9de151d..6da84bf 100644 (file)
@@ -42,6 +42,7 @@ import gnu.classpath.NotImplementedException;
 
 import java.awt.AWTEvent;
 import java.awt.Color;
+import java.awt.Container;
 import java.awt.Dimension;
 import java.awt.Insets;
 import java.awt.Point;
@@ -90,9 +91,10 @@ public abstract class JTextComponent extends JComponent
   implements Scrollable, Accessible
 {
   /**
-   * This class implements accessibility support for the JTextComponent class. 
-   * It provides an implementation of the Java Accessibility API appropriate 
-   * to menu user-interface elements.
+   * AccessibleJTextComponent implements accessibility hooks for
+   * JTextComponent.  It allows an accessibility driver to read and
+   * manipulate the text component's contents as well as update UI
+   * elements such as the caret.
    */
   public class AccessibleJTextComponent extends AccessibleJComponent implements
       AccessibleText, CaretListener, DocumentListener, AccessibleAction,
@@ -100,15 +102,18 @@ public abstract class JTextComponent extends JComponent
   {
     private static final long serialVersionUID = 7664188944091413696L;
 
-    /** The caret's offset. */
+    /**
+     * The caret's offset.
+     */
     int dot = 0;
-    
-    /** The current JTextComponent. */
+
+    /**
+     * The current JTextComponent.
+     */
     JTextComponent textComp = JTextComponent.this;
-    
+
     /**
-     * Constructs an AccessibleJTextComponent. 
-     * Adds a listener to track caret change.
+     * Construct an AccessibleJTextComponent.
      */
     public AccessibleJTextComponent()
     {
@@ -117,11 +122,10 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Returns the zero-based offset of the caret. Note: The character 
-     * to the right of the caret will have the same index value as the 
-     * offset (the caret is between two characters).
-     * 
-     * @return offset of caret
+     * Retrieve the current caret position.  The index of the first
+     * caret position is 0.
+     *
+     * @return caret position
      */
     public int getCaretPosition()
     {
@@ -130,9 +134,10 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Returns the portion of the text that is selected.
-     * 
-     * @return null if no text is selected.
+     * Retrieve the current text selection.  If no text is selected
+     * this method returns null.
+     *
+     * @return the currently selected text or null
      */
     public String getSelectedText()
     {
@@ -140,11 +145,14 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Returns the start offset within the selected text. If there is no 
-     * selection, but there is a caret, the start and end offsets will be 
-     * the same. Return 0 if the text is empty, or the caret position if no selection.
-     * 
-     * @return index of the start of the text >= 0.
+     * Retrieve the index of the first character in the current text
+     * selection.  If there is no text in the text component, this
+     * method returns 0.  If there is text in the text component, but
+     * there is no selection, this method returns the current caret
+     * position.
+     *
+     * @return the index of the first character in the selection, the
+     * current caret position or 0
      */
     public int getSelectionStart()
     {
@@ -154,12 +162,14 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Returns the end offset within the selected text. If there is no 
-     * selection, but there is a caret, the start and end offsets will 
-     * be the same. Return 0 if the text is empty, or the caret position
-     * if no selection.
-     * 
-     * @return index of the end of the text >= 0.
+     * Retrieve the index of the last character in the current text
+     * selection.  If there is no text in the text component, this
+     * method returns 0.  If there is text in the text component, but
+     * there is no selection, this method returns the current caret
+     * position.
+     *
+     * @return the index of the last character in the selection, the
+     * current caret position or 0
      */
     public int getSelectionEnd()
     {
@@ -169,13 +179,10 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Handles caret updates (fire appropriate property change event, which are 
-     * AccessibleContext.ACCESSIBLE_CARET_PROPERTY and 
-     * AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY). This keeps track of 
-     * the dot position internally. When the caret moves, the internal position 
-     * is updated after firing the event.
-     * 
-     * @param e - caret event
+     * Handle a change in the caret position and fire any applicable
+     * property change events.
+     *
+     * @param e - the caret update event
      */
     public void caretUpdate(CaretEvent e)
       throws NotImplementedException
@@ -185,7 +192,7 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Returns the accessible state set of this component.
+     * Retreive the accessible state set of this component.
      *
      * @return the accessible state set of this component
      */
@@ -198,7 +205,7 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Returns the accessible role of this component.
+     * Retrieve the accessible role of this component.
      *
      * @return the accessible role of this component
      *
@@ -210,20 +217,19 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Returns the AccessibleEditableText interface for this text component.
-     * 
+     * Retrieve an AccessibleEditableText object that controls this
+     * text component.
+     *
      * @return this
      */
     public AccessibleEditableText getAccessibleEditableText()
     {
       return this;
     }
-    
+
     /**
-     * Get the AccessibleText associated with this object. In the implementation 
-     * of the Java Accessibility API for this class, return this object, 
-     * which is responsible for implementing the AccessibleText interface on 
-     * behalf of itself.
+     * Retrieve an AccessibleText object that controls this text
+     * component.
      *
      * @return this
      *
@@ -235,10 +241,11 @@ public abstract class JTextComponent extends JComponent
     }
     
     /**
-     * Insert update. Fire appropriate property change event which 
-     * is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY.
-     * 
-     * @param e - document event
+     * Handle a text insertion event and fire an
+     * AccessibleContext.ACCESSIBLE_TEXT_PROPERTY property change
+     * event.
+     *
+     * @param e - the insertion event
      */
     public void insertUpdate(DocumentEvent e)
       throws NotImplementedException
@@ -247,10 +254,11 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Remove update. Fire appropriate property change event which 
-     * is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY.
-     * 
-     * @param e - document event
+     * Handle a text removal event and fire an
+     * AccessibleContext.ACCESSIBLE_TEXT_PROPERTY property change
+     * event.
+     *
+     * @param e - the removal event
      */
     public void removeUpdate(DocumentEvent e)
       throws NotImplementedException
@@ -259,10 +267,11 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Changed update. Fire appropriate property change event which 
-     * is AccessibleContext.ACCESSIBLE_TEXT_PROPERTY.
-     * 
-     * @param e - document event
+     * Handle a text change event and fire an
+     * AccessibleContext.ACCESSIBLE_TEXT_PROPERTY property change
+     * event.
+     *
+     * @param e - text change event
      */
     public void changedUpdate(DocumentEvent e)
       throws NotImplementedException
@@ -271,11 +280,13 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Given a point in the coordinate system of this object, return the
-     * 0-based index of the character at that point, or -1 if there is none.
+     * Get the index of the character at the given point, in component
+     * pixel co-ordinates.  If the point argument is invalid this
+     * method returns -1.
      *
-     * @param p the point to look at
-     * @return the character index, or -1
+     * @param p - a point in component pixel co-ordinates
+     *
+     * @return a character index, or -1
      */
     public int getIndexAtPoint(Point p)
       throws NotImplementedException
@@ -284,17 +295,14 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Determines the bounding box of the indexed character. Returns an empty
-     * rectangle if the index is out of bounds.  The bounds are returned in local coordinates. 
-     * If the index is invalid a null rectangle is returned. The screen coordinates returned are 
-     * "unscrolled coordinates" if the JTextComponent is contained in a JScrollPane in which 
-     * case the resulting rectangle should be composed with the parent coordinates. 
-     * Note: the JTextComponent must have a valid size (e.g. have been added to a parent 
-     * container whose ancestor container is a valid top-level window) for this method to 
-     * be able to return a meaningful (non-null) value.
+     * Calculate the bounding box of the character at the given index.
+     * The returned x and y co-ordinates are relative to this text
+     * component's top-left corner.  If the index is invalid this
+     * method returns null.
+     *
+     * @param index - the character index
      *
-     * @param index the 0-based character index
-     * @return the bounding box, may be empty or null.
+     * @return a character's bounding box, or null
      */
     public Rectangle getCharacterBounds(int index)
       throws NotImplementedException
@@ -303,9 +311,9 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Return the number of characters.
+     * Return the length of the text in this text component.
      *
-     * @return the character count
+     * @return a character length
      */
     public int getCharCount()
     {
@@ -313,10 +321,11 @@ public abstract class JTextComponent extends JComponent
     }
 
    /** 
-    * Returns the attributes of a character at an index, or null if the index
-    * is out of bounds.
+    * Gets the character attributes of the character at index. If
+    * the index is out of bounds, null is returned.
     *
-    * @param index the 0-based character index
+    * @param index - index of the character
+    * 
     * @return the character's attributes
     */
     public AttributeSet getCharacterAttribute(int index)
@@ -326,26 +335,28 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Returns the section of text at the index, or null if the index or part
-     * is invalid.
-     *
-     * @param part {@link #CHARACTER}, {@link #WORD}, or {@link #SENTENCE}
-     * @param index the 0-based character index
-     * @return the selection of text at that index, or null
+     * Gets the text located at index. null is returned if the index
+     * or part is invalid.
+     * 
+     * @param part - {@link #CHARACTER}, {@link #WORD}, or {@link #SENTENCE}
+     * @param index - index of the part
+     * 
+     * @return the part of text at that index, or null
      */
     public String getAtIndex(int part, int index)
       throws NotImplementedException
     {
       return null; // TODO
     }
-
+    
     /**
-     * Returns the section of text after the index, or null if the index or part
-     * is invalid.
-     *
-     * @param part {@link #CHARACTER}, {@link #WORD}, or {@link #SENTENCE}
-     * @param index the 0-based character index
-     * @return the selection of text after that index, or null
+     * Gets the text located after index. null is returned if the index
+     * or part is invalid.
+     * 
+     * @param part - {@link #CHARACTER}, {@link #WORD}, or {@link #SENTENCE}
+     * @param index - index after the part
+     * 
+     * @return the part of text after that index, or null
      */
     public String getAfterIndex(int part, int index)
       throws NotImplementedException
@@ -354,12 +365,13 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Returns the section of text before the index, or null if the index or part
-     * is invalid.
-     *
-     * @param part {@link #CHARACTER}, {@link #WORD}, or {@link #SENTENCE}
-     * @param index the 0-based character index
-     * @return the selection of text before that index, or null
+     * Gets the text located before index. null is returned if the index
+     * or part is invalid.
+     * 
+     * @param part - {@link #CHARACTER}, {@link #WORD}, or {@link #SENTENCE}
+     * @param index - index before the part
+     * 
+     * @return the part of text before that index, or null
      */
     public String getBeforeIndex(int part, int index)
       throws NotImplementedException
@@ -368,10 +380,10 @@ public abstract class JTextComponent extends JComponent
     }
     
     /**
-     * Get the number possible actions for this object, with the zeroth
-     * representing the default action.
+     * Returns the number of actions for this object. The zero-th
+     * object represents the default action.
      * 
-     * @return the 0-based number of actions
+     * @return the number of actions (0-based).
      */
     public int getAccessibleActionCount()
       throws NotImplementedException
@@ -380,11 +392,12 @@ public abstract class JTextComponent extends JComponent
     }
     
     /**
-     * Get a description for the specified action. Returns null if out of
-     * bounds.
+     * Returns the description of the i-th action. Null is returned if
+     * i is out of bounds.
      * 
-     * @param i  the action to describe, 0-based
-     * @return description of the action
+     * @param i - the action to get the description for
+     * 
+     * @return description of the i-th action
      */
     public String getAccessibleActionDescription(int i)
       throws NotImplementedException
@@ -394,10 +407,12 @@ public abstract class JTextComponent extends JComponent
     }
     
     /**
-     * Perform the specified action. Does nothing if out of bounds.
+     * Performs the i-th action. Nothing happens if i is 
+     * out of bounds.
      *
-     * @param i the action to perform, 0-based
-     * @return true if the action was performed
+     * @param i - the action to perform
+     * 
+     * @return true if the action was performed successfully
      */
     public boolean doAccessibleAction(int i)
       throws NotImplementedException
@@ -406,9 +421,9 @@ public abstract class JTextComponent extends JComponent
     }
     
     /**
-     * Set the text contents to the given string.
+     * Sets the text contents.
      *
-     * @param s the new text
+     * @param s - the new text contents.
      */
     public void setTextContents(String s)
       throws NotImplementedException
@@ -417,10 +432,10 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Inserts the given string at the specified location.
+     * Inserts the text at the given index.
      *
-     * @param index the index for insertion
-     * @param s the new text
+     * @param index - the index to insert the new text at.
+     * @param s the new text
      */
     public void insertTextAtIndex(int index, String s)
       throws NotImplementedException
@@ -429,10 +444,10 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Return the text between two points.
+     * Gets the text between two indexes.
      *
-     * @param start the start position, inclusive
-     * @param end the end position, exclusive
+     * @param start - the starting index (inclusive)
+     * @param end - the ending index (exclusive)
      */
     public String getTextRange(int start, int end)
     {
@@ -447,10 +462,10 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Delete the text between two points.
+     * Deletes the text between two indexes.
      *
-     * @param start the start position, inclusive
-     * @param end the end position, exclusive
+     * @param start - the starting index (inclusive)
+     * @param end - the ending index (exclusive)
      */
     public void delete(int start, int end)
     {
@@ -458,10 +473,11 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Cut the text between two points to the system clipboard.
+     * Cuts the text between two indexes. The text is put
+     * into the system clipboard.
      *
-     * @param start the start position, inclusive
-     * @param end the end position, exclusive
+     * @param start - the starting index (inclusive)
+     * @param end - the ending index (exclusive)
      */
     public void cut(int start, int end)
     {
@@ -470,9 +486,9 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Paste the text from the system clipboard at the given index.
+     * Pastes the text from the system clipboard to the given index.
      *
-     * @param start the start position
+     * @param start - the starting index
      */
     public void paste(int start)
     {
@@ -481,11 +497,12 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Replace the text between two points with the given string.
+     * Replaces the text between two indexes with the given text.
      *
-     * @param start the start position, inclusive
-     * @param end the end position, exclusive
-     * @param s the string to paste
+     *
+     * @param start - the starting index (inclusive)
+     * @param end - the ending index (exclusive)
+     * @param s - the text to paste
      */
     public void replaceText(int start, int end, String s)
     {
@@ -494,10 +511,10 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Select the text between two points.
+     * Selects the text between two indexes.
      *
-     * @param start the start position, inclusive
-     * @param end the end position, exclusive
+     * @param start - the starting index (inclusive)
+     * @param end - the ending index (exclusive)
      */
     public void selectText(int start, int end)
     {
@@ -505,11 +522,11 @@ public abstract class JTextComponent extends JComponent
     }
 
     /**
-     * Set the attributes of text between two points.
+     * Sets the attributes of all the text between two indexes.
      *
-     * @param start the start position, inclusive
-     * @param end the end position, exclusive
-     * @param s the new attribute set for the range
+     * @param start - the starting index (inclusive)
+     * @param end - the ending index (exclusive)
+     * @param s - the new attribute set for the text in the range
      */
     public void setAttributes(int start, int end, AttributeSet s)
       throws NotImplementedException
@@ -1163,8 +1180,19 @@ public abstract class JTextComponent extends JComponent
   public void setDocument(Document newDoc)
   {
     Document oldDoc = doc;
-    doc = newDoc;
-    firePropertyChange("document", oldDoc, newDoc);
+    try
+      {
+        if (oldDoc instanceof AbstractDocument)
+          ((AbstractDocument) oldDoc).readLock();
+
+        doc = newDoc;
+        firePropertyChange("document", oldDoc, newDoc);
+      }
+    finally
+      {
+        if (oldDoc instanceof AbstractDocument)
+          ((AbstractDocument) oldDoc).readUnlock();
+      }
     revalidate();
     repaint();
   }
@@ -1641,10 +1669,12 @@ public abstract class JTextComponent extends JComponent
 
   public boolean getScrollableTracksViewportWidth()
   {
-    if (getParent() instanceof JViewport)
-      return getParent().getWidth() > getPreferredSize().width;
+    boolean res = false;;
+    Container c = getParent();
+    if (c instanceof JViewport)
+      res = ((JViewport) c).getExtentSize().width > getPreferredSize().width;
 
-    return false;
+    return res;
   }
 
   /**