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: enter a example text
39*cdf0e10cSrcweir //          Step 4: use the paragraph collection
40*cdf0e10cSrcweir //          Step 5: apply a different paragraph style on the paragraphs
41*cdf0e10cSrcweir //***************************************************************************
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir public class StyleInitialization {
47*cdf0e10cSrcweir 
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             // BEGIN: 'Style basics' Section from the Tutorial
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir             // create text document
59*cdf0e10cSrcweir             com.sun.star.text.XTextDocument xTextDocument = null;
60*cdf0e10cSrcweir             xTextDocument = createTextdocument( xDesktop );
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir             // the text interface contains all methods and properties to
63*cdf0e10cSrcweir             // manipulate the content from a text document
64*cdf0e10cSrcweir             com.sun.star.text.XText xText = null;
65*cdf0e10cSrcweir             xText = xTextDocument.getText();
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir             String sMyText = "A very short paragraph for illustration only";
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir             // you can travel with the cursor throught the text document.
70*cdf0e10cSrcweir             // you travel only at the model, not at the view. The cursor that you can
71*cdf0e10cSrcweir             // see on the document doesn't change the position
72*cdf0e10cSrcweir             com.sun.star.text.XTextCursor xTextCursor = null;
73*cdf0e10cSrcweir             xTextCursor = (com.sun.star.text.XTextCursor)
74*cdf0e10cSrcweir                 xTextDocument.getText().createTextCursor();
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir             com.sun.star.beans.XPropertySet oCPS = (com.sun.star.beans.XPropertySet)
77*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
78*cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet.class, xTextCursor);
79*cdf0e10cSrcweir             try {
80*cdf0e10cSrcweir                 oCPS.setPropertyValue("CharFontName","Helvetica");
81*cdf0e10cSrcweir             }
82*cdf0e10cSrcweir             catch (Exception ex) {
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir             }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir             xText.insertString( xTextCursor, "Headline", false );
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir             try {
89*cdf0e10cSrcweir                 oCPS.setPropertyValue("CharFontName","Times");
90*cdf0e10cSrcweir             }
91*cdf0e10cSrcweir             catch (Exception ex) {
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir             }
94*cdf0e10cSrcweir             xText.insertControlCharacter(xTextCursor,
95*cdf0e10cSrcweir                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir             xText.insertString( xTextCursor, sMyText, false );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir             com.sun.star.text.XTextRange xTextRange = null;
100*cdf0e10cSrcweir             com.sun.star.beans.XPropertySet xPropertySet = null;
101*cdf0e10cSrcweir 
102*cdf0e10cSrcweir             // the text range not the cursor contains the 'parastyle' property
103*cdf0e10cSrcweir             xTextRange = xText.getEnd();
104*cdf0e10cSrcweir             xPropertySet = (com.sun.star.beans.XPropertySet)
105*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
106*cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet.class, xTextRange );
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir             // To run the sample with StarOffice 5.2 you'll have to change
109*cdf0e10cSrcweir             // 'ParaStyleName' to 'ParaStyle' in the next line
110*cdf0e10cSrcweir             System.out.println( "Current Parastyle : "
111*cdf0e10cSrcweir                                 + xPropertySet.getPropertyValue("ParaStyleName") );
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir             // END: 'Style basics' Section from the Tutorial
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir             // There are two way to travel throught the paragraphs, with a
116*cdf0e10cSrcweir             // paragraph cursor, or a enumeration.
117*cdf0e10cSrcweir             // You find both ways in this example
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir             // The first way, with the paragraph cursor
120*cdf0e10cSrcweir             com.sun.star.text.XParagraphCursor xParagraphCursor = null;
121*cdf0e10cSrcweir             xParagraphCursor = (com.sun.star.text.XParagraphCursor)
122*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
123*cdf0e10cSrcweir                     com.sun.star.text.XParagraphCursor.class, xTextRange );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir             xParagraphCursor.gotoStart( false );
126*cdf0e10cSrcweir             xParagraphCursor.gotoEndOfParagraph( true );
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir             // The second way, with the paragraph enumeration
129*cdf0e10cSrcweir             com.sun.star.container.XEnumerationAccess xEnumerationAccess = null;
130*cdf0e10cSrcweir             xEnumerationAccess = (com.sun.star.container.XEnumerationAccess)
131*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
132*cdf0e10cSrcweir                     com.sun.star.container.XEnumerationAccess.class, xText );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir             // the enumeration contains all paragraph form the document
135*cdf0e10cSrcweir             com.sun.star.container.XEnumeration xParagraphEnumeration = null;
136*cdf0e10cSrcweir             xParagraphEnumeration = xEnumerationAccess.createEnumeration();
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir             com.sun.star.text.XTextContent xParagraph = null;
139*cdf0e10cSrcweir             com.sun.star.text.XTextRange xWord = null;
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir             com.sun.star.container.XEnumerationAccess xParaEnumerationAccess = null;
142*cdf0e10cSrcweir             com.sun.star.container.XEnumeration xPortionEnumeration = null;
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir             // check if a paragraph is available
145*cdf0e10cSrcweir             while ( xParagraphEnumeration.hasMoreElements() ) {
146*cdf0e10cSrcweir                 // get the next paragraph
147*cdf0e10cSrcweir                 xParagraph = (com.sun.star.text.XTextContent)
148*cdf0e10cSrcweir                     UnoRuntime.queryInterface(
149*cdf0e10cSrcweir                         com.sun.star.text.XTextContent.class,
150*cdf0e10cSrcweir                         xParagraphEnumeration.nextElement());
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir                 // you need the method getAnchor to a TextRange -> to manipulate
153*cdf0e10cSrcweir                 // the paragraph
154*cdf0e10cSrcweir                 String sText = xParagraph.getAnchor().getString();
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir                 // create a cursor from this paragraph
157*cdf0e10cSrcweir                 com.sun.star.text.XTextCursor xParaCursor = null;
158*cdf0e10cSrcweir                 xParaCursor = xParagraph.getAnchor().getText().createTextCursor();
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir                 // goto the start and end of the paragraph
161*cdf0e10cSrcweir                 xParaCursor.gotoStart( false );
162*cdf0e10cSrcweir                 xParaCursor.gotoEnd( true );
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir                 // The enumeration from the paragraphs contain parts from the
165*cdf0e10cSrcweir                 // paragraph with a different attributes.
166*cdf0e10cSrcweir                 xParaEnumerationAccess = (com.sun.star.container.XEnumerationAccess)
167*cdf0e10cSrcweir                     UnoRuntime.queryInterface(
168*cdf0e10cSrcweir                         com.sun.star.container.XEnumerationAccess.class, xParagraph);
169*cdf0e10cSrcweir                 xPortionEnumeration = xParaEnumerationAccess.createEnumeration();
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir                 while ( xPortionEnumeration.hasMoreElements() ) {
172*cdf0e10cSrcweir                     // output of all parts from the paragraph with different attributes
173*cdf0e10cSrcweir                     xWord = (com.sun.star.text.XTextRange) UnoRuntime.queryInterface(
174*cdf0e10cSrcweir                         com.sun.star.text.XTextRange.class,
175*cdf0e10cSrcweir                         xPortionEnumeration.nextElement());
176*cdf0e10cSrcweir                     String sWordString = xWord.getString();
177*cdf0e10cSrcweir                     System.out.println( "Content of the paragraph : " + sWordString );
178*cdf0e10cSrcweir                 }
179*cdf0e10cSrcweir             }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir             // BEGIN: 'Finding a suitable style' Section from the Tutorial
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir             // craete a supplier to get the styles-collection
184*cdf0e10cSrcweir             com.sun.star.style.XStyleFamiliesSupplier xSupplier = null;
185*cdf0e10cSrcweir             xSupplier = ( com.sun.star.style.XStyleFamiliesSupplier ) UnoRuntime.queryInterface(
186*cdf0e10cSrcweir                 com.sun.star.style.XStyleFamiliesSupplier.class, xTextDocument );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir             // use the name access from the collection
189*cdf0e10cSrcweir             com.sun.star.container.XNameAccess xNameAccess = null;
190*cdf0e10cSrcweir             xNameAccess = xSupplier.getStyleFamilies();
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir             com.sun.star.container.XNameContainer xParaStyleCollection = null;
193*cdf0e10cSrcweir             xParaStyleCollection = (com.sun.star.container.XNameContainer) UnoRuntime.queryInterface(
194*cdf0e10cSrcweir                 com.sun.star.container.XNameContainer.class, xNameAccess.getByName( "ParagraphStyles" ));
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir             // create a array from strings with the name of all paragraph styles from the text document
197*cdf0e10cSrcweir             String[] sElementNames = xParaStyleCollection.getElementNames();
198*cdf0e10cSrcweir             int iElementCount = sElementNames.length;
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir             for( int iCounter = 0;  iCounter < iElementCount; iCounter++ ) {
201*cdf0e10cSrcweir                 // specify one paragraph style
202*cdf0e10cSrcweir                 com.sun.star.style.XStyle xStyle = null;
203*cdf0e10cSrcweir                 xStyle = (com.sun.star.style.XStyle) UnoRuntime.queryInterface(
204*cdf0e10cSrcweir                     com.sun.star.style.XStyle.class,
205*cdf0e10cSrcweir                     xParaStyleCollection.getByName( sElementNames[iCounter] ));
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir                 // create a property set of all properties from the style
208*cdf0e10cSrcweir                 xPropertySet = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
209*cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet.class, xStyle );
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir                 AnyConverter aAnyConv = new AnyConverter();
212*cdf0e10cSrcweir                 String sFontname = aAnyConv.toString(xPropertySet.getPropertyValue("CharFontName"));
213*cdf0e10cSrcweir                 sFontname = sFontname.toLowerCase();
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir                 // if the style use the font 'Albany', apply it to the current paragraph
216*cdf0e10cSrcweir                 if( sFontname.compareTo("albany") == 0 ) {
217*cdf0e10cSrcweir                     // create a property set from the current paragraph, to change the paragraph style
218*cdf0e10cSrcweir                     xPropertySet = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(
219*cdf0e10cSrcweir                         com.sun.star.beans.XPropertySet.class, xTextRange );
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir                     // To run the sample with StarOffice 5.2 you'll have to change 'ParaStyleName'
222*cdf0e10cSrcweir                     // to 'ParaStyle' in the next line
223*cdf0e10cSrcweir                     xPropertySet.setPropertyValue("ParaStyleName", new String( sElementNames[iCounter] ) );
224*cdf0e10cSrcweir                     System.out.println( "Apply the paragraph style : " + sElementNames[iCounter] );
225*cdf0e10cSrcweir                     break;
226*cdf0e10cSrcweir                 }
227*cdf0e10cSrcweir             }
228*cdf0e10cSrcweir             // END: 'Finding a suitable style' Section from the Tutorial
229*cdf0e10cSrcweir         }
230*cdf0e10cSrcweir         catch( Exception e) {
231*cdf0e10cSrcweir             e.printStackTrace(System.err);
232*cdf0e10cSrcweir         }
233*cdf0e10cSrcweir 
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir         System.out.println("Done");
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir         System.exit(0);
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir     }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir     public static com.sun.star.frame.XDesktop getDesktop() {
242*cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop = null;
243*cdf0e10cSrcweir         com.sun.star.lang.XMultiComponentFactory xMCF = null;
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir         try {
246*cdf0e10cSrcweir             com.sun.star.uno.XComponentContext xContext = null;
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir             // get the remote office component context
249*cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir             // get the remote office service manager
252*cdf0e10cSrcweir             xMCF = xContext.getServiceManager();
253*cdf0e10cSrcweir             if( xMCF != null ) {
254*cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir                 Object oDesktop = xMCF.createInstanceWithContext(
257*cdf0e10cSrcweir                     "com.sun.star.frame.Desktop", xContext);
258*cdf0e10cSrcweir                 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
259*cdf0e10cSrcweir                     com.sun.star.frame.XDesktop.class, oDesktop);
260*cdf0e10cSrcweir             }
261*cdf0e10cSrcweir             else
262*cdf0e10cSrcweir                 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
263*cdf0e10cSrcweir         }
264*cdf0e10cSrcweir         catch( Exception e) {
265*cdf0e10cSrcweir             e.printStackTrace(System.err);
266*cdf0e10cSrcweir             System.exit(1);
267*cdf0e10cSrcweir         }
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir         return xDesktop;
271*cdf0e10cSrcweir     }
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir     public static com.sun.star.text.XTextDocument createTextdocument(
274*cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop )
275*cdf0e10cSrcweir     {
276*cdf0e10cSrcweir         com.sun.star.text.XTextDocument aTextDocument = null;
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir         try {
279*cdf0e10cSrcweir             com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
280*cdf0e10cSrcweir                                                                         "swriter");
281*cdf0e10cSrcweir             aTextDocument = (com.sun.star.text.XTextDocument)
282*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
283*cdf0e10cSrcweir                     com.sun.star.text.XTextDocument.class, xComponent);
284*cdf0e10cSrcweir         }
285*cdf0e10cSrcweir         catch( Exception e) {
286*cdf0e10cSrcweir             e.printStackTrace(System.err);
287*cdf0e10cSrcweir         }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir         return aTextDocument;
290*cdf0e10cSrcweir     }
291*cdf0e10cSrcweir 
292*cdf0e10cSrcweir 
293*cdf0e10cSrcweir     protected static com.sun.star.lang.XComponent CreateNewDocument(
294*cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop,
295*cdf0e10cSrcweir         String sDocumentType )
296*cdf0e10cSrcweir     {
297*cdf0e10cSrcweir         String sURL = "private:factory/" + sDocumentType;
298*cdf0e10cSrcweir 
299*cdf0e10cSrcweir         com.sun.star.lang.XComponent xComponent = null;
300*cdf0e10cSrcweir         com.sun.star.frame.XComponentLoader xComponentLoader = null;
301*cdf0e10cSrcweir         com.sun.star.beans.PropertyValue xValues[] =
302*cdf0e10cSrcweir             new com.sun.star.beans.PropertyValue[1];
303*cdf0e10cSrcweir         com.sun.star.beans.PropertyValue xEmptyArgs[] =
304*cdf0e10cSrcweir             new com.sun.star.beans.PropertyValue[0];
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir         try {
307*cdf0e10cSrcweir             xComponentLoader = (com.sun.star.frame.XComponentLoader)
308*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
309*cdf0e10cSrcweir                     com.sun.star.frame.XComponentLoader.class, xDesktop);
310*cdf0e10cSrcweir 
311*cdf0e10cSrcweir             xComponent  = xComponentLoader.loadComponentFromURL(
312*cdf0e10cSrcweir                 sURL, "_blank", 0, xEmptyArgs);
313*cdf0e10cSrcweir         }
314*cdf0e10cSrcweir         catch( Exception e) {
315*cdf0e10cSrcweir             e.printStackTrace(System.err);
316*cdf0e10cSrcweir         }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir         return xComponent ;
319*cdf0e10cSrcweir     }
320*cdf0e10cSrcweir }
321