|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +#import "CommonTextAccessibility.h" |
| 27 | +#import "ThreadUtilities.h" |
| 28 | +#import "JNIUtilities.h" |
| 29 | + |
| 30 | +#define DEFAULT_RANGE NSMakeRange(0, 0) |
| 31 | +#define DEFAULT_RECT NSMakeRect(0, 0, 0, 0) |
| 32 | + |
| 33 | +static jclass sjc_CAccessibility = NULL; |
| 34 | +static jmethodID sjm_getAccessibleText = NULL; |
| 35 | +#define GET_ACCESSIBLETEXT_METHOD_RETURN(ret) \ |
| 36 | + GET_CACCESSIBILITY_CLASS_RETURN(ret); \ |
| 37 | + GET_STATIC_METHOD_RETURN(sjm_getAccessibleText, sjc_CAccessibility, "getAccessibleText", \ |
| 38 | + "(Ljavax/accessibility/Accessible;Ljava/awt/Component;)Ljavax/accessibility/AccessibleText;", ret); |
| 39 | + |
| 40 | +static jclass sjc_CAccessibleText = NULL; |
| 41 | +#define GET_CACCESSIBLETEXT_CLASS() \ |
| 42 | + GET_CLASS(sjc_CAccessibleText, "sun/lwawt/macosx/CAccessibleText"); |
| 43 | +#define GET_CACCESSIBLETEXT_CLASS_RETURN(ret) \ |
| 44 | + GET_CLASS_RETURN(sjc_CAccessibleText, "sun/lwawt/macosx/CAccessibleText", ret); |
| 45 | + |
| 46 | +static jmethodID sjm_getAccessibleEditableText = NULL; |
| 47 | +#define GET_ACCESSIBLEEDITABLETEXT_METHOD_RETURN(ret) \ |
| 48 | + GET_CACCESSIBLETEXT_CLASS_RETURN(ret); \ |
| 49 | + GET_STATIC_METHOD_RETURN(sjm_getAccessibleEditableText, sjc_CAccessibleText, "getAccessibleEditableText", \ |
| 50 | + "(Ljavax/accessibility/Accessible;Ljava/awt/Component;)Ljavax/accessibility/AccessibleEditableText;", ret); |
| 51 | + |
| 52 | +/* |
| 53 | + * Converts an int array to an NSRange wrapped inside an NSValue |
| 54 | + * takes [start, end] values and returns [start, end - start] |
| 55 | + */ |
| 56 | +static NSRange javaIntArrayToNSRange(JNIEnv* env, jintArray array) { |
| 57 | + jint *values = (*env)->GetIntArrayElements(env, array, 0); |
| 58 | + if (values == NULL) { |
| 59 | + NSLog(@"%s failed calling GetIntArrayElements", __FUNCTION__); |
| 60 | + return DEFAULT_RANGE; |
| 61 | + }; |
| 62 | + return NSMakeRange(values[0], values[1] - values[0]); |
| 63 | +} |
| 64 | + |
| 65 | +@implementation CommonTextAccessibility |
| 66 | + |
| 67 | +- (nullable NSString *)accessibilityValueAttribute |
| 68 | +{ |
| 69 | + JNIEnv *env = [ThreadUtilities getJNIEnv]; |
| 70 | + GET_CACCESSIBILITY_CLASS_RETURN(nil); |
| 71 | + DECLARE_STATIC_METHOD_RETURN(sjm_getAccessibleName, sjc_CAccessibility, "getAccessibleName", |
| 72 | + "(Ljavax/accessibility/Accessible;Ljava/awt/Component;)Ljava/lang/String;", nil); |
| 73 | + if ([[self accessibilityRoleAttribute] isEqualToString:NSAccessibilityStaticTextRole]) { |
| 74 | + jobject axName = (*env)->CallStaticObjectMethod(env, sjc_CAccessibility, |
| 75 | + sjm_getAccessibleName, fAccessible, fComponent); |
| 76 | + CHECK_EXCEPTION(); |
| 77 | + if (axName != NULL) { |
| 78 | + NSString* str = JNFJavaToNSString(env, axName); |
| 79 | + (*env)->DeleteLocalRef(env, axName); |
| 80 | + return str; |
| 81 | + } |
| 82 | + // value is still nil if no accessibleName for static text. Below, try to get the accessibleText. |
| 83 | + } |
| 84 | + |
| 85 | + GET_ACCESSIBLETEXT_METHOD_RETURN(@""); |
| 86 | + jobject axText = (*env)->CallStaticObjectMethod(env, sjc_CAccessibility, |
| 87 | + sjm_getAccessibleText, fAccessible, fComponent); |
| 88 | + CHECK_EXCEPTION(); |
| 89 | + if (axText == NULL) return nil; |
| 90 | + (*env)->DeleteLocalRef(env, axText); |
| 91 | + |
| 92 | + GET_ACCESSIBLEEDITABLETEXT_METHOD_RETURN(nil); |
| 93 | + jobject axEditableText = (*env)->CallStaticObjectMethod(env, sjc_CAccessibleText, |
| 94 | + sjm_getAccessibleEditableText, fAccessible, fComponent); |
| 95 | + CHECK_EXCEPTION(); |
| 96 | + if (axEditableText == NULL) return nil; |
| 97 | + |
| 98 | + DECLARE_STATIC_METHOD_RETURN(jm_getTextRange, sjc_CAccessibleText, "getTextRange", |
| 99 | + "(Ljavax/accessibility/AccessibleEditableText;IILjava/awt/Component;)Ljava/lang/String;", nil); |
| 100 | + jobject jrange = (*env)->CallStaticObjectMethod(env, sjc_CAccessibleText, jm_getTextRange, |
| 101 | + axEditableText, 0, getAxTextCharCount(env, axEditableText, fComponent), fComponent); |
| 102 | + CHECK_EXCEPTION(); |
| 103 | + NSString *string = JNFJavaToNSString(env, jrange); // AWT_THREADING Safe (AWTRunLoop) |
| 104 | + |
| 105 | + (*env)->DeleteLocalRef(env, jrange); |
| 106 | + (*env)->DeleteLocalRef(env, axEditableText); |
| 107 | + |
| 108 | + if (string == nil) string = @""; |
| 109 | + return string; |
| 110 | +} |
| 111 | + |
| 112 | +- (NSRange)accessibilityVisibleCharacterRangeAttribute |
| 113 | +{ |
| 114 | + JNIEnv *env = [ThreadUtilities getJNIEnv]; |
| 115 | + GET_CACCESSIBLETEXT_CLASS_RETURN(DEFAULT_RANGE); |
| 116 | + DECLARE_STATIC_METHOD_RETURN(jm_getVisibleCharacterRange, sjc_CAccessibleText, "getVisibleCharacterRange", |
| 117 | + "(Ljavax/accessibility/Accessible;Ljava/awt/Component;)[I", DEFAULT_RANGE); |
| 118 | + jintArray axTextRange = (*env)->CallStaticObjectMethod(env, sjc_CAccessibleText, |
| 119 | + jm_getVisibleCharacterRange, fAccessible, fComponent); // AWT_THREADING Safe (AWTRunLoop) |
| 120 | + CHECK_EXCEPTION(); |
| 121 | + if (axTextRange == NULL) return DEFAULT_RANGE; |
| 122 | + |
| 123 | + return javaIntArrayToNSRange(env, axTextRange); |
| 124 | +} |
| 125 | + |
| 126 | +- (nullable NSString *)accessibilityStringForRangeAttribute:(NSRange)range |
| 127 | +{ |
| 128 | + JNIEnv *env = [ThreadUtilities getJNIEnv]; |
| 129 | + GET_CACCESSIBLETEXT_CLASS_RETURN(nil); |
| 130 | + DECLARE_STATIC_METHOD_RETURN(jm_getStringForRange, sjc_CAccessibleText, "getStringForRange", |
| 131 | + "(Ljavax/accessibility/Accessible;Ljava/awt/Component;II)Ljava/lang/String;", nil); |
| 132 | + jstring jstringForRange = (jstring)(*env)->CallStaticObjectMethod(env, sjc_CAccessibleText, jm_getStringForRange, |
| 133 | + fAccessible, fComponent, range.location, range.length); // AWT_THREADING Safe (AWTRunLoop) |
| 134 | + CHECK_EXCEPTION(); |
| 135 | + if (jstringForRange == NULL) return @""; |
| 136 | + NSString* str = JNFJavaToNSString(env, jstringForRange); |
| 137 | + (*env)->DeleteLocalRef(env, jstringForRange); |
| 138 | + return str; |
| 139 | +} |
| 140 | + |
| 141 | +@end |
1 commit comments
openjdk-notifier[bot] commentedon Jan 22, 2021
Review
Issues