package sun.lwawt.macosx;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.util.concurrent.Callable;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleEditableText;
import javax.accessibility.AccessibleText;
import javax.swing.text.Element;
import javax.swing.text.JTextComponent;
class CAccessibleText {
static AccessibleEditableText getAccessibleEditableText(final Accessible a, final Component c) {
if (a == null) return null;
return CAccessibility.invokeAndWait(new Callable<AccessibleEditableText>() {
public AccessibleEditableText call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
return ac.getAccessibleEditableText();
}
}, c);
}
static String getSelectedText(final Accessible a, final Component c) {
if (a == null) return null;
return CAccessibility.invokeAndWait(new Callable<String>() {
public String call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final AccessibleText at = ac.getAccessibleText();
if (at == null) return null;
return at.getSelectedText();
}
}, c);
}
static void setSelectedText(final Accessible a, final Component c, final String newText) {
if (a == null) return;
CAccessibility.invokeLater(new Runnable() {
public void run() {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return;
final AccessibleEditableText aet = ac.getAccessibleEditableText();
if (aet == null) return;
final int selectionStart = aet.getSelectionStart();
final int selectionEnd = aet.getSelectionEnd();
aet.replaceText(selectionStart, selectionEnd, newText);
}
}, c);
}
static void (final Accessible a, final Component c, final int startIndex, final int endIndex) {
if (a == null) return;
CAccessibility.invokeLater(new Runnable() {
public void run() {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return;
final AccessibleEditableText aet = ac.getAccessibleEditableText();
if (aet == null) return;
final boolean validRange = (startIndex >= 0) && (endIndex >= startIndex) && (endIndex <= aet.getCharCount());
if (!validRange) return;
aet.selectText(startIndex, endIndex);
}
}, c);
}
static String (final AccessibleEditableText aet, final int start, final int stop, final Component c) {
if (aet == null) return null;
return CAccessibility.invokeAndWait(new Callable<String>() {
public String call() throws Exception {
return aet.getTextRange(start, stop);
}
}, c);
}
static int getCharacterIndexAtPosition(final Accessible a, final Component c, final int x, final int y) {
if (a == null) return 0;
return CAccessibility.invokeAndWait(new Callable<Integer>() {
public Integer call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final AccessibleText at = ac.getAccessibleText();
if (at == null) return null;
final Point componentLocation = ac.getAccessibleComponent().getLocationOnScreen();
final int localX = x - (int)componentLocation.getX();
final int localY = y - (int)componentLocation.getY();
return at.getIndexAtPoint(new Point(localX, localY));
}
}, c);
}
static int[] (final Accessible a, final Component c) {
if (a == null) return new int[2];
return CAccessibility.invokeAndWait(new Callable<int[]>() {
public int[] call() {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return new int[2];
final AccessibleText at = ac.getAccessibleText();
if (at == null) return new int[2];
final int[] ret = new int[2];
ret[0] = at.getSelectionStart();
ret[1] = at.getSelectionEnd();
return ret;
}
}, c);
}
static int[] getVisibleCharacterRange(final Accessible a, final Component c) {
if (a == null) return null;
return CAccessibility.invokeAndWait(new Callable<int[]>() {
public int[] call() {
return getVisibleCharacterRange(a);
}
}, c);
}
static int getLineNumberForIndex(final Accessible a, final Component c, final int index) {
if (a == null) return 0;
return CAccessibility.invokeAndWait(new Callable<Integer>() {
public Integer call() {
return Integer.valueOf(getLineNumberForIndex(a, index));
}
}, c);
}
static int getLineNumberForInsertionPoint(final Accessible a, final Component c) {
if (a == null) return 0;
return CAccessibility.invokeAndWait(new Callable<Integer>() {
public Integer call() {
return Integer.valueOf(getLineNumberForInsertionPoint(a));
}
}, c);
}
static int[] getRangeForLine(final Accessible a, final Component c, final int line) {
if (a == null) return null;
return CAccessibility.invokeAndWait(new Callable<int[]>() {
public int[] call() {
return getRangeForLine(a, line);
}
}, c);
}
static int[] getRangeForIndex(final Accessible a, final Component c, final int index) {
if (a == null) return new int[2];
return CAccessibility.invokeAndWait(new Callable<int[]>() {
public int[] call() {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return new int[2];
final AccessibleEditableText aet = ac.getAccessibleEditableText();
if (aet == null) return new int[2];
final int charCount = aet.getCharCount();
if (index >= charCount) return new int[2];
final String foundWord = aet.getAtIndex(AccessibleText.WORD, index);
final int foundWordLength = foundWord.length();
final String wholeString = aet.getTextRange(0, charCount - 1);
int foundWordIndex = -1;
int offset = 0;
while ((foundWordIndex == -1) && (offset < foundWordLength)) {
if (wholeString.regionMatches(true, index - offset, foundWord, 0, foundWordLength)) {
foundWordIndex = index - offset;
}
if (wholeString.regionMatches(true, index + offset, foundWord, 0, foundWordLength)) {
foundWordIndex = index + offset;
}
offset++;
}
final int[] ret = new int[2];
ret[0] = foundWordIndex;
ret[1] = foundWordIndex + foundWordLength;
return ret;
}
}, c);
}
static double[] getBoundsForRange(final Accessible a, final Component c, final int location, final int length) {
final double[] ret = new double[4];
if (a == null) return ret;
return CAccessibility.invokeAndWait(new Callable<double[]>() {
public double[] call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return ret;
final AccessibleText at = ac.getAccessibleText();
if (at == null) {
ac.getAccessibleName();
ac.getAccessibleEditableText();
return ret;
}
final Rectangle2D boundsStart = at.getCharacterBounds(location);
final Rectangle2D boundsEnd = at.getCharacterBounds(location + length - 1);
if (boundsEnd == null || boundsStart == null) return ret;
final Rectangle2D boundsUnion = boundsStart.createUnion(boundsEnd);
if (boundsUnion.isEmpty()) return ret;
final double localX = boundsUnion.getX();
final double localY = boundsUnion.getY();
final Point componentLocation = ac.getAccessibleComponent().getLocationOnScreen();
if (componentLocation == null) return ret;
final double screenX = componentLocation.getX() + localX;
final double screenY = componentLocation.getY() + localY;
ret[0] = screenX;
ret[1] = screenY;
ret[2] = boundsUnion.getWidth();
ret[3] = boundsUnion.getHeight();
return ret;
}
}, c);
}
static String getStringForRange(final Accessible a, final Component c, final int location, final int length) {
if (a == null) return null;
return CAccessibility.invokeAndWait(new Callable<String>() {
public String call() throws Exception {
final AccessibleContext ac = a.getAccessibleContext();
if (ac == null) return null;
final AccessibleEditableText aet = ac.getAccessibleEditableText();
if (aet == null) return null;
return aet.getTextRange(location, location + length);
}
}, c);
}
@SuppressWarnings("deprecation")
static int[] getVisibleCharacterRange(final Accessible a) {
final Accessible sa = CAccessible.getSwingAccessible(a);
if (!(sa instanceof JTextComponent)) return null;
final JTextComponent jc = (JTextComponent) sa;
final Rectangle rect = jc.getVisibleRect();
final Point topLeft = new Point(rect.x, rect.y);
final Point topRight = new Point(rect.x + rect.width, rect.y);
final Point bottomLeft = new Point(rect.x, rect.y + rect.height);
final Point bottomRight = new Point(rect.x + rect.width, rect.y + rect.height);
int start = Math.min(jc.viewToModel(topLeft), jc.viewToModel(topRight));
int end = Math.max(jc.viewToModel(bottomLeft), jc.viewToModel(bottomRight));
if (start < 0) start = 0;
if (end < 0) end = 0;
return new int[] { start, end };
}
static int getLineNumberForIndex(final Accessible a, int index) {
final Accessible sa = CAccessible.getSwingAccessible(a);
if (!(sa instanceof JTextComponent)) return -1;
final JTextComponent jc = (JTextComponent) sa;
final Element root = jc.getDocument().getDefaultRootElement();
if (index == -1) index = jc.getCaretPosition();
return root.getElementIndex(index);
}
static int getLineNumberForInsertionPoint(final Accessible a) {
return getLineNumberForIndex(a, -1);
}
static int[] getRangeForLine(final Accessible a, final int lineIndex) {
Accessible sa = CAccessible.getSwingAccessible(a);
if (!(sa instanceof JTextComponent)) return null;
final JTextComponent jc = (JTextComponent) sa;
final Element root = jc.getDocument().getDefaultRootElement();
final Element line = root.getElement(lineIndex);
if (line == null) return null;
return new int[] { line.getStartOffset(), line.getEndOffset() };
}
}