summaryrefslogtreecommitdiff
path: root/accessibility/bridge/org/openoffice/java/accessibility/AccessibleEditableTextImpl.java
blob: c7202bc5f60e08060d007035a0f9e996ed31d941 (plain)
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
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

package org.openoffice.java.accessibility;

import com.sun.star.awt.*;
import com.sun.star.style.*;
import com.sun.star.accessibility.XAccessibleEditableText;
import com.sun.star.beans.PropertyValue;

import javax.swing.text.StyleConstants;

/** The GenericAccessibleEditableText mapps the calls to the java AccessibleEditableText
 *  interface to the corresponding methods of the UNO XAccessibleEditableText interface.
 */
public class AccessibleEditableTextImpl extends AccessibleTextImpl implements javax.accessibility.AccessibleEditableText {
    final static double toPointFactor = 1 / (7/10 + 34.5);

    /** Creates new GenericAccessibleEditableText object */
    public AccessibleEditableTextImpl(XAccessibleEditableText xAccessibleEditableText) {
        super(xAccessibleEditableText);
    }

    /** Cuts the text between two indices into the system clipboard */
    public void cut(int startIndex, int endIndex) {
        try {
            ((XAccessibleEditableText) unoObject).cutText(startIndex, endIndex);
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
        } catch (com.sun.star.uno.RuntimeException e) {
        }
    }

    /** Deletes the text between two indices */
    public void delete(int startIndex, int endIndex) {
        try {
            ((XAccessibleEditableText) unoObject).deleteText(startIndex, endIndex);
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
        } catch (com.sun.star.uno.RuntimeException e) {
        }
    }

    /** Returns the text range between two indices */
    public String getTextRange(int startIndex, int endIndex) {
        try {
            return unoObject.getTextRange(startIndex, endIndex);
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
        } catch (com.sun.star.uno.RuntimeException e) {
        }
        return null;
    }

    /** Inserts the specified string at the given index */
    public void insertTextAtIndex(int index, String s){
        try {
            ((XAccessibleEditableText) unoObject).insertText(s, index);
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
        } catch (com.sun.star.uno.RuntimeException e) {
        }
    }

    /** Pastes the text form the system clipboard into the text starting at the specified index */
    public void paste(int startIndex) {
        try {
            ((XAccessibleEditableText) unoObject).pasteText(startIndex);
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
        } catch (com.sun.star.uno.RuntimeException e) {
        }
    }

    /** Replaces the text between two indices with the specified string */
    public void replaceText(int startIndex, int endIndex, String s) {
        try {
            ((XAccessibleEditableText) unoObject).replaceText(startIndex, endIndex, s);
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
        } catch (com.sun.star.uno.RuntimeException e) {
        }
    }

    /** Selects the text between two indices */
    public void selectText(int startIndex, int endIndex) {
        try {
            unoObject.setSelection(startIndex, endIndex);
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
        } catch (com.sun.star.uno.RuntimeException e) {
        }
    }

    /** Sets the attributes for the text between two indices */
    public void setAttributes(int startIndex, int endIndex, javax.swing.text.AttributeSet as) {
        java.util.ArrayList<PropertyValue> propertyValues = new java.util.ArrayList<PropertyValue>();

        // Convert Alignment attribute
        Object attribute = as.getAttribute(StyleConstants.Alignment);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "ParaAdjust";

            switch (StyleConstants.getAlignment(as)) {
                case StyleConstants.ALIGN_RIGHT:
                    propertyValue.Value = ParagraphAdjust.RIGHT;
                    break;
                case StyleConstants.ALIGN_CENTER:
                    propertyValue.Value = ParagraphAdjust.CENTER;
                    break;
                case StyleConstants.ALIGN_JUSTIFIED:
                    propertyValue.Value = ParagraphAdjust.BLOCK;
                    break;
                default:
                    propertyValue.Value = ParagraphAdjust.LEFT;
                    break;
            }
            propertyValues.add(propertyValue);
        }

