Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Text / StyleCreation.java
blob63f8825acd026fdb8d766adbecdb298553605a90
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: get the Desktop object from the office
37 // Step 2: open an empty text document
38 // Step 3: create a new Paragraph style
39 // Step 4: apply the Paragraph style
41 // Chapter 4.1.3 Defining Your Own Style
44 import com.sun.star.uno.UnoRuntime;
47 public class StyleCreation {
48 public static void main(String args[]) {
49 // You need the desktop to create a document
50 // The getDesktop method does the UNO bootstrapping, gets the
51 // remote servie manager and the desktop object.
52 com.sun.star.frame.XDesktop xDesktop = null;
53 xDesktop = getDesktop();
55 try {
56 // create text document
57 com.sun.star.text.XTextDocument xTextDocument = null;
58 xTextDocument = createTextdocument(xDesktop);
60 // the service '..ParagraphStyle' is context dependent, you need
61 // the multi service factory from the document to use the service
62 com.sun.star.lang.XMultiServiceFactory xDocMSF =
63 UnoRuntime.queryInterface(
64 com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
66 // use the service 'com.sun.star.style.ParagraphStyle'
67 com.sun.star.uno.XInterface xInterface = (com.sun.star.uno.XInterface)
68 xDocMSF.createInstance("com.sun.star.style.ParagraphStyle");
70 // create a supplier to get the Style family collection
71 com.sun.star.style.XStyleFamiliesSupplier xSupplier =
72 UnoRuntime.queryInterface(
73 com.sun.star.style.XStyleFamiliesSupplier.class, xTextDocument );
75 // get the NameAccess interface from the Style family collection
76 com.sun.star.container.XNameAccess xNameAccess =
77 xSupplier.getStyleFamilies();
79 // select the Paragraph styles, you get the Paragraph style collection
80 com.sun.star.container.XNameContainer xParaStyleCollection =
81 UnoRuntime.queryInterface(
82 com.sun.star.container.XNameContainer.class,
83 xNameAccess.getByName("ParagraphStyles"));
85 // create a PropertySet to set the properties for the new Paragraphstyle
86 com.sun.star.beans.XPropertySet xPropertySet =
87 UnoRuntime.queryInterface(
88 com.sun.star.beans.XPropertySet.class, xInterface );
89 System.out.println( "create a PropertySet to set the properties for the new Paragraphstyle" );
91 // set some properties from the Paragraph style
92 xPropertySet.setPropertyValue("CharFontName", "Helvetica" );
93 System.out.println( "set name of the font to 'Helvetica'" );
95 xPropertySet.setPropertyValue("CharHeight", new Float( 36 ) );
96 System.out.println( "Change the height of th font to 36" );
98 xPropertySet.setPropertyValue("CharWeight",
99 new Float( com.sun.star.awt.FontWeight.BOLD ) );
100 System.out.println( "set the font attribute 'Bold'" );
102 xPropertySet.setPropertyValue("CharAutoKerning", Boolean.TRUE );
103 System.out.println( "set the paragraph attribute 'AutoKerning'" );
104 xPropertySet.setPropertyValue("ParaAdjust",
105 Integer.valueOf( com.sun.star.style.ParagraphAdjust.CENTER_value ) );
106 System.out.println( "set the paragraph adjust to LEFT" );
108 xPropertySet.setPropertyValue("ParaFirstLineIndent", Integer.valueOf( 0 ) );
109 System.out.println( "set the first line indent to 0 cm" );
111 xPropertySet.setPropertyValue("BreakType",
112 com.sun.star.style.BreakType.PAGE_AFTER );
113 System.out.println( "set the paragraph attribute Breaktype to PageAfter" );
115 // insert the new Paragraph style in the Paragraph style collection
116 xParaStyleCollection.insertByName( "myheading", xPropertySet );
117 System.out.println( "create new paragraph style, with the values from the Propertyset");
119 // get the Textrange from the document
120 com.sun.star.text.XTextRange xTextRange =
121 xTextDocument.getText().getStart();
123 // get the PropertySet from the current paragraph
124 com.sun.star.beans.XPropertySet xParagraphPropertySet =
125 UnoRuntime.queryInterface(
126 com.sun.star.beans.XPropertySet.class, xTextRange );
127 // change the value from the property 'ParaStyle' to apply the
128 // Paragraph style
129 // To run the sample with StarOffice 5.2 you'll have to change
130 // 'ParaStyleName' to 'ParaStyle' in the next line
131 xParagraphPropertySet.setPropertyValue("ParaStyleName",
132 "myheading" );
133 System.out.println( "apply the new paragraph style");
135 catch( Exception e) {
136 e.printStackTrace(System.err);
137 System.exit(1);
140 System.out.println("done");
142 System.exit(0);
146 public static com.sun.star.frame.XDesktop getDesktop() {
147 com.sun.star.frame.XDesktop xDesktop = null;
148 com.sun.star.lang.XMultiComponentFactory xMCF = null;
150 try {
151 com.sun.star.uno.XComponentContext xContext = null;
153 // get the remote office component context
154 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
156 // get the remote office service manager
157 xMCF = xContext.getServiceManager();
158 if( xMCF != null ) {
159 System.out.println("Connected to a running office ...");
161 Object oDesktop = xMCF.createInstanceWithContext(
162 "com.sun.star.frame.Desktop", xContext);
163 xDesktop = UnoRuntime.queryInterface(
164 com.sun.star.frame.XDesktop.class, oDesktop);
166 else
167 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
169 catch( Exception e) {
170 e.printStackTrace(System.err);
171 System.exit(1);
175 return xDesktop;
178 public static com.sun.star.text.XTextDocument createTextdocument(
179 com.sun.star.frame.XDesktop xDesktop )
181 com.sun.star.text.XTextDocument aTextDocument = null;
183 try {
184 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
185 "swriter");
186 aTextDocument = UnoRuntime.queryInterface(
187 com.sun.star.text.XTextDocument.class, xComponent);
189 catch( Exception e) {
190 e.printStackTrace(System.err);
193 return aTextDocument;
197 protected static com.sun.star.lang.XComponent CreateNewDocument(
198 com.sun.star.frame.XDesktop xDesktop,
199 String sDocumentType )
201 String sURL = "private:factory/" + sDocumentType;
203 com.sun.star.lang.XComponent xComponent = null;
204 com.sun.star.frame.XComponentLoader xComponentLoader = null;
205 com.sun.star.beans.PropertyValue xEmptyArgs[] =
206 new com.sun.star.beans.PropertyValue[0];
208 try {
209 xComponentLoader = UnoRuntime.queryInterface(
210 com.sun.star.frame.XComponentLoader.class, xDesktop);
212 xComponent = xComponentLoader.loadComponentFromURL(
213 sURL, "_blank", 0, xEmptyArgs);
215 catch( Exception e) {
216 e.printStackTrace(System.err);
219 return xComponent ;