File: AtkEditableText.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 (132 lines) | stat: -rw-r--r-- 4,418 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
/*
 * 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.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import javax.swing.text.*;
import java.lang.ref.WeakReference;

public class AtkEditableText extends AtkText {

  WeakReference<AccessibleEditableText> _acc_edt_text;

  public AtkEditableText (AccessibleContext ac) {
    super(ac);
    _acc_edt_text = new WeakReference<AccessibleEditableText>(ac.getAccessibleEditableText());
  }

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

  public void set_text_contents (String s) {
      AccessibleEditableText acc_edt_text = _acc_edt_text.get();
      if (acc_edt_text == null)
          return;

      AtkUtil.invokeInSwing( () -> {
          acc_edt_text.setTextContents(s);
      });
  }

    public void insert_text (String s, int position) {
        AccessibleEditableText acc_edt_text = _acc_edt_text.get();
        if (acc_edt_text == null)
            return;

        if (position < 0)
            position = 0;
        final int rightPosition = position;
        AtkUtil.invokeInSwing( () -> { acc_edt_text.insertTextAtIndex(rightPosition, s); });
    }

    public void copy_text (int start, int end) {
        AccessibleEditableText acc_edt_text = _acc_edt_text.get();
        if (acc_edt_text == null)
            return;

        int n = acc_edt_text.getCharCount();
        if (start < 0) {
            start = 0;
        }
        if (end > n || end == -1) {
            end = n;
        } else if (end < -1) {
            end = 0;
        }
        final int rightStart = start;
        final int rightEnd = end;
        AtkUtil.invokeInSwing ( () -> {
            String s = acc_edt_text.getTextRange(rightStart, rightEnd);
            if (s != null) {
                StringSelection stringSel = new StringSelection(s);
                Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSel, stringSel);
            }
        });
    }

    public void cut_text (int start, int end) {
        AccessibleEditableText acc_edt_text = _acc_edt_text.get();
        if (acc_edt_text == null)
            return;

        AtkUtil.invokeInSwing( () -> { acc_edt_text.cut(start, end); });
    }

    public void delete_text (int start, int end) {
        AccessibleEditableText acc_edt_text = _acc_edt_text.get();
        if (acc_edt_text == null)
            return;

        AtkUtil.invokeInSwing( () -> { acc_edt_text.delete(start, end); });
    }

    public void paste_text (int position) {
        AccessibleEditableText acc_edt_text = _acc_edt_text.get();
        if (acc_edt_text == null)
            return;

        AtkUtil.invokeInSwing( () -> { acc_edt_text.paste(position); });
    }

    /**
    * Sets run attributes for the text between two indices.
    *
    * @param as the AttributeSet for the text
    * @param start the start index of the text as an int
    * @param end the end index for the text as an int
    * @return whether setRunAttributes was called
    * TODO return is a bit presumptious. This should ideally include a check for whether
    *      attributes were set.
    */
    public boolean setRunAttributes(AttributeSet as, int start, int end) {
        AccessibleEditableText acc_edt_text = _acc_edt_text.get();
        if (acc_edt_text == null)
            return false;

        return AtkUtil.invokeInSwing( () -> {
            acc_edt_text.setAttributes(start, end, as);
            return true;
        }, false);
    }

}