1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
37 // comment: Step 1: get the Desktop object from the office
38 // Step 2: open an empty text document
39 // Step 3: enter an example text
40 // Step 4: use the paragraph collection
41 // Step 5: apply a different paragraph style on the paragraphs
44 import com
.sun
.star
.uno
.UnoRuntime
;
45 import com
.sun
.star
.uno
.AnyConverter
;
47 public class StyleInitialization
{
49 public static void main(String args
[]) {
50 // You need the desktop to create a document
51 // The getDesktop method does the UNO bootstrapping, gets the
52 // remote service manager and the desktop object.
53 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
54 xDesktop
= getDesktop();
57 // BEGIN: 'Style basics' Section from the Tutorial
59 // create text document
60 com
.sun
.star
.text
.XTextDocument xTextDocument
= null;
61 xTextDocument
= createTextdocument( xDesktop
);
63 // the text interface contains all methods and properties to
64 // manipulate the content from a text document
65 com
.sun
.star
.text
.XText xText
= null;
66 xText
= xTextDocument
.getText();
68 String sMyText
= "A very short paragraph for illustration only";
70 // you can travel with the cursor through the text document.
71 // you travel only at the model, not at the view. The cursor that you can
72 // see on the document doesn't change the position
73 com
.sun
.star
.text
.XTextCursor xTextCursor
= null;
74 xTextCursor
= xTextDocument
.getText().createTextCursor();
76 com
.sun
.star
.beans
.XPropertySet oCPS
= UnoRuntime
.queryInterface(
77 com
.sun
.star
.beans
.XPropertySet
.class, xTextCursor
);
79 oCPS
.setPropertyValue("CharFontName","Helvetica");
81 catch (Exception ex
) {
85 xText
.insertString( xTextCursor
, "Headline", false );
88 oCPS
.setPropertyValue("CharFontName","Times");
90 catch (Exception ex
) {
93 xText
.insertControlCharacter(xTextCursor
,
94 com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false);
96 xText
.insertString( xTextCursor
, sMyText
, false );
98 com
.sun
.star
.text
.XTextRange xTextRange
= null;
99 com
.sun
.star
.beans
.XPropertySet xPropertySet
= null;
101 // the text range not the cursor contains the 'parastyle' property
102 xTextRange
= xText
.getEnd();
103 xPropertySet
= UnoRuntime
.queryInterface(
104 com
.sun
.star
.beans
.XPropertySet
.class, xTextRange
);
106 // To run the sample with StarOffice 5.2 you'll have to change
107 // 'ParaStyleName' to 'ParaStyle' in the next line
108 System
.out
.println( "Current Parastyle : "
109 + xPropertySet
.getPropertyValue("ParaStyleName") );
111 // END: 'Style basics' Section from the Tutorial
113 // There are two way to travel through the paragraphs, with a
114 // paragraph cursor, or an enumeration.
115 // You find both ways in this example
117 // The first way, with the paragraph cursor
118 com
.sun
.star
.text
.XParagraphCursor xParagraphCursor
= null;
119 xParagraphCursor
= UnoRuntime
.queryInterface(
120 com
.sun
.star
.text
.XParagraphCursor
.class, xTextRange
);
122 xParagraphCursor
.gotoStart( false );
123 xParagraphCursor
.gotoEndOfParagraph( true );
125 // The second way, with the paragraph enumeration
126 com
.sun
.star
.container
.XEnumerationAccess xEnumerationAccess
= null;
127 xEnumerationAccess
= UnoRuntime
.queryInterface(
128 com
.sun
.star
.container
.XEnumerationAccess
.class, xText
);
130 // the enumeration contains all paragraph from the document
131 com
.sun
.star
.container
.XEnumeration xParagraphEnumeration
= null;
132 xParagraphEnumeration
= xEnumerationAccess
.createEnumeration();
134 com
.sun
.star
.text
.XTextContent xParagraph
= null;
135 com
.sun
.star
.text
.XTextRange xWord
= null;
137 com
.sun
.star
.container
.XEnumerationAccess xParaEnumerationAccess
= null;
138 com
.sun
.star
.container
.XEnumeration xPortionEnumeration
= null;
140 // check if a paragraph is available
141 while ( xParagraphEnumeration
.hasMoreElements() ) {
142 // get the next paragraph
143 xParagraph
= UnoRuntime
.queryInterface(
144 com
.sun
.star
.text
.XTextContent
.class,
145 xParagraphEnumeration
.nextElement());
147 // you need the method getAnchor to a TextRange -> to manipulate
149 String sText
= xParagraph
.getAnchor().getString();
151 // create a cursor from this paragraph
152 com
.sun
.star
.text
.XTextCursor xParaCursor
= null;
153 xParaCursor
= xParagraph
.getAnchor().getText().createTextCursor();
155 // goto the start and end of the paragraph
156 xParaCursor
.gotoStart( false );
157 xParaCursor
.gotoEnd( true );
159 // The enumeration from the paragraphs contain parts from the
160 // paragraph with a different attributes.
161 xParaEnumerationAccess
= UnoRuntime
.queryInterface(
162 com
.sun
.star
.container
.XEnumerationAccess
.class, xParagraph
);
163 xPortionEnumeration
= xParaEnumerationAccess
.createEnumeration();
165 while ( xPortionEnumeration
.hasMoreElements() ) {
166 // output of all parts from the paragraph with different attributes
167 xWord
= UnoRuntime
.queryInterface(
168 com
.sun
.star
.text
.XTextRange
.class,
169 xPortionEnumeration
.nextElement());
170 String sWordString
= xWord
.getString();
171 System
.out
.println( "Content of the paragraph : " + sWordString
);
175 // BEGIN: 'Finding a suitable style' Section from the Tutorial
177 // create a supplier to get the styles-collection
178 com
.sun
.star
.style
.XStyleFamiliesSupplier xSupplier
= null;
179 xSupplier
= UnoRuntime
.queryInterface(
180 com
.sun
.star
.style
.XStyleFamiliesSupplier
.class, xTextDocument
);
182 // use the name access from the collection
183 com
.sun
.star
.container
.XNameAccess xNameAccess
= null;
184 xNameAccess
= xSupplier
.getStyleFamilies();
186 com
.sun
.star
.container
.XNameContainer xParaStyleCollection
= null;
187 xParaStyleCollection
= UnoRuntime
.queryInterface(
188 com
.sun
.star
.container
.XNameContainer
.class, xNameAccess
.getByName( "ParagraphStyles" ));
190 // create an array from strings with the name of all paragraph styles from the text document
191 String
[] sElementNames
= xParaStyleCollection
.getElementNames();
192 int iElementCount
= sElementNames
.length
;
194 for( int iCounter
= 0; iCounter
< iElementCount
; iCounter
++ ) {
195 // specify one paragraph style
196 com
.sun
.star
.style
.XStyle xStyle
= null;
197 xStyle
= UnoRuntime
.queryInterface(
198 com
.sun
.star
.style
.XStyle
.class,
199 xParaStyleCollection
.getByName( sElementNames
[iCounter
] ));
201 // create a property set of all properties from the style
202 xPropertySet
= UnoRuntime
.queryInterface(
203 com
.sun
.star
.beans
.XPropertySet
.class, xStyle
);
205 String sFontname
= AnyConverter
.toString(xPropertySet
.getPropertyValue("CharFontName"));
206 sFontname
= sFontname
.toLowerCase();
208 // if the style use the font 'Albany', apply it to the current paragraph
209 if( sFontname
.equals("albany") ) {
210 // create a property set from the current paragraph, to change the paragraph style
211 xPropertySet
= UnoRuntime
.queryInterface(
212 com
.sun
.star
.beans
.XPropertySet
.class, xTextRange
);
214 // To run the sample with StarOffice 5.2 you'll have to change 'ParaStyleName'
215 // to 'ParaStyle' in the next line
216 xPropertySet
.setPropertyValue("ParaStyleName", sElementNames
[iCounter
] );
217 System
.out
.println( "Apply the paragraph style : " + sElementNames
[iCounter
] );
221 // END: 'Finding a suitable style' Section from the Tutorial
223 catch( Exception e
) {
224 e
.printStackTrace(System
.err
);
228 System
.out
.println("Done");
234 public static com
.sun
.star
.frame
.XDesktop
getDesktop() {
235 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
236 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
= null;
239 com
.sun
.star
.uno
.XComponentContext xContext
= null;
241 // get the remote office component context
242 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
244 // get the remote office service manager
245 xMCF
= xContext
.getServiceManager();
247 System
.out
.println("Connected to a running office ...");
249 Object oDesktop
= xMCF
.createInstanceWithContext(
250 "com.sun.star.frame.Desktop", xContext
);
251 xDesktop
= UnoRuntime
.queryInterface(
252 com
.sun
.star
.frame
.XDesktop
.class, oDesktop
);
255 System
.out
.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
257 catch( Exception e
) {
258 e
.printStackTrace(System
.err
);
266 public static com
.sun
.star
.text
.XTextDocument
createTextdocument(
267 com
.sun
.star
.frame
.XDesktop xDesktop
)
269 com
.sun
.star
.text
.XTextDocument aTextDocument
= null;
272 com
.sun
.star
.lang
.XComponent xComponent
= CreateNewDocument(xDesktop
,
274 aTextDocument
= UnoRuntime
.queryInterface(
275 com
.sun
.star
.text
.XTextDocument
.class, xComponent
);
277 catch( Exception e
) {
278 e
.printStackTrace(System
.err
);
281 return aTextDocument
;
285 protected static com
.sun
.star
.lang
.XComponent
CreateNewDocument(
286 com
.sun
.star
.frame
.XDesktop xDesktop
,
287 String sDocumentType
)
289 String sURL
= "private:factory/" + sDocumentType
;
291 com
.sun
.star
.lang
.XComponent xComponent
= null;
292 com
.sun
.star
.frame
.XComponentLoader xComponentLoader
= null;
293 com
.sun
.star
.beans
.PropertyValue xEmptyArgs
[] =
294 new com
.sun
.star
.beans
.PropertyValue
[0];
297 xComponentLoader
= UnoRuntime
.queryInterface(
298 com
.sun
.star
.frame
.XComponentLoader
.class, xDesktop
);
300 xComponent
= xComponentLoader
.loadComponentFromURL(
301 sURL
, "_blank", 0, xEmptyArgs
);
303 catch( Exception e
) {
304 e
.printStackTrace(System
.err
);
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */