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