1 /*************************************************************************
3 * $RCSfile: StyleInitialization.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:18:02 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 //***************************************************************************
42 // comment: Step 1: get the Desktop object from the office
43 // Step 2: open an empty text document
44 // Step 3: enter a example text
45 // Step 4: use the paragraph collection
46 // Step 5: apply a different paragraph style on the paragraphs
47 //***************************************************************************
49 import com
.sun
.star
.uno
.UnoRuntime
;
50 import com
.sun
.star
.uno
.AnyConverter
;
52 public class StyleInitialization
{
54 public static void main(String args
[]) {
55 // You need the desktop to create a document
56 // The getDesktop method does the UNO bootstrapping, gets the
57 // remote servie manager and the desktop object.
58 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
59 xDesktop
= getDesktop();
62 // BEGIN: 'Style basics' Section from the Tutorial
64 // create text document
65 com
.sun
.star
.text
.XTextDocument xTextDocument
= null;
66 xTextDocument
= createTextdocument( xDesktop
);
68 // the text interface contains all methods and properties to
69 // manipulate the content from a text document
70 com
.sun
.star
.text
.XText xText
= null;
71 xText
= xTextDocument
.getText();
73 String sMyText
= "A very short paragraph for illustration only";
75 // you can travel with the cursor throught the text document.
76 // you travel only at the model, not at the view. The cursor that you can
77 // see on the document doesn't change the position
78 com
.sun
.star
.text
.XTextCursor xTextCursor
= null;
79 xTextCursor
= (com
.sun
.star
.text
.XTextCursor
)
80 xTextDocument
.getText().createTextCursor();
82 com
.sun
.star
.beans
.XPropertySet oCPS
= (com
.sun
.star
.beans
.XPropertySet
)
83 UnoRuntime
.queryInterface(
84 com
.sun
.star
.beans
.XPropertySet
.class, xTextCursor
);
86 oCPS
.setPropertyValue("CharFontName","Helvetica");
88 catch (Exception ex
) {
92 xText
.insertString( xTextCursor
, "Headline", false );
95 oCPS
.setPropertyValue("CharFontName","Times");
97 catch (Exception ex
) {
100 xText
.insertControlCharacter(xTextCursor
,
101 com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false);
103 xText
.insertString( xTextCursor
, sMyText
, false );
105 com
.sun
.star
.text
.XTextRange xTextRange
= null;
106 com
.sun
.star
.beans
.XPropertySet xPropertySet
= null;
108 // the text range not the cursor contains the 'parastyle' property
109 xTextRange
= xText
.getEnd();
110 xPropertySet
= (com
.sun
.star
.beans
.XPropertySet
)
111 UnoRuntime
.queryInterface(
112 com
.sun
.star
.beans
.XPropertySet
.class, xTextRange
);
114 // To run the sample with StarOffice 5.2 you'll have to change
115 // 'ParaStyleName' to 'ParaStyle' in the next line
116 System
.out
.println( "Current Parastyle : "
117 + xPropertySet
.getPropertyValue("ParaStyleName") );
119 // END: 'Style basics' Section from the Tutorial
121 // There are two way to travel throught the paragraphs, with a
122 // paragraph cursor, or a enumeration.
123 // You find both ways in this example
125 // The first way, with the paragraph cursor
126 com
.sun
.star
.text
.XParagraphCursor xParagraphCursor
= null;
127 xParagraphCursor
= (com
.sun
.star
.text
.XParagraphCursor
)
128 UnoRuntime
.queryInterface(
129 com
.sun
.star
.text
.XParagraphCursor
.class, xTextRange
);
131 xParagraphCursor
.gotoStart( false );
132 xParagraphCursor
.gotoEndOfParagraph( true );
134 // The second way, with the paragraph enumeration
135 com
.sun
.star
.container
.XEnumerationAccess xEnumerationAccess
= null;
136 xEnumerationAccess
= (com
.sun
.star
.container
.XEnumerationAccess
)
137 UnoRuntime
.queryInterface(
138 com
.sun
.star
.container
.XEnumerationAccess
.class, xText
);
140 // the enumeration contains all paragraph form the document
141 com
.sun
.star
.container
.XEnumeration xParagraphEnumeration
= null;
142 xParagraphEnumeration
= xEnumerationAccess
.createEnumeration();
144 com
.sun
.star
.text
.XTextContent xParagraph
= null;
145 com
.sun
.star
.text
.XTextRange xWord
= null;
147 com
.sun
.star
.container
.XEnumerationAccess xParaEnumerationAccess
= null;
148 com
.sun
.star
.container
.XEnumeration xPortionEnumeration
= null;
150 // check if a paragraph is available
151 while ( xParagraphEnumeration
.hasMoreElements() ) {
152 // get the next paragraph
153 xParagraph
= (com
.sun
.star
.text
.XTextContent
)
154 UnoRuntime
.queryInterface(
155 com
.sun
.star
.text
.XTextContent
.class,
156 xParagraphEnumeration
.nextElement());
158 // you need the method getAnchor to a TextRange -> to manipulate
160 String sText
= xParagraph
.getAnchor().getString();
162 // create a cursor from this paragraph
163 com
.sun
.star
.text
.XTextCursor xParaCursor
= null;
164 xParaCursor
= xParagraph
.getAnchor().getText().createTextCursor();
166 // goto the start and end of the paragraph
167 xParaCursor
.gotoStart( false );
168 xParaCursor
.gotoEnd( true );
170 // The enumeration from the paragraphs contain parts from the
171 // paragraph with a different attributes.
172 xParaEnumerationAccess
= (com
.sun
.star
.container
.XEnumerationAccess
)
173 UnoRuntime
.queryInterface(
174 com
.sun
.star
.container
.XEnumerationAccess
.class, xParagraph
);
175 xPortionEnumeration
= xParaEnumerationAccess
.createEnumeration();
177 while ( xPortionEnumeration
.hasMoreElements() ) {
178 // output of all parts from the paragraph with different attributes
179 xWord
= (com
.sun
.star
.text
.XTextRange
) UnoRuntime
.queryInterface(
180 com
.sun
.star
.text
.XTextRange
.class,
181 xPortionEnumeration
.nextElement());
182 String sWordString
= xWord
.getString();
183 System
.out
.println( "Content of the paragraph : " + sWordString
);
187 // BEGIN: 'Finding a suitable style' Section from the Tutorial
189 // craete a supplier to get the styles-collection
190 com
.sun
.star
.style
.XStyleFamiliesSupplier xSupplier
= null;
191 xSupplier
= ( com
.sun
.star
.style
.XStyleFamiliesSupplier
) UnoRuntime
.queryInterface(
192 com
.sun
.star
.style
.XStyleFamiliesSupplier
.class, xTextDocument
);
194 // use the name access from the collection
195 com
.sun
.star
.container
.XNameAccess xNameAccess
= null;
196 xNameAccess
= xSupplier
.getStyleFamilies();
198 com
.sun
.star
.container
.XNameContainer xParaStyleCollection
= null;
199 xParaStyleCollection
= (com
.sun
.star
.container
.XNameContainer
) UnoRuntime
.queryInterface(
200 com
.sun
.star
.container
.XNameContainer
.class, xNameAccess
.getByName( "ParagraphStyles" ));
202 // create a array from strings with the name of all paragraph styles from the text document
203 String
[] sElementNames
= xParaStyleCollection
.getElementNames();
204 int iElementCount
= sElementNames
.length
;
206 for( int iCounter
= 0; iCounter
< iElementCount
; iCounter
++ ) {
207 // specify one paragraph style
208 com
.sun
.star
.style
.XStyle xStyle
= null;
209 xStyle
= (com
.sun
.star
.style
.XStyle
) UnoRuntime
.queryInterface(
210 com
.sun
.star
.style
.XStyle
.class,
211 xParaStyleCollection
.getByName( sElementNames
[iCounter
] ));
213 // create a property set of all properties from the style
214 xPropertySet
= (com
.sun
.star
.beans
.XPropertySet
) UnoRuntime
.queryInterface(
215 com
.sun
.star
.beans
.XPropertySet
.class, xStyle
);
217 AnyConverter aAnyConv
= new AnyConverter();
218 String sFontname
= aAnyConv
.toString(xPropertySet
.getPropertyValue("CharFontName"));
219 sFontname
= sFontname
.toLowerCase();
221 // if the style use the font 'Albany', apply it to the current paragraph
222 if( sFontname
.compareTo("albany") == 0 ) {
223 // create a property set from the current paragraph, to change the paragraph style
224 xPropertySet
= (com
.sun
.star
.beans
.XPropertySet
) UnoRuntime
.queryInterface(
225 com
.sun
.star
.beans
.XPropertySet
.class, xTextRange
);
227 // To run the sample with StarOffice 5.2 you'll have to change 'ParaStyleName'
228 // to 'ParaStyle' in the next line
229 xPropertySet
.setPropertyValue("ParaStyleName", new String( sElementNames
[iCounter
] ) );
230 System
.out
.println( "Apply the paragraph style : " + sElementNames
[iCounter
] );
234 // END: 'Finding a suitable style' Section from the Tutorial
236 catch( Exception e
) {
237 e
.printStackTrace(System
.err
);
241 System
.out
.println("Done");
247 public static com
.sun
.star
.frame
.XDesktop
getDesktop() {
248 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
249 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
= null;
252 com
.sun
.star
.uno
.XComponentContext xContext
= null;
254 // get the remote office component context
255 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
257 // get the remote office service manager
258 xMCF
= xContext
.getServiceManager();
260 System
.out
.println("Connected to a running office ...");
262 Object oDesktop
= xMCF
.createInstanceWithContext(
263 "com.sun.star.frame.Desktop", xContext
);
264 xDesktop
= (com
.sun
.star
.frame
.XDesktop
) UnoRuntime
.queryInterface(
265 com
.sun
.star
.frame
.XDesktop
.class, oDesktop
);
268 System
.out
.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
270 catch( Exception e
) {
271 e
.printStackTrace(System
.err
);
279 public static com
.sun
.star
.text
.XTextDocument
createTextdocument(
280 com
.sun
.star
.frame
.XDesktop xDesktop
)
282 com
.sun
.star
.text
.XTextDocument aTextDocument
= null;
285 com
.sun
.star
.lang
.XComponent xComponent
= CreateNewDocument(xDesktop
,
287 aTextDocument
= (com
.sun
.star
.text
.XTextDocument
)
288 UnoRuntime
.queryInterface(
289 com
.sun
.star
.text
.XTextDocument
.class, xComponent
);
291 catch( Exception e
) {
292 e
.printStackTrace(System
.err
);
295 return aTextDocument
;
299 protected static com
.sun
.star
.lang
.XComponent
CreateNewDocument(
300 com
.sun
.star
.frame
.XDesktop xDesktop
,
301 String sDocumentType
)
303 String sURL
= "private:factory/" + sDocumentType
;
305 com
.sun
.star
.lang
.XComponent xComponent
= null;
306 com
.sun
.star
.frame
.XComponentLoader xComponentLoader
= null;
307 com
.sun
.star
.beans
.PropertyValue xValues
[] =
308 new com
.sun
.star
.beans
.PropertyValue
[1];
309 com
.sun
.star
.beans
.PropertyValue xEmptyArgs
[] =
310 new com
.sun
.star
.beans
.PropertyValue
[0];
313 xComponentLoader
= (com
.sun
.star
.frame
.XComponentLoader
)
314 UnoRuntime
.queryInterface(
315 com
.sun
.star
.frame
.XComponentLoader
.class, xDesktop
);
317 xComponent
= xComponentLoader
.loadComponentFromURL(
318 sURL
, "_blank", 0, xEmptyArgs
);
320 catch( Exception e
) {
321 e
.printStackTrace(System
.err
);