Skip to content

Commit acbcde8

Browse files
author
Pankaj Bansal
committedJan 22, 2021
8256111: Create implementation for NSAccessibilityStaticText protocol
Reviewed-by: serb, kizune
1 parent f928265 commit acbcde8

File tree

5 files changed

+272
-2
lines changed

5 files changed

+272
-2
lines changed
 

Diff for: ‎src/java.desktop/macosx/native/libawt_lwawt/awt/a11y/CommonComponentAccessibility.m

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ + (void) initializeRolesMap {
4646
/*
4747
* Here we should keep all the mapping between the accessibility roles and implementing classes
4848
*/
49-
rolesMap = [[NSMutableDictionary alloc] initWithCapacity:4];
49+
rolesMap = [[NSMutableDictionary alloc] initWithCapacity:6];
5050

5151
[rolesMap setObject:@"ButtonAccessibility" forKey:@"pushbutton"];
5252
[rolesMap setObject:@"ImageAccessibility" forKey:@"icon"];
5353
[rolesMap setObject:@"ImageAccessibility" forKey:@"desktopicon"];
5454
[rolesMap setObject:@"SpinboxAccessibility" forKey:@"spinbox"];
55-
55+
[rolesMap setObject:@"StaticTextAccessibility" forKey:@"hyperlink"];
56+
[rolesMap setObject:@"StaticTextAccessibility" forKey:@"label"];
5657
}
5758

5859
/*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
#ifndef COMMON_TEXT_ACCESSIBILITY
27+
#define COMMON_TEXT_ACCESSIBILITY
28+
29+
#import "CommonComponentAccessibility.h"
30+
#import "JavaAccessibilityUtilities.h"
31+
32+
#import <AppKit/NSAccessibility.h>
33+
34+
@interface CommonTextAccessibility : CommonComponentAccessibility {
35+
36+
}
37+
- (nullable NSString *)accessibilityValueAttribute;
38+
- (NSRange)accessibilityVisibleCharacterRangeAttribute;
39+
- (nullable NSString *)accessibilityStringForRangeAttribute:(NSRange)parameter;
40+
@end
41+
42+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#ifndef STATIC_TEXT_ACCESSIBILITY
27+
#define STATIC_TEXT_ACCESSIBILITY
28+
29+
#import "CommonTextAccessibility.h"
30+
31+
#import <AppKit/NSAccessibility.h>
32+
33+
34+
@interface StaticTextAccessibility : CommonTextAccessibility<NSAccessibilityStaticText> {
35+
36+
};
37+
- (nullable NSString *)accessibilityAttributedString:(NSRange)range;
38+
- (nullable NSString *)accessibilityValue;
39+
- (NSRange)accessibilityVisibleCharacterRange;
40+
@end
41+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 "StaticTextAccessibility.h"
27+
28+
@implementation StaticTextAccessibility
29+
30+
- (nullable NSString *)accessibilityAttributedString:(NSRange)range
31+
{
32+
return [self accessibilityStringForRangeAttribute:range];
33+
}
34+
35+
- (nullable NSString *)accessibilityValue
36+
{
37+
return [self accessibilityValueAttribute];
38+
}
39+
40+
- (NSRange)accessibilityVisibleCharacterRange
41+
{
42+
return [self accessibilityVisibleCharacterRangeAttribute];
43+
}
44+
45+
@end

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on Jan 22, 2021

@openjdk-notifier[bot]
Please sign in to comment.