cid#1640468 Dereference after null check
[LibreOffice.git] / odk / examples / java / Text / StyleCreation.java
blob5ad1c9498a2689703d1ed4b795fa025b4001f10d
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: get the Desktop object from the office
38 // Step 2: open an empty text document
39 // Step 3: create a new Paragraph style
40 // Step 4: apply the Paragraph style
42 // Chapter 4.1.3 Defining Your Own Style
45 import com.sun.star.uno.UnoRuntime;
48 public class StyleCreation {
49 public static void main(String args[]) {
50 // You need the desktop to create a document
51 // The getDesktop method does the UNO bootstrapping, gets the
52 // remote service manager and the desktop object.
53 com.sun.star.frame.XDesktop xDesktop = null;
54 xDesktop = getDesktop();
56 try {
57 // create text document
58 com.sun.star.text.XTextDocument xTextDocument = null;
59 xTextDocument = createTextdocument(xDesktop);
61 // the service '..ParagraphStyle' is context dependent, you need
62 // the multi service factory from the document to use the service
63 com.sun.star.lang.XMultiServiceFactory xDocMSF =
64 UnoRuntime.queryInterface(
65 com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
67 // use the service 'com.sun.star.style.ParagraphStyle'
68 com.sun.star.uno.XInterface xInterface = (com.sun.star.uno.XInterface)
69 xDocMSF.createInstance("com.sun.star.style.ParagraphStyle");
71 // create a supplier to get the Style family collection
72 com.sun.star.style.XStyleFamiliesSupplier xSupplier =
73 UnoRuntime.queryInterface(
74 com.sun.star.style.XStyleFamiliesSupplier.class, xTextDocument );
76 // get the NameAccess interface from the Style family collection
77 com.sun.star.container.XNameAccess xNameAccess =
78 xSupplier.getStyleFamilies();
80 // select the Paragraph styles, you get the Paragraph style collection
81 com.sun.star.container.XNameContainer xParaStyleCollection =
82 UnoRuntime.queryInterface(
83 com.sun.star.container.XNameContainer.class,
84 xNameAccess.getByName("ParagraphStyles"));
86 // create a PropertySet to set the properties for the new Paragraphstyle
87 com.sun.star.beans.XPropertySet xPropertySet =
88 UnoRuntime.queryInterface(
89 com.sun.star.beans.XPropertySet.class, xInterface );
90 System.out.println( "create a PropertySet to set the properties for the new Paragraphstyle" );
92 // set some properties from the Paragraph style
93 xPropertySet.setPropertyValue("CharFontName", "Helvetica" );
94 System.out.println( "set name of the font to 'Helvetica'" );
96 xPropertySet.setPropertyValue("CharHeight", Float.valueOf( 36 ) );
97 System.out.println( "Change the height of th font to 36" );
99 xPropertySet.setPropertyValue("CharWeight",
100 Float.valueOf( com.sun.star.awt.FontWeight.BOLD ) );
101 System.out.println( "set the font attribute 'Bold'" );
103 xPropertySet.setPropertyValue("CharAutoKerning", Boolean.TRUE );
104 System.out.println( "set the paragraph attribute 'AutoKerning'" );
105 xPropertySet.setPropertyValue("ParaAdjust",
106 Integer.valueOf( com.sun.star.style.ParagraphAdjust.CENTER_value ) );
107 System.out.println( "set the paragraph adjust to LEFT" );
109 xPropertySet.setPropertyValue("ParaFirstLineIndent", Integer.valueOf( 0 ) );
110 System.out.println( "set the first line indent to 0 cm" );
112 xPropertySet.setPropertyValue("BreakType",
113 com.sun.star.style.BreakType.PAGE_AFTER );
114 System.out.println( "set the paragraph attribute Breaktype to PageAfter" );
116 // insert the new Paragraph style in the Paragraph style collection
117 xParaStyleCollection.insertByName( "myheading", xPropertySet );
118 System.out.println( "create new paragraph style, with the values from the Propertyset");
120 // get the Textrange from the document
121 com.sun.star.text.XTextRange xTextRange =
122 xTextDocument.getText().getStart();
124 // get the PropertySet from the current paragraph
125 com.sun.star.beans.XPropertySet xParagraphPropertySet =
126 UnoRuntime.queryInterface(
127 com.sun.star.beans.XPropertySet.class, xTextRange );
128 // change the value from the property 'ParaStyle' to apply the
129 // Paragraph style
130 // To run the sample with StarOffice 5.2 you'll have to change
131 // 'ParaStyleName' to 'ParaStyle' in the next line
132 xParagraphPropertySet.setPropertyValue("ParaStyleName",
133 "myheading" );
134 System.out.println( "apply the new paragraph style");
136 catch( Exception e) {
137 e.printStackTrace(System.err);
138 System.exit(1);
141 System.out.println("done");
143 System.exit(0);
147 public static com.sun.star.frame.XDesktop getDesktop() {
148 com.sun.star.frame.XDesktop xDesktop = null;
149 com.sun.star.lang.XMultiComponentFactory xMCF = null;
151 try {
152 com.sun.star.uno.XComponentContext xContext = null;
154 // get the remote office component context
155 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
157 // get the remote office service manager
158 xMCF = xContext.getServiceManager();
159 if( xMCF != null ) {
160 System.out.println("Connected to a running office ...");
162 Object oDesktop = xMCF.createInstanceWithContext(
163 "com.sun.star.frame.Desktop", xContext);
164 xDesktop = UnoRuntime.queryInterface(
165 com.sun.star.frame.XDesktop.class, oDesktop);
167 else
168 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
170 catch( Exception e) {
171 e.printStackTrace(System.err);
172 System.exit(1);
176 return xDesktop;
179 public static com.sun.star.text.XTextDocument createTextdocument(
180 com.sun.star.frame.XDesktop xDesktop )
182 com.sun.star.text.XTextDocument aTextDocument = null;
184 try {
185 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
186 "swriter");
187 aTextDocument = UnoRuntime.queryInterface(
188 com.sun.star.text.XTextDocument.class, xComponent);
190 catch( Exception e) {
191 e.printStackTrace(System.err);
194 return aTextDocument;
198 protected static com.sun.star.lang.XComponent CreateNewDocument(
199 com.sun.star.frame.XDesktop xDesktop,
200 String sDocumentType )
202 String sURL = "private:factory/" + sDocumentType;
204 com.sun.star.lang.XComponent xComponent = null;
205 com.sun.star.frame.XComponentLoader xComponentLoader = null;
206 com.sun.star.beans.PropertyValue xEmptyArgs[] =
207 new com.sun.star.beans.PropertyValue[0];
209 try {
210 xComponentLoader = UnoRuntime.queryInterface(
211 com.sun.star.frame.XComponentLoader.class, xDesktop);
213 xComponent = xComponentLoader.loadComponentFromURL(
214 sURL, "_blank", 0, xEmptyArgs);
216 catch( Exception e) {
217 e.printStackTrace(System.err);
220 return xComponent ;
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */