aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/awt/TextComponent.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/java/awt/TextComponent.java')
-rw-r--r--libjava/java/awt/TextComponent.java248
1 files changed, 247 insertions, 1 deletions
diff --git a/libjava/java/awt/TextComponent.java b/libjava/java/awt/TextComponent.java
index 9cdb507..8f9f875 100644
--- a/libjava/java/awt/TextComponent.java
+++ b/libjava/java/awt/TextComponent.java
@@ -42,8 +42,16 @@ import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.peer.TextComponentPeer;
import java.io.Serializable;
+import java.text.BreakIterator;
import java.util.EventListener;
+import javax.accessibility.Accessible;
+import javax.accessibility.AccessibleRole;
+import javax.accessibility.AccessibleState;
+import javax.accessibility.AccessibleStateSet;
+import javax.accessibility.AccessibleText;
+import javax.swing.text.AttributeSet;
+
/**
* This class provides common functionality for widgets than
* contain text.
@@ -51,7 +59,7 @@ import java.util.EventListener;
* @author Aaron M. Renn (arenn@urbanophile.com)
*/
public class TextComponent extends Component
- implements Serializable
+ implements Serializable, Accessible
{
/*
@@ -90,6 +98,219 @@ private String text;
*/
protected transient TextListener textListener;
+ protected class AccessibleAWTTextComponent
+ extends AccessibleAWTComponent
+ implements AccessibleText, TextListener
+ {
+ // Constructor
+ // Adds a listener for tracking caret changes
+ public AccessibleAWTTextComponent()
+ {
+ TextComponent.this.addTextListener(this);
+ }
+
+ public AccessibleRole getAccessibleRole()
+ {
+ return AccessibleRole.TEXT;
+ }
+
+ public AccessibleStateSet getAccessibleStateSet()
+ {
+ // TODO: Docs say PropertyChangeEvent will fire if this state changes.
+ // That means that the event has to fire when editable changes.
+ AccessibleStateSet ss = super.getAccessibleStateSet();
+ if (editable)
+ ss.add(AccessibleState.EDITABLE);
+ return ss;
+ }
+
+ public AccessibleText getAccessibleText()
+ {
+ return this;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getIndexAtPoint(java.awt.Point)
+ */
+ public int getIndexAtPoint(Point point)
+ {
+ return TextComponent.this.getIndexAtPoint(point);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getCharacterBounds(int)
+ */
+ public Rectangle getCharacterBounds(int index)
+ {
+ return TextComponent.this.getCharacterBounds(index);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getCharCount()
+ */
+ public int getCharCount()
+ {
+ return text.length();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getCaretPosition()
+ */
+ public int getCaretPosition()
+ {
+ return TextComponent.this.getCaretPosition();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getAtIndex(int, int)
+ */
+ public String getAtIndex(int part, int index)
+ {
+ if (index < 0 || index >= text.length())
+ return null;
+ BreakIterator it = null;
+ switch (part)
+ {
+ case CHARACTER:
+ return text.substring(index, index + 1);
+ case WORD:
+ it = BreakIterator.getWordInstance();
+ break;
+ case SENTENCE:
+ it = BreakIterator.getSentenceInstance();
+ break;
+ default:
+ return null;
+ }
+ it.setText(text);
+ int start = index;
+ if (!it.isBoundary(index))
+ start = it.preceding(index);
+ int end = it.following(index);
+ if (end == -1)
+ return text.substring(index);
+ else
+ return text.substring(index, end);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getAfterIndex(int, int)
+ */
+ public String getAfterIndex(int part, int index) {
+ if (index < 0 || index >= text.length())
+ return null;
+ BreakIterator it = null;
+ switch (part)
+ {
+ case CHARACTER:
+ return text.substring(index, index + 1);
+ case WORD:
+ it = BreakIterator.getWordInstance();
+ break;
+ case SENTENCE:
+ it = BreakIterator.getSentenceInstance();
+ break;
+ default:
+ return null;
+ }
+ it.setText(text);
+ int start = index;
+ if (!it.isBoundary(index))
+ start = it.following(index);
+ // Make sure there was a complete unit. I.e. if index is in the middle
+ // of a word, return null if there is no word after the that one.
+ if (start == -1)
+ return null;
+ int end = it.following(start);
+ if (end == -1)
+ return text.substring(index);
+ else
+ return text.substring(index, end);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getBeforeIndex(int, int)
+ */
+ public String getBeforeIndex(int part, int index)
+ {
+ if (index < 1 || index >= text.length())
+ return null;
+ BreakIterator it = null;
+ switch (part)
+ {
+ case CHARACTER:
+ return text.substring(index - 1, index);
+ case WORD:
+ it = BreakIterator.getWordInstance();
+ break;
+ case SENTENCE:
+ it = BreakIterator.getSentenceInstance();
+ break;
+ default:
+ return null;
+ }
+ it.setText(text);
+ int end = index;
+ if (!it.isBoundary(index))
+ end = it.preceding(index);
+ // Make sure there was a complete unit. I.e. if index is in the middle
+ // of a word, return null if there is no word before that one.
+ if (end == -1)
+ return null;
+ int start = it.preceding(end);
+ if (start == -1)
+ return text.substring(0, end);
+ else
+ return text.substring(start, end);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getCharacterAttribute(int)
+ */
+ public AttributeSet getCharacterAttribute(int index)
+ {
+ // FIXME: I suspect this really gets filled in by subclasses.
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getSelectionStart()
+ */
+ public int getSelectionStart() {
+ // TODO Auto-generated method stub
+ return selectionStart;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getSelectionEnd()
+ */
+ public int getSelectionEnd()
+ {
+ return selectionEnd;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.accessibility.AccessibleText#getSelectedText()
+ */
+ public String getSelectedText()
+ {
+ if (selectionEnd - selectionStart > 0)
+ return text.substring(selectionStart, selectionEnd);
+ else
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see java.awt.event.TextListener#textValueChanged(java.awt.event.TextEvent)
+ */
+ public void textValueChanged(TextEvent event)
+ {
+ // TODO Auto-generated method stub
+
+ }
+
+ }
+
/*************************************************************************/
/*
@@ -468,5 +689,30 @@ paramString()
{
return (TextListener[]) getListeners (TextListener.class);
}
+
+ /*******************************/
+ // Provide AccessibleAWTTextComponent access to several peer functions that
+ // aren't publicly exposed.
+ private synchronized int
+ getIndexAtPoint(Point p)
+ {
+ TextComponentPeer tcp = (TextComponentPeer)getPeer();
+ if (tcp != null)
+ return tcp.getIndexAtPoint(p.x, p.y);
+ return -1;
+ }
+
+ private synchronized Rectangle
+ getCharacterBounds(int i)
+ {
+ TextComponentPeer tcp = (TextComponentPeer)getPeer();
+ if (tcp != null)
+ return tcp.getCharacterBounds(i);
+ return null;
+ }
+
+
+
+
} // class TextComponent