1 /*
   2  * Copyright (c) 2011, 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 "JavaAccessibilityAction.h"
  27 #import "JavaAccessibilityUtilities.h"
  28 
  29 #import "ThreadUtilities.h"
  30 #import "JNIUtilities.h"
  31 
  32 
  33 @implementation JavaAxAction
  34 
  35 - (id)initWithEnv:(JNIEnv *)env withAccessibleAction:(jobject)accessibleAction withIndex:(jint)index withComponent:(jobject)component
  36 {
  37     self = [super init];
  38     if (self) {
  39         fAccessibleAction = (*env)->NewWeakGlobalRef(env, accessibleAction);
  40         CHECK_EXCEPTION();
  41         fIndex = index;
  42         fComponent = (*env)->NewWeakGlobalRef(env, component);
  43         CHECK_EXCEPTION();
  44     }
  45     return self;
  46 }
  47 
  48 - (void)dealloc
  49 {
  50     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
  51 
  52     (*env)->DeleteWeakGlobalRef(env, fAccessibleAction);
  53     fAccessibleAction = NULL;
  54 
  55     (*env)->DeleteWeakGlobalRef(env, fComponent);
  56     fComponent = NULL;
  57 
  58     [super dealloc];
  59 }
  60 
  61 - (NSString *)getDescription
  62 {
  63     JNIEnv* env = [ThreadUtilities getJNIEnv];
  64     DECLARE_CLASS_RETURN(sjc_CAccessibility, "sun/lwawt/macosx/CAccessibility", nil);
  65     DECLARE_STATIC_METHOD_RETURN(jm_getAccessibleActionDescription, sjc_CAccessibility,
  66                           "getAccessibleActionDescription",
  67                           "(Ljavax/accessibility/AccessibleAction;ILjava/awt/Component;)Ljava/lang/String;", nil);
  68 
  69     /* WeakGlobalRefs can be cleared at any time, so first get strong local refs and use those */
  70     jobject fCompLocal = (*env)->NewLocalRef(env, fComponent);
  71     if ((*env)->IsSameObject(env, fCompLocal, NULL)) {
  72         return nil;
  73     }
  74     jobject fAccessibleActionLocal = (*env)->NewLocalRef(env, fAccessibleAction);
  75     if ((*env)->IsSameObject(env, fAccessibleActionLocal, NULL)) {
  76         (*env)->DeleteLocalRef(env, fCompLocal);
  77         return nil;
  78     }
  79     NSString *str = nil;
  80     jstring jstr = (*env)->CallStaticObjectMethod(env, sjc_CAccessibility,
  81                                               jm_getAccessibleActionDescription,
  82                                               fAccessibleActionLocal,
  83                                               fIndex,
  84                                               fCompLocal );
  85     CHECK_EXCEPTION();
  86     if (jstr != NULL) {
  87         str = JavaStringToNSString(env, jstr);
  88         (*env)->DeleteLocalRef(env, jstr);
  89     }
  90     (*env)->DeleteLocalRef(env, fCompLocal);
  91     (*env)->DeleteLocalRef(env, fAccessibleActionLocal);
  92     return str;
  93 }
  94 
  95 - (void)perform
  96 {
  97     JNIEnv* env = [ThreadUtilities getJNIEnv];
  98     DECLARE_CLASS(sjc_CAccessibility, "sun/lwawt/macosx/CAccessibility");
  99     DECLARE_STATIC_METHOD(jm_doAccessibleAction, sjc_CAccessibility, "doAccessibleAction",
 100                     "(Ljavax/accessibility/AccessibleAction;ILjava/awt/Component;)V");
 101 
 102     (*env)->CallStaticVoidMethod(env, sjc_CAccessibility, jm_doAccessibleAction,
 103              fAccessibleAction, fIndex, fComponent);
 104     CHECK_EXCEPTION();
 105 }
 106 
 107 @end
 108 
 109 
 110 @implementation TabGroupAction
 111 
 112 - (id)initWithEnv:(JNIEnv *)env withTabGroup:(jobject)tabGroup withIndex:(jint)index withComponent:(jobject)component
 113 {
 114     self = [super init];
 115     if (self) {
 116         fTabGroup = (*env)->NewWeakGlobalRef(env, tabGroup);
 117         CHECK_EXCEPTION();
 118         fIndex = index;
 119         fComponent = (*env)->NewWeakGlobalRef(env, component);
 120         CHECK_EXCEPTION();
 121     }
 122     return self;
 123 }
 124 
 125 - (void)dealloc
 126 {
 127     JNIEnv *env = [ThreadUtilities getJNIEnvUncached];
 128 
 129     (*env)->DeleteWeakGlobalRef(env, fTabGroup);
 130     fTabGroup = NULL;
 131 
 132     (*env)->DeleteWeakGlobalRef(env, fComponent);
 133     fComponent = NULL;
 134 
 135     [super dealloc];
 136 }
 137 
 138 - (NSString *)getDescription
 139 {
 140     return @"click";
 141 }
 142 
 143 - (void)perform
 144 {
 145     JNIEnv* env = [ThreadUtilities getJNIEnv];
 146 
 147     setAxContextSelection(env, fTabGroup, fIndex, fComponent);
 148 }
 149 
 150 @end