xref: /aoo42x/main/odk/examples/java/Text/SWriter.java (revision cdf0e10c)
1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 //***************************************************************************
36 // comment: Step 1: bootstrap UNO and get the remote component context
37 //          Step 2: open an empty text document
38 //          Step 3: enter some text
39 //          Step 4: insert a text table
40 //          Step 5: insert colored text
41 //          Step 6: insert a text frame
42 //***************************************************************************
43 
44 import com.sun.star.uno.UnoRuntime;
45 
46 public class SWriter  {
47 
48     public static void main(String args[]) {
49 
50 
51         //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo
52         // bootstrap UNO and get the remote component context. The context can
53         // be used to get the service manager
54         //*************************************************************************
55         com.sun.star.uno.XComponentContext xContext = null;
56 
57         try {
58             // get the remote office component context
59             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
60             if( xContext != null )
61                 System.out.println("Connected to a running office ...");
62         }
63         catch( Exception e) {
64             e.printStackTrace(System.err);
65             System.exit(1);
66         }
67 
68         //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo
69         // open an empty document. In this case it's a writer document.
70         // For this purpose an instance of com.sun.star.frame.Desktop
71         // is created. It's interface XDesktop provides the XComponentLoader,
72         // which is used to open the document via loadComponentFromURL
73         //*************************************************************************
74 
75         //Open Writer document
76 
77         System.out.println("Opening an empty Writer document");
78         com.sun.star.text.XTextDocument myDoc = openWriter(xContext);
79 
80         //*************************************************************************
81 
82 
83         //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo
84         // insert some text.
85         // For this purpose get the Text-Object of the document an create the
86         // cursor. Now it is possible to insert a text at the cursor-position
87         // via insertString
88         //*************************************************************************
89 
90 
91         //getting the text object
92         com.sun.star.text.XText xText = myDoc.getText();
93 
94         //create a cursor object
95         com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor();
96 
97         //inserting some Text
98         xText.insertString( xTCursor, "The first line in the newly created text document.\n", false );
99 
100         //inserting a second line
101         xText.insertString( xTCursor, "Now we're in the second line\n", false );
102 
103         //*************************************************************************
104 
105 
106         //oooooooooooooooooooooooooooStep 4oooooooooooooooooooooooooooooooooooooooo
107         // insert a text table.
108         // For this purpose get MultiServiceFactory of the document, create an
109         // instance of com.sun.star.text.TextTable and initialize it. Now it can
110         // be inserted at the cursor position via insertTextContent.
111         // After that some properties are changed and some data is inserted.
112         //*************************************************************************
113 
114         //inserting a text table
115         System.out.println("Inserting a text table");
116 
117         //getting MSF of the document
118         com.sun.star.lang.XMultiServiceFactory xDocMSF =
119             (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface(
120                 com.sun.star.lang.XMultiServiceFactory.class, myDoc);
121 
122         //create instance of a text table
123         com.sun.star.text.XTextTable xTT = null;
124 
125         try {
126             Object oInt = xDocMSF.createInstance("com.sun.star.text.TextTable");
127             xTT = (com.sun.star.text.XTextTable)
128                 UnoRuntime.queryInterface(com.sun.star.text.XTextTable.class,oInt);
129         } catch (Exception e) {
130             System.err.println("Couldn't create instance "+ e);
131             e.printStackTrace(System.err);
132         }
133 
134         //initialize the text table with 4 columns an 4 rows
135         xTT.initialize(4,4);
136 
137         com.sun.star.beans.XPropertySet xTTRowPS = null;
138 
139         //insert the table
140         try {
141             xText.insertTextContent(xTCursor, xTT, false);
142             // get first Row
143             com.sun.star.container.XIndexAccess xTTRows = xTT.getRows();
144             xTTRowPS = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
145                 com.sun.star.beans.XPropertySet.class, xTTRows.getByIndex(0));
146 
147         } catch (Exception e) {
148             System.err.println("Couldn't insert the table " + e);
149             e.printStackTrace(System.err);
150         }
151 
152 
153         // get the property set of the text table
154 
155         com.sun.star.beans.XPropertySet xTTPS = (com.sun.star.beans.XPropertySet)
156             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTT);
157 
158         // Change the BackColor
159         try {
160             xTTPS.setPropertyValue("BackTransparent", new Boolean(false));
161             xTTPS.setPropertyValue("BackColor",new Integer(13421823));
162             xTTRowPS.setPropertyValue("BackTransparent", new Boolean(false));
163             xTTRowPS.setPropertyValue("BackColor",new Integer(6710932));
164 
165         } catch (Exception e) {
166             System.err.println("Couldn't change the color " + e);
167             e.printStackTrace(System.err);
168         }
169 
170         // write Text in the Table headers
171         System.out.println("Write text in the table headers");
172 
173         insertIntoCell("A1","FirstColumn", xTT);
174         insertIntoCell("B1","SecondColumn", xTT) ;
175         insertIntoCell("C1","ThirdColumn", xTT) ;
176         insertIntoCell("D1","SUM", xTT) ;
177 
178 
179         //Insert Something in the text table
180         System.out.println("Insert something in the text table");
181 
182         (xTT.getCellByName("A2")).setValue(22.5);
183         (xTT.getCellByName("B2")).setValue(5615.3);
184         (xTT.getCellByName("C2")).setValue(-2315.7);
185         (xTT.getCellByName("D2")).setFormula("sum <A2:C2>");
186 
187         (xTT.getCellByName("A3")).setValue(21.5);
188         (xTT.getCellByName("B3")).setValue(615.3);
189         (xTT.getCellByName("C3")).setValue(-315.7);
190         (xTT.getCellByName("D3")).setFormula("sum <A3:C3>");
191 
192         (xTT.getCellByName("A4")).setValue(121.5);
193         (xTT.getCellByName("B4")).setValue(-615.3);
194         (xTT.getCellByName("C4")).setValue(415.7);
195         (xTT.getCellByName("D4")).setFormula("sum <A4:C4>");
196 
197 
198         //oooooooooooooooooooooooooooStep 5oooooooooooooooooooooooooooooooooooooooo
199         // insert a colored text.
200         // Get the propertySet of the cursor, change the CharColor and add a
201         // shadow. Then insert the Text via InsertString at the cursor position.
202         //*************************************************************************
203 
204         // get the property set of the cursor
205         com.sun.star.beans.XPropertySet xTCPS = (com.sun.star.beans.XPropertySet)
206             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class,
207                                       xTCursor);
208 
209         Object oldValue = null;
210 
211         // Change the CharColor and add a Shadow
212         try {
213             xTCPS.setPropertyValue("CharColor",new Integer(255));
214             xTCPS.setPropertyValue("CharShadowed", new Boolean(true));
215         } catch (Exception e) {
216             System.err.println("Couldn't change the color " + e);
217             e.printStackTrace(System.err);
218         }
219 
220         //create a paragraph break
221         try {
222             xText.insertControlCharacter(xTCursor,
223                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
224 
225         } catch (Exception e) {
226             System.err.println("Couldn't insert break "+ e);
227             e.printStackTrace(System.err);
228         }
229 
230         //inserting colored Text
231         System.out.println("Inserting colored Text");
232 
233         xText.insertString(xTCursor, " This is a colored Text - blue with shadow\n",
234                            false );
235 
236         //create a paragraph break
237         try {
238             xText.insertControlCharacter(xTCursor,
239                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
240 
241         } catch (Exception e) {
242             System.err.println("Couldn't insert break "+ e);
243             e.printStackTrace(System.err);
244         }
245 
246         //oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooo
247         // insert a text frame.
248         // create an instance of com.sun.star.text.TextFrame using the MSF of the
249         // document. Change some properties an insert it.
250         // Now get the text-Object of the frame an the corresponding cursor.
251         // Insert some text via insertString.
252         //*************************************************************************
253 
254         // Create a TextFrame
255         com.sun.star.text.XTextFrame xTF = null;
256         com.sun.star.drawing.XShape xTFS = null;
257 
258         try {
259             Object oInt = xDocMSF.createInstance("com.sun.star.text.TextFrame");
260             xTF = (com.sun.star.text.XTextFrame) UnoRuntime.queryInterface(
261                 com.sun.star.text.XTextFrame.class,oInt);
262             xTFS = (com.sun.star.drawing.XShape) UnoRuntime.queryInterface(
263                 com.sun.star.drawing.XShape.class,oInt);
264 
265             com.sun.star.awt.Size aSize = new com.sun.star.awt.Size();
266             aSize.Height = 400;
267             aSize.Width = 15000;
268 
269             xTFS.setSize(aSize);
270         } catch (Exception e) {
271             System.err.println("Couldn't create instance "+ e);
272             e.printStackTrace(System.err);
273         }
274 
275         // get the property set of the text frame
276         com.sun.star.beans.XPropertySet xTFPS = (com.sun.star.beans.XPropertySet)
277             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTF);
278 
279         // Change the AnchorType
280         try {
281             xTFPS.setPropertyValue("AnchorType",
282                       com.sun.star.text.TextContentAnchorType.AS_CHARACTER);
283         } catch (Exception e) {
284             System.err.println("Couldn't change the color " + e);
285             e.printStackTrace(System.err);
286         }
287 
288         //insert the frame
289         System.out.println("Insert the text frame");
290 
291         try {
292             xText.insertTextContent(xTCursor, xTF, false);
293         } catch (Exception e) {
294             System.err.println("Couldn't insert the frame " + e);
295             e.printStackTrace(System.err);
296         }
297 
298         //getting the text object of Frame
299         com.sun.star.text.XText xTextF = xTF.getText();
300 
301         //create a cursor object
302         com.sun.star.text.XTextCursor xTCF = xTextF.createTextCursor();
303 
304         //inserting some Text
305         xTextF.insertString(xTCF,
306                  "The first line in the newly created text frame.", false);
307 
308 
309         xTextF.insertString(xTCF,
310                  "\nWith this second line the height of the frame raises.", false);
311 
312         //insert a paragraph break
313         try {
314             xText.insertControlCharacter(xTCursor,
315                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false );
316 
317         } catch (Exception e) {
318             System.err.println("Couldn't insert break "+ e);
319             e.printStackTrace(System.err);
320         }
321 
322         // Change the CharColor and add a Shadow
323         try {
324             xTCPS.setPropertyValue("CharColor",new Integer(65536));
325             xTCPS.setPropertyValue("CharShadowed", new Boolean(false));
326         } catch (Exception e) {
327             System.err.println("Couldn't change the color " + e);
328             e.printStackTrace(System.err);
329         }
330 
331         xText.insertString(xTCursor, " That's all for now !!", false );
332 
333         System.out.println("done");
334 
335         System.exit(0);
336     }
337 
338 
339     public static com.sun.star.text.XTextDocument openWriter(
340         com.sun.star.uno.XComponentContext xContext)
341     {
342         //define variables
343         com.sun.star.frame.XComponentLoader xCLoader;
344         com.sun.star.text.XTextDocument xDoc = null;
345         com.sun.star.lang.XComponent xComp = null;
346 
347         try {
348             // get the remote office service manager
349             com.sun.star.lang.XMultiComponentFactory xMCF =
350                 xContext.getServiceManager();
351 
352             Object oDesktop = xMCF.createInstanceWithContext(
353                                         "com.sun.star.frame.Desktop", xContext);
354 
355             xCLoader = (com.sun.star.frame.XComponentLoader)
356                 UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
357                                           oDesktop);
358             com.sun.star.beans.PropertyValue [] szEmptyArgs =
359                 new com.sun.star.beans.PropertyValue [0];
360             String strDoc = "private:factory/swriter";
361             xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
362             xDoc = (com.sun.star.text.XTextDocument)
363                 UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class,
364                                           xComp);
365 
366         } catch(Exception e){
367             System.err.println(" Exception " + e);
368             e.printStackTrace(System.err);
369         }
370         return xDoc;
371     }
372 
373     public static void insertIntoCell(String CellName, String theText,
374                                       com.sun.star.text.XTextTable xTTbl) {
375 
376         com.sun.star.text.XText xTableText = (com.sun.star.text.XText)
377             UnoRuntime.queryInterface(com.sun.star.text.XText.class,
378                                       xTTbl.getCellByName(CellName));
379 
380         //create a cursor object
381         com.sun.star.text.XTextCursor xTC = xTableText.createTextCursor();
382 
383         com.sun.star.beans.XPropertySet xTPS = (com.sun.star.beans.XPropertySet)
384             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTC);
385 
386         try {
387             xTPS.setPropertyValue("CharColor",new Integer(16777215));
388         } catch (Exception e) {
389             System.err.println(" Exception " + e);
390             e.printStackTrace(System.err);
391         }
392 
393         //inserting some Text
394         xTableText.setString( theText );
395 
396     }
397 }
398