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