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