summaryrefslogtreecommitdiff
path: root/accessibility/workben/org/openoffice/accessibility/awb/view/TextView.java
blob: 56f4c8758e61b14a5c11041570d589fa4ea513cc (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
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
/*************************************************************************
 *
 *  $RCSfile: TextView.java,v $
 *
 *  $Revision: 1.2 $
 *
 *  last change: $Author: obr $ $Date: 2008/05/14 13:21:35 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  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., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (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.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

package org.openoffice.accessibility.awb.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTree;
import javax.swing.tree.TreeNode;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;

import com.sun.star.accessibility.AccessibleEventId;
import com.sun.star.accessibility.AccessibleEventObject;
import com.sun.star.accessibility.AccessibleTextType;
import com.sun.star.accessibility.AccessibleStateType;
import com.sun.star.accessibility.TextSegment;
import com.sun.star.accessibility.XAccessibleText;
import com.sun.star.accessibility.XAccessibleContext;
import com.sun.star.accessibility.XAccessibleMultiLineText;
import com.sun.star.accessibility.XAccessibleStateSet;
import com.sun.star.awt.Point;
import com.sun.star.awt.Rectangle;
import com.sun.star.beans.PropertyValue;
import com.sun.star.lang.IndexOutOfBoundsException;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.uno.UnoRuntime;

import org.openoffice.accessibility.awb.view.text.CaretSpinnerModel;
import org.openoffice.accessibility.awb.view.text.TextDialogFactory;


public class TextView
    extends ObjectView
    implements ActionListener
{

    /** Create a TextView when the given object supports the
        XAccessibleText interface.
    */
    static public ObjectView Create (
        ObjectViewContainer aContainer,
        XAccessibleContext xContext)
    {
        XAccessibleText xText = (XAccessibleText)UnoRuntime.queryInterface(
                XAccessibleText.class, xContext);
        if (xText != null)
            return new TextView (aContainer);
        else
            return null;
    }


    public TextView (ObjectViewContainer aContainer)
    {
        super (aContainer);

        ViewGridLayout aLayout = new ViewGridLayout (this);

        maTextLabel = aLayout.AddLabeledString ("Text: ");
        maCharacterArrayLabel = aLayout.AddLabeledEntry ("Characters: ");
        maCharacterCountLabel = aLayout.AddLabeledEntry ("Character Count: ");
        maSelectionLabel = aLayout.AddLabeledEntry ("Selection: ");
        maBoundsLabel = aLayout.AddLabeledEntry ("Bounds Test: ");
        maCaretPositionSpinner = (JSpinner)aLayout.AddLabeledComponent (
            "Caret position:", new JSpinner());
        Dimension aSize = maCaretPositionSpinner.getSize();
        maCaretPositionSpinner.setPreferredSize (new Dimension (100,20));
        maCaretLineNoLabel = aLayout.AddLabeledEntry ("Line number at caret: ");
        maCaretLineTextLabel = aLayout.AddLabeledEntry ("Text of line at caret: ");
        maLineNoFromCaretPosLabel = aLayout.AddLabeledEntry ("Line number at index of caret: ");
        maLineTextFromCaretPosLabel = aLayout.AddLabeledEntry ("Text of line at index of caret: ");

        JPanel aButtonPanel = new JPanel ();
        aLayout.AddComponent (aButtonPanel);

        JButton aButton = new JButton ("select...");
        aButton.setFont (aLayout.GetFont());
        aButton.addActionListener (this);
        aButtonPanel.add (aButton);

        aButton = new JButton ("copy...");
        aButton.setFont (aLayout.GetFont());
        aButton.addActionListener (this);
        aButtonPanel.add (aButton);

        // A tree that holds the text broken down into various segments.
        maTree = new JTree ();
        aLayout.AddComponent (new JScrollPane (
            maTree,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
    }


    /** Additionally to the context store a reference to the
        XAccessibleText interface.
    */
    public void SetObject (XAccessibleContext xObject)
    {
        mxText = (XAccessibleText)UnoRuntime.queryInterface(
            XAccessibleText.class, xObject);
        maCaretSpinnerModel = new CaretSpinnerModel(mxText);
        maCaretPositionSpinner.setModel (maCaretSpinnerModel);
        super.SetObject (xObject);
    }

    synchronized public void Destroy ()
    {
        mxText = null;
        super.Destroy();
    }

    synchronized public void Update ()
    {
        maCaretPositionSpinner.setEnabled (mxText != null);
        DefaultMutableTreeNode aRoot = new DefaultMutableTreeNode ("Text Segments");
        if (mxText == null)
        {
            maTextLabel.setText ("<null object>");
            maCharacterArrayLabel.setText ("<null object>");
            maCharacterCountLabel.setText ("<null object>");
            maSelectionLabel.setText ("<null object>");
            maBoundsLabel.setText ("<null object>");
            maCaretLineNoLabel.setText ("<null object>");
            maCaretLineTextLabel.setText ("<null object>");
            maLineNoFromCaretPosLabel.setText ("<null object>");
            maLineTextFromCaretPosLabel.setText ("<null object>");
        }
        else
        {
            maTextLabel.setText (mxText.getText());
            maCharacterArrayLabel.setText (GetCharacterArray());
            maCharacterCountLabel.setText (
                Integer.toString(mxText.getCharacterCount()));
            // Selection.
            maSelectionLabel.setText (
                "[" + mxText.getSelectionStart()
                + "," + mxText.getSelectionEnd()
                + "] \"" + mxText.getSelectedText() + "\"");

            // Character bounds.
            maBoundsLabel.setText (GetTextBoundsString());

            // Caret position.
            maCaretPositionSpinner.setValue (new Integer (mxText.getCaretPosition()));

            // Multi line methods.
            XAccessibleMultiLineText xMultiText = (XAccessibleMultiLineText)
                UnoRuntime.queryInterface( XAccessibleMultiLineText.class, mxText );

            if( null != xMultiText ) {
                try {
                  maCaretLineNoLabel.setText ( Integer.toString( xMultiText.getNumberOfLineWithCaret() ) );
                  TextSegment ts = xMultiText.getTextAtLineWithCaret();
                  maCaretLineTextLabel.setText ( "[" + ts.SegmentStart
                     + "," + ts.SegmentEnd
                     + "] \"" + ts.SegmentText + "\"");
                  maLineNoFromCaretPosLabel.setText ( Integer.toString( xMultiText.getLineNumberAtIndex( mxText.getCaretPosition() ) ) );
                  ts = xMultiText.getTextAtLineNumber(xMultiText.getLineNumberAtIndex( mxText.getCaretPosition() ) );
                  maLineTextFromCaretPosLabel.setText  ( "[" + ts.SegmentStart
                    + "," + ts.SegmentEnd
                    + "] \"" + ts.SegmentText + "\"");
                } catch( IndexOutOfBoundsException e) {
                }
            }

            // Text segments.
            aRoot.add (CreateNode ("Character", AccessibleTextType.CHARACTER));
            aRoot.add (CreateNode ("Word", AccessibleTextType.WORD));
            aRoot.add (CreateNode ("Sentence", AccessibleTextType.SENTENCE));
            aRoot.add (CreateNode ("Paragraph", AccessibleTextType.PARAGRAPH));
            aRoot.add (CreateNode ("Line", AccessibleTextType.LINE));
            aRoot.add (CreateNode ("Attribute", AccessibleTextType.ATTRIBUTE_RUN));
            aRoot.add (CreateNode ("Glyph", AccessibleTextType.GLYPH));
        }
        ((DefaultTreeModel)maTree.getModel()).setRoot (aRoot);
    }

    public String GetTitle ()
    {
        return ("Text");
    }

    public void notifyEvent (AccessibleEventObject aEvent)
    {
        System.out.println (aEvent);
        switch (aEvent.EventId)
        {
            case AccessibleEventId.CARET_CHANGED :
                maCaretSpinnerModel.Update();
                Update ();
                break;

            case AccessibleEventId.TEXT_CHANGED :
            case AccessibleEventId.TEXT_SELECTION_CHANGED:
                Update ();
                break;
        }
    }

    public void actionPerformed (ActionEvent aEvent)
    {
        String sCommand = aEvent.getActionCommand();
        if (sCommand.equals ("select..."))
            TextDialogFactory.CreateSelectionDialog (mxContext);
        else if (sCommand.equals ("copy..."))
            TextDialogFactory.CreateCopyDialog (mxContext);
    }



    /** Create a string that is a list of all characters returned by the
        getCharacter() method.
    */
    private String GetCharacterArray ()
    {
        // Do not show more than 30 characters.
        int nCharacterCount = mxText.getCharacterCount();
        int nMaxDisplayCount = 30;

        // build up string
        StringBuffer aCharacterArray = new StringBuffer();
        int nIndex = 0;
        try
        {
            while (nIndex<nCharacterCount && nIndex<nMaxDisplayCount)
            {
                aCharacterArray.append (mxText.getCharacter (nIndex));
                if (nIndex < nCharacterCount-1)
                    aCharacterArray.append (",");
                nIndex ++;
            }
            if (nMaxDisplayCount < nCharacterCount)
                aCharacterArray.append (", ...");
        }
        catch (IndexOutOfBoundsException e)
        {
            aCharacterArray.append ("; Index Out Of Bounds at index " + nIndex);
        }

        return aCharacterArray.toString();
    }



    /** Iterate over all characters and translate their positions
        back and forth.
        */
    private String GetTextBoundsString ()
    {
        StringBuffer aBuffer = new StringBuffer ();
        try
        {
            // Iterate over all characters in the text.
            int nCount = mxText.getCharacterCount();
            for (int i=0; i<nCount; i++)
            {
                // Get bounds for this character.
                Rectangle aBBox = mxText.getCharacterBounds (i);

                // get the character by 'clicking' into the middle of
                // the bounds
                Point aMiddle = new Point();
                aMiddle.X = aBBox.X + (aBBox.Width / 2) - 1;
                aMiddle.Y = aBBox.Y + (aBBox.Height / 2) - 1;
                int nIndex = mxText.getIndexAtPoint (aMiddle);

                // get the character, or a '#' for an illegal index
                if ((nIndex >= 0) && (nIndex < mxText.getCharacter(i)))
                    aBuffer.append (mxText.getCharacter(nIndex));
                else
                    aBuffer.append ('#');
            }
        }
        catch (IndexOutOfBoundsException aEvent)
        {
            // Ignore errors.
        }

        return aBuffer.toString();
    }




    private final static int BEFORE = -1;
    private final static int AT = 0;
    private final static int BEHIND = +1;

    private MutableTreeNode CreateNode (String sTitle, short nTextType)
    {
        DefaultMutableTreeNode aNode = new DefaultMutableTreeNode (sTitle);

        aNode.add (CreateSegmentNode ("Before", nTextType, BEFORE));
        aNode.add (CreateSegmentNode ("At", nTextType, AT));
        aNode.add (CreateSegmentNode ("Behind", nTextType, BEHIND));

        return aNode;
    }

    private MutableTreeNode CreateSegmentNode (String sTitle, short nTextType, int nWhere)
    {
        TextSegment aSegment;
        int nTextLength = mxText.getCharacterCount();
        DefaultMutableTreeNode aNode = new DefaultMutableTreeNode (sTitle);
        for (int nIndex=0; nIndex<=nTextLength; /* empty */)
        {
            aSegment = GetTextSegment (nIndex, nTextType, nWhere);
            DefaultMutableTreeNode aSegmentNode = new DefaultMutableTreeNode (
                new StringBuffer (
                    Integer.toString (nIndex) + " -> "
                    + Integer.toString (aSegment.SegmentStart) + " - "
                    + Integer.toString (aSegment.SegmentEnd) + " : "
                    + aSegment.SegmentText.toString()));
            aNode.add (aSegmentNode);
            if (nTextType ==  AccessibleTextType.ATTRIBUTE_RUN)
                AddAttributeNodes (aSegmentNode, aSegment);
            if (aSegment.SegmentEnd > nIndex)
                nIndex = aSegment.SegmentEnd;
            else
                nIndex ++;
        }

        return aNode;
    }


    private TextSegment GetTextSegment (int nIndex, short nTextType, int nWhere)
    {
        TextSegment aSegment;

        try
        {
            switch (nWhere)
            {
                case BEFORE:
                    aSegment = mxText.getTextBeforeIndex (nIndex, nTextType);
                    break;

                case AT:
                    aSegment = mxText.getTextAtIndex (nIndex, nTextType);
                    break;

                case BEHIND:
                    aSegment = mxText.getTextBehindIndex (nIndex, nTextType);
                    break;

                default:
                    aSegment = new TextSegment();
                    aSegment.SegmentText = new String ("unknown position "   + nWhere);
                    aSegment.SegmentStart = nIndex;
                    aSegment.SegmentStart = nIndex+1;
                    break;
            }
        }
        catch (IndexOutOfBoundsException aException)
        {
            aSegment = new TextSegment ();
            aSegment.SegmentText = new String ("Invalid index at ") + nIndex + " : "
                + aException.toString();
            aSegment.SegmentStart = nIndex;
            aSegment.SegmentEnd = nIndex+1;
        }
        catch (IllegalArgumentException aException)
        {
            aSegment = new TextSegment ();
            aSegment.SegmentText = new String ("Illegal argument at ") + nIndex + " : "
                + aException.toString();
            aSegment.SegmentStart = nIndex;
            aSegment.SegmentEnd = nIndex+1;
        }

        return aSegment;
    }


    /** Add to the given node one node for every attribute of the given segment.
    */
    private void AddAttributeNodes (
        DefaultMutableTreeNode aNode,
        TextSegment aSegment)
    {
        try
        {
            PropertyValue[] aValues = mxText.getCharacterAttributes (
                aSegment.SegmentStart, aAttributeList);
            for (int i=0; i<aValues.length; i++)
                aNode.add (new DefaultMutableTreeNode (
                    aValues[i].Name + ": " + aValues[i].Value));
        }
        catch (IndexOutOfBoundsException aException)
        {
            aNode.add (new DefaultMutableTreeNode (
                "caught IndexOutOfBoundsException while retrieveing attributes"));
        }
    }

    private XAccessibleText mxText;
    private JLabel
        maTextLabel,
        maCharacterArrayLabel,
        maCharacterCountLabel,
        maSelectionLabel,
        maBoundsLabel,
        maCaretLineNoLabel,
        maCaretLineTextLabel,
        maLineNoFromCaretPosLabel,
        maLineTextFromCaretPosLabel;

    private JSpinner maCaretPositionSpinner;
    private JTree maTree;
    private CaretSpinnerModel maCaretSpinnerModel;

    private static String[] aAttributeList = new String[] {
        "CharBackColor",
        "CharColor",
        "CharEscapement",
        "CharHeight",
        "CharPosture",
        "CharStrikeout",
        "CharUnderline",
        "CharWeight",
        "ParaAdjust",
        "ParaBottomMargin",
        "ParaFirstLineIndent",
        "ParaLeftMargin",
        "ParaLineSpacing",
        "ParaRightMargin",
        "ParaTabStops"};
}