1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
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.
20  *
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.
32  *
33  *************************************************************************/
34 
35 //***************************************************************************
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
40 //
41 //          Chapter 4.1.3 Defining Your Own Style
42 //***************************************************************************
43 
44 import com.sun.star.uno.UnoRuntime;
45 
46 
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();
54 
55         try {
56             // create text document
57             com.sun.star.text.XTextDocument xTextDocument = null;
58             xTextDocument = createTextdocument(xDesktop);
59 
60             // the service '..ParagraphStyle' is context dependend, you need
61             // the multi service factory from the document to use the service
62             com.sun.star.lang.XMultiServiceFactory xDocMSF =
63                 (com.sun.star.lang.XMultiServiceFactory)UnoRuntime.queryInterface(
64                     com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
65 
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");
69 
70             // create a supplier to get the Style family collection
71             com.sun.star.style.XStyleFamiliesSupplier xSupplier =
72                 (com.sun.star.style.XStyleFamiliesSupplier)UnoRuntime.queryInterface(
73                     com.sun.star.style.XStyleFamiliesSupplier.class, xTextDocument );
74 
75             // get the NameAccess interface from the Style family collection
76             com.sun.star.container.XNameAccess xNameAccess =
77                 xSupplier.getStyleFamilies();
78 
79             // select the Paragraph styles, you get the Paragraph style collection
80             com.sun.star.container.XNameContainer xParaStyleCollection =
81                 (com.sun.star.container.XNameContainer) UnoRuntime.queryInterface(
82                     com.sun.star.container.XNameContainer.class,
83                     xNameAccess.getByName("ParagraphStyles"));
84 
85             // create a PropertySet to set the properties for the new Paragraphstyle
86             com.sun.star.beans.XPropertySet xPropertySet =
87                 (com.sun.star.beans.XPropertySet) 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" );
90 
91             // set some properties from the Paragraph style
92             xPropertySet.setPropertyValue("CharFontName", new String( "Helvetica" ) );
93             System.out.println( "set name of the font to 'Helvetica'" );
94 
95             xPropertySet.setPropertyValue("CharHeight", new Float( 36 ) );
96             System.out.println( "Change the height of th font to 36" );
97 
98             xPropertySet.setPropertyValue("CharWeight",
99                 new Float( com.sun.star.awt.FontWeight.BOLD ) );
100             System.out.println( "set the font attribute 'Bold'" );
101 
102             xPropertySet.setPropertyValue("CharAutoKerning", new Boolean( true ) );
103             System.out.println( "set the paragraph attribute 'AutoKerning'" );
104             xPropertySet.setPropertyValue("ParaAdjust",
105                 new Integer( com.sun.star.style.ParagraphAdjust.CENTER_value ) );
106             System.out.println( "set the paragraph adjust to LEFT" );
107 
108             xPropertySet.setPropertyValue("ParaFirstLineIndent", new Integer( 0 ) );
109             System.out.println( "set the first line indent to 0 cm" );
110 
111             xPropertySet.setPropertyValue("BreakType",
112                 com.sun.star.style.BreakType.PAGE_AFTER );
113             System.out.println( "set the paragraph attribute Breaktype to PageAfter" );
114 
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");
118 
119             // get the Textrange from the document
120             com.sun.star.text.XTextRange xTextRange =
121                 xTextDocument.getText().getStart();
122 
123             // get the PropertySet from the current paragraph
124             com.sun.star.beans.XPropertySet xParagraphPropertySet =
125                 (com.sun.star.beans.XPropertySet)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                 new String( "myheading" ) );
133             System.out.println( "apply the new paragraph style");
134         }
135         catch( Exception e) {
136             e.printStackTrace(System.err);
137             System.exit(1);
138         }
139 
140         System.out.println("done");
141 
142         System.exit(0);
143     }
144 
145 
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;
149 
150         try {
151             com.sun.star.uno.XComponentContext xContext = null;
152 
153             // get the remote office component context
154             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
155 
156             // get the remote office service manager
157             xMCF = xContext.getServiceManager();
158             if( xMCF != null ) {
159                 System.out.println("Connected to a running office ...");
160 
161                 Object oDesktop = xMCF.createInstanceWithContext(
162                     "com.sun.star.frame.Desktop", xContext);
163                 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
164                     com.sun.star.frame.XDesktop.class, oDesktop);
165             }
166             else
167                 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
168         }
169         catch( Exception e) {
170             e.printStackTrace(System.err);
171             System.exit(1);
172         }
173 
174 
175         return xDesktop;
176     }
177 
178     public static com.sun.star.text.XTextDocument createTextdocument(
179         com.sun.star.frame.XDesktop xDesktop )
180     {
181         com.sun.star.text.XTextDocument aTextDocument = null;
182 
183         try {
184             com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
185                                                                         "swriter");
186             aTextDocument = (com.sun.star.text.XTextDocument)
187                 UnoRuntime.queryInterface(
188                     com.sun.star.text.XTextDocument.class, xComponent);
189         }
190         catch( Exception e) {
191             e.printStackTrace(System.err);
192         }
193 
194         return aTextDocument;
195     }
196 
197 
198     protected static com.sun.star.lang.XComponent CreateNewDocument(
199         com.sun.star.frame.XDesktop xDesktop,
200         String sDocumentType )
201     {
202         String sURL = "private:factory/" + sDocumentType;
203 
204         com.sun.star.lang.XComponent xComponent = null;
205         com.sun.star.frame.XComponentLoader xComponentLoader = null;
206         com.sun.star.beans.PropertyValue xValues[] =
207             new com.sun.star.beans.PropertyValue[1];
208         com.sun.star.beans.PropertyValue xEmptyArgs[] =
209             new com.sun.star.beans.PropertyValue[0];
210 
211         try {
212             xComponentLoader = (com.sun.star.frame.XComponentLoader)
213                 UnoRuntime.queryInterface(
214                     com.sun.star.frame.XComponentLoader.class, xDesktop);
215 
216             xComponent  = xComponentLoader.loadComponentFromURL(
217                 sURL, "_blank", 0, xEmptyArgs);
218         }
219         catch( Exception e) {
220             e.printStackTrace(System.err);
221         }
222 
223         return xComponent ;
224     }
225 }
226 
227