        // Convert Background attribute
        attribute = as.getAttribute(StyleConstants.Background);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "CharBackColor";
            propertyValue.Value = new Integer(StyleConstants.getBackground(as).getRGB());
            propertyValues.add(propertyValue);
        }

        // FIXME: BidiLevel

        // Set Bold attribute
        attribute = as.getAttribute(StyleConstants.Bold);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "CharWeight";
            if (StyleConstants.isBold(as)) {
                propertyValue.Value =  new Float(150);
            } else {
                propertyValue.Value =  new Float(100);
            }
            propertyValues.add(propertyValue);
        }

        // FIXME: Java 1.4 ComponentAttribute, ComponentElementName, ComposedTextAttribute

        // Set FirstLineIndent attribute
        attribute = as.getAttribute(StyleConstants.FirstLineIndent);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "ParaFirstLineIndent";
            propertyValue.Value = new Double(StyleConstants.getFirstLineIndent(as) / toPointFactor);
            propertyValues.add(propertyValue);
        }

        // Set font family attribute
        attribute = as.getAttribute(StyleConstants.FontFamily);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "CharFontPitch";

            if (StyleConstants.getFontFamily(as).equals( "Proportional" )) {
                propertyValue.Value = new Short("2");
            } else {
                propertyValue.Value = new Short("1");
            }
            propertyValues.add(propertyValue);
        }

        // Set font size attribute
        attribute = as.getAttribute(StyleConstants.FontSize);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "CharHeight";
            propertyValue.Value = new Integer(StyleConstants.getFontSize(as));
            propertyValues.add(propertyValue);
        }

        // Map foreground color
        attribute = as.getAttribute(StyleConstants.Foreground);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "CharColor";
            propertyValue.Value = new Integer (StyleConstants.getForeground(as).getRGB());
            propertyValues.add(propertyValue);
        }

        // FIXME: IconAttribute, IconElementName

        // Set italic attribute
        attribute = as.getAttribute(StyleConstants.Italic);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "CharPosture";

            if (StyleConstants.isItalic(as)) {
                propertyValue.Value = FontSlant.ITALIC;
            } else {
                propertyValue.Value = FontSlant.DONTKNOW;
            }
            propertyValues.add(propertyValue);
        }

        // Set left indent attribute
        attribute = as.getAttribute(StyleConstants.LeftIndent);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "ParaFirstLeftMargin";
            propertyValue.Value = new Integer(new Double(StyleConstants.getLeftIndent(as) / toPointFactor).intValue());
            propertyValues.add(propertyValue);
        }

        // Set right indent attribute
        attribute = as.getAttribute(StyleConstants.RightIndent);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "ParaFirstRightMargin";
            propertyValue.Value = new Integer(new Double(StyleConstants.getRightIndent(as) / toPointFactor).intValue());
            propertyValues.add(propertyValue);
        }

        // Set line spacing attribute
        attribute = as.getAttribute(StyleConstants.LineSpacing);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "ParaLineSpacing";
            propertyValue.Value = new Integer(new Double(StyleConstants.getLineSpacing(as) / toPointFactor).intValue());
            propertyValues.add(propertyValue);
        }

        // FIXME: Java 1.4 NameAttribute, Orientation, ResolveAttribute

        // Set space above attribute
        attribute = as.getAttribute(StyleConstants.SpaceAbove);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "ParaTopMargin";
            propertyValue.Value = new Integer(new Double( StyleConstants.getSpaceAbove(as) / toPointFactor).intValue());
            propertyValues.add(propertyValue);
        }

        // Set space below attribute
        attribute = as.getAttribute(StyleConstants.SpaceBelow);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
                        propertyValue.Name = "ParaBottomMargin";
            propertyValue.Value = new Integer(new Double(StyleConstants.getSpaceBelow(as) / toPointFactor).intValue());
            propertyValues.add(propertyValue);
        }

        // Set strike through attribute
        attribute = as.getAttribute(StyleConstants.StrikeThrough);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
                        propertyValue.Name = "CharPosture";
            if (StyleConstants.isStrikeThrough(as)) {
                propertyValue.Value = new Short(FontStrikeout.SINGLE);
            } else {
                propertyValue.Value = new Short(FontStrikeout.NONE);
            }
            propertyValues.add(propertyValue);
        }

        // Set sub-/superscript attribute
        attribute = as.getAttribute(StyleConstants.Superscript);
        if (null == attribute) {
            attribute = as.getAttribute(StyleConstants.Subscript);
        }
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "CharEscapement";

            if (StyleConstants.isSuperscript(as)) {
                propertyValue.Value = new Short( "1" );
            } else if (StyleConstants.isSubscript(as)) {
                propertyValue.Value = new Short( "-1" );
            } else {
                propertyValue.Value = new Short( "0" );
            }
            propertyValues.add(propertyValue);
        }

        // Set tabset attribute
        attribute = as.getAttribute(StyleConstants.TabSet);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "ParaTabStops";

            javax.swing.text.TabSet tabSet = StyleConstants.getTabSet(as);
            java.util.ArrayList<TabStop> tabStops = new java.util.ArrayList<TabStop>(tabSet.getTabCount());

            for (int i = 0, max = tabSet.getTabCount(); i < max; i++) {
                javax.swing.text.TabStop tab = tabSet.getTab(i);
                com.sun.star.style.TabStop unoTab = new com.sun.star.style.TabStop();

                unoTab.Position = new Double(tab.getPosition() / toPointFactor).intValue();

                switch (tab.getAlignment()) {
                    case javax.swing.text.TabStop.ALIGN_CENTER:
                        unoTab.Alignment = TabAlign.CENTER;
                        break;
                    case javax.swing.text.TabStop.ALIGN_RIGHT:
                        unoTab.Alignment = TabAlign.RIGHT;
                        break;
                    case javax.swing.text.TabStop.ALIGN_DECIMAL:
                        unoTab.Alignment = TabAlign.DECIMAL;
                        break;
                    default:
                        unoTab.Alignment = TabAlign.LEFT;
                        break;
                }

                tabStops.add(unoTab);
            }
            propertyValue.Value = tabStops.toArray();
            propertyValues.add(propertyValue);
        }

        // Set underline attribute
        attribute = as.getAttribute(StyleConstants.Underline);
        if (null != attribute) {
            com.sun.star.beans.PropertyValue propertyValue = new com.sun.star.beans.PropertyValue();
            propertyValue.Name = "CharUnderline";

            if (StyleConstants.isUnderline(as)) {
                propertyValue.Value = new Short(FontUnderline.SINGLE);
            } else {
                propertyValue.Value = new Short(FontUnderline.NONE);
            }
            propertyValues.add(propertyValue);
        }

        try {
            ((XAccessibleEditableText) unoObject).setAttributes(startIndex, endIndex, (com.sun.star.beans.PropertyValue[]) propertyValues.toArray());
        } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
        } catch (com.sun.star.uno.RuntimeException e) {
        }
    }

    /** Sets the text contents to the specified string */
    public void setTextContents(String s) {
        try {
            ((XAccessibleEditableText) unoObject).setText(s);
        } catch (com.sun.star.uno.RuntimeException e) {
        }
    }
}