Update ooo320-m1
[ooovba.git] / odk / examples / java / Text / StyleCreation.java
blob0cf635aa4b3f25c1e62cae26554c1798cbfaacbb
1 /*************************************************************************
3 * $RCSfile: StyleCreation.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:17:43 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 //***************************************************************************
42 // comment: Step 1: get the Desktop object from the office
43 // Step 2: open an empty text document
44 // Step 3: create a new Paragraph style
45 // Step 4: apply the Paragraph style
47 // Chapter 4.1.3 Defining Your Own Style
48 //***************************************************************************
50 import com.sun.star.uno.UnoRuntime;
53 public class StyleCreation {
54 public static void main(String args[]) {
55 // You need the desktop to create a document
56 // The getDesktop method does the UNO bootstrapping, gets the
57 // remote servie manager and the desktop object.
58 com.sun.star.frame.XDesktop xDesktop = null;
59 xDesktop = getDesktop();
61 try {
62 // create text document
63 com.sun.star.text.XTextDocument xTextDocument = null;
64 xTextDocument = createTextdocument(xDesktop);
66 // the service '..ParagraphStyle' is context dependend, you need
67 // the multi service factory from the document to use the service
68 com.sun.star.lang.XMultiServiceFactory xDocMSF =
69 (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
70 com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
72 // use the service 'com.sun.star.style.ParagraphStyle'
73 com.sun.star.uno.XInterface xInterface = (com.sun.star.uno.XInterface)
74 xDocMSF.createInstance("com.sun.star.style.ParagraphStyle");
76 // create a supplier to get the Style family collection
77 com.sun.star.style.XStyleFamiliesSupplier xSupplier =
78 (com.sun.star.style.XStyleFamiliesSupplier)UnoRuntime.queryInterface(
79 com.sun.star.style.XStyleFamiliesSupplier.class, xTextDocument );
81 // get the NameAccess interface from the Style family collection
82 com.sun.star.container.XNameAccess xNameAccess =
83 xSupplier.getStyleFamilies();
85 // select the Paragraph styles, you get the Paragraph style collection
86 com.sun.star.container.XNameContainer xParaStyleCollection =
87 (com.sun.star.container.XNameContainer) UnoRuntime.queryInterface(
88 com.sun.star.container.XNameContainer.class,
89 xNameAccess.getByName("ParagraphStyles"));
91 // create a PropertySet to set the properties for the new Paragraphstyle
92 com.sun.star.beans.XPropertySet xPropertySet =
93 (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
94 com.sun.star.beans.XPropertySet.class, xInterface );
95 System.out.println( "create a PropertySet to set the properties for the new Paragraphstyle" );
97 // set some properties from the Paragraph style
98 xPropertySet.setPropertyValue("CharFontName", new String( "Helvetica" ) );
99 System.out.println( "set name of the font to 'Helvetica'" );
101 xPropertySet.setPropertyValue("CharHeight", new Float( 36 ) );
102 System.out.println( "Change the height of th font to 36" );
104 xPropertySet.setPropertyValue("CharWeight",
105 new Float( com.sun.star.awt.FontWeight.BOLD ) );
106 System.out.println( "set the font attribute 'Bold'" );
108 xPropertySet.setPropertyValue("CharAutoKerning", new Boolean( true ) );
109 System.out.println( "set the paragraph attribute 'AutoKerning'" );
110 xPropertySet.setPropertyValue("ParaAdjust",
111 new Integer( com.sun.star.style.ParagraphAdjust.CENTER_value ) );
112 System.out.println( "set the paragraph adjust to LEFT" );
114 xPropertySet.setPropertyValue("ParaFirstLineIndent", new Integer( 0 ) );
115 System.out.println( "set the first line indent to 0 cm" );
117 xPropertySet.setPropertyValue("BreakType",
118 com.sun.star.style.BreakType.PAGE_AFTER );
119 System.out.println( "set the paragraph attribute Breaktype to PageAfter" );
121 // insert the new Paragraph style in the Paragraph style collection
122 xParaStyleCollection.insertByName( "myheading", xPropertySet );
123 System.out.println( "create new paragraph style, with the values from the Propertyset");
125 // get the Textrange from the document
126 com.sun.star.text.XTextRange xTextRange =
127 xTextDocument.getText().getStart();
129 // get the PropertySet from the current paragraph
130 com.sun.star.beans.XPropertySet xParagraphPropertySet =
131 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
132 com.sun.star.beans.XPropertySet.class, xTextRange );
133 // change the value from the property 'ParaStyle' to apply the
134 // Paragraph style
135 // To run the sample with StarOffice 5.2 you'll have to change
136 // 'ParaStyleName' to 'ParaStyle' in the next line
137 xParagraphPropertySet.setPropertyValue("ParaStyleName",
138 new String( "myheading" ) );
139 System.out.println( "apply the new paragraph style");
141 catch( Exception e) {
142 e.printStackTrace(System.err);
143 System.exit(1);
146 System.out.println("done");
148 System.exit(0);
152 public static com.sun.star.frame.XDesktop getDesktop() {
153 com.sun.star.frame.XDesktop xDesktop = null;
154 com.sun.star.lang.XMultiComponentFactory xMCF = null;
156 try {
157 com.sun.star.uno.XComponentContext xContext = null;
159 // get the remote office component context
160 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
162 // get the remote office service manager
163 xMCF = xContext.getServiceManager();
164 if( xMCF != null ) {
165 System.out.println("Connected to a running office ...");
167 Object oDesktop = xMCF.createInstanceWithContext(
168 "com.sun.star.frame.Desktop", xContext);
169 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
170 com.sun.star.frame.XDesktop.class, oDesktop);
172 else
173 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
175 catch( Exception e) {
176 e.printStackTrace(System.err);
177 System.exit(1);
181 return xDesktop;
184 public static com.sun.star.text.XTextDocument createTextdocument(
185 com.sun.star.frame.XDesktop xDesktop )
187 com.sun.star.text.XTextDocument aTextDocument = null;
189 try {
190 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
191 "swriter");
192 aTextDocument = (com.sun.star.text.XTextDocument)
193 UnoRuntime.queryInterface(
194 com.sun.star.text.XTextDocument.class, xComponent);
196 catch( Exception e) {
197 e.printStackTrace(System.err);
200 return aTextDocument;
204 protected static com.sun.star.lang.XComponent CreateNewDocument(
205 com.sun.star.frame.XDesktop xDesktop,
206 String sDocumentType )
208 String sURL = "private:factory/" + sDocumentType;
210 com.sun.star.lang.XComponent xComponent = null;
211 com.sun.star.frame.XComponentLoader xComponentLoader = null;
212 com.sun.star.beans.PropertyValue xValues[] =
213 new com.sun.star.beans.PropertyValue[1];
214 com.sun.star.beans.PropertyValue xEmptyArgs[] =
215 new com.sun.star.beans.PropertyValue[0];
217 try {
218 xComponentLoader = (com.sun.star.frame.XComponentLoader)
219 UnoRuntime.queryInterface(
220 com.sun.star.frame.XComponentLoader.class, xDesktop);
222 xComponent = xComponentLoader.loadComponentFromURL(
223 sURL, "_blank", 0, xEmptyArgs);
225 catch( Exception e) {
226 e.printStackTrace(System.err);
229 return xComponent ;