1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir //***************************************************************************
25cdf0e10cSrcweir // comment: Step 1: get the Desktop object from the office
26cdf0e10cSrcweir //          Step 2: open an empty text document
27cdf0e10cSrcweir //          Step 3: create a new Paragraph style
28cdf0e10cSrcweir //          Step 4: apply the Paragraph style
29cdf0e10cSrcweir //
30cdf0e10cSrcweir //          Chapter 4.1.3 Defining Your Own Style
31cdf0e10cSrcweir //***************************************************************************
32cdf0e10cSrcweir 
33cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir 
36cdf0e10cSrcweir public class StyleCreation {
main(String args[])37cdf0e10cSrcweir     public static void main(String args[]) {
38cdf0e10cSrcweir         // You need the desktop to create a document
39cdf0e10cSrcweir         // The getDesktop method does the UNO bootstrapping, gets the
40cdf0e10cSrcweir         // remote servie manager and the desktop object.
41cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop = null;
42cdf0e10cSrcweir         xDesktop = getDesktop();
43cdf0e10cSrcweir 
44cdf0e10cSrcweir         try {
45cdf0e10cSrcweir             // create text document
46cdf0e10cSrcweir             com.sun.star.text.XTextDocument xTextDocument = null;
47cdf0e10cSrcweir             xTextDocument = createTextdocument(xDesktop);
48cdf0e10cSrcweir 
49cdf0e10cSrcweir             // the service '..ParagraphStyle' is context dependend, you need
50cdf0e10cSrcweir             // the multi service factory from the document to use the service
51cdf0e10cSrcweir             com.sun.star.lang.XMultiServiceFactory xDocMSF =
52cdf0e10cSrcweir                 (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
53cdf0e10cSrcweir                     com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
54cdf0e10cSrcweir 
55cdf0e10cSrcweir             // use the service 'com.sun.star.style.ParagraphStyle'
56cdf0e10cSrcweir             com.sun.star.uno.XInterface xInterface = (com.sun.star.uno.XInterface)
57cdf0e10cSrcweir                 xDocMSF.createInstance("com.sun.star.style.ParagraphStyle");
58cdf0e10cSrcweir 
59cdf0e10cSrcweir             // create a supplier to get the Style family collection
60cdf0e10cSrcweir             com.sun.star.style.XStyleFamiliesSupplier xSupplier =
61cdf0e10cSrcweir                 (com.sun.star.style.XStyleFamiliesSupplier)UnoRuntime.queryInterface(
62cdf0e10cSrcweir                     com.sun.star.style.XStyleFamiliesSupplier.class, xTextDocument );
63cdf0e10cSrcweir 
64cdf0e10cSrcweir             // get the NameAccess interface from the Style family collection
65cdf0e10cSrcweir             com.sun.star.container.XNameAccess xNameAccess =
66cdf0e10cSrcweir                 xSupplier.getStyleFamilies();
67cdf0e10cSrcweir 
68cdf0e10cSrcweir             // select the Paragraph styles, you get the Paragraph style collection
69cdf0e10cSrcweir             com.sun.star.container.XNameContainer xParaStyleCollection =
70cdf0e10cSrcweir                 (com.sun.star.container.XNameContainer) UnoRuntime.queryInterface(
71cdf0e10cSrcweir                     com.sun.star.container.XNameContainer.class,
72cdf0e10cSrcweir                     xNameAccess.getByName("ParagraphStyles"));
73cdf0e10cSrcweir 
74cdf0e10cSrcweir             // create a PropertySet to set the properties for the new Paragraphstyle
75cdf0e10cSrcweir             com.sun.star.beans.XPropertySet xPropertySet =
76cdf0e10cSrcweir                 (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
77cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet.class, xInterface );
78cdf0e10cSrcweir             System.out.println( "create a PropertySet to set the properties for the new Paragraphstyle" );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir             // set some properties from the Paragraph style
81cdf0e10cSrcweir             xPropertySet.setPropertyValue("CharFontName", new String( "Helvetica" ) );
82cdf0e10cSrcweir             System.out.println( "set name of the font to 'Helvetica'" );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir             xPropertySet.setPropertyValue("CharHeight", new Float( 36 ) );
85cdf0e10cSrcweir             System.out.println( "Change the height of th font to 36" );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             xPropertySet.setPropertyValue("CharWeight",
88cdf0e10cSrcweir                 new Float( com.sun.star.awt.FontWeight.BOLD ) );
89cdf0e10cSrcweir             System.out.println( "set the font attribute 'Bold'" );
90cdf0e10cSrcweir 
91cdf0e10cSrcweir             xPropertySet.setPropertyValue("CharAutoKerning", new Boolean( true ) );
92cdf0e10cSrcweir             System.out.println( "set the paragraph attribute 'AutoKerning'" );
93cdf0e10cSrcweir             xPropertySet.setPropertyValue("ParaAdjust",
94cdf0e10cSrcweir                 new Integer( com.sun.star.style.ParagraphAdjust.CENTER_value ) );
95cdf0e10cSrcweir             System.out.println( "set the paragraph adjust to LEFT" );
96cdf0e10cSrcweir 
97cdf0e10cSrcweir             xPropertySet.setPropertyValue("ParaFirstLineIndent", new Integer( 0 ) );
98cdf0e10cSrcweir             System.out.println( "set the first line indent to 0 cm" );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir             xPropertySet.setPropertyValue("BreakType",
101cdf0e10cSrcweir                 com.sun.star.style.BreakType.PAGE_AFTER );
102cdf0e10cSrcweir             System.out.println( "set the paragraph attribute Breaktype to PageAfter" );
103cdf0e10cSrcweir 
104cdf0e10cSrcweir             // insert the new Paragraph style in the Paragraph style collection
105cdf0e10cSrcweir             xParaStyleCollection.insertByName( "myheading", xPropertySet );
106cdf0e10cSrcweir             System.out.println( "create new paragraph style, with the values from the Propertyset");
107cdf0e10cSrcweir 
108cdf0e10cSrcweir             // get the Textrange from the document
109cdf0e10cSrcweir             com.sun.star.text.XTextRange xTextRange =
110cdf0e10cSrcweir                 xTextDocument.getText().getStart();
111cdf0e10cSrcweir 
112cdf0e10cSrcweir             // get the PropertySet from the current paragraph
113cdf0e10cSrcweir             com.sun.star.beans.XPropertySet xParagraphPropertySet =
114cdf0e10cSrcweir                 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
115cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet.class, xTextRange );
116cdf0e10cSrcweir             // change the value from the property 'ParaStyle' to apply the
117cdf0e10cSrcweir             // Paragraph style
118cdf0e10cSrcweir             // To run the sample with StarOffice 5.2 you'll have to change
119cdf0e10cSrcweir             // 'ParaStyleName' to 'ParaStyle' in the next line
120cdf0e10cSrcweir             xParagraphPropertySet.setPropertyValue("ParaStyleName",
121cdf0e10cSrcweir                 new String( "myheading" ) );
122cdf0e10cSrcweir             System.out.println( "apply the new paragraph style");
123cdf0e10cSrcweir         }
124cdf0e10cSrcweir         catch( Exception e) {
125cdf0e10cSrcweir             e.printStackTrace(System.err);
126cdf0e10cSrcweir             System.exit(1);
127cdf0e10cSrcweir         }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         System.out.println("done");
130cdf0e10cSrcweir 
131cdf0e10cSrcweir         System.exit(0);
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 
getDesktop()135cdf0e10cSrcweir     public static com.sun.star.frame.XDesktop getDesktop() {
136cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop = null;
137cdf0e10cSrcweir         com.sun.star.lang.XMultiComponentFactory xMCF = null;
138cdf0e10cSrcweir 
139cdf0e10cSrcweir         try {
140cdf0e10cSrcweir             com.sun.star.uno.XComponentContext xContext = null;
141cdf0e10cSrcweir 
142cdf0e10cSrcweir             // get the remote office component context
143cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
144cdf0e10cSrcweir 
145cdf0e10cSrcweir             // get the remote office service manager
146cdf0e10cSrcweir             xMCF = xContext.getServiceManager();
147cdf0e10cSrcweir             if( xMCF != null ) {
148cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
149cdf0e10cSrcweir 
150cdf0e10cSrcweir                 Object oDesktop = xMCF.createInstanceWithContext(
151cdf0e10cSrcweir                     "com.sun.star.frame.Desktop", xContext);
152cdf0e10cSrcweir                 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
153cdf0e10cSrcweir                     com.sun.star.frame.XDesktop.class, oDesktop);
154cdf0e10cSrcweir             }
155cdf0e10cSrcweir             else
156cdf0e10cSrcweir                 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
157cdf0e10cSrcweir         }
158cdf0e10cSrcweir         catch( Exception e) {
159cdf0e10cSrcweir             e.printStackTrace(System.err);
160cdf0e10cSrcweir             System.exit(1);
161cdf0e10cSrcweir         }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         return xDesktop;
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
createTextdocument( com.sun.star.frame.XDesktop xDesktop )167cdf0e10cSrcweir     public static com.sun.star.text.XTextDocument createTextdocument(
168cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop )
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir         com.sun.star.text.XTextDocument aTextDocument = null;
171cdf0e10cSrcweir 
172cdf0e10cSrcweir         try {
173cdf0e10cSrcweir             com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
174cdf0e10cSrcweir                                                                         "swriter");
175cdf0e10cSrcweir             aTextDocument = (com.sun.star.text.XTextDocument)
176cdf0e10cSrcweir                 UnoRuntime.queryInterface(
177cdf0e10cSrcweir                     com.sun.star.text.XTextDocument.class, xComponent);
178cdf0e10cSrcweir         }
179cdf0e10cSrcweir         catch( Exception e) {
180cdf0e10cSrcweir             e.printStackTrace(System.err);
181cdf0e10cSrcweir         }
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         return aTextDocument;
184cdf0e10cSrcweir     }
185cdf0e10cSrcweir 
186cdf0e10cSrcweir 
CreateNewDocument( com.sun.star.frame.XDesktop xDesktop, String sDocumentType )187cdf0e10cSrcweir     protected static com.sun.star.lang.XComponent CreateNewDocument(
188cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop,
189cdf0e10cSrcweir         String sDocumentType )
190cdf0e10cSrcweir     {
191cdf0e10cSrcweir         String sURL = "private:factory/" + sDocumentType;
192cdf0e10cSrcweir 
193cdf0e10cSrcweir         com.sun.star.lang.XComponent xComponent = null;
194cdf0e10cSrcweir         com.sun.star.frame.XComponentLoader xComponentLoader = null;
195cdf0e10cSrcweir         com.sun.star.beans.PropertyValue xValues[] =
196cdf0e10cSrcweir             new com.sun.star.beans.PropertyValue[1];
197cdf0e10cSrcweir         com.sun.star.beans.PropertyValue xEmptyArgs[] =
198cdf0e10cSrcweir             new com.sun.star.beans.PropertyValue[0];
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         try {
201cdf0e10cSrcweir             xComponentLoader = (com.sun.star.frame.XComponentLoader)
202cdf0e10cSrcweir                 UnoRuntime.queryInterface(
203cdf0e10cSrcweir                     com.sun.star.frame.XComponentLoader.class, xDesktop);
204cdf0e10cSrcweir 
205cdf0e10cSrcweir             xComponent  = xComponentLoader.loadComponentFromURL(
206cdf0e10cSrcweir                 sURL, "_blank", 0, xEmptyArgs);
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir         catch( Exception e) {
209cdf0e10cSrcweir             e.printStackTrace(System.err);
210cdf0e10cSrcweir         }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir         return xComponent ;
213cdf0e10cSrcweir     }
214cdf0e10cSrcweir }
215cdf0e10cSrcweir 
216