xref: /aoo4110/main/odk/examples/java/Text/SWriter.java (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 //***************************************************************************
25 // comment: Step 1: bootstrap UNO and get the remote component context
26 //          Step 2: open an empty text document
27 //          Step 3: enter some text
28 //          Step 4: insert a text table
29 //          Step 5: insert colored text
30 //          Step 6: insert a text frame
31 //***************************************************************************
32 
33 import com.sun.star.uno.UnoRuntime;
34 
35 public class SWriter  {
36 
main(String args[])37     public static void main(String args[]) {
38 
39 
40         //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo
41         // bootstrap UNO and get the remote component context. The context can
42         // be used to get the service manager
43         //*************************************************************************
44         com.sun.star.uno.XComponentContext xContext = null;
45 
46         try {
47             // get the remote office component context
48             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
49             if( xContext != null )
50                 System.out.println("Connected to a running office ...");
51         }
52         catch( Exception e) {
53             e.printStackTrace(System.err);
54             System.exit(1);
55         }
56 
57         //oooooooooooooooooooooooooooStep 2oooooooooooooooooooooooooooooooooooooooo
58         // open an empty document. In this case it's a writer document.
59         // For this purpose an instance of com.sun.star.frame.Desktop
60         // is created. It's interface XDesktop provides the XComponentLoader,
61         // which is used to open the document via loadComponentFromURL
62         //*************************************************************************
63 
64         //Open Writer document
65 
66         System.out.println("Opening an empty Writer document");
67         com.sun.star.text.XTextDocument myDoc = openWriter(xContext);
68 
69         //*************************************************************************
70 
71 
72         //oooooooooooooooooooooooooooStep 3oooooooooooooooooooooooooooooooooooooooo
73         // insert some text.
74         // For this purpose get the Text-Object of the document an create the
75         // cursor. Now it is possible to insert a text at the cursor-position
76         // via insertString
77         //*************************************************************************
78 
79 
80         //getting the text object
81         com.sun.star.text.XText xText = myDoc.getText();
82 
83         //create a cursor object
84         com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor();
85 
86         //inserting some Text
87         xText.insertString( xTCursor, "The first line in the newly created text document.\n", false );
88 
89         //inserting a second line
90         xText.insertString( xTCursor, "Now we're in the second line\n", false );
91 
92         //*************************************************************************
93 
94 
95         //oooooooooooooooooooooooooooStep 4oooooooooooooooooooooooooooooooooooooooo
96         // insert a text table.
97         // For this purpose get MultiServiceFactory of the document, create an
98         // instance of com.sun.star.text.TextTable and initialize it. Now it can
99         // be inserted at the cursor position via insertTextContent.
100         // After that some properties are changed and some data is inserted.
101         //*************************************************************************
102 
103         //inserting a text table
104         System.out.println("Inserting a text table");
105 
106         //getting MSF of the document
107         com.sun.star.lang.XMultiServiceFactory xDocMSF =
108             (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface(
109                 com.sun.star.lang.XMultiServiceFactory.class, myDoc);
110 
111         //create instance of a text table
112         com.sun.star.text.XTextTable xTT = null;
113 
114         try {
115             Object oInt = xDocMSF.createInstance("com.sun.star.text.TextTable");
116             xTT = (com.sun.star.text.XTextTable)
117                 UnoRuntime.queryInterface(com.sun.star.text.XTextTable.class,oInt);
118         } catch (Exception e) {
119             System.err.println("Couldn't create instance "+ e);
120             e.printStackTrace(System.err);
121         }
122 
123         //initialize the text table with 4 columns an 4 rows
124         xTT.initialize(4,4);
125 
126         com.sun.star.beans.XPropertySet xTTRowPS = null;
127 
128         //insert the table
129         try {
130             xText.insertTextContent(xTCursor, xTT, false);
131             // get first Row
132             com.sun.star.container.XIndexAccess xTTRows = xTT.getRows();
133             xTTRowPS = (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
134                 com.sun.star.beans.XPropertySet.class, xTTRows.getByIndex(0));
135 
136         } catch (Exception e) {
137             System.err.println("Couldn't insert the table " + e);
138             e.printStackTrace(System.err);
139         }
140 
141 
142         // get the property set of the text table
143 
144         com.sun.star.beans.XPropertySet xTTPS = (com.sun.star.beans.XPropertySet)
145             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTT);
146 
147         // Change the BackColor
148         try {
149             xTTPS.setPropertyValue("BackTransparent", new Boolean(false));
150             xTTPS.setPropertyValue("BackColor",new Integer(13421823));
151             xTTRowPS.setPropertyValue("BackTransparent", new Boolean(false));
152             xTTRowPS.setPropertyValue("BackColor",new Integer(6710932));
153 
154         } catch (Exception e) {
155             System.err.println("Couldn't change the color " + e);
156             e.printStackTrace(System.err);
157         }
158 
159         // write Text in the Table headers
160         System.out.println("Write text in the table headers");
161 
162         insertIntoCell("A1","FirstColumn", xTT);
163         insertIntoCell("B1","SecondColumn", xTT) ;
164         insertIntoCell("C1","ThirdColumn", xTT) ;
165         insertIntoCell("D1","SUM", xTT) ;
166 
167 
168         //Insert Something in the text table
169         System.out.println("Insert something in the text table");
170 
171         (xTT.getCellByName("A2")).setValue(22.5);
172         (xTT.getCellByName("B2")).setValue(5615.3);
173         (xTT.getCellByName("C2")).setValue(-2315.7);
174         (xTT.getCellByName("D2")).setFormula("sum <A2:C2>");
175 
176         (xTT.getCellByName("A3")).setValue(21.5);
177         (xTT.getCellByName("B3")).setValue(615.3);
178         (xTT.getCellByName("C3")).setValue(-315.7);
179         (xTT.getCellByName("D3")).setFormula("sum <A3:C3>");
180 
181         (xTT.getCellByName("A4")).setValue(121.5);
182         (xTT.getCellByName("B4")).setValue(-615.3);
183         (xTT.getCellByName("C4")).setValue(415.7);
184         (xTT.getCellByName("D4")).setFormula("sum <A4:C4>");
185 
186 
187         //oooooooooooooooooooooooooooStep 5oooooooooooooooooooooooooooooooooooooooo
188         // insert a colored text.
189         // Get the propertySet of the cursor, change the CharColor and add a
190         // shadow. Then insert the Text via InsertString at the cursor position.
191         //*************************************************************************
192 
193         // get the property set of the cursor
194         com.sun.star.beans.XPropertySet xTCPS = (com.sun.star.beans.XPropertySet)
195             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class,
196                                       xTCursor);
197 
198         Object oldValue = null;
199 
200         // Change the CharColor and add a Shadow
201         try {
202             xTCPS.setPropertyValue("CharColor",new Integer(255));
203             xTCPS.setPropertyValue("CharShadowed", new Boolean(true));
204         } catch (Exception e) {
205             System.err.println("Couldn't change the color " + e);
206             e.printStackTrace(System.err);
207         }
208 
209         //create a paragraph break
210         try {
211             xText.insertControlCharacter(xTCursor,
212                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
213 
214         } catch (Exception e) {
215             System.err.println("Couldn't insert break "+ e);
216             e.printStackTrace(System.err);
217         }
218 
219         //inserting colored Text
220         System.out.println("Inserting colored Text");
221 
222         xText.insertString(xTCursor, " This is a colored Text - blue with shadow\n",
223                            false );
224 
225         //create a paragraph break
226         try {
227             xText.insertControlCharacter(xTCursor,
228                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
229 
230         } catch (Exception e) {
231             System.err.println("Couldn't insert break "+ e);
232             e.printStackTrace(System.err);
233         }
234 
235         //oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooo
236         // insert a text frame.
237         // create an instance of com.sun.star.text.TextFrame using the MSF of the
238         // document. Change some properties an insert it.
239         // Now get the text-Object of the frame an the corresponding cursor.
240         // Insert some text via insertString.
241         //*************************************************************************
242 
243         // Create a TextFrame
244         com.sun.star.text.XTextFrame xTF = null;
245         com.sun.star.drawing.XShape xTFS = null;
246 
247         try {
248             Object oInt = xDocMSF.createInstance("com.sun.star.text.TextFrame");
249             xTF = (com.sun.star.text.XTextFrame) UnoRuntime.queryInterface(
250                 com.sun.star.text.XTextFrame.class,oInt);
251             xTFS = (com.sun.star.drawing.XShape) UnoRuntime.queryInterface(
252                 com.sun.star.drawing.XShape.class,oInt);
253 
254             com.sun.star.awt.Size aSize = new com.sun.star.awt.Size();
255             aSize.Height = 400;
256             aSize.Width = 15000;
257 
258             xTFS.setSize(aSize);
259         } catch (Exception e) {
260             System.err.println("Couldn't create instance "+ e);
261             e.printStackTrace(System.err);
262         }
263 
264         // get the property set of the text frame
265         com.sun.star.beans.XPropertySet xTFPS = (com.sun.star.beans.XPropertySet)
266             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTF);
267 
268         // Change the AnchorType
269         try {
270             xTFPS.setPropertyValue("AnchorType",
271                       com.sun.star.text.TextContentAnchorType.AS_CHARACTER);
272         } catch (Exception e) {
273             System.err.println("Couldn't change the color " + e);
274             e.printStackTrace(System.err);
275         }
276 
277         //insert the frame
278         System.out.println("Insert the text frame");
279 
280         try {
281             xText.insertTextContent(xTCursor, xTF, false);
282         } catch (Exception e) {
283             System.err.println("Couldn't insert the frame " + e);
284             e.printStackTrace(System.err);
285         }
286 
287         //getting the text object of Frame
288         com.sun.star.text.XText xTextF = xTF.getText();
289 
290         //create a cursor object
291         com.sun.star.text.XTextCursor xTCF = xTextF.createTextCursor();
292 
293         //inserting some Text
294         xTextF.insertString(xTCF,
295                  "The first line in the newly created text frame.", false);
296 
297 
298         xTextF.insertString(xTCF,
299                  "\nWith this second line the height of the frame raises.", false);
300 
301         //insert a paragraph break
302         try {
303             xText.insertControlCharacter(xTCursor,
304                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false );
305 
306         } catch (Exception e) {
307             System.err.println("Couldn't insert break "+ e);
308             e.printStackTrace(System.err);
309         }
310 
311         // Change the CharColor and add a Shadow
312         try {
313             xTCPS.setPropertyValue("CharColor",new Integer(65536));
314             xTCPS.setPropertyValue("CharShadowed", new Boolean(false));
315         } catch (Exception e) {
316             System.err.println("Couldn't change the color " + e);
317             e.printStackTrace(System.err);
318         }
319 
320         xText.insertString(xTCursor, " That's all for now !!", false );
321 
322         System.out.println("done");
323 
324         System.exit(0);
325     }
326 
327 
openWriter( com.sun.star.uno.XComponentContext xContext)328     public static com.sun.star.text.XTextDocument openWriter(
329         com.sun.star.uno.XComponentContext xContext)
330     {
331         //define variables
332         com.sun.star.frame.XComponentLoader xCLoader;
333         com.sun.star.text.XTextDocument xDoc = null;
334         com.sun.star.lang.XComponent xComp = null;
335 
336         try {
337             // get the remote office service manager
338             com.sun.star.lang.XMultiComponentFactory xMCF =
339                 xContext.getServiceManager();
340 
341             Object oDesktop = xMCF.createInstanceWithContext(
342                                         "com.sun.star.frame.Desktop", xContext);
343 
344             xCLoader = (com.sun.star.frame.XComponentLoader)
345                 UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
346                                           oDesktop);
347             com.sun.star.beans.PropertyValue [] szEmptyArgs =
348                 new com.sun.star.beans.PropertyValue [0];
349             String strDoc = "private:factory/swriter";
350             xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
351             xDoc = (com.sun.star.text.XTextDocument)
352                 UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class,
353                                           xComp);
354 
355         } catch(Exception e){
356             System.err.println(" Exception " + e);
357             e.printStackTrace(System.err);
358         }
359         return xDoc;
360     }
361 
insertIntoCell(String CellName, String theText, com.sun.star.text.XTextTable xTTbl)362     public static void insertIntoCell(String CellName, String theText,
363                                       com.sun.star.text.XTextTable xTTbl) {
364 
365         com.sun.star.text.XText xTableText = (com.sun.star.text.XText)
366             UnoRuntime.queryInterface(com.sun.star.text.XText.class,
367                                       xTTbl.getCellByName(CellName));
368 
369         //create a cursor object
370         com.sun.star.text.XTextCursor xTC = xTableText.createTextCursor();
371 
372         com.sun.star.beans.XPropertySet xTPS = (com.sun.star.beans.XPropertySet)
373             UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTC);
374 
375         try {
376             xTPS.setPropertyValue("CharColor",new Integer(16777215));
377         } catch (Exception e) {
378             System.err.println(" Exception " + e);
379             e.printStackTrace(System.err);
380         }
381 
382         //inserting some Text
383         xTableText.setString( theText );
384 
385     }
386 }
387