File: AtkObject.java

package info (click to toggle)
java-atk-wrapper 0.42.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,668 kB
  • sloc: ansic: 5,531; sh: 5,080; java: 2,195; makefile: 100
file content (284 lines) | stat: -rw-r--r-- 11,281 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (c) 2018, Red Hat, Inc.
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package org.GNOME.Accessibility;

import javax.accessibility.*;
import java.util.Locale;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import java.awt.event.KeyEvent;
import java.awt.event.InputEvent;

/**
* AtkObject:
*   That class is used to wrap AccessibleContext Java object
*   to avoid the concurrency of AWT objects.
* @autor Giuseppe Capaldo
*/
public class AtkObject{

    public static final int INTERFACE_ACTION = 0x00000001;
    public static final int INTERFACE_COMPONENT = 0x00000002;
    public static final int INTERFACE_DOCUMENT = 0x00000004;
    public static final int INTERFACE_EDITABLE_TEXT = 0x00000008;
    public static final int INTERFACE_HYPERLINK = 0x00000010;
    public static final int INTERFACE_HYPERTEXT = 0x00000020;
    public static final int INTERFACE_IMAGE = 0x00000040;
    public static final int INTERFACE_SELECTION = 0x00000080;
    public static final int INTERFACE_STREAMABLE_CONTENT = 0x00000100;
    public static final int INTERFACE_TABLE = 0x00000200;
    public static final int INTERFACE_TABLE_CELL = 0x00000400;
    public static final int INTERFACE_TEXT = 0x00000800;
    public static final int INTERFACE_VALUE = 0x00001000;

    public static int getTFlagFromObj(Object o){
      return AtkUtil.invokeInSwing( () -> {
        int flags = 0;
        AccessibleContext ac;

        if (o instanceof AccessibleContext)
            ac = (AccessibleContext) o;
        else if (o instanceof Accessible)
            ac = ( (Accessible) o).getAccessibleContext();
        else
            return flags;

        if (ac.getAccessibleAction() != null)
            flags |= AtkObject.INTERFACE_ACTION;
        if (ac.getAccessibleComponent() != null)
            flags |= AtkObject.INTERFACE_COMPONENT;
        AccessibleText text = ac.getAccessibleText();
        if (text != null){
            flags |= AtkObject.INTERFACE_TEXT;
            if (text instanceof AccessibleHypertext)
                flags |= AtkObject.INTERFACE_HYPERTEXT;
	    if (ac.getAccessibleEditableText() != null)
		flags |= AtkObject.INTERFACE_EDITABLE_TEXT;
        }
        if (ac.getAccessibleIcon() != null)
            flags |= AtkObject.INTERFACE_IMAGE;
        if (ac.getAccessibleSelection() != null)
            flags |= AtkObject.INTERFACE_SELECTION;
        AccessibleTable table = ac.getAccessibleTable();
        if (table != null){
            flags |= AtkObject.INTERFACE_TABLE;
        }
        Accessible parent = ac.getAccessibleParent();
        if (parent != null){
            AccessibleContext pc = parent.getAccessibleContext();
            if (pc != null){
                table = pc.getAccessibleTable();
                // Unfortunately without the AccessibleExtendedTable interface
                // we can't determine the column/row of this accessible in the
                // table
                if (table != null && table instanceof AccessibleExtendedTable){
                    flags |= AtkObject.INTERFACE_TABLE_CELL;
                }
            }
        }
        if (ac.getAccessibleValue() != null)
            flags |= AtkObject.INTERFACE_VALUE;
        return flags;
      }, 0);
    }

    public static AccessibleContext getAccessibleParent(AccessibleContext ac){
        return AtkUtil.invokeInSwing( () -> {
            Accessible father = ac.getAccessibleParent();
            if (father != null)
                return father.getAccessibleContext();
            else
                return null;
        }, null);
    }

    public static void setAccessibleParent(AccessibleContext ac, AccessibleContext pa){
        AtkUtil.invokeInSwing( () -> {
            if (pa instanceof Accessible){
                Accessible father = (Accessible) pa;
                ac.setAccessibleParent(father);
            }
        } );
    }

    public static String getAccessibleName(AccessibleContext ac) {
        return AtkUtil.invokeInSwing(() -> {
            String accessibleName = ac.getAccessibleName();
            if (accessibleName == null) {
                return null;
            }
            final String acceleratorText = getAcceleratorText(ac);
            if (!acceleratorText.isEmpty()) {
                return accessibleName + " " + acceleratorText;
            }
            return accessibleName;
        }, "");
    }

