1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *************************************************************************/
36 // comment: Step 1: get the Desktop object from the office
37 // Step 2: open an empty text document
38 // Step 3: enter a example text
39 // Step 4: use the paragraph collection
40 // Step 5: apply a different paragraph style on the paragraphs
43 import com
.sun
.star
.uno
.UnoRuntime
;
44 import com
.sun
.star
.uno
.AnyConverter
;
46 public class StyleInitialization
{
48 public static void main(String args
[]) {
49 // You need the desktop to create a document
50 // The getDesktop method does the UNO bootstrapping, gets the
51 // remote servie manager and the desktop object.
52 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
53 xDesktop
= getDesktop();
56 // BEGIN: 'Style basics' Section from the Tutorial
58 // create text document
59 com
.sun
.star
.text
.XTextDocument xTextDocument
= null;
60 xTextDocument
= createTextdocument( xDesktop
);
62 // the text interface contains all methods and properties to
63 // manipulate the content from a text document
64 com
.sun
.star
.text
.XText xText
= null;
65 xText
= xTextDocument
.getText();
67 String sMyText
= "A very short paragraph for illustration only";
69 // you can travel with the cursor through the text document.
70 // you travel only at the model, not at the view. The cursor that you can
71 // see on the document doesn't change the position
72 com
.sun
.star
.text
.XTextCursor xTextCursor
= null;
73 xTextCursor
= xTextDocument
.getText().createTextCursor();
75 com
.sun
.star
.beans
.XPropertySet oCPS
= UnoRuntime
.queryInterface(
76 com
.sun
.star
.beans
.XPropertySet
.class, xTextCursor
);
78 oCPS
.setPropertyValue("CharFontName","Helvetica");
80 catch (Exception ex
) {
84 xText
.insertString( xTextCursor
, "Headline", false );
87 oCPS
.setPropertyValue("CharFontName","Times");
89 catch (Exception ex
) {
92 xText
.insertControlCharacter(xTextCursor
,
93 com
.sun
.star
.text
.ControlCharacter
.PARAGRAPH_BREAK
, false);
95 xText
.insertString( xTextCursor
, sMyText
, false );
97 com
.sun
.star
.text
.XTextRange xTextRange
= null;
98 com
.sun
.star
.beans
.XPropertySet xPropertySet
= null;
100 // the text range not the cursor contains the 'parastyle' property
101 xTextRange
= xText
.getEnd();
102 xPropertySet
= UnoRuntime
.queryInterface(
103 com
.sun
.star
.beans
.XPropertySet
.class, xTextRange
);
105 // To run the sample with StarOffice 5.2 you'll have to change
106 // 'ParaStyleName' to 'ParaStyle' in the next line
107 System
.out
.println( "Current Parastyle : "
108 + xPropertySet
.getPropertyValue("ParaStyleName") );
110 // END: 'Style basics' Section from the Tutorial
112 // There are two way to travel through the paragraphs, with a
113 // paragraph cursor, or a enumeration.
114 // You find both ways in this example
116 // The first way, with the paragraph cursor
117 com
.sun
.star
.text
.XParagraphCursor xParagraphCursor
= null;
118 xParagraphCursor
= UnoRuntime
.queryInterface(
119 com
.sun
.star
.text
.XParagraphCursor
.class, xTextRange
);
121 xParagraphCursor
.gotoStart( false );
122 xParagraphCursor
.gotoEndOfParagraph( true );
124 // The second way, with the paragraph enumeration
125 com
.sun
.star
.container
.XEnumerationAccess xEnumerationAccess
= null;
126 xEnumerationAccess
= UnoRuntime
.queryInterface(
127 com
.sun
.star
.container
.XEnumerationAccess
.class, xText
);
129 // the enumeration contains all paragraph form the document
130 com
.sun
.star
.container
.XEnumeration xParagraphEnumeration
= null;
131 xParagraphEnumeration
= xEnumerationAccess
.createEnumeration();
133 com
.sun
.star
.text
.XTextContent xParagraph
= null;
134 com
.sun
.star
.text
.XTextRange xWord
= null;
136 com
.sun
.star
.container
.XEnumerationAccess xParaEnumerationAccess
= null;
137 com
.sun
.star
.container
.XEnumeration xPortionEnumeration
= null;
139 // check if a paragraph is available
140 while ( xParagraphEnumeration
.hasMoreElements() ) {
141 // get the next paragraph
142 xParagraph
= UnoRuntime
.queryInterface(
143 com
.sun
.star
.text
.XTextContent
.class,
144 xParagraphEnumeration
.nextElement());
146 // you need the method getAnchor to a TextRange -> to manipulate
148 String sText
= xParagraph
.getAnchor().getString();
150 // create a cursor from this paragraph
151 com
.sun
.star
.text
.XTextCursor xParaCursor
= null;
152 xParaCursor
= xParagraph
.getAnchor().getText().createTextCursor();
154 // goto the start and end of the paragraph
155 xParaCursor
.gotoStart( false );
156 xParaCursor
.gotoEnd( true );
158 // The enumeration from the paragraphs contain parts from the
159 // paragraph with a different attributes.
160 xParaEnumerationAccess
= UnoRuntime
.queryInterface(
161 com
.sun
.star
.container
.XEnumerationAccess
.class, xParagraph
);
162 xPortionEnumeration
= xParaEnumerationAccess
.createEnumeration();
164 while ( xPortionEnumeration
.hasMoreElements() ) {
165 // output of all parts from the paragraph with different attributes
166 xWord
= UnoRuntime
.queryInterface(
167 com
.sun
.star
.text
.XTextRange
.class,
168 xPortionEnumeration
.nextElement());
169 String sWordString
= xWord
.getString();
170 System
.out
.println( "Content of the paragraph : " + sWordString
);
174 // BEGIN: 'Finding a suitable style' Section from the Tutorial
176 // craete a supplier to get the styles-collection
177 com
.sun
.star
.style
.XStyleFamiliesSupplier xSupplier
= null;
178 xSupplier
= UnoRuntime
.queryInterface(
179 com
.sun
.star
.style
.XStyleFamiliesSupplier
.class, xTextDocument
);
181 // use the name access from the collection
182 com
.sun
.star
.container
.XNameAccess xNameAccess
= null;
183 xNameAccess
= xSupplier
.getStyleFamilies();
185 com
.sun
.star
.container
.XNameContainer xParaStyleCollection
= null;
186 xParaStyleCollection
= UnoRuntime
.queryInterface(
187 com
.sun
.star
.container
.XNameContainer
.class, xNameAccess
.getByName( "ParagraphStyles" ));
189 // create a array from strings with the name of all paragraph styles from the text document
190 String
[] sElementNames
= xParaStyleCollection
.getElementNames();
191 int iElementCount
= sElementNames
.length
;
193 for( int iCounter
= 0; iCounter
< iElementCount
; iCounter
++ ) {
194 // specify one paragraph style
195 com
.sun
.star
.style
.XStyle xStyle
= null;
196 xStyle
= UnoRuntime
.queryInterface(
197 com
.sun
.star
.style
.XStyle
.class,
198 xParaStyleCollection
.getByName( sElementNames
[iCounter
] ));
200 // create a property set of all properties from the style
201 xPropertySet
= UnoRuntime
.queryInterface(
202 com
.sun
.star
.beans
.XPropertySet
.class, xStyle
);
204 String sFontname
= AnyConverter
.toString(xPropertySet
.getPropertyValue("CharFontName"));
205 sFontname
= sFontname
.toLowerCase();
207 // if the style use the font 'Albany', apply it to the current paragraph
208 if( sFontname
.equals("albany") ) {
209 // create a property set from the current paragraph, to change the paragraph style
210 xPropertySet
= UnoRuntime
.queryInterface(
211 com
.sun
.star
.beans
.XPropertySet
.class, xTextRange
);
213 // To run the sample with StarOffice 5.2 you'll have to change 'ParaStyleName'
214 // to 'ParaStyle' in the next line
215 xPropertySet
.setPropertyValue("ParaStyleName", sElementNames
[iCounter
] );
216 System
.out
.println( "Apply the paragraph style : " + sElementNames
[iCounter
] );
220 // END: 'Finding a suitable style' Section from the Tutorial
222 catch( Exception e
) {
223 e
.printStackTrace(System
.err
);
227 System
.out
.println("Done");
233 public static com
.sun
.star
.frame
.XDesktop
getDesktop() {
234 com
.sun
.star
.frame
.XDesktop xDesktop
= null;
235 com
.sun
.star
.lang
.XMultiComponentFactory xMCF
= null;
238 com
.sun
.star
.uno
.XComponentContext xContext
= null;
240 // get the remote office component context
241 xContext
= com
.sun
.star
.comp
.helper
.Bootstrap
.bootstrap();
243 // get the remote office service manager
244 xMCF
= xContext
.getServiceManager();
246 System
.out
.println("Connected to a running office ...");
248 Object oDesktop
= xMCF
.createInstanceWithContext(
249 "com.sun.star.frame.Desktop", xContext
);
250 xDesktop
= UnoRuntime
.queryInterface(
251 com
.sun
.star
.frame
.XDesktop
.class, oDesktop
);
254 System
.out
.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
256 catch( Exception e
) {
257 e
.printStackTrace(System
.err
);
265 public static com
.sun
.star
.text
.XTextDocument
createTextdocument(
266 com
.sun
.star
.frame
.XDesktop xDesktop
)
268 com
.sun
.star
.text
.XTextDocument aTextDocument
= null;
271 com
.sun
.star
.lang
.XComponent xComponent
= CreateNewDocument(xDesktop
,
273 aTextDocument
= UnoRuntime
.queryInterface(
274 com
.sun
.star
.text
.XTextDocument
.class, xComponent
);
276 catch( Exception e
) {
277 e
.printStackTrace(System
.err
);
280 return aTextDocument
;
284 protected static com
.sun
.star
.lang
.XComponent
CreateNewDocument(
285 com
.sun
.star
.frame
.XDesktop xDesktop
,
286 String sDocumentType
)
288 String sURL
= "private:factory/" + sDocumentType
;
290 com
.sun
.star
.lang
.XComponent xComponent
= null;
291 com
.sun
.star
.frame
.XComponentLoader xComponentLoader
= null;
292 com
.sun
.star
.beans
.PropertyValue xEmptyArgs
[] =
293 new com
.sun
.star
.beans
.PropertyValue
[0];
296 xComponentLoader
= UnoRuntime
.queryInterface(
297 com
.sun
.star
.frame
.XComponentLoader
.class, xDesktop
);
299 xComponent
= xComponentLoader
.loadComponentFromURL(
300 sURL
, "_blank", 0, xEmptyArgs
);
302 catch( Exception e
) {
303 e
.printStackTrace(System
.err
);