1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir import com.sun.star.awt.Point;
36*cdf0e10cSrcweir import com.sun.star.awt.Size;
37*cdf0e10cSrcweir import com.sun.star.awt.FontWeight;
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir import com.sun.star.beans.PropertyState;
40*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
41*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet;
42*cdf0e10cSrcweir import com.sun.star.beans.XPropertyState;
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir import com.sun.star.comp.servicemanager.ServiceManager;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir import com.sun.star.connection.XConnector;
49*cdf0e10cSrcweir import com.sun.star.connection.XConnection;
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
52*cdf0e10cSrcweir import com.sun.star.container.XNameContainer;
53*cdf0e10cSrcweir import com.sun.star.container.XNamed;
54*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
55*cdf0e10cSrcweir import com.sun.star.container.XIndexReplace;
56*cdf0e10cSrcweir import com.sun.star.container.XEnumeration;
57*cdf0e10cSrcweir import com.sun.star.container.XEnumerationAccess;
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir import com.sun.star.drawing.XShape;
60*cdf0e10cSrcweir import com.sun.star.drawing.XShapeGrouper;
61*cdf0e10cSrcweir import com.sun.star.drawing.XShapes;
62*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPageSupplier;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir import com.sun.star.frame.XDesktop;
65*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader;
66*cdf0e10cSrcweir import com.sun.star.frame.XModel;
67*cdf0e10cSrcweir import com.sun.star.frame.XController;
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir import com.sun.star.lang.XComponent;
70*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory;
71*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
72*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir import com.sun.star.style.NumberingType;
75*cdf0e10cSrcweir import com.sun.star.style.XStyle;
76*cdf0e10cSrcweir import com.sun.star.style.XStyleFamiliesSupplier;
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir import com.sun.star.table.XCell;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir import com.sun.star.text.ControlCharacter;
81*cdf0e10cSrcweir import com.sun.star.text.ReferenceFieldSource;
82*cdf0e10cSrcweir import com.sun.star.text.ReferenceFieldPart;
83*cdf0e10cSrcweir import com.sun.star.text.TextColumn;
84*cdf0e10cSrcweir import com.sun.star.text.TextContentAnchorType;
85*cdf0e10cSrcweir import com.sun.star.text.XAutoTextContainer;
86*cdf0e10cSrcweir import com.sun.star.text.XAutoTextGroup;
87*cdf0e10cSrcweir import com.sun.star.text.XAutoTextEntry;
88*cdf0e10cSrcweir import com.sun.star.text.XDependentTextField;
89*cdf0e10cSrcweir import com.sun.star.text.XDocumentIndex;
90*cdf0e10cSrcweir import com.sun.star.text.XFootnote;
91*cdf0e10cSrcweir import com.sun.star.text.XFootnotesSupplier;
92*cdf0e10cSrcweir import com.sun.star.text.XParagraphCursor;
93*cdf0e10cSrcweir import com.sun.star.text.XReferenceMarksSupplier;
94*cdf0e10cSrcweir import com.sun.star.text.XRelativeTextContentInsert;
95*cdf0e10cSrcweir import com.sun.star.text.XSentenceCursor;
96*cdf0e10cSrcweir import com.sun.star.text.XSimpleText;
97*cdf0e10cSrcweir import com.sun.star.text.XText;
98*cdf0e10cSrcweir import com.sun.star.text.XTextColumns;
99*cdf0e10cSrcweir import com.sun.star.text.XTextContent;
100*cdf0e10cSrcweir import com.sun.star.text.XTextCursor;
101*cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
102*cdf0e10cSrcweir import com.sun.star.text.XTextField;
103*cdf0e10cSrcweir import com.sun.star.text.XTextFrame;
104*cdf0e10cSrcweir import com.sun.star.text.XTextRange;
105*cdf0e10cSrcweir import com.sun.star.text.XTextSection;
106*cdf0e10cSrcweir import com.sun.star.text.XTextTable;
107*cdf0e10cSrcweir import com.sun.star.text.XTextTableCursor;
108*cdf0e10cSrcweir import com.sun.star.text.XTextTablesSupplier;
109*cdf0e10cSrcweir import com.sun.star.text.XTextFieldsSupplier;
110*cdf0e10cSrcweir import com.sun.star.text.XBookmarksSupplier;
111*cdf0e10cSrcweir import com.sun.star.text.XTextViewCursorSupplier;
112*cdf0e10cSrcweir import com.sun.star.text.XTextViewCursor;
113*cdf0e10cSrcweir import com.sun.star.text.XPageCursor;
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir import com.sun.star.text.XWordCursor;
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
118*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
119*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext;
120*cdf0e10cSrcweir import com.sun.star.uno.XInterface;
121*cdf0e10cSrcweir import com.sun.star.uno.XNamingService;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir import com.sun.star.util.XRefreshable;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir import com.sun.star.frame.XStorable;
126*cdf0e10cSrcweir import com.sun.star.view.XPrintable;
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir import java.lang.Math;
129*cdf0e10cSrcweir import java.util.Random;
130*cdf0e10cSrcweir import java.util.Hashtable;
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir /*
133*cdf0e10cSrcweir  * TextDocuments.java
134*cdf0e10cSrcweir  *
135*cdf0e10cSrcweir  * Created on 11. April 2002, 08:47
136*cdf0e10cSrcweir  */
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir /**
139*cdf0e10cSrcweir  *
140*cdf0e10cSrcweir  * @author  Martin Gallwey, Dietrich Schulten
141*cdf0e10cSrcweir  */
142*cdf0e10cSrcweir public class TextDocuments {
143*cdf0e10cSrcweir     // adjust these constant to your local printer!
144*cdf0e10cSrcweir     private static String sOutputDir;
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     private String aPrinterName = "\\\\so-print\\xml3sof";
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir     private XComponentContext mxRemoteContext = null;
149*cdf0e10cSrcweir     private XMultiComponentFactory mxRemoteServiceManager = null;
150*cdf0e10cSrcweir     private XTextDocument mxDoc = null;
151*cdf0e10cSrcweir     private XMultiServiceFactory mxDocFactory = null;
152*cdf0e10cSrcweir     private XMultiServiceFactory mxFactory = null;
153*cdf0e10cSrcweir     private XPropertySet  mxDocProps = null;
154*cdf0e10cSrcweir     private XText mxDocText = null;
155*cdf0e10cSrcweir     private XTextCursor mxDocCursor = null;
156*cdf0e10cSrcweir     private XTextContent mxFishSection = null;
157*cdf0e10cSrcweir     private Random maRandom = null;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir     /** Creates a new instance of TextDocuments */
160*cdf0e10cSrcweir     public TextDocuments() {
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     /**
164*cdf0e10cSrcweir      * @param args the command line arguments
165*cdf0e10cSrcweir      */
166*cdf0e10cSrcweir     public static void main(String[] args) {
167*cdf0e10cSrcweir     TextDocuments textDocuments1 = new TextDocuments();
168*cdf0e10cSrcweir     try {
169*cdf0e10cSrcweir             // output directory for store test;
170*cdf0e10cSrcweir             sOutputDir = args[0];
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir             textDocuments1.runDemo();
173*cdf0e10cSrcweir         }
174*cdf0e10cSrcweir         catch (java.lang.Exception e){
175*cdf0e10cSrcweir             System.out.println(e.getMessage());
176*cdf0e10cSrcweir             e.printStackTrace();
177*cdf0e10cSrcweir         }
178*cdf0e10cSrcweir         finally {
179*cdf0e10cSrcweir             System.exit(0);
180*cdf0e10cSrcweir         }
181*cdf0e10cSrcweir     }
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir     protected void runDemo() throws java.lang.Exception {
184*cdf0e10cSrcweir             storePrintExample(); // depends on printer name
185*cdf0e10cSrcweir             templateExample();
186*cdf0e10cSrcweir             viewCursorExample(); // makes changes to the current document,
187*cdf0e10cSrcweir                                  // use with care
188*cdf0e10cSrcweir             editingExample();
189*cdf0e10cSrcweir     }
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     /** Sample for use of templates
192*cdf0e10cSrcweir      *  This sample uses the file TextTemplateWithUserFields.odt from the Samples
193*cdf0e10cSrcweir      *  folder. The file contains a number of User text fields (Variables - User)
194*cdf0e10cSrcweir      *  and a bookmark which we use to fill in various values
195*cdf0e10cSrcweir      */
196*cdf0e10cSrcweir     protected void templateExample() throws java.lang.Exception {
197*cdf0e10cSrcweir         // create a small hashtable that simulates a rowset
198*cdf0e10cSrcweir         Hashtable recipient = new Hashtable();
199*cdf0e10cSrcweir         recipient.put("Company", "Manatee Books");
200*cdf0e10cSrcweir         recipient.put("Contact", "Rod Martin");
201*cdf0e10cSrcweir         recipient.put("ZIP", "34567");
202*cdf0e10cSrcweir         recipient.put("City", "Fort Lauderdale");
203*cdf0e10cSrcweir         recipient.put("State", "Florida");
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir         // load template with User fields and bookmark
206*cdf0e10cSrcweir         java.io.File sourceFile = new java.io.File("TextTemplateWithUserFields.odt");
207*cdf0e10cSrcweir         StringBuffer sTemplateFileUrl = new StringBuffer("file:///");
208*cdf0e10cSrcweir         sTemplateFileUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir         XComponent xTemplateComponent =
211*cdf0e10cSrcweir             newDocComponentFromTemplate( sTemplateFileUrl.toString() );
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir         // get XTextFieldsSupplier, XBookmarksSupplier interfaces
214*cdf0e10cSrcweir         XTextFieldsSupplier xTextFieldsSupplier = (XTextFieldsSupplier)
215*cdf0e10cSrcweir             UnoRuntime.queryInterface(XTextFieldsSupplier.class,
216*cdf0e10cSrcweir                                       xTemplateComponent);
217*cdf0e10cSrcweir         XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier)
218*cdf0e10cSrcweir             UnoRuntime.queryInterface(XBookmarksSupplier.class, xTemplateComponent);
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir         // access the TextFields and the TextFieldMasters collections
221*cdf0e10cSrcweir         XNameAccess xNamedFieldMasters = xTextFieldsSupplier.getTextFieldMasters();
222*cdf0e10cSrcweir         XEnumerationAccess xEnumeratedFields = xTextFieldsSupplier.getTextFields();
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir         // iterate over hashtable and insert values into field masters
225*cdf0e10cSrcweir         java.util.Enumeration keys = recipient.keys();
226*cdf0e10cSrcweir         while(keys.hasMoreElements()) {
227*cdf0e10cSrcweir             // get column name
228*cdf0e10cSrcweir             String key = (String)keys.nextElement();
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir             // access corresponding field master
231*cdf0e10cSrcweir             Object fieldMaster = xNamedFieldMasters.getByName(
232*cdf0e10cSrcweir                 "com.sun.star.text.fieldmaster.User." + key);
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir             // query the XPropertySet interface, we need to set the Content property
235*cdf0e10cSrcweir             XPropertySet xPropertySet = (XPropertySet)UnoRuntime.queryInterface(
236*cdf0e10cSrcweir                 XPropertySet.class, fieldMaster);
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir             // insert the column value into field master
239*cdf0e10cSrcweir             xPropertySet.setPropertyValue("Content", recipient.get(key));
240*cdf0e10cSrcweir         }
241*cdf0e10cSrcweir         // afterwards we must refresh the textfields collection
242*cdf0e10cSrcweir         XRefreshable xRefreshable = (XRefreshable)UnoRuntime.queryInterface(
243*cdf0e10cSrcweir             XRefreshable.class, xEnumeratedFields);
244*cdf0e10cSrcweir         xRefreshable.refresh();
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir         // accessing the Bookmarks collection of the document
247*cdf0e10cSrcweir         XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks();
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir         // find the bookmark named "Subscription"
250*cdf0e10cSrcweir         Object bookmark = xNamedBookmarks.getByName("Subscription");
251*cdf0e10cSrcweir         // we need its XTextRange which is available from getAnchor(),
252*cdf0e10cSrcweir         // so query for XTextContent
253*cdf0e10cSrcweir         XTextContent xBookmarkContent = (XTextContent)UnoRuntime.queryInterface(
254*cdf0e10cSrcweir             XTextContent.class, bookmark);
255*cdf0e10cSrcweir         // get the anchor of the bookmark (its XTextRange)
256*cdf0e10cSrcweir         XTextRange xBookmarkRange = xBookmarkContent.getAnchor();
257*cdf0e10cSrcweir         // set string at the bookmark position
258*cdf0e10cSrcweir         xBookmarkRange.setString("subscription for the Manatee Journal");
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir     }
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir     /** Sample for document changes, starting at the current view cursor position
263*cdf0e10cSrcweir      *  The sample changes the paragraph style and the character style at the
264*cdf0e10cSrcweir      *  current view cursor selection Open the sample file ViewCursorExampleFile,
265*cdf0e10cSrcweir      *  select some text and run the example.
266*cdf0e10cSrcweir      *  The current paragraph will be set to Quotations paragraph style.
267*cdf0e10cSrcweir      *  The selected text will be set to Quotation character style.
268*cdf0e10cSrcweir      */
269*cdf0e10cSrcweir     private void viewCursorExample() throws java.lang.Exception {
270*cdf0e10cSrcweir         // get the remote service manager
271*cdf0e10cSrcweir         mxRemoteServiceManager = this.getRemoteServiceManager();
272*cdf0e10cSrcweir         // get the Desktop service
273*cdf0e10cSrcweir         Object desktop = mxRemoteServiceManager.createInstanceWithContext(
274*cdf0e10cSrcweir             "com.sun.star.frame.Desktop", mxRemoteContext);
275*cdf0e10cSrcweir         // query its XDesktop interface, we need the current component
276*cdf0e10cSrcweir         XDesktop xDesktop = (XDesktop)UnoRuntime.queryInterface(
277*cdf0e10cSrcweir             XDesktop.class, desktop);
278*cdf0e10cSrcweir         // retrieve the current component and access the controller
279*cdf0e10cSrcweir         XComponent xCurrentComponent = xDesktop.getCurrentComponent();
280*cdf0e10cSrcweir         XModel xModel = (XModel)UnoRuntime.queryInterface(XModel.class,
281*cdf0e10cSrcweir                                                           xCurrentComponent);
282*cdf0e10cSrcweir         XController xController = xModel.getCurrentController();
283*cdf0e10cSrcweir         // the controller gives us the TextViewCursor
284*cdf0e10cSrcweir         XTextViewCursorSupplier xViewCursorSupplier =
285*cdf0e10cSrcweir             (XTextViewCursorSupplier)UnoRuntime.queryInterface(
286*cdf0e10cSrcweir                 XTextViewCursorSupplier.class, xController);
287*cdf0e10cSrcweir         XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir         // query its XPropertySet interface, we want to set character and paragraph
290*cdf0e10cSrcweir         // properties
291*cdf0e10cSrcweir         XPropertySet xCursorPropertySet = (XPropertySet)UnoRuntime.queryInterface(
292*cdf0e10cSrcweir             XPropertySet.class, xViewCursor);
293*cdf0e10cSrcweir         // set the appropriate properties for character and paragraph style
294*cdf0e10cSrcweir         xCursorPropertySet.setPropertyValue("CharStyleName", "Quotation");
295*cdf0e10cSrcweir         xCursorPropertySet.setPropertyValue("ParaStyleName", "Quotations");
296*cdf0e10cSrcweir         // print the current page number
297*cdf0e10cSrcweir         XPageCursor xPageCursor = (XPageCursor)UnoRuntime.queryInterface(
298*cdf0e10cSrcweir             XPageCursor.class, xViewCursor);
299*cdf0e10cSrcweir         System.out.println("The current page number is " + xPageCursor.getPage());
300*cdf0e10cSrcweir         // the model cursor is much more powerful, so
301*cdf0e10cSrcweir         // we create a model cursor at the current view cursor position with the
302*cdf0e10cSrcweir         // following steps:
303*cdf0e10cSrcweir         // get the Text service from the TextViewCursor, it is an XTextRange:
304*cdf0e10cSrcweir          XText xDocumentText = xViewCursor.getText();
305*cdf0e10cSrcweir         // create a model cursor from the viewcursor
306*cdf0e10cSrcweir         XTextCursor xModelCursor = xDocumentText.createTextCursorByRange(
307*cdf0e10cSrcweir             xViewCursor.getStart());
308*cdf0e10cSrcweir         // now we could query XWordCursor, XSentenceCursor and XParagraphCursor
309*cdf0e10cSrcweir         // or XDocumentInsertable, XSortable or XContentEnumerationAccess
310*cdf0e10cSrcweir         // and work with the properties of com.sun.star.text.TextCursor
311*cdf0e10cSrcweir         // in this case we just go to the end of the paragraph and add some text.
312*cdf0e10cSrcweir         XParagraphCursor xParagraphCursor = (XParagraphCursor)
313*cdf0e10cSrcweir             UnoRuntime.queryInterface(XParagraphCursor.class, xModelCursor);
314*cdf0e10cSrcweir         // goto the end of the paragraph
315*cdf0e10cSrcweir         xParagraphCursor.gotoEndOfParagraph(false);
316*cdf0e10cSrcweir         xParagraphCursor.setString(" ***** Fin de semana! ******");
317*cdf0e10cSrcweir     }
318*cdf0e10cSrcweir 
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir     /** Sample for the various editing facilities described in the
321*cdf0e10cSrcweir      * developer's manual
322*cdf0e10cSrcweir      */
323*cdf0e10cSrcweir     private void editingExample () throws java.lang.Exception {
324*cdf0e10cSrcweir         // create empty swriter document
325*cdf0e10cSrcweir         XComponent xEmptyWriterComponent = newDocComponent("swriter");
326*cdf0e10cSrcweir         // query its XTextDocument interface to get the text
327*cdf0e10cSrcweir         mxDoc = (XTextDocument)UnoRuntime.queryInterface(
328*cdf0e10cSrcweir             XTextDocument.class, xEmptyWriterComponent);
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir         // get a reference to the body text of the document
331*cdf0e10cSrcweir         mxDocText = mxDoc.getText();
332*cdf0e10cSrcweir 
333*cdf0e10cSrcweir         // Get a reference to the document's property set. This contains document
334*cdf0e10cSrcweir         // information like the current word count
335*cdf0e10cSrcweir         mxDocProps = (XPropertySet) UnoRuntime.queryInterface(
336*cdf0e10cSrcweir             XPropertySet.class, mxDoc );
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir         // Simple text insertion example
339*cdf0e10cSrcweir         BodyTextExample ();
340*cdf0e10cSrcweir         // Example using text ranges to insert strings at the beginning or end
341*cdf0e10cSrcweir         // of a text range
342*cdf0e10cSrcweir         TextRangeExample ();
343*cdf0e10cSrcweir         // Create a document cursor and remember it, it will be used in most
344*cdf0e10cSrcweir         // of the following examples
345*cdf0e10cSrcweir         mxDocCursor = mxDocText.createTextCursor();
346*cdf0e10cSrcweir         // Demonstrate some of the different cursor types (word, sentence)
347*cdf0e10cSrcweir         TextCursorExample ();
348*cdf0e10cSrcweir 
349*cdf0e10cSrcweir         // Access the text document's multi service factory, which we will need
350*cdf0e10cSrcweir         // for most of the following examples
351*cdf0e10cSrcweir         mxDocFactory = (XMultiServiceFactory) UnoRuntime.queryInterface(
352*cdf0e10cSrcweir             XMultiServiceFactory.class, mxDoc );
353*cdf0e10cSrcweir 
354*cdf0e10cSrcweir         // Examples of text fields, dependant text fields and field masters
355*cdf0e10cSrcweir         TextFieldExample ();
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir         // Example of using an XEnumerationAccess to iterate over paragraphs and
358*cdf0e10cSrcweir         // set properties of each paragraph as we do so
359*cdf0e10cSrcweir         ParagraphExample ();
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir         // Example of creating and manipulating a text frame
362*cdf0e10cSrcweir         TextFrameExample ();
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir         // Example of creating and manipulating a text table, text table rows
365*cdf0e10cSrcweir         // and text table cells get a new random generator
366*cdf0e10cSrcweir         maRandom = new Random();
367*cdf0e10cSrcweir         TextTableExample ();
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir         // Example of creating, inserting and manipulating text sections, as
370*cdf0e10cSrcweir         // well as an example of how to refresh the document
371*cdf0e10cSrcweir         TextSectionExample ();
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir         // Example of creating a text section over a block of text and formatting
374*cdf0e10cSrcweir         // the text section into columns, as well as how to insert an empty
375*cdf0e10cSrcweir         // paragraph using the XRelativeTextContentInsert
376*cdf0e10cSrcweir         TextColumnsExample ();
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir         // Example of creating the NumberingRules service and adjusting
379*cdf0e10cSrcweir         // NumberingTypes and NumberingLevels
380*cdf0e10cSrcweir         NumberingExample ();
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir         // Example of how to use the XStyleFamiliesSupplier interface of the
383*cdf0e10cSrcweir         // document and how to create, insert and apply styles
384*cdf0e10cSrcweir         StylesExample ();
385*cdf0e10cSrcweir         IndexExample ();
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir         // Example of how to create and manipulate reference marks and GetReference
388*cdf0e10cSrcweir         // text fields
389*cdf0e10cSrcweir         ReferenceExample ();
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir         // Example of how to create and insert Footnotes and how to use the
392*cdf0e10cSrcweir         // XFootnotesSupplier interface of the document
393*cdf0e10cSrcweir         FootnoteExample ();
394*cdf0e10cSrcweir 
395*cdf0e10cSrcweir         // This method demonstrates how to create shapes from the document factory
396*cdf0e10cSrcweir         // and how to access the draw page of the document using the
397*cdf0e10cSrcweir         // XDrawPageSupplier interface
398*cdf0e10cSrcweir         DrawPageExample ();
399*cdf0e10cSrcweir 
400*cdf0e10cSrcweir         mxFactory = (XMultiServiceFactory)UnoRuntime.queryInterface(
401*cdf0e10cSrcweir             XMultiServiceFactory.class, mxRemoteServiceManager);
402*cdf0e10cSrcweir         // This example demonstrates the use of the AutoTextContainer,
403*cdf0e10cSrcweir         // AutoTextGroup and AutoTextEntry services and shows how to create,
404*cdf0e10cSrcweir         // insert and modify auto text blocks
405*cdf0e10cSrcweir         AutoTextExample ();
406*cdf0e10cSrcweir     }
407*cdf0e10cSrcweir 
408*cdf0e10cSrcweir     protected void storePrintExample() throws java.lang.Exception {
409*cdf0e10cSrcweir         // get the remote service manager
410*cdf0e10cSrcweir         mxRemoteServiceManager = this.getRemoteServiceManager();
411*cdf0e10cSrcweir         // retrieve the Desktop object, we need its XComponentLoader
412*cdf0e10cSrcweir         Object desktop = mxRemoteServiceManager.createInstanceWithContext(
413*cdf0e10cSrcweir             "com.sun.star.frame.Desktop", mxRemoteContext);
414*cdf0e10cSrcweir         XComponentLoader xComponentLoader = (XComponentLoader)
415*cdf0e10cSrcweir             UnoRuntime.queryInterface(XComponentLoader.class, desktop);
416*cdf0e10cSrcweir         PropertyValue[] loadProps = new PropertyValue[0];
417*cdf0e10cSrcweir 
418*cdf0e10cSrcweir         java.io.File sourceFile = new java.io.File("PrintDemo.odt");
419*cdf0e10cSrcweir         StringBuffer sLoadFileUrl = new StringBuffer("file:///");
420*cdf0e10cSrcweir         sLoadFileUrl.append(sourceFile.getCanonicalPath().replace('\\', '/'));
421*cdf0e10cSrcweir 
422*cdf0e10cSrcweir         XComponent xDoc = xComponentLoader.loadComponentFromURL(
423*cdf0e10cSrcweir             sLoadFileUrl.toString(), "_blank", 0, loadProps);
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir         if ( xDoc != null ) {
426*cdf0e10cSrcweir             sourceFile = new java.io.File(sOutputDir);
427*cdf0e10cSrcweir             StringBuffer sStoreFileUrl = new StringBuffer();
428*cdf0e10cSrcweir             sStoreFileUrl.append(sourceFile.toURL().toString());
429*cdf0e10cSrcweir             sStoreFileUrl.append("somepopularfileformat.doc");
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir             storeDocComponent(xDoc, sStoreFileUrl.toString() );
432*cdf0e10cSrcweir             printDocComponent(xDoc);
433*cdf0e10cSrcweir         }
434*cdf0e10cSrcweir     }
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir     private XMultiComponentFactory getRemoteServiceManager()
437*cdf0e10cSrcweir         throws java.lang.Exception
438*cdf0e10cSrcweir     {
439*cdf0e10cSrcweir         if (mxRemoteContext == null && mxRemoteServiceManager == null) {
440*cdf0e10cSrcweir             // get the remote office context. If necessary a new office
441*cdf0e10cSrcweir             // process is started
442*cdf0e10cSrcweir             mxRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
443*cdf0e10cSrcweir             System.out.println("Connected to a running office ...");
444*cdf0e10cSrcweir             mxRemoteServiceManager = mxRemoteContext.getServiceManager();
445*cdf0e10cSrcweir         }
446*cdf0e10cSrcweir         return mxRemoteServiceManager;
447*cdf0e10cSrcweir     }
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir     protected XComponent newDocComponent(String docType)
450*cdf0e10cSrcweir         throws java.lang.Exception
451*cdf0e10cSrcweir     {
452*cdf0e10cSrcweir         String loadUrl = "private:factory/" + docType;
453*cdf0e10cSrcweir         mxRemoteServiceManager = this.getRemoteServiceManager();
454*cdf0e10cSrcweir         Object desktop = mxRemoteServiceManager.createInstanceWithContext(
455*cdf0e10cSrcweir             "com.sun.star.frame.Desktop", mxRemoteContext);
456*cdf0e10cSrcweir         XComponentLoader xComponentLoader = (XComponentLoader)
457*cdf0e10cSrcweir             UnoRuntime.queryInterface(XComponentLoader.class, desktop);
458*cdf0e10cSrcweir         PropertyValue[] loadProps = new PropertyValue[0];
459*cdf0e10cSrcweir         return xComponentLoader.loadComponentFromURL(loadUrl, "_blank",
460*cdf0e10cSrcweir                                                      0, loadProps);
461*cdf0e10cSrcweir     }
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir     /** Load a document as template
464*cdf0e10cSrcweir      */
465*cdf0e10cSrcweir     protected XComponent newDocComponentFromTemplate(String loadUrl)
466*cdf0e10cSrcweir         throws java.lang.Exception
467*cdf0e10cSrcweir     {
468*cdf0e10cSrcweir         // get the remote service manager
469*cdf0e10cSrcweir         mxRemoteServiceManager = this.getRemoteServiceManager();
470*cdf0e10cSrcweir         // retrieve the Desktop object, we need its XComponentLoader
471*cdf0e10cSrcweir         Object desktop = mxRemoteServiceManager.createInstanceWithContext(
472*cdf0e10cSrcweir             "com.sun.star.frame.Desktop", mxRemoteContext);
473*cdf0e10cSrcweir         XComponentLoader xComponentLoader = (XComponentLoader)
474*cdf0e10cSrcweir             UnoRuntime.queryInterface(XComponentLoader.class, desktop);
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir         // define load properties according to com.sun.star.document.MediaDescriptor
477*cdf0e10cSrcweir         // the boolean property AsTemplate tells the office to create a new document
478*cdf0e10cSrcweir         // from the given file
479*cdf0e10cSrcweir         PropertyValue[] loadProps = new PropertyValue[1];
480*cdf0e10cSrcweir         loadProps[0] = new PropertyValue();
481*cdf0e10cSrcweir         loadProps[0].Name = "AsTemplate";
482*cdf0e10cSrcweir         loadProps[0].Value = new Boolean(true);
483*cdf0e10cSrcweir         // load
484*cdf0e10cSrcweir         return xComponentLoader.loadComponentFromURL(loadUrl, "_blank",
485*cdf0e10cSrcweir                                                      0, loadProps);
486*cdf0e10cSrcweir     }
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir     /** Load a document with arguments (text purposes)
489*cdf0e10cSrcweir      */
490*cdf0e10cSrcweir     protected void storeDocComponent(XComponent xDoc, String storeUrl)
491*cdf0e10cSrcweir         throws java.lang.Exception
492*cdf0e10cSrcweir     {
493*cdf0e10cSrcweir 
494*cdf0e10cSrcweir         XStorable xStorable = (XStorable)UnoRuntime.queryInterface(
495*cdf0e10cSrcweir             XStorable.class, xDoc);
496*cdf0e10cSrcweir         PropertyValue[] storeProps = new PropertyValue[1];
497*cdf0e10cSrcweir         storeProps[0] = new PropertyValue();
498*cdf0e10cSrcweir         storeProps[0].Name = "FilterName";
499*cdf0e10cSrcweir         storeProps[0].Value = "MS Word 97";
500*cdf0e10cSrcweir 
501*cdf0e10cSrcweir         System.out.println("... store \"PrintDemo.odt\" to \"" + storeUrl + "\".");
502*cdf0e10cSrcweir         xStorable.storeAsURL(storeUrl, storeProps);
503*cdf0e10cSrcweir     }
504*cdf0e10cSrcweir 
505*cdf0e10cSrcweir     protected void printDocComponent(XComponent xDoc) throws java.lang.Exception {
506*cdf0e10cSrcweir         XPrintable xPrintable = (XPrintable)UnoRuntime.queryInterface(
507*cdf0e10cSrcweir             XPrintable.class, xDoc);
508*cdf0e10cSrcweir         PropertyValue[] printerDesc = new PropertyValue[1];
509*cdf0e10cSrcweir         printerDesc[0] = new PropertyValue();
510*cdf0e10cSrcweir         printerDesc[0].Name = "Name";
511*cdf0e10cSrcweir         printerDesc[0].Value = aPrinterName;
512*cdf0e10cSrcweir 
513*cdf0e10cSrcweir         xPrintable.setPrinter(printerDesc);
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir         PropertyValue[] printOpts = new PropertyValue[1];
516*cdf0e10cSrcweir         printOpts[0] = new PropertyValue();
517*cdf0e10cSrcweir         printOpts[0].Name = "Pages";
518*cdf0e10cSrcweir         printOpts[0].Value = "1";
519*cdf0e10cSrcweir 
520*cdf0e10cSrcweir         xPrintable.print(printOpts);
521*cdf0e10cSrcweir     }
522*cdf0e10cSrcweir 
523*cdf0e10cSrcweir     // Setting the whole text of a document as one string
524*cdf0e10cSrcweir     protected void BodyTextExample ()
525*cdf0e10cSrcweir     {
526*cdf0e10cSrcweir         // Body Text and TextDocument example
527*cdf0e10cSrcweir         try
528*cdf0e10cSrcweir         {
529*cdf0e10cSrcweir             // demonstrate simple text insertion
530*cdf0e10cSrcweir             mxDocText.setString ( "This is the new body text of the document."
531*cdf0e10cSrcweir                                   + "\n\nThis is on the second line.\n\n" );
532*cdf0e10cSrcweir         }
533*cdf0e10cSrcweir         catch ( Exception e )
534*cdf0e10cSrcweir         {
535*cdf0e10cSrcweir             e.printStackTrace();
536*cdf0e10cSrcweir         }
537*cdf0e10cSrcweir     }
538*cdf0e10cSrcweir 
539*cdf0e10cSrcweir     // Adding a string at the end or the beginning of text
540*cdf0e10cSrcweir     protected void TextRangeExample ()
541*cdf0e10cSrcweir     {
542*cdf0e10cSrcweir         try
543*cdf0e10cSrcweir         {
544*cdf0e10cSrcweir             // Get a text range refering to the beginning of the text document
545*cdf0e10cSrcweir             XTextRange xStart = mxDocText.getStart();
546*cdf0e10cSrcweir             // use setString to insert text at the beginning
547*cdf0e10cSrcweir             xStart.setString ( "This is text inserted at the beginning.\n\n" );
548*cdf0e10cSrcweir             // Get a text range refering to the end of the text document
549*cdf0e10cSrcweir             XTextRange xEnd = mxDocText.getEnd();
550*cdf0e10cSrcweir             // use setString to insert text at the end
551*cdf0e10cSrcweir             xEnd.setString ( "This is text inserted at the end.\n\n" );
552*cdf0e10cSrcweir         }
553*cdf0e10cSrcweir         catch ( Exception e )
554*cdf0e10cSrcweir         {
555*cdf0e10cSrcweir             e.printStackTrace();
556*cdf0e10cSrcweir         }
557*cdf0e10cSrcweir     }
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir     /** moving a text cursor, selecting text and overwriting it
560*cdf0e10cSrcweir     */
561*cdf0e10cSrcweir     protected void TextCursorExample ()
562*cdf0e10cSrcweir     {
563*cdf0e10cSrcweir         try
564*cdf0e10cSrcweir         {
565*cdf0e10cSrcweir             // First, get the XSentenceCursor interface of our text cursor
566*cdf0e10cSrcweir             XSentenceCursor xSentenceCursor = (XSentenceCursor)
567*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XSentenceCursor.class, mxDocCursor );
568*cdf0e10cSrcweir             // Goto the next cursor, without selecting it
569*cdf0e10cSrcweir             xSentenceCursor.gotoNextSentence( false );
570*cdf0e10cSrcweir             // Get the XWordCursor interface of our text cursor
571*cdf0e10cSrcweir             XWordCursor xWordCursor = (XWordCursor) UnoRuntime.queryInterface(
572*cdf0e10cSrcweir                 XWordCursor.class, mxDocCursor );
573*cdf0e10cSrcweir             // Skip the first four words of this sentence and select the fifth
574*cdf0e10cSrcweir             xWordCursor.gotoNextWord( false );
575*cdf0e10cSrcweir             xWordCursor.gotoNextWord( false );
576*cdf0e10cSrcweir             xWordCursor.gotoNextWord( false );
577*cdf0e10cSrcweir             xWordCursor.gotoNextWord( false );
578*cdf0e10cSrcweir             xWordCursor.gotoNextWord( true );
579*cdf0e10cSrcweir             // Use the XSimpleText interface to insert a word at the current cursor
580*cdf0e10cSrcweir             // location, over-writing the current selection (the fifth word
581*cdf0e10cSrcweir             // selected above)
582*cdf0e10cSrcweir             mxDocText.insertString ( xWordCursor, "old ", true );
583*cdf0e10cSrcweir 
584*cdf0e10cSrcweir             // Access the property set of the cursor, and set the currently
585*cdf0e10cSrcweir             // selected text (which is the string we just inserted) to be bold
586*cdf0e10cSrcweir             XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
587*cdf0e10cSrcweir                 XPropertySet.class, mxDocCursor );
588*cdf0e10cSrcweir             xCursorProps.setPropertyValue ( "CharWeight",
589*cdf0e10cSrcweir                              new Float(com.sun.star.awt.FontWeight.BOLD) );
590*cdf0e10cSrcweir 
591*cdf0e10cSrcweir             // replace the '.' at the end of the sentence with a new string
592*cdf0e10cSrcweir             xSentenceCursor.gotoEndOfSentence( false );
593*cdf0e10cSrcweir             xWordCursor.gotoPreviousWord( true );
594*cdf0e10cSrcweir             mxDocText.insertString (xWordCursor,
595*cdf0e10cSrcweir                                     ", which has been changed with text cursors!",
596*cdf0e10cSrcweir                                     true);
597*cdf0e10cSrcweir         }
598*cdf0e10cSrcweir         catch ( Exception e )
599*cdf0e10cSrcweir         {
600*cdf0e10cSrcweir             e.printStackTrace();
601*cdf0e10cSrcweir         }
602*cdf0e10cSrcweir     }
603*cdf0e10cSrcweir 
604*cdf0e10cSrcweir     /** This method inserts both a date field and a user field containing the
605*cdf0e10cSrcweir      * number '42'
606*cdf0e10cSrcweir      */
607*cdf0e10cSrcweir     protected void TextFieldExample ()
608*cdf0e10cSrcweir     {
609*cdf0e10cSrcweir         try
610*cdf0e10cSrcweir         {
611*cdf0e10cSrcweir             // Use the text document's factory to create a DateTime text field,
612*cdf0e10cSrcweir             // and access it's XTextField interface
613*cdf0e10cSrcweir             XTextField xDateField = (XTextField) UnoRuntime.queryInterface (
614*cdf0e10cSrcweir                 XTextField.class, mxDocFactory.createInstance (
615*cdf0e10cSrcweir                     "com.sun.star.text.TextField.DateTime" ) );
616*cdf0e10cSrcweir 
617*cdf0e10cSrcweir             // Insert it at the end of the document
618*cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocText.getEnd(), xDateField, false );
619*cdf0e10cSrcweir 
620*cdf0e10cSrcweir             // Use the text document's factory to create a user text field,
621*cdf0e10cSrcweir             // and access it's XDependentTextField interface
622*cdf0e10cSrcweir             XDependentTextField xUserField =
623*cdf0e10cSrcweir                 (XDependentTextField) UnoRuntime.queryInterface (
624*cdf0e10cSrcweir                     XDependentTextField.class, mxDocFactory.createInstance (
625*cdf0e10cSrcweir                         "com.sun.star.text.TextField.User" ) );
626*cdf0e10cSrcweir 
627*cdf0e10cSrcweir             // Create a fieldmaster for our newly created User Text field, and
628*cdf0e10cSrcweir             // access it's XPropertySet interface
629*cdf0e10cSrcweir             XPropertySet xMasterPropSet = (XPropertySet)UnoRuntime.queryInterface(
630*cdf0e10cSrcweir                 XPropertySet.class, mxDocFactory.createInstance (
631*cdf0e10cSrcweir                     "com.sun.star.text.fieldmaster.User" ) );
632*cdf0e10cSrcweir 
633*cdf0e10cSrcweir             // Set the name and value of the FieldMaster
634*cdf0e10cSrcweir             xMasterPropSet.setPropertyValue ( "Name", "UserEmperor" );
635*cdf0e10cSrcweir             xMasterPropSet.setPropertyValue ( "Value", new Integer ( 42 ) );
636*cdf0e10cSrcweir 
637*cdf0e10cSrcweir             // Attach the field master to the user field
638*cdf0e10cSrcweir             xUserField.attachTextFieldMaster ( xMasterPropSet );
639*cdf0e10cSrcweir 
640*cdf0e10cSrcweir             // Move the cursor to the end of the document
641*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
642*cdf0e10cSrcweir             // insert a paragraph break using the XSimpleText interface
643*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
644*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
645*cdf0e10cSrcweir 
646*cdf0e10cSrcweir             // Insert the user field at the end of the document
647*cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocText.getEnd(), xUserField, false );
648*cdf0e10cSrcweir         }
649*cdf0e10cSrcweir         catch ( Exception e )
650*cdf0e10cSrcweir         {
651*cdf0e10cSrcweir             e.printStackTrace();
652*cdf0e10cSrcweir         }
653*cdf0e10cSrcweir     }
654*cdf0e10cSrcweir 
655*cdf0e10cSrcweir     /** This method demonstrates how to iterate over paragraphs
656*cdf0e10cSrcweir      */
657*cdf0e10cSrcweir     protected void ParagraphExample ()
658*cdf0e10cSrcweir     {
659*cdf0e10cSrcweir         try
660*cdf0e10cSrcweir         {
661*cdf0e10cSrcweir             // The service 'com.sun.star.text.Text' supports the XEnumerationAccess
662*cdf0e10cSrcweir             // interface to provide an enumeration of the paragraphs contained by
663*cdf0e10cSrcweir             // the text the service refers to.
664*cdf0e10cSrcweir 
665*cdf0e10cSrcweir             // Here, we access this interface
666*cdf0e10cSrcweir             XEnumerationAccess xParaAccess = (XEnumerationAccess)
667*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XEnumerationAccess.class, mxDocText );
668*cdf0e10cSrcweir             // Call the XEnumerationAccess's only method to access the actual
669*cdf0e10cSrcweir             // Enumeration
670*cdf0e10cSrcweir             XEnumeration xParaEnum = xParaAccess.createEnumeration();
671*cdf0e10cSrcweir 
672*cdf0e10cSrcweir             // While there are paragraphs, do things to them
673*cdf0e10cSrcweir             while ( xParaEnum.hasMoreElements() )
674*cdf0e10cSrcweir             {
675*cdf0e10cSrcweir                 // Get a reference to the next paragraphs XServiceInfo interface.
676*cdf0e10cSrcweir                 // TextTables are also part of this enumeration access, so we ask
677*cdf0e10cSrcweir                 // the element if it is a TextTable, if it doesn't support the
678*cdf0e10cSrcweir                 // com.sun.star.text.TextTable service, then it is safe to assume
679*cdf0e10cSrcweir                 // that it really is a paragraph
680*cdf0e10cSrcweir                 XServiceInfo xInfo = (XServiceInfo) UnoRuntime.queryInterface(
681*cdf0e10cSrcweir                     XServiceInfo.class, xParaEnum.nextElement() );
682*cdf0e10cSrcweir                 if ( !xInfo.supportsService ( "com.sun.star.text.TextTable" ) )
683*cdf0e10cSrcweir                 {
684*cdf0e10cSrcweir                     // Access the paragraph's property set...the properties in this
685*cdf0e10cSrcweir                     // property set are listed in:
686*cdf0e10cSrcweir                     // com.sun.star.style.ParagraphProperties
687*cdf0e10cSrcweir                     XPropertySet xSet = (XPropertySet) UnoRuntime.queryInterface(
688*cdf0e10cSrcweir                         XPropertySet.class, xInfo );
689*cdf0e10cSrcweir                     // Set the justification to be center justified
690*cdf0e10cSrcweir                     xSet.setPropertyValue ( "ParaAdjust",
691*cdf0e10cSrcweir                              com.sun.star.style.ParagraphAdjust.CENTER );
692*cdf0e10cSrcweir                 }
693*cdf0e10cSrcweir             }
694*cdf0e10cSrcweir         }
695*cdf0e10cSrcweir         catch ( Exception e )
696*cdf0e10cSrcweir         {
697*cdf0e10cSrcweir             e.printStackTrace();
698*cdf0e10cSrcweir         }
699*cdf0e10cSrcweir     }
700*cdf0e10cSrcweir 
701*cdf0e10cSrcweir     /** This method returns a random double which isn't too high or too low
702*cdf0e10cSrcweir      */
703*cdf0e10cSrcweir     protected double getRandomDouble ()
704*cdf0e10cSrcweir     {
705*cdf0e10cSrcweir         return ( ( maRandom.nextInt() % 1000 ) * maRandom.nextDouble () );
706*cdf0e10cSrcweir     }
707*cdf0e10cSrcweir 
708*cdf0e10cSrcweir     /** This method sets the text colour of the cell refered to by sCellName to
709*cdf0e10cSrcweir         white and inserts the string sText in it
710*cdf0e10cSrcweir      */
711*cdf0e10cSrcweir     protected static void insertIntoCell(String sCellName, String sText,
712*cdf0e10cSrcweir                                          XTextTable xTable)
713*cdf0e10cSrcweir     {
714*cdf0e10cSrcweir         // Access the XText interface of the cell referred to by sCellName
715*cdf0e10cSrcweir         XText xCellText = (XText) UnoRuntime.queryInterface(
716*cdf0e10cSrcweir             XText.class, xTable.getCellByName ( sCellName ) );
717*cdf0e10cSrcweir 
718*cdf0e10cSrcweir         // create a text cursor from the cells XText interface
719*cdf0e10cSrcweir         XTextCursor xCellCursor = xCellText.createTextCursor();
720*cdf0e10cSrcweir         // Get the property set of the cell's TextCursor
721*cdf0e10cSrcweir         XPropertySet xCellCursorProps = (XPropertySet)UnoRuntime.queryInterface(
722*cdf0e10cSrcweir             XPropertySet.class, xCellCursor );
723*cdf0e10cSrcweir 
724*cdf0e10cSrcweir         try
725*cdf0e10cSrcweir         {
726*cdf0e10cSrcweir             // Set the colour of the text to white
727*cdf0e10cSrcweir             xCellCursorProps.setPropertyValue( "CharColor", new Integer(16777215));
728*cdf0e10cSrcweir         }
729*cdf0e10cSrcweir         catch ( Exception e)
730*cdf0e10cSrcweir         {
731*cdf0e10cSrcweir             e.printStackTrace();
732*cdf0e10cSrcweir         }
733*cdf0e10cSrcweir         // Set the text in the cell to sText
734*cdf0e10cSrcweir         xCellText.setString( sText );
735*cdf0e10cSrcweir     }
736*cdf0e10cSrcweir 
737*cdf0e10cSrcweir     /** This method shows how to create and insert a text table, as well as insert
738*cdf0e10cSrcweir 		text and formulae into the cells of the table
739*cdf0e10cSrcweir      */
740*cdf0e10cSrcweir     protected void TextTableExample ()
741*cdf0e10cSrcweir     {
742*cdf0e10cSrcweir         try
743*cdf0e10cSrcweir         {
744*cdf0e10cSrcweir             // Create a new table from the document's factory
745*cdf0e10cSrcweir             XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(
746*cdf0e10cSrcweir                 XTextTable.class, mxDocFactory .createInstance(
747*cdf0e10cSrcweir                     "com.sun.star.text.TextTable" ) );
748*cdf0e10cSrcweir 
749*cdf0e10cSrcweir             // Specify that we want the table to have 4 rows and 4 columns
750*cdf0e10cSrcweir             xTable.initialize( 4, 4 );
751*cdf0e10cSrcweir 
752*cdf0e10cSrcweir             // Insert the table into the document
753*cdf0e10cSrcweir             mxDocText.insertTextContent( mxDocCursor, xTable, false);
754*cdf0e10cSrcweir             // Get an XIndexAccess of the table rows
755*cdf0e10cSrcweir             XIndexAccess xRows = xTable.getRows();
756*cdf0e10cSrcweir 
757*cdf0e10cSrcweir             // Access the property set of the first row (properties listed in
758*cdf0e10cSrcweir             // service description: com.sun.star.text.TextTableRow)
759*cdf0e10cSrcweir             XPropertySet xRow = (XPropertySet) UnoRuntime.queryInterface(
760*cdf0e10cSrcweir                 XPropertySet.class, xRows.getByIndex ( 0 ) );
761*cdf0e10cSrcweir             // If BackTransparant is false, then the background color is visible
762*cdf0e10cSrcweir             xRow.setPropertyValue( "BackTransparent", new Boolean(false));
763*cdf0e10cSrcweir             // Specify the color of the background to be dark blue
764*cdf0e10cSrcweir             xRow.setPropertyValue( "BackColor", new Integer(6710932));
765*cdf0e10cSrcweir 
766*cdf0e10cSrcweir             // Access the property set of the whole table
767*cdf0e10cSrcweir             XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface(
768*cdf0e10cSrcweir                 XPropertySet.class, xTable );
769*cdf0e10cSrcweir             // We want visible background colors
770*cdf0e10cSrcweir             xTableProps.setPropertyValue( "BackTransparent", new Boolean(false));
771*cdf0e10cSrcweir             // Set the background colour to light blue
772*cdf0e10cSrcweir             xTableProps.setPropertyValue( "BackColor", new Integer(13421823));
773*cdf0e10cSrcweir 
774*cdf0e10cSrcweir             // set the text (and text colour) of all the cells in the first row
775*cdf0e10cSrcweir             // of the table
776*cdf0e10cSrcweir             insertIntoCell( "A1", "First Column", xTable );
777*cdf0e10cSrcweir             insertIntoCell( "B1", "Second Column", xTable );
778*cdf0e10cSrcweir             insertIntoCell( "C1", "Third Column", xTable );
779*cdf0e10cSrcweir             insertIntoCell( "D1", "Results", xTable );
780*cdf0e10cSrcweir 
781*cdf0e10cSrcweir             // Insert random numbers into the first this three cells of each
782*cdf0e10cSrcweir             // remaining row
783*cdf0e10cSrcweir             xTable.getCellByName( "A2" ).setValue( getRandomDouble() );
784*cdf0e10cSrcweir             xTable.getCellByName( "B2" ).setValue( getRandomDouble() );
785*cdf0e10cSrcweir             xTable.getCellByName( "C2" ).setValue( getRandomDouble() );
786*cdf0e10cSrcweir 
787*cdf0e10cSrcweir             xTable.getCellByName( "A3" ).setValue( getRandomDouble() );
788*cdf0e10cSrcweir             xTable.getCellByName( "B3" ).setValue( getRandomDouble() );
789*cdf0e10cSrcweir             xTable.getCellByName( "C3" ).setValue( getRandomDouble() );
790*cdf0e10cSrcweir 
791*cdf0e10cSrcweir             xTable.getCellByName( "A4" ).setValue( getRandomDouble() );
792*cdf0e10cSrcweir             xTable.getCellByName( "B4" ).setValue( getRandomDouble() );
793*cdf0e10cSrcweir             xTable.getCellByName( "C4" ).setValue( getRandomDouble() );
794*cdf0e10cSrcweir 
795*cdf0e10cSrcweir             // Set the last cell in each row to be a formula that calculates
796*cdf0e10cSrcweir             // the sum of the first three cells
797*cdf0e10cSrcweir             xTable.getCellByName( "D2" ).setFormula( "sum <A2:C2>" );
798*cdf0e10cSrcweir             xTable.getCellByName( "D3" ).setFormula( "sum <A3:C3>" );
799*cdf0e10cSrcweir             xTable.getCellByName( "D4" ).setFormula( "sum <A4:C4>" );
800*cdf0e10cSrcweir         }
801*cdf0e10cSrcweir         catch (Exception e)
802*cdf0e10cSrcweir         {
803*cdf0e10cSrcweir             e.printStackTrace();
804*cdf0e10cSrcweir         }
805*cdf0e10cSrcweir     }
806*cdf0e10cSrcweir     /** This method shows how to create and manipulate text frames
807*cdf0e10cSrcweir      */
808*cdf0e10cSrcweir     protected void TextFrameExample ()
809*cdf0e10cSrcweir     {
810*cdf0e10cSrcweir         try
811*cdf0e10cSrcweir         {
812*cdf0e10cSrcweir             // Use the document's factory to create a new text frame and
813*cdf0e10cSrcweir             // immediately access it's XTextFrame interface
814*cdf0e10cSrcweir             XTextFrame xFrame = (XTextFrame) UnoRuntime.queryInterface (
815*cdf0e10cSrcweir                 XTextFrame.class, mxDocFactory.createInstance (
816*cdf0e10cSrcweir                     "com.sun.star.text.TextFrame" ) );
817*cdf0e10cSrcweir 
818*cdf0e10cSrcweir             // Access the XShape interface of the TextFrame
819*cdf0e10cSrcweir             XShape xShape = (XShape)UnoRuntime.queryInterface(XShape.class, xFrame);
820*cdf0e10cSrcweir             // Access the XPropertySet interface of the TextFrame
821*cdf0e10cSrcweir             XPropertySet xFrameProps = (XPropertySet)UnoRuntime.queryInterface(
822*cdf0e10cSrcweir                 XPropertySet.class, xFrame );
823*cdf0e10cSrcweir 
824*cdf0e10cSrcweir             // Set the size of the new Text Frame using the XShape's 'setSize'
825*cdf0e10cSrcweir             // method
826*cdf0e10cSrcweir             Size aSize = new Size();
827*cdf0e10cSrcweir             aSize.Height = 400;
828*cdf0e10cSrcweir             aSize.Width = 15000;
829*cdf0e10cSrcweir             xShape.setSize(aSize);
830*cdf0e10cSrcweir             // Set the AnchorType to
831*cdf0e10cSrcweir             // com.sun.star.text.TextContentAnchorType.AS_CHARACTER
832*cdf0e10cSrcweir             xFrameProps.setPropertyValue( "AnchorType",
833*cdf0e10cSrcweir                                           TextContentAnchorType.AS_CHARACTER );
834*cdf0e10cSrcweir             // Go to the end of the text document
835*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
836*cdf0e10cSrcweir             // Insert a new paragraph
837*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
838*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
839*cdf0e10cSrcweir             // Then insert the new frame
840*cdf0e10cSrcweir             mxDocText.insertTextContent(mxDocCursor, xFrame, false);
841*cdf0e10cSrcweir 
842*cdf0e10cSrcweir             // Access the XText interface of the text contained within the frame
843*cdf0e10cSrcweir             XText xFrameText = xFrame.getText();
844*cdf0e10cSrcweir             // Create a TextCursor over the frame's contents
845*cdf0e10cSrcweir             XTextCursor xFrameCursor = xFrameText.createTextCursor();
846*cdf0e10cSrcweir             // Insert some text into the frame
847*cdf0e10cSrcweir             xFrameText.insertString(
848*cdf0e10cSrcweir                 xFrameCursor, "The first line in the newly created text frame.",
849*cdf0e10cSrcweir                 false );
850*cdf0e10cSrcweir             xFrameText.insertString(
851*cdf0e10cSrcweir                 xFrameCursor, "\nThe second line in the new text frame.", false );
852*cdf0e10cSrcweir             // Insert a paragraph break into the document (not the frame)
853*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
854*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
855*cdf0e10cSrcweir         }
856*cdf0e10cSrcweir         catch (Exception e)
857*cdf0e10cSrcweir         {
858*cdf0e10cSrcweir             e.printStackTrace();
859*cdf0e10cSrcweir         }
860*cdf0e10cSrcweir     }
861*cdf0e10cSrcweir 
862*cdf0e10cSrcweir     /** This example demonstrates the use of the AutoTextContainer, AutoTextGroup
863*cdf0e10cSrcweir         and AutoTextEntry services and shows how to create, insert and modify
864*cdf0e10cSrcweir         auto text blocks
865*cdf0e10cSrcweir      */
866*cdf0e10cSrcweir     protected void AutoTextExample ()
867*cdf0e10cSrcweir     {
868*cdf0e10cSrcweir         try
869*cdf0e10cSrcweir         {
870*cdf0e10cSrcweir             // Go to the end of the document
871*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
872*cdf0e10cSrcweir             // Insert two paragraphs
873*cdf0e10cSrcweir             mxDocText.insertControlCharacter ( mxDocCursor,
874*cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false );
875*cdf0e10cSrcweir             mxDocText.insertControlCharacter ( mxDocCursor,
876*cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false );
877*cdf0e10cSrcweir             // Position the cursor in the second paragraph
878*cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
879*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XParagraphCursor.class, mxDocCursor );
880*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
881*cdf0e10cSrcweir 
882*cdf0e10cSrcweir             // Get an XNameAccess interface to all auto text groups from the
883*cdf0e10cSrcweir             // document factory
884*cdf0e10cSrcweir             XNameAccess xContainer = (XNameAccess) UnoRuntime.queryInterface(
885*cdf0e10cSrcweir                 XNameAccess.class, mxFactory.createInstance (
886*cdf0e10cSrcweir                     "com.sun.star.text.AutoTextContainer" ) );
887*cdf0e10cSrcweir 
888*cdf0e10cSrcweir             // Create a new table at the document factory
889*cdf0e10cSrcweir             XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(
890*cdf0e10cSrcweir                 XTextTable.class, mxDocFactory .createInstance(
891*cdf0e10cSrcweir                     "com.sun.star.text.TextTable" ) );
892*cdf0e10cSrcweir 
893*cdf0e10cSrcweir             // Store the names of all auto text groups in an array of strings
894*cdf0e10cSrcweir             String[] aGroupNames = xContainer.getElementNames();
895*cdf0e10cSrcweir 
896*cdf0e10cSrcweir             // Make sure we have at least one group name
897*cdf0e10cSrcweir             if ( aGroupNames.length > 0 )
898*cdf0e10cSrcweir             {
899*cdf0e10cSrcweir                 // initialise the table to have a row for every autotext group
900*cdf0e10cSrcweir                 // in a single column + one additional row for a header
901*cdf0e10cSrcweir                 xTable.initialize( aGroupNames.length+1,1);
902*cdf0e10cSrcweir 
903*cdf0e10cSrcweir                 // Access the XPropertySet of the table
904*cdf0e10cSrcweir                 XPropertySet xTableProps = (XPropertySet)UnoRuntime.queryInterface(
905*cdf0e10cSrcweir                     XPropertySet.class, xTable );
906*cdf0e10cSrcweir 
907*cdf0e10cSrcweir                 // We want a visible background
908*cdf0e10cSrcweir                 xTableProps.setPropertyValue( "BackTransparent",
909*cdf0e10cSrcweir                                               new Boolean(false));
910*cdf0e10cSrcweir 
911*cdf0e10cSrcweir                 // We want the background to be light blue
912*cdf0e10cSrcweir                 xTableProps.setPropertyValue( "BackColor", new Integer(13421823));
913*cdf0e10cSrcweir 
914*cdf0e10cSrcweir                 // Inser the table into the document
915*cdf0e10cSrcweir                 mxDocText.insertTextContent( mxDocCursor, xTable, false);
916*cdf0e10cSrcweir 
917*cdf0e10cSrcweir                 // Get an XIndexAccess to all table rows
918*cdf0e10cSrcweir                 XIndexAccess xRows = xTable.getRows();
919*cdf0e10cSrcweir 
920*cdf0e10cSrcweir                 // Get the first row in the table
921*cdf0e10cSrcweir                 XPropertySet xRow = (XPropertySet) UnoRuntime.queryInterface(
922*cdf0e10cSrcweir                     XPropertySet.class, xRows.getByIndex ( 0 ) );
923*cdf0e10cSrcweir 
924*cdf0e10cSrcweir                 // We want the background of the first row to be visible too
925*cdf0e10cSrcweir                 xRow.setPropertyValue( "BackTransparent", new Boolean(false));
926*cdf0e10cSrcweir 
927*cdf0e10cSrcweir                 // And let's make it dark blue
928*cdf0e10cSrcweir                 xRow.setPropertyValue( "BackColor", new Integer(6710932));
929*cdf0e10cSrcweir 
930*cdf0e10cSrcweir                 // Put a description of the table contents into the first cell
931*cdf0e10cSrcweir                 insertIntoCell( "A1", "AutoText Groups", xTable);
932*cdf0e10cSrcweir 
933*cdf0e10cSrcweir                 // Create a table cursor pointing at the second cell in the first
934*cdf0e10cSrcweir                 // column
935*cdf0e10cSrcweir                 XTextTableCursor xTableCursor = xTable.createCursorByCellName("A2");
936*cdf0e10cSrcweir 
937*cdf0e10cSrcweir                 // Loop over the group names
938*cdf0e10cSrcweir                 for ( int i = 0 ; i < aGroupNames.length ; i ++ )
939*cdf0e10cSrcweir                 {
940*cdf0e10cSrcweir                     // Get the name of the current cell
941*cdf0e10cSrcweir                     String sCellName = xTableCursor.getRangeName ();
942*cdf0e10cSrcweir 
943*cdf0e10cSrcweir                     // Get the XText interface of the current cell
944*cdf0e10cSrcweir                     XText xCellText = (XText) UnoRuntime.queryInterface (
945*cdf0e10cSrcweir                         XText.class, xTable.getCellByName ( sCellName ) );
946*cdf0e10cSrcweir 
947*cdf0e10cSrcweir                     // Set the cell contents of the current cell to be
948*cdf0e10cSrcweir                     //the name of the of an autotext group
949*cdf0e10cSrcweir                     xCellText.setString ( aGroupNames[i] );
950*cdf0e10cSrcweir 
951*cdf0e10cSrcweir                     // Access the autotext group with this name
952*cdf0e10cSrcweir                     XAutoTextGroup xGroup = (XAutoTextGroup)
953*cdf0e10cSrcweir                         UnoRuntime.queryInterface (XAutoTextGroup.class,
954*cdf0e10cSrcweir                                        xContainer.getByName(aGroupNames[i]));
955*cdf0e10cSrcweir 
956*cdf0e10cSrcweir                     // Get the titles of each autotext block in this group
957*cdf0e10cSrcweir                     String [] aBlockNames = xGroup.getTitles();
958*cdf0e10cSrcweir 
959*cdf0e10cSrcweir                     // Make sure that the autotext group contains at least one block
960*cdf0e10cSrcweir                     if ( aBlockNames.length > 0 )
961*cdf0e10cSrcweir                     {
962*cdf0e10cSrcweir                         // Split the current cell vertically into two seperate cells
963*cdf0e10cSrcweir                         xTableCursor.splitRange ( (short) 1, false );
964*cdf0e10cSrcweir 
965*cdf0e10cSrcweir                         // Put the cursor in the newly created right hand cell
966*cdf0e10cSrcweir                         // and select it
967*cdf0e10cSrcweir                         xTableCursor.goRight ( (short) 1, false );
968*cdf0e10cSrcweir 
969*cdf0e10cSrcweir                         // Split this cell horizontally to make a seperate cell
970*cdf0e10cSrcweir                         // for each Autotext block
971*cdf0e10cSrcweir                         if ( ( aBlockNames.length -1 ) > 0 )
972*cdf0e10cSrcweir                             xTableCursor.splitRange (
973*cdf0e10cSrcweir                                 (short) (aBlockNames.length - 1), true );
974*cdf0e10cSrcweir 
975*cdf0e10cSrcweir                         // loop over the block names
976*cdf0e10cSrcweir                         for ( int j = 0 ; j < aBlockNames.length ; j ++ )
977*cdf0e10cSrcweir                         {
978*cdf0e10cSrcweir                             // Get the XText interface of the current cell
979*cdf0e10cSrcweir                             xCellText = (XText) UnoRuntime.queryInterface (
980*cdf0e10cSrcweir                                 XText.class, xTable.getCellByName (
981*cdf0e10cSrcweir                                     xTableCursor.getRangeName() ) );
982*cdf0e10cSrcweir 
983*cdf0e10cSrcweir                             // Set the text contents of the current cell to the
984*cdf0e10cSrcweir                             // title of an Autotext block
985*cdf0e10cSrcweir                             xCellText.setString ( aBlockNames[j] );
986*cdf0e10cSrcweir 
987*cdf0e10cSrcweir                             // Move the cursor down one cell
988*cdf0e10cSrcweir                             xTableCursor.goDown( (short)1, false);
989*cdf0e10cSrcweir                         }
990*cdf0e10cSrcweir                     }
991*cdf0e10cSrcweir                     // Go back to the cell we originally split
992*cdf0e10cSrcweir                     xTableCursor.gotoCellByName ( sCellName, false );
993*cdf0e10cSrcweir 
994*cdf0e10cSrcweir                                     // Go down one cell
995*cdf0e10cSrcweir                     xTableCursor.goDown( (short)1, false);
996*cdf0e10cSrcweir                 }
997*cdf0e10cSrcweir 
998*cdf0e10cSrcweir                 XAutoTextGroup xGroup;
999*cdf0e10cSrcweir                 String [] aBlockNames;
1000*cdf0e10cSrcweir 
1001*cdf0e10cSrcweir                 // Add a depth so that we only generate 200 numbers before giving up
1002*cdf0e10cSrcweir                 // on finding a random autotext group that contains autotext blocks
1003*cdf0e10cSrcweir                 int nDepth = 0;
1004*cdf0e10cSrcweir                 do
1005*cdf0e10cSrcweir                 {
1006*cdf0e10cSrcweir                     // Generate a random, positive number which is lower than
1007*cdf0e10cSrcweir                     // the number of autotext groups
1008*cdf0e10cSrcweir                     int nRandom = Math.abs ( maRandom.nextInt() %
1009*cdf0e10cSrcweir                                              aGroupNames.length );
1010*cdf0e10cSrcweir 
1011*cdf0e10cSrcweir                     // Get the autotext group at this name
1012*cdf0e10cSrcweir                     xGroup 	= ( XAutoTextGroup ) UnoRuntime.queryInterface (
1013*cdf0e10cSrcweir                         XAutoTextGroup.class, xContainer.getByName (
1014*cdf0e10cSrcweir                             aGroupNames[ nRandom ] ) );
1015*cdf0e10cSrcweir 
1016*cdf0e10cSrcweir                     // Fill our string array with the names of all the blocks in
1017*cdf0e10cSrcweir                     // this group
1018*cdf0e10cSrcweir                     aBlockNames = xGroup.getElementNames();
1019*cdf0e10cSrcweir 
1020*cdf0e10cSrcweir                     // increment our depth counter
1021*cdf0e10cSrcweir                     ++nDepth;
1022*cdf0e10cSrcweir                 }
1023*cdf0e10cSrcweir                 while ( nDepth < 200 && aBlockNames.length == 0 );
1024*cdf0e10cSrcweir                 // If we managed to find a group containg blocks...
1025*cdf0e10cSrcweir                 if ( aBlockNames.length > 0 )
1026*cdf0e10cSrcweir                 {
1027*cdf0e10cSrcweir                     // Pick a random block in this group and get it's
1028*cdf0e10cSrcweir                     // XAutoTextEntry interface
1029*cdf0e10cSrcweir                     int nRandom = Math.abs ( maRandom.nextInt()
1030*cdf0e10cSrcweir                                              % aBlockNames.length );
1031*cdf0e10cSrcweir                     XAutoTextEntry xEntry = ( XAutoTextEntry )
1032*cdf0e10cSrcweir                         UnoRuntime.queryInterface (
1033*cdf0e10cSrcweir                             XAutoTextEntry.class, xGroup.getByName (
1034*cdf0e10cSrcweir                                 aBlockNames[ nRandom ] ) );
1035*cdf0e10cSrcweir                     // insert the modified autotext block at the end of the document
1036*cdf0e10cSrcweir                     xEntry.applyTo ( mxDocCursor );
1037*cdf0e10cSrcweir 
1038*cdf0e10cSrcweir                     // Get the titles of all text blocks in this AutoText group
1039*cdf0e10cSrcweir                     String [] aBlockTitles = xGroup.getTitles();
1040*cdf0e10cSrcweir 
1041*cdf0e10cSrcweir                     // Get the XNamed interface of the autotext group
1042*cdf0e10cSrcweir                     XNamed xGroupNamed = ( XNamed ) UnoRuntime.queryInterface (
1043*cdf0e10cSrcweir                         XNamed.class, xGroup );
1044*cdf0e10cSrcweir 
1045*cdf0e10cSrcweir                     // Output the short cut and title of the random block
1046*cdf0e10cSrcweir                     //and the name of the group it's from
1047*cdf0e10cSrcweir                     System.out.println ( "Inserted the Autotext '" +
1048*cdf0e10cSrcweir                                          aBlockTitles[nRandom]
1049*cdf0e10cSrcweir                                          + "', shortcut '" + aBlockNames[nRandom]
1050*cdf0e10cSrcweir                                          + "' from group '"
1051*cdf0e10cSrcweir                                          + xGroupNamed.getName());
1052*cdf0e10cSrcweir                 }
1053*cdf0e10cSrcweir             }
1054*cdf0e10cSrcweir 
1055*cdf0e10cSrcweir             // Go to the end of the document
1056*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1057*cdf0e10cSrcweir             // Insert new paragraph
1058*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1059*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1060*cdf0e10cSrcweir 
1061*cdf0e10cSrcweir             // Position cursor in new paragraph
1062*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1063*cdf0e10cSrcweir 
1064*cdf0e10cSrcweir             // Insert a string in the new paragraph
1065*cdf0e10cSrcweir             mxDocText.insertString ( mxDocCursor,
1066*cdf0e10cSrcweir                                      "Some text for a new autotext block", false );
1067*cdf0e10cSrcweir 
1068*cdf0e10cSrcweir             // Go to the end of the document
1069*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1070*cdf0e10cSrcweir         }
1071*cdf0e10cSrcweir         catch (Exception e)
1072*cdf0e10cSrcweir         {
1073*cdf0e10cSrcweir             e.printStackTrace();
1074*cdf0e10cSrcweir         }
1075*cdf0e10cSrcweir     }
1076*cdf0e10cSrcweir 
1077*cdf0e10cSrcweir     /** This method demonstrates how to insert indexes and index marks
1078*cdf0e10cSrcweir      */
1079*cdf0e10cSrcweir     protected void IndexExample ()
1080*cdf0e10cSrcweir     {
1081*cdf0e10cSrcweir         try
1082*cdf0e10cSrcweir         {
1083*cdf0e10cSrcweir             // Go to the end of the document
1084*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1085*cdf0e10cSrcweir             // Insert a new paragraph and position the cursor in it
1086*cdf0e10cSrcweir             mxDocText.insertControlCharacter ( mxDocCursor,
1087*cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false );
1088*cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1089*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1090*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1091*cdf0e10cSrcweir 
1092*cdf0e10cSrcweir             // Create a new ContentIndexMark and get it's XPropertySet interface
1093*cdf0e10cSrcweir             XPropertySet xEntry = (XPropertySet)UnoRuntime.queryInterface(
1094*cdf0e10cSrcweir                 XPropertySet.class,
1095*cdf0e10cSrcweir                 mxDocFactory.createInstance("com.sun.star.text.ContentIndexMark"));
1096*cdf0e10cSrcweir 
1097*cdf0e10cSrcweir             // Set the text to be displayed in the index
1098*cdf0e10cSrcweir             xEntry.setPropertyValue(
1099*cdf0e10cSrcweir                 "AlternativeText", "Big dogs! Falling on my head!");
1100*cdf0e10cSrcweir 
1101*cdf0e10cSrcweir             // The Level property _must_ be set
1102*cdf0e10cSrcweir             xEntry.setPropertyValue ( "Level", new Short ( (short) 1 ) );
1103*cdf0e10cSrcweir 
1104*cdf0e10cSrcweir             // Create a ContentIndex and access it's XPropertySet interface
1105*cdf0e10cSrcweir             XPropertySet xIndex = (XPropertySet) UnoRuntime.queryInterface(
1106*cdf0e10cSrcweir                 XPropertySet.class,
1107*cdf0e10cSrcweir                 mxDocFactory.createInstance ( "com.sun.star.text.ContentIndex" ) );
1108*cdf0e10cSrcweir 
1109*cdf0e10cSrcweir             // Again, the Level property _must_ be set
1110*cdf0e10cSrcweir             xIndex.setPropertyValue ( "Level", new Short ( (short) 10 ) );
1111*cdf0e10cSrcweir 
1112*cdf0e10cSrcweir             // Access the XTextContent interfaces of both the Index and the
1113*cdf0e10cSrcweir             // IndexMark
1114*cdf0e10cSrcweir             XTextContent xIndexContent = (XTextContent) UnoRuntime.queryInterface(
1115*cdf0e10cSrcweir                 XTextContent.class, xIndex );
1116*cdf0e10cSrcweir             XTextContent xEntryContent = (XTextContent) UnoRuntime.queryInterface(
1117*cdf0e10cSrcweir                 XTextContent.class, xEntry );
1118*cdf0e10cSrcweir 
1119*cdf0e10cSrcweir             // Insert both in the document
1120*cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xEntryContent, false );
1121*cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xIndexContent, false );
1122*cdf0e10cSrcweir 
1123*cdf0e10cSrcweir             // Get the XDocumentIndex interface of the Index
1124*cdf0e10cSrcweir             XDocumentIndex xDocIndex = (XDocumentIndex) UnoRuntime.queryInterface(
1125*cdf0e10cSrcweir                 XDocumentIndex.class, xIndex );
1126*cdf0e10cSrcweir 
1127*cdf0e10cSrcweir             // And call it's update method
1128*cdf0e10cSrcweir             xDocIndex.update();
1129*cdf0e10cSrcweir         }
1130*cdf0e10cSrcweir         catch (Exception e)
1131*cdf0e10cSrcweir         {
1132*cdf0e10cSrcweir             e.printStackTrace();
1133*cdf0e10cSrcweir         }
1134*cdf0e10cSrcweir     }
1135*cdf0e10cSrcweir 
1136*cdf0e10cSrcweir     /** This method demonstrates how to create and insert reference marks, and
1137*cdf0e10cSrcweir      * GetReference Text Fields
1138*cdf0e10cSrcweir      */
1139*cdf0e10cSrcweir     protected void ReferenceExample ()
1140*cdf0e10cSrcweir     {
1141*cdf0e10cSrcweir         try
1142*cdf0e10cSrcweir         {
1143*cdf0e10cSrcweir             // Go to the end of the document
1144*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1145*cdf0e10cSrcweir 
1146*cdf0e10cSrcweir             // Insert a paragraph break
1147*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1148*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1149*cdf0e10cSrcweir 
1150*cdf0e10cSrcweir             // Get the Paragraph cursor
1151*cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1152*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1153*cdf0e10cSrcweir 
1154*cdf0e10cSrcweir             // Move the cursor into the new paragraph
1155*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1156*cdf0e10cSrcweir 
1157*cdf0e10cSrcweir             // Create a new ReferenceMark and get it's XNamed interface
1158*cdf0e10cSrcweir             XNamed xRefMark = (XNamed) UnoRuntime.queryInterface(XNamed.class,
1159*cdf0e10cSrcweir                   mxDocFactory.createInstance ("com.sun.star.text.ReferenceMark"));
1160*cdf0e10cSrcweir 
1161*cdf0e10cSrcweir             // Set the name to TableHeader
1162*cdf0e10cSrcweir             xRefMark.setName ( "TableHeader" );
1163*cdf0e10cSrcweir 
1164*cdf0e10cSrcweir             // Get the TextTablesSupplier interface of the document
1165*cdf0e10cSrcweir             XTextTablesSupplier xTableSupplier = ( XTextTablesSupplier )
1166*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XTextTablesSupplier.class, mxDoc);
1167*cdf0e10cSrcweir 
1168*cdf0e10cSrcweir             // Get an XIndexAccess of TextTables
1169*cdf0e10cSrcweir             XIndexAccess xTables = (XIndexAccess)UnoRuntime.queryInterface(
1170*cdf0e10cSrcweir                 XIndexAccess.class, xTableSupplier.getTextTables());
1171*cdf0e10cSrcweir 
1172*cdf0e10cSrcweir             // We've only inserted one table, so get the first one from index zero
1173*cdf0e10cSrcweir             XTextTable xTable = ( XTextTable ) UnoRuntime.queryInterface (
1174*cdf0e10cSrcweir                 XTextTable.class, xTables.getByIndex( 0 ) );
1175*cdf0e10cSrcweir 
1176*cdf0e10cSrcweir             // Get the first cell from the table
1177*cdf0e10cSrcweir             XText xTableText = (XText) UnoRuntime.queryInterface(
1178*cdf0e10cSrcweir                 XText.class, xTable.getCellByName ( "A1" ) );
1179*cdf0e10cSrcweir 
1180*cdf0e10cSrcweir             // Get a text cursor for the first cell
1181*cdf0e10cSrcweir             XTextCursor xTableCursor = xTableText.createTextCursor();
1182*cdf0e10cSrcweir 
1183*cdf0e10cSrcweir             // Get the XTextContent interface of the reference mark so we can
1184*cdf0e10cSrcweir             // insert it
1185*cdf0e10cSrcweir             XTextContent xContent = ( XTextContent ) UnoRuntime.queryInterface (
1186*cdf0e10cSrcweir                 XTextContent.class, xRefMark );
1187*cdf0e10cSrcweir 
1188*cdf0e10cSrcweir             // Insert the reference mark into the first cell of the table
1189*cdf0e10cSrcweir             xTableText.insertTextContent ( xTableCursor, xContent, false );
1190*cdf0e10cSrcweir 
1191*cdf0e10cSrcweir             // Create a 'GetReference' text field to refer to the reference mark
1192*cdf0e10cSrcweir             // we just inserted, and get it's XPropertySet interface
1193*cdf0e10cSrcweir             XPropertySet xFieldProps = (XPropertySet) UnoRuntime.queryInterface(
1194*cdf0e10cSrcweir                 XPropertySet.class, mxDocFactory.createInstance (
1195*cdf0e10cSrcweir                     "com.sun.star.text.TextField.GetReference" ) );
1196*cdf0e10cSrcweir 
1197*cdf0e10cSrcweir             // Get the XReferenceMarksSupplier interface of the document
1198*cdf0e10cSrcweir             XReferenceMarksSupplier xRefSupplier = ( XReferenceMarksSupplier )
1199*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XReferenceMarksSupplier.class, mxDoc );
1200*cdf0e10cSrcweir 
1201*cdf0e10cSrcweir             // Get an XNameAccess which refers to all inserted reference marks
1202*cdf0e10cSrcweir             XNameAccess xMarks = ( XNameAccess ) UnoRuntime.queryInterface (
1203*cdf0e10cSrcweir                 XNameAccess.class, xRefSupplier.getReferenceMarks() );
1204*cdf0e10cSrcweir 
1205*cdf0e10cSrcweir             // Put the names of each reference mark into an array of strings
1206*cdf0e10cSrcweir             String[] aNames = xMarks.getElementNames();
1207*cdf0e10cSrcweir 
1208*cdf0e10cSrcweir             // Make sure that at least 1 reference mark actually exists
1209*cdf0e10cSrcweir             // (well, we just inserted one!)
1210*cdf0e10cSrcweir             if ( aNames.length > 0 )
1211*cdf0e10cSrcweir             {
1212*cdf0e10cSrcweir                 // Output the name of the first reference mark ('TableHeader')
1213*cdf0e10cSrcweir                 System.out.println (
1214*cdf0e10cSrcweir                     "GetReference text field inserted for ReferenceMark : "
1215*cdf0e10cSrcweir                     + aNames[0] );
1216*cdf0e10cSrcweir 
1217*cdf0e10cSrcweir                 // Set the SourceName of the GetReference text field to
1218*cdf0e10cSrcweir                 // 'TableHeader'
1219*cdf0e10cSrcweir                 xFieldProps.setPropertyValue ( "SourceName", aNames[0] );
1220*cdf0e10cSrcweir 
1221*cdf0e10cSrcweir                 // specify that the source is a reference mark (could also be a
1222*cdf0e10cSrcweir                 // footnote, bookmark or sequence field )
1223*cdf0e10cSrcweir                 xFieldProps.setPropertyValue ( "ReferenceFieldSource",
1224*cdf0e10cSrcweir                                 new Short(ReferenceFieldSource.REFERENCE_MARK));
1225*cdf0e10cSrcweir 
1226*cdf0e10cSrcweir                 // We want the reference displayed as 'above' or 'below'
1227*cdf0e10cSrcweir                 xFieldProps.setPropertyValue ( "ReferenceFieldPart",
1228*cdf0e10cSrcweir                                 new Short(ReferenceFieldPart.UP_DOWN));
1229*cdf0e10cSrcweir 
1230*cdf0e10cSrcweir 
1231*cdf0e10cSrcweir                 // Get the XTextContent interface of the GetReference text field
1232*cdf0e10cSrcweir                 XTextContent xRefContent = (XTextContent) UnoRuntime.queryInterface(
1233*cdf0e10cSrcweir                     XTextContent.class, xFieldProps );
1234*cdf0e10cSrcweir 
1235*cdf0e10cSrcweir                 // Go to the end of the document
1236*cdf0e10cSrcweir                 mxDocCursor.gotoEnd( false );
1237*cdf0e10cSrcweir 
1238*cdf0e10cSrcweir                 // Make some text to precede the reference
1239*cdf0e10cSrcweir                 mxDocText.insertString(mxDocText.getEnd(), "The table ", false);
1240*cdf0e10cSrcweir 
1241*cdf0e10cSrcweir                 // Insert the text field
1242*cdf0e10cSrcweir                 mxDocText.insertTextContent(mxDocText.getEnd(), xRefContent, false);
1243*cdf0e10cSrcweir 
1244*cdf0e10cSrcweir                 // And some text after the reference..
1245*cdf0e10cSrcweir                 mxDocText.insertString(mxDocText.getEnd(),
1246*cdf0e10cSrcweir                               " contains the sum of some random numbers.", false );
1247*cdf0e10cSrcweir 
1248*cdf0e10cSrcweir                 // Refresh the document
1249*cdf0e10cSrcweir                 XRefreshable xRefresh = (XRefreshable) UnoRuntime.queryInterface(
1250*cdf0e10cSrcweir                     XRefreshable.class, mxDoc );
1251*cdf0e10cSrcweir                 xRefresh.refresh();
1252*cdf0e10cSrcweir             }
1253*cdf0e10cSrcweir         }
1254*cdf0e10cSrcweir         catch (Exception e)
1255*cdf0e10cSrcweir         {
1256*cdf0e10cSrcweir             e.printStackTrace();
1257*cdf0e10cSrcweir         }
1258*cdf0e10cSrcweir     }
1259*cdf0e10cSrcweir 
1260*cdf0e10cSrcweir     /** This method demonstrates how to create and insert footnotes, and how to
1261*cdf0e10cSrcweir         access the XFootnotesSupplier interface of the document
1262*cdf0e10cSrcweir      */
1263*cdf0e10cSrcweir     protected void FootnoteExample ()
1264*cdf0e10cSrcweir     {
1265*cdf0e10cSrcweir         try
1266*cdf0e10cSrcweir         {
1267*cdf0e10cSrcweir             // Create a new footnote from the document factory and get it's
1268*cdf0e10cSrcweir             // XFootnote interface
1269*cdf0e10cSrcweir             XFootnote xFootnote = (XFootnote) UnoRuntime.queryInterface(
1270*cdf0e10cSrcweir                 XFootnote.class, mxDocFactory.createInstance (
1271*cdf0e10cSrcweir                     "com.sun.star.text.Footnote" ) );
1272*cdf0e10cSrcweir 
1273*cdf0e10cSrcweir             // Set the label to 'Numbers'
1274*cdf0e10cSrcweir             xFootnote.setLabel ( "Numbers" );
1275*cdf0e10cSrcweir 
1276*cdf0e10cSrcweir             // Get the footnotes XTextContent interface so we can...
1277*cdf0e10cSrcweir             XTextContent xContent = ( XTextContent ) UnoRuntime.queryInterface (
1278*cdf0e10cSrcweir                 XTextContent.class, xFootnote );
1279*cdf0e10cSrcweir 
1280*cdf0e10cSrcweir             // ...insert it into the document
1281*cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xContent, false );
1282*cdf0e10cSrcweir 
1283*cdf0e10cSrcweir             // Get the XFootnotesSupplier interface of the document
1284*cdf0e10cSrcweir             XFootnotesSupplier xFootnoteSupplier = (XFootnotesSupplier)
1285*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XFootnotesSupplier.class, mxDoc );
1286*cdf0e10cSrcweir 
1287*cdf0e10cSrcweir             // Get an XIndexAccess interface to all footnotes
1288*cdf0e10cSrcweir             XIndexAccess xFootnotes = ( XIndexAccess ) UnoRuntime.queryInterface (
1289*cdf0e10cSrcweir                 XIndexAccess.class, xFootnoteSupplier.getFootnotes() );
1290*cdf0e10cSrcweir 
1291*cdf0e10cSrcweir             // Get the XFootnote interface to the first footnote inserted ('Numbers')
1292*cdf0e10cSrcweir             XFootnote xNumbers = ( XFootnote ) UnoRuntime.queryInterface (
1293*cdf0e10cSrcweir                 XFootnote.class, xFootnotes.getByIndex( 0 ) );
1294*cdf0e10cSrcweir 
1295*cdf0e10cSrcweir             // Get the XSimpleText interface to the Footnote
1296*cdf0e10cSrcweir             XSimpleText xSimple = (XSimpleText ) UnoRuntime.queryInterface (
1297*cdf0e10cSrcweir                 XSimpleText.class, xNumbers );
1298*cdf0e10cSrcweir 
1299*cdf0e10cSrcweir             // Create a text cursor for the foot note text
1300*cdf0e10cSrcweir             XTextRange xRange = (XTextRange ) UnoRuntime.queryInterface (
1301*cdf0e10cSrcweir                 XTextRange.class, xSimple.createTextCursor() );
1302*cdf0e10cSrcweir 
1303*cdf0e10cSrcweir             // And insert the actual text of the footnote.
1304*cdf0e10cSrcweir             xSimple.insertString (
1305*cdf0e10cSrcweir                 xRange, "  The numbers were generated by using java.util.Random", false );
1306*cdf0e10cSrcweir         }
1307*cdf0e10cSrcweir         catch (Exception e)
1308*cdf0e10cSrcweir         {
1309*cdf0e10cSrcweir             e.printStackTrace();
1310*cdf0e10cSrcweir         }
1311*cdf0e10cSrcweir     }
1312*cdf0e10cSrcweir 
1313*cdf0e10cSrcweir     /** This method demonstrates how to create and manipulate shapes, and how to
1314*cdf0e10cSrcweir         access the draw page of the document to insert shapes
1315*cdf0e10cSrcweir      */
1316*cdf0e10cSrcweir     protected void DrawPageExample ()
1317*cdf0e10cSrcweir     {
1318*cdf0e10cSrcweir         try
1319*cdf0e10cSrcweir         {
1320*cdf0e10cSrcweir             // Go to the end of the document
1321*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1322*cdf0e10cSrcweir             // Insert two new paragraphs
1323*cdf0e10cSrcweir             mxDocText.insertControlCharacter(mxDocCursor,
1324*cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false);
1325*cdf0e10cSrcweir             mxDocText.insertControlCharacter(mxDocCursor,
1326*cdf0e10cSrcweir                           ControlCharacter.PARAGRAPH_BREAK, false);
1327*cdf0e10cSrcweir 
1328*cdf0e10cSrcweir             // Get the XParagraphCursor interface of our document cursor
1329*cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1330*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1331*cdf0e10cSrcweir 
1332*cdf0e10cSrcweir             // Position the cursor in the 2nd paragraph
1333*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1334*cdf0e10cSrcweir 
1335*cdf0e10cSrcweir             // Create a RectangleShape using the document factory
1336*cdf0e10cSrcweir             XShape xRect = (XShape) UnoRuntime.queryInterface(
1337*cdf0e10cSrcweir                 XShape.class, mxDocFactory.createInstance (
1338*cdf0e10cSrcweir                     "com.sun.star.drawing.RectangleShape" ) );
1339*cdf0e10cSrcweir 
1340*cdf0e10cSrcweir             // Create an EllipseShape using the document factory
1341*cdf0e10cSrcweir             XShape xEllipse = (XShape) UnoRuntime.queryInterface(
1342*cdf0e10cSrcweir                 XShape.class, mxDocFactory.createInstance (
1343*cdf0e10cSrcweir                     "com.sun.star.drawing.EllipseShape" ) );
1344*cdf0e10cSrcweir 
1345*cdf0e10cSrcweir             // Set the size of both the ellipse and the rectangle
1346*cdf0e10cSrcweir             Size aSize = new Size();
1347*cdf0e10cSrcweir             aSize.Height = 4000;
1348*cdf0e10cSrcweir             aSize.Width = 10000;
1349*cdf0e10cSrcweir             xRect.setSize(aSize);
1350*cdf0e10cSrcweir             aSize.Height = 3000;
1351*cdf0e10cSrcweir             aSize.Width = 6000;
1352*cdf0e10cSrcweir             xEllipse.setSize ( aSize );
1353*cdf0e10cSrcweir 
1354*cdf0e10cSrcweir             // Set the position of the Rectangle to the right of the ellipse
1355*cdf0e10cSrcweir             Point aPoint = new Point();
1356*cdf0e10cSrcweir             aPoint.X = 6100;
1357*cdf0e10cSrcweir             aPoint.Y = 0;
1358*cdf0e10cSrcweir             xRect.setPosition ( aPoint );
1359*cdf0e10cSrcweir 
1360*cdf0e10cSrcweir             // Get the XPropertySet interfaces of both shapes
1361*cdf0e10cSrcweir             XPropertySet xRectProps = (XPropertySet) UnoRuntime.queryInterface(
1362*cdf0e10cSrcweir                 XPropertySet.class, xRect );
1363*cdf0e10cSrcweir             XPropertySet xEllipseProps = (XPropertySet) UnoRuntime.queryInterface(
1364*cdf0e10cSrcweir                 XPropertySet.class, xEllipse );
1365*cdf0e10cSrcweir 
1366*cdf0e10cSrcweir             // And set the AnchorTypes of both shapes to 'AT_PARAGRAPH'
1367*cdf0e10cSrcweir             xRectProps.setPropertyValue ( "AnchorType",
1368*cdf0e10cSrcweir                                           TextContentAnchorType.AT_PARAGRAPH );
1369*cdf0e10cSrcweir             xEllipseProps.setPropertyValue ( "AnchorType",
1370*cdf0e10cSrcweir                                              TextContentAnchorType.AT_PARAGRAPH );
1371*cdf0e10cSrcweir 
1372*cdf0e10cSrcweir             // Access the XDrawPageSupplier interface of the document
1373*cdf0e10cSrcweir             XDrawPageSupplier xDrawPageSupplier = (XDrawPageSupplier)
1374*cdf0e10cSrcweir                 UnoRuntime.queryInterface (XDrawPageSupplier.class, mxDoc );
1375*cdf0e10cSrcweir 
1376*cdf0e10cSrcweir             // Get the XShapes interface of the draw page
1377*cdf0e10cSrcweir             XShapes xShapes = ( XShapes ) UnoRuntime.queryInterface (
1378*cdf0e10cSrcweir                 XShapes.class, xDrawPageSupplier.getDrawPage () );
1379*cdf0e10cSrcweir 
1380*cdf0e10cSrcweir             // Add both shapes
1381*cdf0e10cSrcweir             xShapes.add ( xEllipse );
1382*cdf0e10cSrcweir             xShapes.add ( xRect );
1383*cdf0e10cSrcweir 
1384*cdf0e10cSrcweir             /*
1385*cdf0e10cSrcweir               This doesn't work, I am assured that FME and AMA are fixing it.
1386*cdf0e10cSrcweir 
1387*cdf0e10cSrcweir               XShapes xGrouper = (XShapes) UnoRuntime.queryInterface(
1388*cdf0e10cSrcweir               XShapes.class, mxDocFactory.createInstance (
1389*cdf0e10cSrcweir               "com.sun.star.drawing.GroupShape" ) );
1390*cdf0e10cSrcweir 
1391*cdf0e10cSrcweir               XShape xGrouperShape = (XShape) UnoRuntime.queryInterface(
1392*cdf0e10cSrcweir                      XShape.class, xGrouper );
1393*cdf0e10cSrcweir               xShapes.add ( xGrouperShape );
1394*cdf0e10cSrcweir 
1395*cdf0e10cSrcweir               xGrouper.add ( xRect );
1396*cdf0e10cSrcweir               xGrouper.add ( xEllipse );
1397*cdf0e10cSrcweir 
1398*cdf0e10cSrcweir               XShapeGrouper xShapeGrouper = (XShapeGrouper)
1399*cdf0e10cSrcweir               UnoRuntime.queryInterface(XShapeGrouper.class, xShapes);
1400*cdf0e10cSrcweir               xShapeGrouper.group ( xGrouper );
1401*cdf0e10cSrcweir             */
1402*cdf0e10cSrcweir 
1403*cdf0e10cSrcweir         }
1404*cdf0e10cSrcweir         catch (Exception e)
1405*cdf0e10cSrcweir         {
1406*cdf0e10cSrcweir             e.printStackTrace();
1407*cdf0e10cSrcweir         }
1408*cdf0e10cSrcweir     }
1409*cdf0e10cSrcweir 
1410*cdf0e10cSrcweir     /** This method demonstrates how to create, insert and apply styles
1411*cdf0e10cSrcweir      */
1412*cdf0e10cSrcweir     protected void StylesExample ()
1413*cdf0e10cSrcweir     {
1414*cdf0e10cSrcweir         try
1415*cdf0e10cSrcweir         {
1416*cdf0e10cSrcweir             // Go to the end of the document
1417*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1418*cdf0e10cSrcweir 
1419*cdf0e10cSrcweir             // Insert two paragraph breaks
1420*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1421*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1422*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1423*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1424*cdf0e10cSrcweir 
1425*cdf0e10cSrcweir             // Create a new style from the document's factory
1426*cdf0e10cSrcweir             XStyle xStyle = (XStyle) UnoRuntime.queryInterface(
1427*cdf0e10cSrcweir                 XStyle.class, mxDocFactory.createInstance(
1428*cdf0e10cSrcweir                     "com.sun.star.style.ParagraphStyle" ) );
1429*cdf0e10cSrcweir 
1430*cdf0e10cSrcweir             // Access the XPropertySet interface of the new style
1431*cdf0e10cSrcweir             XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(
1432*cdf0e10cSrcweir                 XPropertySet.class, xStyle );
1433*cdf0e10cSrcweir 
1434*cdf0e10cSrcweir             // Give the new style a light blue background
1435*cdf0e10cSrcweir             xStyleProps.setPropertyValue ( "ParaBackColor", new Integer (13421823));
1436*cdf0e10cSrcweir 
1437*cdf0e10cSrcweir             // Get the StyleFamiliesSupplier interface of the document
1438*cdf0e10cSrcweir             XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)
1439*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XStyleFamiliesSupplier.class, mxDoc);
1440*cdf0e10cSrcweir 
1441*cdf0e10cSrcweir             // Use the StyleFamiliesSupplier interface to get the XNameAccess
1442*cdf0e10cSrcweir             // interface of the actual style families
1443*cdf0e10cSrcweir             XNameAccess xFamilies = ( XNameAccess ) UnoRuntime.queryInterface (
1444*cdf0e10cSrcweir                 XNameAccess.class, xSupplier.getStyleFamilies() );
1445*cdf0e10cSrcweir 
1446*cdf0e10cSrcweir             // Access the 'ParagraphStyles' Family
1447*cdf0e10cSrcweir             XNameContainer xFamily = (XNameContainer ) UnoRuntime.queryInterface (
1448*cdf0e10cSrcweir                         XNameContainer.class,
1449*cdf0e10cSrcweir                         xFamilies.getByName ( "ParagraphStyles" ) );
1450*cdf0e10cSrcweir 
1451*cdf0e10cSrcweir             // Insert the newly created style into the ParagraphStyles family
1452*cdf0e10cSrcweir             xFamily.insertByName ( "All-Singing All-Dancing Style", xStyle );
1453*cdf0e10cSrcweir 
1454*cdf0e10cSrcweir             // Get the XParagraphCursor interface of the document cursor
1455*cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1456*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1457*cdf0e10cSrcweir 
1458*cdf0e10cSrcweir             // Select the first paragraph inserted
1459*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1460*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( true );
1461*cdf0e10cSrcweir 
1462*cdf0e10cSrcweir             // Access the property set of the cursor selection
1463*cdf0e10cSrcweir             XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
1464*cdf0e10cSrcweir                 XPropertySet.class, mxDocCursor );
1465*cdf0e10cSrcweir 
1466*cdf0e10cSrcweir             // Set the style of the cursor selection to our newly created style
1467*cdf0e10cSrcweir             xCursorProps.setPropertyValue ( "ParaStyleName",
1468*cdf0e10cSrcweir                                             "All-Singing All-Dancing Style" );
1469*cdf0e10cSrcweir 
1470*cdf0e10cSrcweir             // Go back to the end
1471*cdf0e10cSrcweir             mxDocCursor.gotoEnd ( false );
1472*cdf0e10cSrcweir 
1473*cdf0e10cSrcweir             // Select the last paragraph in the document
1474*cdf0e10cSrcweir             xParaCursor.gotoNextParagraph ( true );
1475*cdf0e10cSrcweir 
1476*cdf0e10cSrcweir             // And reset it's style to 'Standard' (the programmatic name for
1477*cdf0e10cSrcweir             // the default style)
1478*cdf0e10cSrcweir             xCursorProps.setPropertyValue ( "ParaStyleName", "Standard" );
1479*cdf0e10cSrcweir         }
1480*cdf0e10cSrcweir         catch (Exception e)
1481*cdf0e10cSrcweir         {
1482*cdf0e10cSrcweir             e.printStackTrace();
1483*cdf0e10cSrcweir         }
1484*cdf0e10cSrcweir     }
1485*cdf0e10cSrcweir 
1486*cdf0e10cSrcweir     /** This method demonstrates how to set numbering types and numbering levels
1487*cdf0e10cSrcweir         using the com.sun.star.text.NumberingRules service
1488*cdf0e10cSrcweir      */
1489*cdf0e10cSrcweir     protected void NumberingExample ()
1490*cdf0e10cSrcweir     {
1491*cdf0e10cSrcweir         try
1492*cdf0e10cSrcweir         {
1493*cdf0e10cSrcweir             // Go to the end of the document
1494*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1495*cdf0e10cSrcweir             // Get the RelativeTextContentInsert interface of the document
1496*cdf0e10cSrcweir             XRelativeTextContentInsert xRelative =
1497*cdf0e10cSrcweir                 (XRelativeTextContentInsert ) UnoRuntime.queryInterface (
1498*cdf0e10cSrcweir                     XRelativeTextContentInsert.class, mxDocText );
1499*cdf0e10cSrcweir 
1500*cdf0e10cSrcweir             // Use the document's factory to create the NumberingRules service,
1501*cdf0e10cSrcweir             // and get it's XIndexAccess interface
1502*cdf0e10cSrcweir             XIndexAccess xNum = (XIndexAccess) UnoRuntime.queryInterface(
1503*cdf0e10cSrcweir                 XIndexAccess.class,
1504*cdf0e10cSrcweir                 mxDocFactory.createInstance( "com.sun.star.text.NumberingRules" ) );
1505*cdf0e10cSrcweir 
1506*cdf0e10cSrcweir             // Also get the NumberingRule's XIndexReplace interface
1507*cdf0e10cSrcweir             XIndexReplace xReplace = (XIndexReplace) UnoRuntime.queryInterface(
1508*cdf0e10cSrcweir                 XIndexReplace.class, xNum );
1509*cdf0e10cSrcweir 
1510*cdf0e10cSrcweir             // Create an array of XPropertySets, one for each of the three
1511*cdf0e10cSrcweir             // paragraphs we're about to create
1512*cdf0e10cSrcweir             XPropertySet xParas[] = new XPropertySet [ 3 ];
1513*cdf0e10cSrcweir             for ( int i = 0 ; i < 3 ; ++ i )
1514*cdf0e10cSrcweir             {
1515*cdf0e10cSrcweir                 // Create a new paragraph
1516*cdf0e10cSrcweir                 XTextContent xNewPara = (XTextContent) UnoRuntime.queryInterface(
1517*cdf0e10cSrcweir                     XTextContent.class, mxDocFactory.createInstance(
1518*cdf0e10cSrcweir                         "com.sun.star.text.Paragraph" ) );
1519*cdf0e10cSrcweir 
1520*cdf0e10cSrcweir                 // Get the XPropertySet interface of the new paragraph and put
1521*cdf0e10cSrcweir                 // it in our array
1522*cdf0e10cSrcweir                 xParas[i] = (XPropertySet) UnoRuntime.queryInterface(
1523*cdf0e10cSrcweir                     XPropertySet.class, xNewPara );
1524*cdf0e10cSrcweir 
1525*cdf0e10cSrcweir                 // Insert the new paragraph into the document after the fish
1526*cdf0e10cSrcweir                 // section. As it is an insert relative to the fish section, the
1527*cdf0e10cSrcweir                 // first paragraph inserted will be below the next two
1528*cdf0e10cSrcweir                 xRelative.insertTextContentAfter ( xNewPara, mxFishSection );
1529*cdf0e10cSrcweir 
1530*cdf0e10cSrcweir                 // Separate from the above, but also needs to be done three times
1531*cdf0e10cSrcweir 
1532*cdf0e10cSrcweir                 // Get the PropertyValue sequence for this numbering level
1533*cdf0e10cSrcweir                 PropertyValue [] aProps = (PropertyValue [] ) xNum.getByIndex ( i );
1534*cdf0e10cSrcweir 
1535*cdf0e10cSrcweir                 // Iterate over the PropertyValue's for this numbering level,
1536*cdf0e10cSrcweir                 // looking for the 'NumberingType' property
1537*cdf0e10cSrcweir                 for ( int j = 0 ; j < aProps.length ; ++j )
1538*cdf0e10cSrcweir                 {
1539*cdf0e10cSrcweir                     if ( aProps[j].Name.equals ( "NumberingType" ) )
1540*cdf0e10cSrcweir                     {
1541*cdf0e10cSrcweir                         // Once we find it, set it's value to a new type,
1542*cdf0e10cSrcweir                         // dependent on which numbering level we're currently on
1543*cdf0e10cSrcweir                         switch ( i )
1544*cdf0e10cSrcweir                         {
1545*cdf0e10cSrcweir                         case 0 : aProps[j].Value =
1546*cdf0e10cSrcweir                                      new Short(NumberingType.ROMAN_UPPER);
1547*cdf0e10cSrcweir                             break;
1548*cdf0e10cSrcweir                         case 1 : aProps[j].Value =
1549*cdf0e10cSrcweir                                      new Short(NumberingType.CHARS_UPPER_LETTER);
1550*cdf0e10cSrcweir                             break;
1551*cdf0e10cSrcweir                         case 2 : aProps[j].Value =
1552*cdf0e10cSrcweir                                      new Short(NumberingType.ARABIC);
1553*cdf0e10cSrcweir                             break;
1554*cdf0e10cSrcweir                         }
1555*cdf0e10cSrcweir                         // Put the updated PropertyValue sequence back into the
1556*cdf0e10cSrcweir                         // NumberingRules service
1557*cdf0e10cSrcweir                         xReplace.replaceByIndex ( i, aProps );
1558*cdf0e10cSrcweir                         break;
1559*cdf0e10cSrcweir                     }
1560*cdf0e10cSrcweir                 }
1561*cdf0e10cSrcweir             }
1562*cdf0e10cSrcweir             // Get the XParagraphCursor interface of our text cursro
1563*cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1564*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1565*cdf0e10cSrcweir             // Go to the end of the document, then select the preceding paragraphs
1566*cdf0e10cSrcweir             mxDocCursor.gotoEnd ( false );
1567*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1568*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( true );
1569*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( true );
1570*cdf0e10cSrcweir 
1571*cdf0e10cSrcweir             // Get the XPropertySet of the cursor's currently selected text
1572*cdf0e10cSrcweir             XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
1573*cdf0e10cSrcweir                 XPropertySet.class, mxDocCursor );
1574*cdf0e10cSrcweir 
1575*cdf0e10cSrcweir             // Set the updated Numbering rules to the cursor's property set
1576*cdf0e10cSrcweir             xCursorProps.setPropertyValue ( "NumberingRules", xNum );
1577*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1578*cdf0e10cSrcweir 
1579*cdf0e10cSrcweir             // Set the first paragraph that was inserted to a numbering level of
1580*cdf0e10cSrcweir             // 2 (thus it will have Arabic style numbering)
1581*cdf0e10cSrcweir             xParas[0].setPropertyValue ( "NumberingLevel", new Short((short) 2));
1582*cdf0e10cSrcweir 
1583*cdf0e10cSrcweir             // Set the second paragraph that was inserted to a numbering level of
1584*cdf0e10cSrcweir             // 1 (thus it will have 'Chars Upper Letter' style numbering)
1585*cdf0e10cSrcweir             xParas[1].setPropertyValue ( "NumberingLevel", new Short((short) 1));
1586*cdf0e10cSrcweir 
1587*cdf0e10cSrcweir             // Set the third paragraph that was inserted to a numbering level of
1588*cdf0e10cSrcweir             // 0 (thus it will have 'Chars Upper Letter' style numbering)
1589*cdf0e10cSrcweir             xParas[2].setPropertyValue ( "NumberingLevel", new Short((short) 0));
1590*cdf0e10cSrcweir             }
1591*cdf0e10cSrcweir         catch (Exception e)
1592*cdf0e10cSrcweir         {
1593*cdf0e10cSrcweir             e.printStackTrace();
1594*cdf0e10cSrcweir         }
1595*cdf0e10cSrcweir     }
1596*cdf0e10cSrcweir 
1597*cdf0e10cSrcweir     /** This method demonstrates how to create linked and unlinked sections
1598*cdf0e10cSrcweir      */
1599*cdf0e10cSrcweir     protected void TextSectionExample ()
1600*cdf0e10cSrcweir     {
1601*cdf0e10cSrcweir         try
1602*cdf0e10cSrcweir         {
1603*cdf0e10cSrcweir             // Go to the end of the document
1604*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1605*cdf0e10cSrcweir             // Insert two paragraph breaks
1606*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1607*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1608*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1609*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, true );
1610*cdf0e10cSrcweir 
1611*cdf0e10cSrcweir             // Create a new TextSection from the document factory and access
1612*cdf0e10cSrcweir             // it's XNamed interface
1613*cdf0e10cSrcweir             XNamed xChildNamed = (XNamed) UnoRuntime.queryInterface(
1614*cdf0e10cSrcweir                 XNamed.class, mxDocFactory.createInstance(
1615*cdf0e10cSrcweir                     "com.sun.star.text.TextSection" ) );
1616*cdf0e10cSrcweir             // Set the new sections name to 'Child_Section'
1617*cdf0e10cSrcweir             xChildNamed.setName ( "Child_Section" );
1618*cdf0e10cSrcweir 
1619*cdf0e10cSrcweir             // Access the Child_Section's XTextContent interface and insert it
1620*cdf0e10cSrcweir             // into the document
1621*cdf0e10cSrcweir             XTextContent xChildSection = (XTextContent) UnoRuntime.queryInterface(
1622*cdf0e10cSrcweir                 XTextContent.class, xChildNamed );
1623*cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xChildSection, false );
1624*cdf0e10cSrcweir 
1625*cdf0e10cSrcweir             // Access the XParagraphCursor interface of our text cursor
1626*cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1627*cdf0e10cSrcweir                 UnoRuntime.queryInterface(XParagraphCursor.class, mxDocCursor);
1628*cdf0e10cSrcweir 
1629*cdf0e10cSrcweir             // Go back one paragraph (into Child_Section)
1630*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1631*cdf0e10cSrcweir 
1632*cdf0e10cSrcweir             // Insert a string into the Child_Section
1633*cdf0e10cSrcweir             mxDocText.insertString ( mxDocCursor, "This is a test", false );
1634*cdf0e10cSrcweir 
1635*cdf0e10cSrcweir             // Go to the end of the document
1636*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1637*cdf0e10cSrcweir 
1638*cdf0e10cSrcweir             // Go back two paragraphs
1639*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1640*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1641*cdf0e10cSrcweir             // Go to the end of the document, selecting the two paragraphs
1642*cdf0e10cSrcweir             mxDocCursor.gotoEnd ( true );
1643*cdf0e10cSrcweir 
1644*cdf0e10cSrcweir             // Create another text section and access it's XNamed interface
1645*cdf0e10cSrcweir             XNamed xParentNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class,
1646*cdf0e10cSrcweir                       mxDocFactory.createInstance("com.sun.star.text.TextSection"));
1647*cdf0e10cSrcweir 
1648*cdf0e10cSrcweir             // Set this text section's name to Parent_Section
1649*cdf0e10cSrcweir             xParentNamed.setName ( "Parent_Section" );
1650*cdf0e10cSrcweir 
1651*cdf0e10cSrcweir             // Access the Parent_Section's XTextContent interface ...
1652*cdf0e10cSrcweir             XTextContent xParentSection = (XTextContent) UnoRuntime.queryInterface(
1653*cdf0e10cSrcweir                 XTextContent.class, xParentNamed );
1654*cdf0e10cSrcweir             // ...and insert it into the document
1655*cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xParentSection, false );
1656*cdf0e10cSrcweir 
1657*cdf0e10cSrcweir             // Go to the end of the document
1658*cdf0e10cSrcweir             mxDocCursor.gotoEnd ( false );
1659*cdf0e10cSrcweir             // Insert a new paragraph
1660*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1661*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1662*cdf0e10cSrcweir             // And select the new pargraph
1663*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( true );
1664*cdf0e10cSrcweir 
1665*cdf0e10cSrcweir             // Create a new Text Section and access it's XNamed interface
1666*cdf0e10cSrcweir             XNamed xLinkNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class,
1667*cdf0e10cSrcweir                       mxDocFactory.createInstance("com.sun.star.text.TextSection"));
1668*cdf0e10cSrcweir             // Set the new text section's name to Linked_Section
1669*cdf0e10cSrcweir             xLinkNamed.setName ( "Linked_Section" );
1670*cdf0e10cSrcweir 
1671*cdf0e10cSrcweir             // Access the Linked_Section's XTextContent interface
1672*cdf0e10cSrcweir             XTextContent xLinkedSection = (XTextContent) UnoRuntime.queryInterface(
1673*cdf0e10cSrcweir                 XTextContent.class, xLinkNamed );
1674*cdf0e10cSrcweir             // And insert the Linked_Section into the document
1675*cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, xLinkedSection, false );
1676*cdf0e10cSrcweir 
1677*cdf0e10cSrcweir             // Access the Linked_Section's XPropertySet interface
1678*cdf0e10cSrcweir             XPropertySet xLinkProps = (XPropertySet)UnoRuntime.queryInterface(
1679*cdf0e10cSrcweir                 XPropertySet.class, xLinkNamed );
1680*cdf0e10cSrcweir             // Set the linked section to be linked to the Child_Section
1681*cdf0e10cSrcweir             xLinkProps.setPropertyValue ( "LinkRegion", "Child_Section" );
1682*cdf0e10cSrcweir 
1683*cdf0e10cSrcweir             // Access the XPropertySet interface of the Child_Section
1684*cdf0e10cSrcweir             XPropertySet xChildProps = (XPropertySet) UnoRuntime.queryInterface(
1685*cdf0e10cSrcweir                 XPropertySet.class, xChildNamed );
1686*cdf0e10cSrcweir             // Set the Child_Section's background colour to blue
1687*cdf0e10cSrcweir             xChildProps.setPropertyValue( "BackColor", new Integer(13421823));
1688*cdf0e10cSrcweir 
1689*cdf0e10cSrcweir             // Refresh the document, so the linked section matches the Child_Section
1690*cdf0e10cSrcweir             XRefreshable xRefresh = (XRefreshable) UnoRuntime.queryInterface(
1691*cdf0e10cSrcweir                 XRefreshable.class, mxDoc );
1692*cdf0e10cSrcweir             xRefresh.refresh();
1693*cdf0e10cSrcweir         }
1694*cdf0e10cSrcweir         catch (Exception e)
1695*cdf0e10cSrcweir         {
1696*cdf0e10cSrcweir             e.printStackTrace();
1697*cdf0e10cSrcweir         }
1698*cdf0e10cSrcweir     }
1699*cdf0e10cSrcweir 
1700*cdf0e10cSrcweir     /** This method demonstrates the XTextColumns interface and how to insert a
1701*cdf0e10cSrcweir         blank paragraph using the XRelativeTextContentInsert interface
1702*cdf0e10cSrcweir      */
1703*cdf0e10cSrcweir     protected void TextColumnsExample ()
1704*cdf0e10cSrcweir     {
1705*cdf0e10cSrcweir         try
1706*cdf0e10cSrcweir         {
1707*cdf0e10cSrcweir             // Go to the end of the doucment
1708*cdf0e10cSrcweir             mxDocCursor.gotoEnd( false );
1709*cdf0e10cSrcweir             // insert a new paragraph
1710*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1711*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1712*cdf0e10cSrcweir 
1713*cdf0e10cSrcweir             // insert the string 'I am a fish.' 100 times
1714*cdf0e10cSrcweir             for ( int i = 0 ; i < 100 ; ++i )
1715*cdf0e10cSrcweir             {
1716*cdf0e10cSrcweir                 mxDocText.insertString ( mxDocCursor, "I am a fish.", false );
1717*cdf0e10cSrcweir             }
1718*cdf0e10cSrcweir             // insert a paragraph break after the text
1719*cdf0e10cSrcweir             mxDocText.insertControlCharacter (
1720*cdf0e10cSrcweir                 mxDocCursor, ControlCharacter.PARAGRAPH_BREAK, false );
1721*cdf0e10cSrcweir 
1722*cdf0e10cSrcweir             // Get the XParagraphCursor interface of our text cursor
1723*cdf0e10cSrcweir             XParagraphCursor xParaCursor = (XParagraphCursor)
1724*cdf0e10cSrcweir                 UnoRuntime.queryInterface( XParagraphCursor.class, mxDocCursor );
1725*cdf0e10cSrcweir             // Jump back before all the text we just inserted
1726*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1727*cdf0e10cSrcweir             xParaCursor.gotoPreviousParagraph ( false );
1728*cdf0e10cSrcweir 
1729*cdf0e10cSrcweir             // Insert a string at the beginning of the block of text
1730*cdf0e10cSrcweir             mxDocText.insertString ( mxDocCursor, "Fish section begins:", false );
1731*cdf0e10cSrcweir 
1732*cdf0e10cSrcweir             // Then select all of the text
1733*cdf0e10cSrcweir             xParaCursor.gotoNextParagraph ( true );
1734*cdf0e10cSrcweir             xParaCursor.gotoNextParagraph ( true );
1735*cdf0e10cSrcweir 
1736*cdf0e10cSrcweir             // Create a new text section and get it's XNamed interface
1737*cdf0e10cSrcweir             XNamed xSectionNamed = (XNamed) UnoRuntime.queryInterface(XNamed.class,
1738*cdf0e10cSrcweir                       mxDocFactory.createInstance("com.sun.star.text.TextSection"));
1739*cdf0e10cSrcweir 
1740*cdf0e10cSrcweir             // Set the name of our new section (appropiately) to 'Fish'
1741*cdf0e10cSrcweir             xSectionNamed.setName ( "Fish" );
1742*cdf0e10cSrcweir 
1743*cdf0e10cSrcweir             // Create the TextColumns service and get it's XTextColumns interface
1744*cdf0e10cSrcweir             XTextColumns xColumns = (XTextColumns) UnoRuntime.queryInterface(
1745*cdf0e10cSrcweir                 XTextColumns.class,
1746*cdf0e10cSrcweir                 mxDocFactory.createInstance ( "com.sun.star.text.TextColumns" ) );
1747*cdf0e10cSrcweir 
1748*cdf0e10cSrcweir             // We want three columns
1749*cdf0e10cSrcweir             xColumns.setColumnCount ( (short) 3 );
1750*cdf0e10cSrcweir 
1751*cdf0e10cSrcweir             // Get the TextColumns, and make the middle one narrow with a larger
1752*cdf0e10cSrcweir             // margin on the left than the right
1753*cdf0e10cSrcweir             TextColumn[]  aSequence = xColumns.getColumns ();
1754*cdf0e10cSrcweir             aSequence[1].Width /= 2;
1755*cdf0e10cSrcweir             aSequence[1].LeftMargin = 350;
1756*cdf0e10cSrcweir             aSequence[1].RightMargin = 200;
1757*cdf0e10cSrcweir             // Set the updated TextColumns back to the XTextColumns
1758*cdf0e10cSrcweir             xColumns.setColumns ( aSequence );
1759*cdf0e10cSrcweir 
1760*cdf0e10cSrcweir             // Get the property set interface of our 'Fish' section
1761*cdf0e10cSrcweir             XPropertySet xSectionProps = (XPropertySet) UnoRuntime.queryInterface(
1762*cdf0e10cSrcweir                 XPropertySet.class, xSectionNamed );
1763*cdf0e10cSrcweir 
1764*cdf0e10cSrcweir             // Set the columns to the Text Section
1765*cdf0e10cSrcweir             xSectionProps.setPropertyValue ( "TextColumns", xColumns );
1766*cdf0e10cSrcweir 
1767*cdf0e10cSrcweir             // Get the XTextContent interface of our 'Fish' section
1768*cdf0e10cSrcweir             mxFishSection = (XTextContent) UnoRuntime.queryInterface(
1769*cdf0e10cSrcweir                 XTextContent.class, xSectionNamed );
1770*cdf0e10cSrcweir 
1771*cdf0e10cSrcweir             // Insert the 'Fish' section over the currently selected text
1772*cdf0e10cSrcweir             mxDocText.insertTextContent ( mxDocCursor, mxFishSection, true );
1773*cdf0e10cSrcweir 
1774*cdf0e10cSrcweir             // Get the wonderful XRelativeTextContentInsert interface
1775*cdf0e10cSrcweir             XRelativeTextContentInsert xRelative = (XRelativeTextContentInsert )
1776*cdf0e10cSrcweir                 UnoRuntime.queryInterface (
1777*cdf0e10cSrcweir                     XRelativeTextContentInsert.class, mxDocText );
1778*cdf0e10cSrcweir 
1779*cdf0e10cSrcweir             // Create a new empty paragraph and get it's XTextContent interface
1780*cdf0e10cSrcweir             XTextContent xNewPara = (XTextContent) UnoRuntime.queryInterface(
1781*cdf0e10cSrcweir                 XTextContent.class,
1782*cdf0e10cSrcweir                 mxDocFactory.createInstance("com.sun.star.text.Paragraph"));
1783*cdf0e10cSrcweir 
1784*cdf0e10cSrcweir             // Insert the empty paragraph after the fish Text Section
1785*cdf0e10cSrcweir             xRelative.insertTextContentAfter ( xNewPara, mxFishSection );
1786*cdf0e10cSrcweir         }
1787*cdf0e10cSrcweir         catch (Exception e)
1788*cdf0e10cSrcweir         {
1789*cdf0e10cSrcweir             e.printStackTrace();
1790*cdf0e10cSrcweir         }
1791*cdf0e10cSrcweir     }
1792*cdf0e10cSrcweir }
1793