    /**
     * Returns the JMenuItem accelerator. Similar implementation is used on
     * macOS, see CAccessibility.getAcceleratorText(AccessibleContext) in OpenJDK, and
     * on Windows, see AccessBridge.getAccelerator(AccessibleContext) in OpenJDK.
     */
    private static String getAcceleratorText(AccessibleContext ac) {
        String accText = "";
        Accessible parent = ac.getAccessibleParent();
        if (parent != null) {
            int indexInParent = ac.getAccessibleIndexInParent();
            Accessible child = parent.getAccessibleContext()
                    .getAccessibleChild(indexInParent);
            if (child instanceof JMenuItem) {
                JMenuItem menuItem = (JMenuItem)child;
                KeyStroke keyStroke = menuItem.getAccelerator();
                if (keyStroke != null) {
                    int modifiers = keyStroke.getModifiers();
                    String modifiersText = modifiers > 0 ? InputEvent.getModifiersExText(modifiers) : "";

                    int keyCode = keyStroke.getKeyCode();
                    String keyCodeText = keyCode != 0 ? KeyEvent.getKeyText(keyCode) : String.valueOf(keyStroke.getKeyChar());

                    accText += modifiersText;
                    if (!modifiersText.isEmpty() && !keyCodeText.isEmpty()) {
                        accText += "+";
                    }
                    accText += keyCodeText;
                }
            }
        }
        return accText;
    }

    public static void setAccessibleName(AccessibleContext ac, String name){
        AtkUtil.invokeInSwing( () -> { ac.setAccessibleName(name); } );
    }

    public static String getAccessibleDescription(AccessibleContext ac){
        return AtkUtil.invokeInSwing( () -> { return ac.getAccessibleDescription(); }, "");
    }

    public static void setAccessibleDescription(AccessibleContext ac, String description){
        AtkUtil.invokeInSwing( () -> { ac.setAccessibleDescription(description); } );
    }

    public static int getAccessibleChildrenCount(AccessibleContext ac){
        return AtkUtil.invokeInSwing( () -> { return ac.getAccessibleChildrenCount(); }, 0);
    }

    public static int getAccessibleIndexInParent(AccessibleContext ac){
        return AtkUtil.invokeInSwing( () -> { return ac.getAccessibleIndexInParent(); }, -1);
    }

    public static AccessibleRole getAccessibleRole(AccessibleContext ac){
        return AtkUtil.invokeInSwing( () -> { return ac.getAccessibleRole(); }, AccessibleRole.UNKNOWN);
    }

    public static boolean equalsIgnoreCaseLocaleWithRole(AccessibleRole role){
        String displayString = role.toDisplayString(Locale.US);
        return displayString.equalsIgnoreCase("paragraph");
    }

    public static AccessibleState[] getArrayAccessibleState(AccessibleContext ac){
        return AtkUtil.invokeInSwing( () -> {
            AccessibleStateSet stateSet = ac.getAccessibleStateSet();
            if (stateSet == null)
                return null;
            else
                return stateSet.toArray();
        }, null);
    }

    public static String getLocale(AccessibleContext ac){
        return AtkUtil.invokeInSwing( () -> {
            Locale l = ac.getLocale();
            String locale = l.getLanguage();
            String country = l.getCountry();
            String script = l.getScript();
            String variant = l.getVariant();
            if (country.length() != 0) {
                locale += "_" + country;
            }
            if (script.length() != 0) {
                locale += "@" + script;
            }
            if (variant.length() != 0) {
                locale += "@" + variant;
            }
            return locale;
        }, null);
    }

    public static class WrapKeyAndTarget{
        public String key;
        public AccessibleContext[] relations;

        public WrapKeyAndTarget(String key, AccessibleContext[] relations){
            this.key = key;
            this.relations = relations;
        }
    }

    public static WrapKeyAndTarget[] getArrayAccessibleRelation(AccessibleContext ac){
        WrapKeyAndTarget[] d = new WrapKeyAndTarget[0];
        return AtkUtil.invokeInSwing( () -> {
            AccessibleRelationSet relationSet = ac.getAccessibleRelationSet();
            if (relationSet == null)
                return d;
            else {
                AccessibleRelation[] array = relationSet.toArray();
                WrapKeyAndTarget[] result = new WrapKeyAndTarget[array.length];
                for(int i = 0; i < array.length; i++) {
                    String key = array[i].getKey();
                    Object[] objs = array[i].getTarget();
                    AccessibleContext[] contexts = new AccessibleContext[objs.length];
                    for(int j = 0; j < objs.length; j++) {
                        if (objs[i] instanceof Accessible)
                            contexts[i] = ( (Accessible) objs[i]).getAccessibleContext();
                        else
                            contexts[i] = null;
                    }
                    result[i] = new WrapKeyAndTarget(key, contexts);
                }
                return result;
            }
        }, d);
    }

    public static AccessibleContext getAccessibleChild(AccessibleContext ac, int i){
        return AtkUtil.invokeInSwing( () -> {
            Accessible child = ac.getAccessibleChild(i);
            if (child == null)
                return null;
            else
                return child.getAccessibleContext();
        }, null);
    }

    public static int hashCode(AccessibleContext ac){
        return AtkUtil.invokeInSwing( () -> { return ac.hashCode(); }, 0);
    }

}