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