2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 import com
.sun
.star
.accessibility
.*;
20 import com
.sun
.star
.uno
.UnoRuntime
;
22 import javax
.swing
.tree
.*;
23 import javax
.swing
.event
.*;
25 /** listen to tree model changes in order to update XAccessibleText objects
27 class TextUpdateListener
implements TreeModelListener
29 public void treeNodesChanged(TreeModelEvent e
)
32 // if the change is to the first child of a DefaultMutableTreeNode
33 // with an XAccessibleText child, then we call updateText
34 int[] aIndices
= e
.getChildIndices();
35 if( (aIndices
!= null) &&
36 (aIndices
.length
> 0) )
38 // we have a parent... lets check for XAccessibleText then
39 DefaultMutableTreeNode aParent
= (DefaultMutableTreeNode
)
40 (e
.getTreePath().getLastPathComponent());
41 DefaultMutableTreeNode aNode
= (DefaultMutableTreeNode
)
42 (aParent
.getChildAt(aIndices
[0]));
43 if( aParent
.getUserObject() instanceof XAccessibleText
)
45 // aha! we have an xText. So we can now check for
46 // the various cases we support
47 XAccessibleText xText
=
48 (XAccessibleText
)aParent
.getUserObject();
50 if( aIndices
[0] == 0 )
52 // first child! Then we call updateText
53 updateText( xText
, aNode
.toString() );
58 // // check for pattern "Selection:"
59 // Matcher m = Pattern.compile(
60 // "selection: \\[(-?[0-9]+),(-?[0-9]+)\\] \".*" ).
61 // matcher( aNode.toString() );
67 // setSelection( xText,
68 // Integer.parseInt(m.group(1)),
69 // Integer.parseInt(m.group(2)) );
71 // catch( NumberFormatException f )
80 catch (com
.sun
.star
.lang
.IndexOutOfBoundsException aException
)
85 public void treeNodesInserted(TreeModelEvent e
) { ; }
86 public void treeNodesRemoved(TreeModelEvent e
) { ; }
87 public void treeStructureChanged(TreeModelEvent e
) { ; }
89 /** update the text */
90 boolean updateText( XAccessibleText xText
, String sNew
)
91 throws com
.sun
.star
.lang
.IndexOutOfBoundsException
93 // is this text editable? if not, fudge you and return
94 XAccessibleEditableText xEdit
=
95 UnoRuntime
.queryInterface (
96 XAccessibleEditableText
.class, xText
);
100 String sOld
= xText
.getText();
102 // false alarm? Early out if no change was done!
103 if( sOld
.equals( sNew
) )
106 // get the minimum length of both strings
107 int nMinLength
= sOld
.length();
108 if( sNew
.length() < nMinLength
)
109 nMinLength
= sNew
.length();
111 // count equal characters from front and end
113 while( (nFront
< nMinLength
) &&
114 (sNew
.charAt(nFront
) == sOld
.charAt(nFront
)) )
117 while( (nBack
< nMinLength
) &&
118 ( sNew
.charAt(sNew
.length()-nBack
-1) ==
119 sOld
.charAt(sOld
.length()-nBack
-1) ) )
121 if( nFront
+ nBack
> nMinLength
)
122 nBack
= nMinLength
- nFront
;
124 // so... the first nFront and the last nBack characters
125 // are the same. Change the others!
126 String sDel
= sOld
.substring( nFront
, sOld
.length() - nBack
);
127 String sIns
= sNew
.substring( nFront
, sNew
.length() - nBack
);
129 System
.out
.println("edit text: " +
130 sOld
.substring(0, nFront
) +
131 " [ " + sDel
+ " -> " + sIns
+ " ] " +
132 sOld
.substring(sOld
.length() - nBack
) );
134 boolean bRet
= false;
137 // edit the text, and use
138 // (set|insert|delete|replace)Text as needed
139 if( nFront
+nBack
== 0 )
140 bRet
= xEdit
.setText( sIns
);
141 else if( sDel
.length() == 0 )
142 bRet
= xEdit
.insertText( sIns
, nFront
);
143 else if( sIns
.length() == 0 )
144 bRet
= xEdit
.deleteText( nFront
, sOld
.length()-nBack
);
146 bRet
= xEdit
.replaceText(nFront
, sOld
.length()-nBack
,sIns
);
148 catch( IndexOutOfBoundsException e
)
156 boolean setSelection( XAccessibleText xText
, int p1
, int p2
)
160 return xText
.setSelection( p1
, p2
);
162 catch( com
.sun
.star
.lang
.IndexOutOfBoundsException f
)
168 // /** replace the given node with a new xText node */
169 // void updateNode( XAccessibleText xText,
170 // DefaultMutableTreeNode aNode )
172 // // create a new node
173 // DefaultMutableTreeNode aNew = newTextTreeNode( xText );
175 // // get parent (must be DefaultMutableTreeNode)
176 // DefaultMutableTreeNode aParent =
177 // (DefaultMutableTreeNode)aNode.getParent();
178 // if( aParent != null )
180 // // remove old sub-tree, and insert new one
181 // int nIndex = aParent.getIndex( aNode );
182 // aParent.remove( nIndex );
183 // aParent.insert( aNew, nIndex );