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