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