1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.accessibility.awb.view.text;
25 
26 import javax.swing.text.JTextComponent;
27 
28 import com.sun.star.accessibility.XAccessibleContext;
29 import com.sun.star.accessibility.XAccessibleEditableText;
30 import com.sun.star.lang.IndexOutOfBoundsException;
31 import com.sun.star.uno.UnoRuntime;
32 
33 
34 class TextEditDialog
35     extends TextActionDialog
36 {
TextEditDialog( XAccessibleContext xContext, String sExplanation, String sTitle )37     public TextEditDialog (
38         XAccessibleContext xContext,
39         String sExplanation,
40         String sTitle )
41     {
42         super (xContext, sExplanation, sTitle);
43     }
44 
Layout()45     protected void Layout()
46     {
47         super.Layout();
48         maText.setEditable (true);
49     }
50 
51 
52     /** edit the text */
EditableTextAction(XAccessibleEditableText xText)53     boolean EditableTextAction (XAccessibleEditableText xText)
54     {
55         return UpdateText (xText, maText.getText());
56     }
57 
58 
59     /** update the text */
UpdateText(XAccessibleEditableText xText, String sNew)60     boolean UpdateText (XAccessibleEditableText xText, String sNew)
61     {
62         boolean bResult = false;
63 
64         String sOld = xText.getText();
65 
66         // false alarm? Early out if no change was done!
67         if ( ! sOld.equals (sNew))
68         {
69 
70             // Get the minimum length of both strings.
71             int nMinLength = sOld.length();
72             if (sNew.length() < nMinLength)
73                 nMinLength = sNew.length();
74 
75             // Count equal characters from front and end.
76             int nFront = 0;
77             while ((nFront < nMinLength) &&
78                 (sNew.charAt(nFront) == sOld.charAt(nFront)))
79                 nFront++;
80             int nBack = 0;
81             while ((nBack < nMinLength) &&
82                 (sNew.charAt(sNew.length()-nBack-1) ==
83                  sOld.charAt(sOld.length()-nBack-1)    ))
84                 nBack++;
85             if (nFront + nBack > nMinLength)
86                 nBack = nMinLength - nFront;
87 
88             // so... the first nFront and the last nBack characters are the
89             // same. Change the others!
90             String sDel = sOld.substring (nFront, sOld.length() - nBack);
91             String sIns = sNew.substring (nFront, sNew.length() - nBack);
92 
93             System.out.println ("edit text: " +
94                 sOld.substring(0, nFront) +
95                 " [ " + sDel + " -> " + sIns + " ] " +
96                 sOld.substring(sOld.length() - nBack));
97 
98             try
99             {
100                 // edit the text, and use
101                 // (set|insert|delete|replace)Text as needed
102                 if( nFront+nBack == 0 )
103                     bResult = xText.setText( sIns );
104                 else if( sDel.length() == 0 )
105                     bResult = xText.insertText( sIns, nFront );
106                 else if( sIns.length() == 0 )
107                     bResult = xText.deleteText( nFront, sOld.length()-nBack );
108                 else
109                     bResult = xText.replaceText(nFront, sOld.length()-nBack,sIns);
110             }
111             catch( IndexOutOfBoundsException aException)
112             {
113             }
114         }
115 
116         return bResult;
117     }
118 }
119