Update ooo320-m1
[ooovba.git] / odk / examples / java / Text / TextDocumentStructure.java
blob713e5a353f347d8c21959b8ccd26b2b5abf4b3dd
1 /*************************************************************************
3 * $RCSfile: TextDocumentStructure.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:18:15 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 //***************************************************************************
42 // comment: Step 1: bootstrap UNO and get the remote component context
43 // Step 2: open an empty text document
44 // Step 3: create an enumeration of all paragraphs
45 // Step 4: create an enumeration of all text portions
46 //***************************************************************************
48 import com.sun.star.uno.UnoRuntime;
50 public class TextDocumentStructure {
52 public static void main(String args[]) {
53 com.sun.star.uno.XComponentContext xContext = null;
55 try {
56 // get the remote office component context
57 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
58 System.out.println("Connected to a running office ...");
60 // get the rmeote service manger
61 com.sun.star.lang.XMultiComponentFactory xMCF =
62 xContext.getServiceManager();
64 // create a new instance of the desktop
65 Object oDesktop = xMCF.createInstanceWithContext(
66 "com.sun.star.frame.Desktop", xContext);
68 // get the component laoder from the desktop to create a new
69 // text document
70 com.sun.star.frame.XComponentLoader xCLoader =
71 (com.sun.star.frame.XComponentLoader)
72 UnoRuntime.queryInterface(
73 com.sun.star.frame.XComponentLoader.class,oDesktop);
74 com.sun.star.beans.PropertyValue [] szEmptyArgs =
75 new com.sun.star.beans.PropertyValue [0];
76 String strDoc = "private:factory/swriter";
78 System.out.println("create new text document");
80 com.sun.star.lang.XComponent xComp = xCLoader.loadComponentFromURL(
81 strDoc, "_blank", 0, szEmptyArgs);
83 // query the new document for the XTextDocument interface
84 com.sun.star.text.XTextDocument xTextDocument =
85 (com.sun.star.text.XTextDocument)UnoRuntime.queryInterface(
86 com.sun.star.text.XTextDocument.class, xComp);
88 // create some example data
89 com.sun.star.text.XText xText = xTextDocument.getText();
90 createExampleData( xText );
92 // Begin section 'The structure of text documents' of the Tutorial
94 com.sun.star.container.XEnumeration xParagraphEnumeration = null;
95 com.sun.star.container.XEnumerationAccess xParaEnumerationAccess = null;
96 com.sun.star.container.XEnumeration xPortionEnumeration = null;
97 com.sun.star.container.XEnumeration xTextPortionEnum;
98 com.sun.star.text.XTextContent xTextElement = null;
100 System.out.println("create an enumeration of all paragraphs");
101 // create an enumeration access of all paragraphs of a document
102 com.sun.star.container.XEnumerationAccess xEnumerationAccess =
103 (com.sun.star.container.XEnumerationAccess)
104 UnoRuntime.queryInterface(
105 com.sun.star.container.XEnumerationAccess.class, xText);
106 xParagraphEnumeration = xEnumerationAccess.createEnumeration();
108 // Loop through all paragraphs of the document
109 while ( xParagraphEnumeration.hasMoreElements() ) {
110 xTextElement = (com.sun.star.text.XTextContent)
111 UnoRuntime.queryInterface(
112 com.sun.star.text.XTextContent.class,
113 xParagraphEnumeration.nextElement());
114 com.sun.star.lang.XServiceInfo xServiceInfo =
115 (com.sun.star.lang.XServiceInfo)UnoRuntime.queryInterface(
116 com.sun.star.lang.XServiceInfo.class, xTextElement);
118 // check ifs the current paragraph really a paragraph or an
119 // anchor of a frame or picture
120 if( xServiceInfo.supportsService("com.sun.star.text.Paragraph") ) {
121 com.sun.star.text.XTextRange xTextRange =
122 xTextElement.getAnchor();
123 System.out.println( "This is a Paragraph" );
125 // create another enumeration to get all text portions of
126 // the paragraph
127 xParaEnumerationAccess =
128 (com.sun.star.container.XEnumerationAccess)
129 UnoRuntime.queryInterface(
130 com.sun.star.container.XEnumerationAccess.class,
131 xTextElement);
132 xTextPortionEnum = xParaEnumerationAccess.createEnumeration();
134 while ( xTextPortionEnum.hasMoreElements() ) {
135 com.sun.star.text.XTextRange xTextPortion =
136 (com.sun.star.text.XTextRange)UnoRuntime.queryInterface(
137 com.sun.star.text.XTextRange.class,
138 xTextPortionEnum.nextElement());
139 System.out.println( "Text from the portion : "
140 + xTextPortion.getString() );
142 com.sun.star.beans.XPropertySet xPropertySet =
143 (com.sun.star.beans.XPropertySet)
144 UnoRuntime.queryInterface(
145 com.sun.star.beans.XPropertySet.class,
146 xTextPortion);
147 System.out.println( "Name of the font : "
148 + xPropertySet.getPropertyValue( "CharFontName" ) );
150 // PropertyState status of each text portion.
151 com.sun.star.beans.XPropertyState xPropertyState =
152 (com.sun.star.beans.XPropertyState)
153 UnoRuntime.queryInterface(
154 com.sun.star.beans.XPropertyState.class,
155 xTextPortion);
157 if( xPropertyState.getPropertyState("CharWeight").equals(
158 com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE) )
159 System.out.println( "- The text range contains more than one different attributes" );
161 if( xPropertyState.getPropertyState( "CharWeight" ).equals(
162 com.sun.star.beans.PropertyState.DIRECT_VALUE ) )
163 System.out.println( " - The text range contains hard formats" );
165 if( xPropertyState.getPropertyState( "CharWeight" ).equals(
166 com.sun.star.beans.PropertyState.DEFAULT_VALUE ) )
167 System.out.println( " - The text range doesn't contains hard formats" );
170 else
171 System.out.println( "The text portion isn't a text paragraph" );
172 // End section 'The structure of text documents' of the Tutorial
175 catch( Exception e) {
176 e.printStackTrace(System.err);
177 System.exit(1);
180 System.out.println("done");
181 System.exit(0);
184 public static void createExampleData( com.sun.star.text.XText xText ) {
186 try {
187 xText.setString( "This is an example sentence" );
189 com.sun.star.text.XWordCursor xWordCursor =
190 (com.sun.star.text.XWordCursor)UnoRuntime.queryInterface(
191 com.sun.star.text.XWordCursor.class, xText.getStart());
193 xWordCursor.gotoNextWord(false);
194 xWordCursor.gotoNextWord(false);
195 xWordCursor.gotoEndOfWord(true);
197 com.sun.star.beans.XPropertySet xPropertySet =
198 (com.sun.star.beans.XPropertySet)UnoRuntime.queryInterface(
199 com.sun.star.beans.XPropertySet.class, xWordCursor );
200 xPropertySet.setPropertyValue("CharWeight",
201 new Float( com.sun.star.awt.FontWeight.BOLD ));
203 System.out.println("create example data");
205 catch( Exception e) {
206 e.printStackTrace(System.err);