Update ooo320-m1
[ooovba.git] / odk / examples / java / Text / TextReplace.java
blobe82eb703df78d3afe387ec84cf79fea9c44b9e9d
1 /*************************************************************************
3 * $RCSfile: TextReplace.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:18:30 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 //***************************************************************************
42 // comment: Step 1: get the Desktop object from the office
43 // Step 2: open an empty text document
44 // Step 3: enter a example text
45 // Step 4: replace some english spelled words with US spelled
46 //***************************************************************************
49 import com.sun.star.uno.UnoRuntime;
51 public class TextReplace {
53 public static void main(String args[]) {
54 // You need the desktop to create a document
55 // The getDesktop method does the UNO bootstrapping, gets the
56 // remote servie manager and the desktop object.
57 com.sun.star.frame.XDesktop xDesktop = null;
58 xDesktop = getDesktop();
60 com.sun.star.text.XTextDocument xTextDocument =
61 createTextdocument( xDesktop );
63 createExampleData( xTextDocument );
65 String mBritishWords[] = {"colour", "neighbour", "centre", "behaviour",
66 "metre", "through" };
67 String mUSWords[] = { "color", "neighbor", "center", "behavior",
68 "meter", "thru" };
70 try {
71 com.sun.star.util.XReplaceDescriptor xReplaceDescr = null;
72 com.sun.star.util.XSearchDescriptor xSearchDescriptor = null;
73 com.sun.star.util.XReplaceable xReplaceable = null;
75 xReplaceable = (com.sun.star.util.XReplaceable)
76 UnoRuntime.queryInterface(
77 com.sun.star.util.XReplaceable.class, xTextDocument);
79 // You need a descriptor to set properies for Replace
80 xReplaceDescr = (com.sun.star.util.XReplaceDescriptor)
81 xReplaceable.createReplaceDescriptor();
83 System.out.println("Change all occurrences of ...");
84 for( int iArrayCounter = 0; iArrayCounter < mBritishWords.length;
85 iArrayCounter++ )
87 System.out.println(mBritishWords[iArrayCounter] +
88 " -> " + mUSWords[iArrayCounter]);
89 // Set the properties the replace method need
90 xReplaceDescr.setSearchString(mBritishWords[iArrayCounter] );
91 xReplaceDescr.setReplaceString(mUSWords[iArrayCounter] );
93 // Replace all words
94 xReplaceable.replaceAll( xReplaceDescr );
98 catch( Exception e) {
99 e.printStackTrace(System.err);
102 System.out.println("Done");
104 System.exit(0);
108 protected static void createExampleData(
109 com.sun.star.text.XTextDocument xTextDocument )
111 // Create textdocument and insert example text
112 com.sun.star.text.XTextCursor xTextCursor = null;
114 try {
115 xTextCursor = (com.sun.star.text.XTextCursor)
116 xTextDocument.getText().createTextCursor();
117 com.sun.star.text.XText xText = (com.sun.star.text.XText)
118 xTextDocument.getText();
120 xText.insertString( xTextCursor,
121 "He nervously looked all around. Suddenly he saw his ", false );
123 xText.insertString( xTextCursor, "neighbour ", true );
124 com.sun.star.beans.XPropertySet xCPS = (com.sun.star.beans.XPropertySet)
125 UnoRuntime.queryInterface(
126 com.sun.star.beans.XPropertySet.class, xTextCursor);
127 // Set the word blue
128 xCPS.setPropertyValue( "CharColor", new Integer( 255 ) );
129 // Go to last character
130 xTextCursor.gotoEnd(false);
131 xCPS.setPropertyValue( "CharColor", new Integer( 0 ) );
133 xText.insertString( xTextCursor, "in the alley. Like lightening he darted off to the left and disappeared between the two warehouses almost falling over the trash can lying in the ", false );
135 xText.insertString( xTextCursor, "centre ", true );
136 xCPS = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
137 com.sun.star.beans.XPropertySet.class, xTextCursor);
138 // Set the word blue
139 xCPS.setPropertyValue( "CharColor", new Integer( 255 ) );
140 // Go to last character
141 xTextCursor.gotoEnd(false);
142 xCPS.setPropertyValue( "CharColor", new Integer( 0 ) );
144 xText.insertString( xTextCursor, "of the sidewalk.", false );
146 xText.insertControlCharacter( xTextCursor,
147 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false );
148 xText.insertString( xTextCursor, "He tried to nervously tap his way along in the inky darkness and suddenly stiffened: it was a dead-end, he would have to go back the way he had come.", false );
150 xTextCursor.gotoStart(false);
152 catch( Exception e) {
153 e.printStackTrace(System.err);
158 public static com.sun.star.frame.XDesktop getDesktop() {
159 com.sun.star.frame.XDesktop xDesktop = null;
160 com.sun.star.lang.XMultiComponentFactory xMCF = null;
162 try {
163 com.sun.star.uno.XComponentContext xContext = null;
165 // get the remote office component context
166 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
168 // get the remote office service manager
169 xMCF = xContext.getServiceManager();
170 if( xMCF != null ) {
171 System.out.println("Connected to a running office ...");
173 Object oDesktop = xMCF.createInstanceWithContext(
174 "com.sun.star.frame.Desktop", xContext);
175 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
176 com.sun.star.frame.XDesktop.class, oDesktop);
178 else
179 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
181 catch( Exception e) {
182 e.printStackTrace(System.err);
183 System.exit(1);
187 return xDesktop;
190 public static com.sun.star.text.XTextDocument createTextdocument(
191 com.sun.star.frame.XDesktop xDesktop )
193 com.sun.star.text.XTextDocument aTextDocument = null;
195 try {
196 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
197 "swriter");
198 aTextDocument = (com.sun.star.text.XTextDocument)
199 UnoRuntime.queryInterface(
200 com.sun.star.text.XTextDocument.class, xComponent);
202 catch( Exception e) {
203 e.printStackTrace(System.err);
206 return aTextDocument;
210 protected static com.sun.star.lang.XComponent CreateNewDocument(
211 com.sun.star.frame.XDesktop xDesktop,
212 String sDocumentType )
214 String sURL = "private:factory/" + sDocumentType;
216 com.sun.star.lang.XComponent xComponent = null;
217 com.sun.star.frame.XComponentLoader xComponentLoader = null;
218 com.sun.star.beans.PropertyValue xValues[] =
219 new com.sun.star.beans.PropertyValue[1];
220 com.sun.star.beans.PropertyValue xEmptyArgs[] =
221 new com.sun.star.beans.PropertyValue[0];
223 try {
224 xComponentLoader = (com.sun.star.frame.XComponentLoader)
225 UnoRuntime.queryInterface(
226 com.sun.star.frame.XComponentLoader.class, xDesktop);
228 xComponent = xComponentLoader.loadComponentFromURL(
229 sURL, "_blank", 0, xEmptyArgs);
231 catch( Exception e) {
232 e.printStackTrace(System.err);
235 return xComponent ;