Update ooo320-m1
[ooovba.git] / odk / examples / java / Text / HardFormatting.java
blob068431468bd5295465c6865425758924a30a43b2
1 /*************************************************************************
3 * $RCSfile: HardFormatting.java,v $
5 * $Revision: 1.5 $
7 * last change: $Author: rt $ $Date: 2005-01-31 17:17:04 $
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: get the Desktop object from the office
43 // Step 2: open an empty text document
44 // Step 3: enter a example text
45 // Step 4: get some text attributes
46 // Step 5: check the PropertyState from the selection
48 // Chapter 4.1.4 Hard formatting
49 //***************************************************************************
51 import com.sun.star.uno.UnoRuntime;
53 public class HardFormatting {
55 public static void main(String args[]) {
56 // You need the desktop to create a document
57 // The getDesktop method does the UNO bootstrapping, gets the
58 // remote servie manager and the desktop object.
59 com.sun.star.frame.XDesktop xDesktop = null;
60 xDesktop = getDesktop();
62 try {
63 // create text document
64 com.sun.star.text.XTextDocument xTextDocument = null;
65 xTextDocument = createTextdocument(xDesktop);
67 // the text interface contains all methods and properties to
68 // manipulate the content from a text document
69 com.sun.star.text.XText xText = null;
70 xText = xTextDocument.getText();
72 String sMyText = "A very short paragraph for illustration only";
74 // you can travel with the cursor throught the text document.
75 // you travel only at the model, not at the view. The cursor that you can
76 // see on the document doesn't change the position
77 com.sun.star.text.XTextCursor xTextCursor = null;
78 xTextCursor = (com.sun.star.text.XTextCursor)
79 xTextDocument.getText().createTextCursor();
81 xText.insertString( xTextCursor, "Headline", false );
82 xText.insertControlCharacter(xTextCursor,
83 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
85 xText.insertString(xTextCursor, sMyText, false);
87 com.sun.star.text.XTextRange xTextRange = null;
88 com.sun.star.beans.XPropertySet xPropertySet = null;
90 // BEGIN: 'Hard formating'
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);
97 // create a paragraph cursor to travel throught the paragraphs
98 com.sun.star.text.XParagraphCursor xParagraphCursor = null;
99 xParagraphCursor = (com.sun.star.text.XParagraphCursor)
100 UnoRuntime.queryInterface(
101 com.sun.star.text.XParagraphCursor.class, xTextRange);
103 xParagraphCursor.gotoStart( false );
104 xParagraphCursor.gotoEndOfParagraph( true );
105 xTextRange = xParagraphCursor.getText().getStart();
107 // create a WordCursor to travel into the paragraph
108 com.sun.star.text.XWordCursor xWordCursor = null;
109 xWordCursor = (com.sun.star.text.XWordCursor) UnoRuntime.queryInterface(
110 com.sun.star.text.XWordCursor.class, xTextRange);
112 // the PropertySet from the cursor contains the text attributes
113 xPropertySet = (com.sun.star.beans.XPropertySet)
114 UnoRuntime.queryInterface(
115 com.sun.star.beans.XPropertySet.class, xWordCursor);
116 System.out.println(
117 "Parastyle : "
118 +xPropertySet.getPropertyValue("ParaStyleName").toString()
119 + "\nFontname : "
120 + xPropertySet.getPropertyValue("CharFontName").toString()
121 + "\nWeight : "
122 + xPropertySet.getPropertyValue("CharWeight").toString() );
124 xWordCursor.gotoNextWord(false);
125 xWordCursor.gotoNextWord(false);
126 xWordCursor.gotoEndOfWord(true);
128 xPropertySet = (com.sun.star.beans.XPropertySet)
129 UnoRuntime.queryInterface(
130 com.sun.star.beans.XPropertySet.class, xWordCursor);
131 xPropertySet.setPropertyValue("CharWeight",
132 new Float(com.sun.star.awt.FontWeight.BOLD));
133 xPropertySet.setPropertyValue("CharColor", new Integer( 255 ) );
135 System.out.println(
136 "Parastyle : "
137 + xPropertySet.getPropertyValue("ParaStyleName").toString()
138 + "\nFontname : "
139 + xPropertySet.getPropertyValue("CharFontName").toString()
140 + "\nWeight : "
141 + xPropertySet.getPropertyValue("CharWeight").toString() );
143 // the PropertyState contains information where the attribute is set,
144 // is a text part hard formated or not.
145 com.sun.star.beans.XPropertyState xPropertyState = null;
146 xPropertyState = (com.sun.star.beans.XPropertyState)
147 UnoRuntime.queryInterface(
148 com.sun.star.beans.XPropertyState.class, xWordCursor);
150 com.sun.star.beans.PropertyState xPropertyStateValue =
151 xPropertyState.getPropertyState("CharWeight");
153 checkPropertyState( xWordCursor, xPropertyStateValue );
155 xWordCursor.goRight( (short) 3 , true );
156 xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
158 System.out.println("Increase the selection with three characters");
159 checkPropertyState(xWordCursor, xPropertyStateValue);
161 xPropertyState.setPropertyToDefault("CharWeight");
163 System.out.println("Set the default value on the selection");
164 xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
165 checkPropertyState(xWordCursor, xPropertyStateValue);
167 // END: 'Hard formating' Section from the Cookbook
169 catch( Exception e) {
170 e.printStackTrace(System.err);
171 System.exit(1);
175 System.out.println("Done");
177 System.exit(0);
182 public static void checkPropertyState(
183 com.sun.star.text.XWordCursor xWordCursor,
184 com.sun.star.beans.PropertyState xPropertyStateValue )
186 switch( xPropertyStateValue.getValue() ) {
187 case com.sun.star.beans.PropertyState.DIRECT_VALUE_value: {
188 System.out.println( "-> The selection '"
189 + xWordCursor.getString()
190 + "' completly hard formated" );
191 break;
194 case com.sun.star.beans.PropertyState.DEFAULT_VALUE_value: {
195 System.out.println( "-> The selection '"
196 + xWordCursor.getString()
197 + "' isn't hard formated" );
198 break;
201 case com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE_value: {
202 System.out.println( "-> The selection '"
203 + xWordCursor.getString()
204 + "' isn't completly hard formated" );
205 break;
208 default:
209 System.out.println( "No PropertyState found" );
213 public static com.sun.star.frame.XDesktop getDesktop() {
214 com.sun.star.frame.XDesktop xDesktop = null;
215 com.sun.star.lang.XMultiComponentFactory xMCF = null;
217 try {
218 com.sun.star.uno.XComponentContext xContext = null;
220 // get the remote office component context
221 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
223 // get the remote office service manager
224 xMCF = xContext.getServiceManager();
225 if( xMCF != null ) {
226 System.out.println("Connected to a running office ...");
228 Object oDesktop = xMCF.createInstanceWithContext(
229 "com.sun.star.frame.Desktop", xContext);
230 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
231 com.sun.star.frame.XDesktop.class, oDesktop);
233 else
234 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
236 catch( Exception e) {
237 e.printStackTrace(System.err);
238 System.exit(1);
242 return xDesktop;
245 public static com.sun.star.text.XTextDocument createTextdocument(
246 com.sun.star.frame.XDesktop xDesktop )
248 com.sun.star.text.XTextDocument aTextDocument = null;
250 try {
251 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
252 "swriter");
253 aTextDocument = (com.sun.star.text.XTextDocument)
254 UnoRuntime.queryInterface(
255 com.sun.star.text.XTextDocument.class, xComponent);
257 catch( Exception e) {
258 e.printStackTrace(System.err);
261 return aTextDocument;
265 protected static com.sun.star.lang.XComponent CreateNewDocument(
266 com.sun.star.frame.XDesktop xDesktop,
267 String sDocumentType )
269 String sURL = "private:factory/" + sDocumentType;
271 com.sun.star.lang.XComponent xComponent = null;
272 com.sun.star.frame.XComponentLoader xComponentLoader = null;
273 com.sun.star.beans.PropertyValue xValues[] =
274 new com.sun.star.beans.PropertyValue[1];
275 com.sun.star.beans.PropertyValue xEmptyArgs[] =
276 new com.sun.star.beans.PropertyValue[0];
278 try {
279 xComponentLoader = (com.sun.star.frame.XComponentLoader)
280 UnoRuntime.queryInterface(
281 com.sun.star.frame.XComponentLoader.class, xDesktop);
283 xComponent = xComponentLoader.loadComponentFromURL(
284 sURL, "_blank", 0, xEmptyArgs);
286 catch( Exception e) {
287 e.printStackTrace(System.err);
290 return xComponent ;