File: AtkText.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 (672 lines) | stat: -rw-r--r-- 18,194 bytes parent folder | download | duplicates (3)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/*
 * Java ATK Wrapper for GNOME
 * Copyright (C) 2009 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.GNOME.Accessibility;

import javax.accessibility.*;
import java.text.*;
import java.awt.Rectangle;
import java.awt.Point;
import java.lang.ref.WeakReference;

public class AtkText {

	WeakReference<AccessibleContext> _ac;
	WeakReference<AccessibleText> _acc_text;
	WeakReference<AccessibleEditableText> _acc_edt_text;

	public class StringSequence {

		public String str;
		public int start_offset, end_offset;

		public StringSequence (String str, int start_offset, int end_offset) {
			this.str = str;
			this.start_offset = start_offset;
			this.end_offset = end_offset;
		}
	}

	public AtkText (AccessibleContext ac) {
		super();
		this._ac = new WeakReference<AccessibleContext>(ac);
		this._acc_text = new WeakReference<AccessibleText>(ac.getAccessibleText());
		this._acc_edt_text = new WeakReference<AccessibleEditableText>(ac.getAccessibleEditableText());
	}

	public static AtkText createAtkText(AccessibleContext ac){
        return AtkUtil.invokeInSwing ( () -> { return new AtkText(ac); }, null);
    }

	public static int getRightStart(int start) {
		if (start < 0)
			return 0;
		return start;
	}

	public static int getRightEnd(int start, int end, int count) {
		if (end < -1)
			return start;
		else
			if (end > count || end == -1)
				return count;
			else
				return end;
	}

	/* Return string from start, up to, but not including end */
	public String get_text (int start, int end) {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return null;

		return AtkUtil.invokeInSwing ( () -> {
			final int rightStart = getRightStart(start);;
			final int rightEnd = getRightEnd(start, end, acc_text.getCharCount());

			if (acc_text instanceof AccessibleExtendedText) {
				AccessibleExtendedText acc_ext_text = (AccessibleExtendedText)acc_text;
				return acc_ext_text.getTextRange(rightStart, rightEnd);
			}
			StringBuffer buf = new StringBuffer();
			for (int i = rightStart; i <= rightEnd-1; i++) {
				String str = acc_text.getAtIndex(AccessibleText.CHARACTER, i);
				buf.append(str);
			}
			return buf.toString();
		}, null);
	}

	public char get_character_at_offset (int offset) {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return ' ';

		return AtkUtil.invokeInSwing ( () -> {
			String str =  acc_text.getAtIndex(AccessibleText.CHARACTER, offset);
			if (str == null || str.length() == 0)
				return ' ';
			return str.charAt(0);
		}, ' ');
	}

	public StringSequence get_text_at_offset (int offset,int boundary_type) {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return null;

		return AtkUtil.invokeInSwing ( () -> {
			if (false && acc_text instanceof AccessibleExtendedText) {
				// FIXME: this is not using start/end boundaries
				AccessibleExtendedText acc_ext_text = (AccessibleExtendedText)acc_text;
				int part = getPartTypeFromBoundary(boundary_type);
				if (part == -1)
					return null;
				AccessibleTextSequence seq = acc_ext_text.getTextSequenceAt(part, offset);
				if (seq == null)
					return null;
				return new StringSequence(seq.text, seq.startIndex, seq.endIndex+1);
			} else {
				return private_get_text_at_offset(offset, boundary_type);
			}
		}, null);
	}

	public StringSequence get_text_before_offset (int offset,int boundary_type) {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return null;

		return AtkUtil.invokeInSwing ( () -> {
			if (false && acc_text instanceof AccessibleExtendedText) {
				// FIXME: this is not using start/end boundaries
				AccessibleExtendedText acc_ext_text = (AccessibleExtendedText)acc_text;
				int part = getPartTypeFromBoundary(boundary_type);
				if (part == -1)
					return null;
				AccessibleTextSequence seq = acc_ext_text.getTextSequenceBefore(part, offset);
				if (seq == null)
					return null;
				return new StringSequence(seq.text, seq.startIndex, seq.endIndex+1);
			} else {
				StringSequence seq = private_get_text_at_offset(offset, boundary_type);
				if (seq == null)
					return null;
				return private_get_text_at_offset(seq.start_offset-1, boundary_type);
			}
		}, null);
	}

	public StringSequence get_text_after_offset (int offset,int boundary_type) {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return null;

		return AtkUtil.invokeInSwing ( () -> {
			if (false && acc_text instanceof AccessibleExtendedText) {
				// FIXME: this is not using start/end boundaries
				AccessibleExtendedText acc_ext_text = (AccessibleExtendedText)acc_text;
				int part = getPartTypeFromBoundary(boundary_type);
				if (part == -1)
					return null;
				AccessibleTextSequence seq = acc_ext_text.getTextSequenceAfter(part, offset);
				if (seq == null)
					return null;
				return new StringSequence(seq.text, seq.startIndex, seq.endIndex+1);
			} else {
				StringSequence seq = private_get_text_at_offset(offset, boundary_type);
				if (seq == null)
					return null;
				return private_get_text_at_offset(seq.end_offset, boundary_type);
			}
		}, null);
	}

	public int get_caret_offset () {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return 0;

		return AtkUtil.invokeInSwing ( () -> { return acc_text.getCaretPosition(); }, 0);
	}

	public Rectangle get_character_extents (int offset, int coord_type) {
		AccessibleContext ac = _ac.get();
		if (ac == null)
			return null;
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return null;

		return AtkUtil.invokeInSwing ( () -> {
			Rectangle rect = acc_text.getCharacterBounds(offset);
			if (rect == null)
				return null;
			AccessibleComponent component = ac.getAccessibleComponent();
			if (component == null)
				return null;
			Point p = AtkComponent.getComponentOrigin(ac, component, coord_type);
			rect.x += p.x;
			rect.y += p.y;
			return rect;
		}, null);
	}

	public int get_character_count () {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return 0;

		return AtkUtil.invokeInSwing ( () -> { return acc_text.getCharCount(); }, 0);
	}

	public int get_offset_at_point (int x, int y, int coord_type) {
		AccessibleContext ac = _ac.get();
		if (ac == null)
			return -1;
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return -1;

		return AtkUtil.invokeInSwing ( () -> {
			AccessibleComponent component = ac.getAccessibleComponent();
			if (component == null)
				return -1;
			Point p = AtkComponent.getComponentOrigin(ac, component, coord_type);
			return acc_text.getIndexAtPoint(new Point(x-p.x, y-p.y));
		}, -1);
	}

	public Rectangle get_range_extents (int start, int end, int coord_type) {
		AccessibleContext ac = _ac.get();
		if (ac == null)
			return null;
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return null;

		return AtkUtil.invokeInSwing ( () -> {
			if (acc_text instanceof AccessibleExtendedText) {
				final int rightStart = getRightStart(start);;
				final int rightEnd = getRightEnd(start, end, acc_text.getCharCount());

				AccessibleExtendedText acc_ext_text = (AccessibleExtendedText)acc_text;
				Rectangle rect = acc_ext_text.getTextBounds(rightStart, rightEnd);
				if (rect == null)
					return null;
				AccessibleComponent component = ac.getAccessibleComponent();
				if (component == null)
					return null;
				Point p = AtkComponent.getComponentOrigin(ac, component, coord_type);
				rect.x += p.x;
				rect.y += p.y;
				return rect;
			}
			return null;
		}, null);
	}

	public int get_n_selections () {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return 0;

		return AtkUtil.invokeInSwing ( () -> {
			String str = acc_text.getSelectedText();
			if (str != null && str.length() > 0)
				return 1;
			return 0;
		}, 0);
	}

	public StringSequence get_selection () {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return null;

		return AtkUtil.invokeInSwing ( () -> {
			int start = acc_text.getSelectionStart();
			int end = acc_text.getSelectionEnd();
			String text = acc_text.getSelectedText();
			if (text == null)
				return null;
			return new StringSequence(text, start, end);
		}, null);
	}

	public boolean add_selection (int start, int end) {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return false;
		AccessibleEditableText acc_edt_text = _acc_edt_text.get();
		if (acc_edt_text == null)
			return false;

		return AtkUtil.invokeInSwing ( () -> {
			if (acc_edt_text == null || get_n_selections() > 0)
				return false;

			final int rightStart = getRightStart(start);;
			final int rightEnd = getRightEnd(start, end, acc_text.getCharCount());

			return set_selection(0, rightStart, rightEnd);
		}, false);
	}

	public boolean remove_selection(int selection_num) {
		AccessibleEditableText acc_edt_text = _acc_edt_text.get();
		if (acc_edt_text == null)
			return false;

		return AtkUtil.invokeInSwing ( () -> {
			if (acc_edt_text == null || selection_num > 0)
				return false;
			acc_edt_text.selectText(0, 0);
			return true;
		}, false);
	}

	public boolean set_selection (int selection_num, int start, int end) {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return false;
		AccessibleEditableText acc_edt_text = _acc_edt_text.get();
		if (acc_edt_text == null)
			return false;

		return AtkUtil.invokeInSwing ( () -> {
			if (acc_edt_text == null || selection_num > 0)
				return false;

			final int rightStart = getRightStart(start);;
			final int rightEnd = getRightEnd(start, end, acc_text.getCharCount());

			acc_edt_text.selectText(rightStart, rightEnd);
			return true;
		}, false);
	}

	public boolean set_caret_offset (int offset) {
		AccessibleText acc_text = _acc_text.get();
		if (acc_text == null)
			return false;
		AccessibleEditableText acc_edt_text = _acc_edt_text.get();
		if (acc_edt_text == null)
			return false;

		return AtkUtil.invokeInSwing ( () -> {
			if (acc_edt_text != null) {
				final int rightOffset = getRightEnd(0, offset, acc_text.getCharCount());
				acc_edt_text.selectText(offset, offset);
				return true;
			}
			return false;
		}, false);
	}

	private int getPartTypeFromBoundary (int boundary_type) {
		switch (boundary_type) {
			case AtkTextBoundary.CHAR :
				return AccessibleText.CHARACTER;
			case AtkTextBoundary.WORD_START :
			case AtkTextBoundary.WORD_END :
				return AccessibleText.WORD;
			case AtkTextBoundary.SENTENCE_START :
			case AtkTextBoundary.SENTENCE_END :
				return AccessibleText.SENTENCE;
			case AtkTextBoundary.LINE_START :
			case AtkTextBoundary.LINE_END :
				return AccessibleExtendedText.LINE;
			default :
				return -1;
		}
	}

	private int getNextWordStart (int offset, String str) {
		BreakIterator words = BreakIterator.getWordInstance();
		words.setText(str);
		int start = words.following(offset);
		int end = words.next();

		while (end != BreakIterator.DONE) {
			for (int i = start; i < end; i++) {
				if (Character.isLetter(str.codePointAt(i))) {
					return start;
				}
			}

			start = end;
			end = words.next();
		}

		return BreakIterator.DONE;
	}

	private int getNextWordEnd (int offset, String str) {
		int start = getNextWordStart(offset, str);

		BreakIterator words = BreakIterator.getWordInstance();
		words.setText(str);
		int next = words.following(offset);

		if (start == next) {
			return words.following(start);
		} else {
			return next;
		}
	}

	private int getPreviousWordStart (int offset, String str) {
		BreakIterator words = BreakIterator.getWordInstance();
		words.setText(str);
		int start = words.preceding(offset);
		int end = words.next();

		while (start != BreakIterator.DONE) {
			for (int i = start; i < end; i++) {
				if (Character.isLetter(str.codePointAt(i))) {
					return start;
				}
			}

			end = start;
			start = words.preceding(end);
		}

		return BreakIterator.DONE;
	}

	private int getPreviousWordEnd (int offset, String str) {
		int start = getPreviousWordStart(offset, str);

		BreakIterator words = BreakIterator.getWordInstance();
		words.setText(str);
		int pre = words.preceding(offset);

		if (start == pre) {
			return words.preceding(start);
		} else {
			return pre;
		}
	}

	private int getNextSentenceStart (int offset, String str) {
		BreakIterator sentences = BreakIterator.getSentenceInstance();
		sentences.setText(str);
		int start = sentences.following(offset);

		return start;
	}

	private int getNextSentenceEnd (int offset, String str) {
		int start = getNextSentenceStart(offset, str);
		if (start == BreakIterator.DONE) {
			return str.length();
		}

		int index = start;
		do {
			index --;
		} while (index >= 0 && Character.isWhitespace(str.charAt(index)));

		index ++;
		if (index < offset) {
			start = getNextSentenceStart(start, str);
			if (start == BreakIterator.DONE) {
				return str.length();
			}

			index = start;
			do {
				index --;
			} while (index >= 0 && Character.isWhitespace(str.charAt(index)));

			index ++;
		}

		return index;
	}

	private int getPreviousSentenceStart (int offset, String str) {
		BreakIterator sentences = BreakIterator.getSentenceInstance();
		sentences.setText(str);
		int start = sentences.preceding(offset);

		return start;
	}

	private int getPreviousSentenceEnd (int offset, String str) {
		int start = getPreviousSentenceStart(offset, str);
		if (start == BreakIterator.DONE) {
			return 0;
		}

		int end = getNextSentenceEnd(start, str);
		if (offset < end) {
			start = getPreviousSentenceStart(start, str);
			if (start == BreakIterator.DONE) {
				return 0;
			}

			end = getNextSentenceEnd(start, str);
		}

		return end;
	}

	private int getNextLineStart (int offset, String str) {
		int max = str.length();
		while (offset < max) {
			if (str.charAt(offset) == '\n')
				return offset+1;
			offset += 1;
		}
		return offset;
	}

	private int getPreviousLineStart (int offset, String str) {
		offset -= 2;
		while (offset >= 0) {
			if (str.charAt(offset) == '\n')
				return offset+1;
			offset -= 1;
		}
		return 0;
	}

	private int getNextLineEnd (int offset, String str) {
		int max = str.length();
		offset += 1;
		while (offset < max) {
			if (str.charAt(offset) == '\n')
				return offset;
			offset += 1;
		}
		return offset;
	}

	private int getPreviousLineEnd (int offset, String str) {
		offset -= 1;
		while (offset >= 0) {
			if (str.charAt(offset) == '\n')
				return offset;
			offset -= 1;
		}
		return 0;
	}

	private StringSequence private_get_text_at_offset (int offset,
			int boundary_type) {
		int char_count = get_character_count();
		if (offset < 0 || offset > char_count) {
			return null;
		}

		switch (boundary_type) {
			case AtkTextBoundary.CHAR :
			{
				if (offset == char_count)
					return null;
				String str = get_text(offset, offset+1);
				return new StringSequence(str, offset, offset+1);
			}
			case AtkTextBoundary.WORD_START :
			{
				if (offset == char_count)
					return new StringSequence("", char_count, char_count);

				String s = get_text(0, char_count);
				int start = getPreviousWordStart(offset+1, s);
				if (start == BreakIterator.DONE) {
					start = 0;
				}

				int end = getNextWordStart(offset, s);
				if (end == BreakIterator.DONE) {
					end = s.length();
				}

				String str = get_text(start, end);
				return new StringSequence(str, start, end);
			}
			case AtkTextBoundary.WORD_END :
			{
				if (offset == 0)
					return new StringSequence("", 0, 0);

				String s = get_text(0, char_count);
				int start = getPreviousWordEnd(offset, s);
				if (start == BreakIterator.DONE) {
					start = 0;
				}

				int end = getNextWordEnd(offset-1, s);
				if (end == BreakIterator.DONE) {
					end = s.length();
				}

				String str = get_text(start, end);
				return new StringSequence(str, start, end);
			}
			case AtkTextBoundary.SENTENCE_START :
			{
				if (offset == char_count)
					return new StringSequence("", char_count, char_count);

				String s = get_text(0, char_count);
				int start = getPreviousSentenceStart(offset+1, s);
				if (start == BreakIterator.DONE) {
					start = 0;
				}

				int end = getNextSentenceStart(offset, s);
				if (end == BreakIterator.DONE) {
					end = s.length();
				}

				String str = get_text(start, end);
				return new StringSequence(str, start, end);
			}
			case AtkTextBoundary.SENTENCE_END :
			{
				if (offset == 0)
					return new StringSequence("", 0, 0);

				String s = get_text(0, char_count);
				int start = getPreviousSentenceEnd(offset, s);
				if (start == BreakIterator.DONE) {
					start = 0;
				}

				int end = getNextSentenceEnd(offset-1, s);
				if (end == BreakIterator.DONE) {
					end = s.length();
				}

				String str = get_text(start, end);
				return new StringSequence(str, start, end);
			}
			case AtkTextBoundary.LINE_START :
			{
				if (offset == char_count)
					return new StringSequence("", char_count, char_count);

				String s = get_text(0, char_count);
				int start = getPreviousLineStart(offset+1, s);
				int end = getNextLineStart(offset, s);

				String str = get_text(start, end);
				return new StringSequence(str, start, end);
			}
			case AtkTextBoundary.LINE_END :
			{
				String s = get_text(0, char_count);
				int start = getPreviousLineEnd(offset, s);
				int end = getNextLineEnd(offset-1, s);

				String str = get_text(start, end);
				return new StringSequence(str, start, end);
			}
			default :
			{
				return null;
			}
		}
	}
}