cid#1607171 Data race condition
[LibreOffice.git] / odk / examples / java / Text / HardFormatting.java
blob2dcc7b084e3685fe1a5d14311416c2ee91863e9d
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
37 // comment: Step 1: get the Desktop object from the office
38 // Step 2: open an empty text document
39 // Step 3: enter an example text
40 // Step 4: get some text attributes
41 // Step 5: check the PropertyState from the selection
43 // Chapter 4.1.4 Hard formatting
46 import com.sun.star.uno.UnoRuntime;
48 public class HardFormatting {
50 public static void main(String args[]) {
51 // You need the desktop to create a document
52 // The getDesktop method does the UNO bootstrapping, gets the
53 // remote service manager and the desktop object.
54 com.sun.star.frame.XDesktop xDesktop = null;
55 xDesktop = getDesktop();
57 try {
58 // create text document
59 com.sun.star.text.XTextDocument xTextDocument = null;
60 xTextDocument = createTextdocument(xDesktop);
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();
67 String sMyText = "A very short paragraph for illustration only";
69 // you can travel with the cursor through 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 = xTextDocument.getText().createTextCursor();
75 xText.insertString( xTextCursor, "Headline", false );
76 xText.insertControlCharacter(xTextCursor,
77 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
79 xText.insertString(xTextCursor, sMyText, false);
81 com.sun.star.text.XTextRange xTextRange = null;
82 com.sun.star.beans.XPropertySet xPropertySet = null;
84 // BEGIN: 'Hard formatting'
85 // the text range not the cursor contains the 'parastyle' property
86 xTextRange = xText.getEnd();
87 xPropertySet = UnoRuntime.queryInterface(
88 com.sun.star.beans.XPropertySet.class, xTextRange);
90 // create a paragraph cursor to travel through the paragraphs
91 com.sun.star.text.XParagraphCursor xParagraphCursor = null;
92 xParagraphCursor = UnoRuntime.queryInterface(
93 com.sun.star.text.XParagraphCursor.class, xTextRange);
95 xParagraphCursor.gotoStart( false );
96 xParagraphCursor.gotoEndOfParagraph( true );
97 xTextRange = xParagraphCursor.getText().getStart();
99 // create a WordCursor to travel into the paragraph
100 com.sun.star.text.XWordCursor xWordCursor = null;
101 xWordCursor = UnoRuntime.queryInterface(
102 com.sun.star.text.XWordCursor.class, xTextRange);
104 // the PropertySet from the cursor contains the text attributes
105 xPropertySet = UnoRuntime.queryInterface(
106 com.sun.star.beans.XPropertySet.class, xWordCursor);
107 System.out.println(
108 "Parastyle : "
109 +xPropertySet.getPropertyValue("ParaStyleName").toString()
110 + "\nFontname : "
111 + xPropertySet.getPropertyValue("CharFontName").toString()
112 + "\nWeight : "
113 + xPropertySet.getPropertyValue("CharWeight").toString() );
115 xWordCursor.gotoNextWord(false);
116 xWordCursor.gotoNextWord(false);
117 xWordCursor.gotoEndOfWord(true);
119 xPropertySet = UnoRuntime.queryInterface(
120 com.sun.star.beans.XPropertySet.class, xWordCursor);
121 xPropertySet.setPropertyValue("CharWeight",
122 Float.valueOf(com.sun.star.awt.FontWeight.BOLD));
123 xPropertySet.setPropertyValue("CharColor", Integer.valueOf( 255 ) );
125 System.out.println(
126 "Parastyle : "
127 + xPropertySet.getPropertyValue("ParaStyleName").toString()
128 + "\nFontname : "
129 + xPropertySet.getPropertyValue("CharFontName").toString()
130 + "\nWeight : "
131 + xPropertySet.getPropertyValue("CharWeight").toString() );
133 // the PropertyState contains information where the attribute is set,
134 // is a text part hard formatted or not.
135 com.sun.star.beans.XPropertyState xPropertyState = null;
136 xPropertyState = UnoRuntime.queryInterface(
137 com.sun.star.beans.XPropertyState.class, xWordCursor);
139 com.sun.star.beans.PropertyState xPropertyStateValue =
140 xPropertyState.getPropertyState("CharWeight");
142 checkPropertyState( xWordCursor, xPropertyStateValue );
144 xWordCursor.goRight( (short) 3 , true );
145 xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
147 System.out.println("Increase the selection with three characters");
148 checkPropertyState(xWordCursor, xPropertyStateValue);
150 xPropertyState.setPropertyToDefault("CharWeight");
152 System.out.println("Set the default value on the selection");
153 xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
154 checkPropertyState(xWordCursor, xPropertyStateValue);
156 // END: 'Hard formatting' Section from the Cookbook
158 catch( Exception e) {
159 e.printStackTrace(System.err);
160 System.exit(1);
164 System.out.println("Done");
166 System.exit(0);
171 public static void checkPropertyState(
172 com.sun.star.text.XWordCursor xWordCursor,
173 com.sun.star.beans.PropertyState xPropertyStateValue )
175 switch( xPropertyStateValue.getValue() ) {
176 case com.sun.star.beans.PropertyState.DIRECT_VALUE_value: {
177 System.out.println( "-> The selection '"
178 + xWordCursor.getString()
179 + "' completely hard formatted" );
180 break;
183 case com.sun.star.beans.PropertyState.DEFAULT_VALUE_value: {
184 System.out.println( "-> The selection '"
185 + xWordCursor.getString()
186 + "' isn't hard formatted" );
187 break;
190 case com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE_value: {
191 System.out.println( "-> The selection '"
192 + xWordCursor.getString()
193 + "' isn't completely hard formatted" );
194 break;
197 default:
198 System.out.println( "No PropertyState found" );
202 public static com.sun.star.frame.XDesktop getDesktop() {
203 com.sun.star.frame.XDesktop xDesktop = null;
204 com.sun.star.lang.XMultiComponentFactory xMCF = null;
206 try {
207 com.sun.star.uno.XComponentContext xContext = null;
209 // get the remote office component context
210 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
212 // get the remote office service manager
213 xMCF = xContext.getServiceManager();
214 if( xMCF != null ) {
215 System.out.println("Connected to a running office ...");
217 Object oDesktop = xMCF.createInstanceWithContext(
218 "com.sun.star.frame.Desktop", xContext);
219 xDesktop = UnoRuntime.queryInterface(
220 com.sun.star.frame.XDesktop.class, oDesktop);
222 else
223 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
225 catch( Exception e) {
226 e.printStackTrace(System.err);
227 System.exit(1);
231 return xDesktop;
234 public static com.sun.star.text.XTextDocument createTextdocument(
235 com.sun.star.frame.XDesktop xDesktop )
237 com.sun.star.text.XTextDocument aTextDocument = null;
239 try {
240 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
241 "swriter");
242 aTextDocument = UnoRuntime.queryInterface(
243 com.sun.star.text.XTextDocument.class, xComponent);
245 catch( Exception e) {
246 e.printStackTrace(System.err);
249 return aTextDocument;
253 protected static com.sun.star.lang.XComponent CreateNewDocument(
254 com.sun.star.frame.XDesktop xDesktop,
255 String sDocumentType )
257 String sURL = "private:factory/" + sDocumentType;
259 com.sun.star.lang.XComponent xComponent = null;
260 com.sun.star.frame.XComponentLoader xComponentLoader = null;
261 com.sun.star.beans.PropertyValue xEmptyArgs[] =
262 new com.sun.star.beans.PropertyValue[0];
264 try {
265 xComponentLoader = UnoRuntime.queryInterface(
266 com.sun.star.frame.XComponentLoader.class, xDesktop);
268 xComponent = xComponentLoader.loadComponentFromURL(
269 sURL, "_blank", 0, xEmptyArgs);
271 catch( Exception e) {
272 e.printStackTrace(System.err);
275 return xComponent ;
279 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */