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 "ScrollAreaAccessibility.h"
27#import "ThreadUtilities.h"
28#import "JNIUtilities.h"
29
30/*
31 * Implementation of the accessibility peer for the ScrollArea role
32 */
33@implementation ScrollAreaAccessibility
34
35- (NSArray * _Nullable)accessibilityContentsAttribute
36{
37    JNIEnv *env = [ThreadUtilities getJNIEnv];
38    NSArray *children = [JavaComponentAccessibility childrenOfParent:self withEnv:env withChildrenCode:JAVA_AX_ALL_CHILDREN allowIgnored:YES];
39
40    if ([children count] <= 0) return nil;
41    NSArray *contents = [NSMutableArray arrayWithCapacity:[children count]];
42
43    // The scroll bars are in the children. children less the scroll bars is the contents
44    NSEnumerator *enumerator = [children objectEnumerator];
45    JavaComponentAccessibility *aElement;
46    while ((aElement = (JavaComponentAccessibility *)[enumerator nextObject])) {
47        if (![[aElement accessibilityRoleAttribute] isEqualToString:NSAccessibilityScrollBarRole]) {
48            // no scroll bars in contents
49            [(NSMutableArray *)contents addObject:aElement];
50        }
51    }
52    return contents;
53}
54
55- (id _Nullable)getScrollBarwithOrientation:(enum NSAccessibilityOrientation)orientation
56{
57    JNIEnv *env = [ThreadUtilities getJNIEnv];
58
59    NSArray *children = [JavaComponentAccessibility childrenOfParent:self withEnv:env withChildrenCode:JAVA_AX_ALL_CHILDREN allowIgnored:YES];
60    if ([children count] <= 0) return nil;
61
62    // The scroll bars are in the children.
63    JavaComponentAccessibility *aElement;
64    NSEnumerator *enumerator = [children objectEnumerator];
65    while ((aElement = (JavaComponentAccessibility *)[enumerator nextObject])) {
66        if ([[aElement accessibilityRoleAttribute] isEqualToString:NSAccessibilityScrollBarRole]) {
67            jobject elementAxContext = [aElement axContextWithEnv:env];
68            if (orientation == NSAccessibilityOrientationHorizontal) {
69                if (isHorizontal(env, elementAxContext, fComponent)) {
70                    (*env)->DeleteLocalRef(env, elementAxContext);
71                    return aElement;
72                }
73            } else if (orientation == NSAccessibilityOrientationVertical) {
74                if (isVertical(env, elementAxContext, fComponent)) {
75                    (*env)->DeleteLocalRef(env, elementAxContext);
76                    return aElement;
77                }
78            } else {
79                (*env)->DeleteLocalRef(env, elementAxContext);
80            }
81        }
82    }
83    return nil;
84}
85
86- (NSAccessibilityRole _Nonnull)accessibilityRole
87{
88    return NSAccessibilityScrollAreaRole;
89}
90
91- (NSArray * _Nullable)accessibilityContents
92{
93    return [self accessibilityContentsAttribute];
94}
95
96- (id _Nullable)accessibilityHorizontalScrollBar
97{
98    return [self getScrollBarwithOrientation:NSAccessibilityOrientationHorizontal];
99}
100
101- (id _Nullable)accessibilityVerticalScrollBar
102{
103    return [self getScrollBarwithOrientation:NSAccessibilityOrientationVertical];
104}
105@end
106