Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Text / SWriter.java
blob4e65ae288fa813373f6824aeb25a432f662006d8
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
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
44 import com.sun.star.uno.UnoRuntime;
46 public class SWriter {
48 public static void main(String args[]) {
51 //oooooooooooooooooooooooooooStep 1oooooooooooooooooooooooooooooooooooooooo
52 // bootstrap UNO and get the remote component context. The context can
53 // be used to get the service manager
55 com.sun.star.uno.XComponentContext xContext = null;
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 ...");
63 catch( Exception e) {
64 e.printStackTrace(System.err);
65 System.exit(1);
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
75 //Open Writer document
77 System.out.println("Opening an empty Writer document");
78 com.sun.star.text.XTextDocument myDoc = openWriter(xContext);
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
91 //getting the text object
92 com.sun.star.text.XText xText = myDoc.getText();
94 //create a cursor object
95 com.sun.star.text.XTextCursor xTCursor = xText.createTextCursor();
97 //inserting some Text
98 xText.insertString( xTCursor, "The first line in the newly created text document.\n", false );
100 //inserting a second line
101 xText.insertString( xTCursor, "Now we're in the second line\n", false );
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.
114 //inserting a text table
115 System.out.println("Inserting a text table");
117 //getting MSF of the document
118 com.sun.star.lang.XMultiServiceFactory xDocMSF =
119 UnoRuntime.queryInterface(
120 com.sun.star.lang.XMultiServiceFactory.class, myDoc);
122 //create instance of a text table
123 com.sun.star.text.XTextTable xTT = null;
125 try {
126 Object oInt = xDocMSF.createInstance("com.sun.star.text.TextTable");
127 xTT = UnoRuntime.queryInterface(com.sun.star.text.XTextTable.class,oInt);
128 } catch (Exception e) {
129 System.err.println("Couldn't create instance "+ e);
130 e.printStackTrace(System.err);
133 //initialize the text table with 4 columns an 4 rows
134 xTT.initialize(4,4);
136 com.sun.star.beans.XPropertySet xTTRowPS = null;
138 //insert the table
139 try {
140 xText.insertTextContent(xTCursor, xTT, false);
141 // get first Row
142 com.sun.star.container.XIndexAccess xTTRows = xTT.getRows();
143 xTTRowPS = UnoRuntime.queryInterface(
144 com.sun.star.beans.XPropertySet.class, xTTRows.getByIndex(0));
146 } catch (Exception e) {
147 System.err.println("Couldn't insert the table " + e);
148 e.printStackTrace(System.err);
152 // get the property set of the text table
154 com.sun.star.beans.XPropertySet xTTPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTT);
156 // Change the BackColor
157 try {
158 xTTPS.setPropertyValue("BackTransparent", Boolean.FALSE);
159 xTTPS.setPropertyValue("BackColor",Integer.valueOf(13421823));
160 xTTRowPS.setPropertyValue("BackTransparent", Boolean.FALSE);
161 xTTRowPS.setPropertyValue("BackColor",Integer.valueOf(6710932));
163 } catch (Exception e) {
164 System.err.println("Couldn't change the color " + e);
165 e.printStackTrace(System.err);
168 // write Text in the Table headers
169 System.out.println("Write text in the table headers");
171 insertIntoCell("A1","FirstColumn", xTT);
172 insertIntoCell("B1","SecondColumn", xTT) ;
173 insertIntoCell("C1","ThirdColumn", xTT) ;
174 insertIntoCell("D1","SUM", xTT) ;
177 //Insert Something in the text table
178 System.out.println("Insert something in the text table");
180 (xTT.getCellByName("A2")).setValue(22.5);
181 (xTT.getCellByName("B2")).setValue(5615.3);
182 (xTT.getCellByName("C2")).setValue(-2315.7);
183 (xTT.getCellByName("D2")).setFormula("sum <A2:C2>");
185 (xTT.getCellByName("A3")).setValue(21.5);
186 (xTT.getCellByName("B3")).setValue(615.3);
187 (xTT.getCellByName("C3")).setValue(-315.7);
188 (xTT.getCellByName("D3")).setFormula("sum <A3:C3>");
190 (xTT.getCellByName("A4")).setValue(121.5);
191 (xTT.getCellByName("B4")).setValue(-615.3);
192 (xTT.getCellByName("C4")).setValue(415.7);
193 (xTT.getCellByName("D4")).setFormula("sum <A4:C4>");
196 //oooooooooooooooooooooooooooStep 5oooooooooooooooooooooooooooooooooooooooo
197 // insert a colored text.
198 // Get the propertySet of the cursor, change the CharColor and add a
199 // shadow. Then insert the Text via InsertString at the cursor position.
202 // get the property set of the cursor
203 com.sun.star.beans.XPropertySet xTCPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class,
204 xTCursor);
206 // Change the CharColor and add a Shadow
207 try {
208 xTCPS.setPropertyValue("CharColor",Integer.valueOf(255));
209 xTCPS.setPropertyValue("CharShadowed", Boolean.TRUE);
210 } catch (Exception e) {
211 System.err.println("Couldn't change the color " + e);
212 e.printStackTrace(System.err);
215 //create a paragraph break
216 try {
217 xText.insertControlCharacter(xTCursor,
218 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
220 } catch (Exception e) {
221 System.err.println("Couldn't insert break "+ e);
222 e.printStackTrace(System.err);
225 //inserting colored Text
226 System.out.println("Inserting colored Text");
228 xText.insertString(xTCursor, " This is a colored Text - blue with shadow\n",
229 false );
231 //create a paragraph break
232 try {
233 xText.insertControlCharacter(xTCursor,
234 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
236 } catch (Exception e) {
237 System.err.println("Couldn't insert break "+ e);
238 e.printStackTrace(System.err);
241 //oooooooooooooooooooooooooooStep 6oooooooooooooooooooooooooooooooooooooooo
242 // insert a text frame.
243 // create an instance of com.sun.star.text.TextFrame using the MSF of the
244 // document. Change some properties an insert it.
245 // Now get the text-Object of the frame an the corresponding cursor.
246 // Insert some text via insertString.
249 // Create a TextFrame
250 com.sun.star.text.XTextFrame xTF = null;
251 com.sun.star.drawing.XShape xTFS = null;
253 try {
254 Object oInt = xDocMSF.createInstance("com.sun.star.text.TextFrame");
255 xTF = UnoRuntime.queryInterface(
256 com.sun.star.text.XTextFrame.class,oInt);
257 xTFS = UnoRuntime.queryInterface(
258 com.sun.star.drawing.XShape.class,oInt);
260 com.sun.star.awt.Size aSize = new com.sun.star.awt.Size();
261 aSize.Height = 400;
262 aSize.Width = 15000;
264 xTFS.setSize(aSize);
265 } catch (Exception e) {
266 System.err.println("Couldn't create instance "+ e);
267 e.printStackTrace(System.err);
270 // get the property set of the text frame
271 com.sun.star.beans.XPropertySet xTFPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTF);
273 // Change the AnchorType
274 try {
275 xTFPS.setPropertyValue("AnchorType",
276 com.sun.star.text.TextContentAnchorType.AS_CHARACTER);
277 } catch (Exception e) {
278 System.err.println("Couldn't change the color " + e);
279 e.printStackTrace(System.err);
282 //insert the frame
283 System.out.println("Insert the text frame");
285 try {
286 xText.insertTextContent(xTCursor, xTF, false);
287 } catch (Exception e) {
288 System.err.println("Couldn't insert the frame " + e);
289 e.printStackTrace(System.err);
292 //getting the text object of Frame
293 com.sun.star.text.XText xTextF = xTF.getText();
295 //create a cursor object
296 com.sun.star.text.XTextCursor xTCF = xTextF.createTextCursor();
298 //inserting some Text
299 xTextF.insertString(xTCF,
300 "The first line in the newly created text frame.", false);
303 xTextF.insertString(xTCF,
304 "\nWith this second line the height of the frame raises.", false);
306 //insert a paragraph break
307 try {
308 xText.insertControlCharacter(xTCursor,
309 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false );
311 } catch (Exception e) {
312 System.err.println("Couldn't insert break "+ e);
313 e.printStackTrace(System.err);
316 // Change the CharColor and add a Shadow
317 try {
318 xTCPS.setPropertyValue("CharColor",Integer.valueOf(65536));
319 xTCPS.setPropertyValue("CharShadowed", Boolean.FALSE);
320 } catch (Exception e) {
321 System.err.println("Couldn't change the color " + e);
322 e.printStackTrace(System.err);
325 xText.insertString(xTCursor, " That's all for now !!", false );
327 System.out.println("done");
329 System.exit(0);
333 public static com.sun.star.text.XTextDocument openWriter(
334 com.sun.star.uno.XComponentContext xContext)
336 //define variables
337 com.sun.star.frame.XComponentLoader xCLoader;
338 com.sun.star.text.XTextDocument xDoc = null;
339 com.sun.star.lang.XComponent xComp = null;
341 try {
342 // get the remote office service manager
343 com.sun.star.lang.XMultiComponentFactory xMCF =
344 xContext.getServiceManager();
346 Object oDesktop = xMCF.createInstanceWithContext(
347 "com.sun.star.frame.Desktop", xContext);
349 xCLoader = UnoRuntime.queryInterface(com.sun.star.frame.XComponentLoader.class,
350 oDesktop);
351 com.sun.star.beans.PropertyValue [] szEmptyArgs =
352 new com.sun.star.beans.PropertyValue [0];
353 String strDoc = "private:factory/swriter";
354 xComp = xCLoader.loadComponentFromURL(strDoc, "_blank", 0, szEmptyArgs);
355 xDoc = UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class,
356 xComp);
358 } catch(Exception e){
359 System.err.println(" Exception " + e);
360 e.printStackTrace(System.err);
362 return xDoc;
365 public static void insertIntoCell(String CellName, String theText,
366 com.sun.star.text.XTextTable xTTbl) {
368 com.sun.star.text.XText xTableText = UnoRuntime.queryInterface(com.sun.star.text.XText.class,
369 xTTbl.getCellByName(CellName));
371 //create a cursor object
372 com.sun.star.text.XTextCursor xTC = xTableText.createTextCursor();
374 com.sun.star.beans.XPropertySet xTPS = UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xTC);
376 try {
377 xTPS.setPropertyValue("CharColor",Integer.valueOf(16777215));
378 } catch (Exception e) {
379 System.err.println(" Exception " + e);
380 e.printStackTrace(System.err);
383 //inserting some Text
384 xTableText.setString( theText );