Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / java / Text / HardFormatting.java
blob4b00ad333945932fec6158d7d4444ba2b2d614a4
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
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: get some text attributes
40 // Step 5: check the PropertyState from the selection
42 // Chapter 4.1.4 Hard formatting
45 import com.sun.star.uno.UnoRuntime;
47 public class HardFormatting {
49 public static void main(String args[]) {
50 // You need the desktop to create a document
51 // The getDesktop method does the UNO bootstrapping, gets the
52 // remote servie manager and the desktop object.
53 com.sun.star.frame.XDesktop xDesktop = null;
54 xDesktop = getDesktop();
56 try {
57 // create text document
58 com.sun.star.text.XTextDocument xTextDocument = null;
59 xTextDocument = createTextdocument(xDesktop);
61 // the text interface contains all methods and properties to
62 // manipulate the content from a text document
63 com.sun.star.text.XText xText = null;
64 xText = xTextDocument.getText();
66 String sMyText = "A very short paragraph for illustration only";
68 // you can travel with the cursor through the text document.
69 // you travel only at the model, not at the view. The cursor that you can
70 // see on the document doesn't change the position
71 com.sun.star.text.XTextCursor xTextCursor = null;
72 xTextCursor = xTextDocument.getText().createTextCursor();
74 xText.insertString( xTextCursor, "Headline", false );
75 xText.insertControlCharacter(xTextCursor,
76 com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
78 xText.insertString(xTextCursor, sMyText, false);
80 com.sun.star.text.XTextRange xTextRange = null;
81 com.sun.star.beans.XPropertySet xPropertySet = null;
83 // BEGIN: 'Hard formatting'
84 // the text range not the cursor contains the 'parastyle' property
85 xTextRange = xText.getEnd();
86 xPropertySet = UnoRuntime.queryInterface(
87 com.sun.star.beans.XPropertySet.class, xTextRange);
89 // create a paragraph cursor to travel through the paragraphs
90 com.sun.star.text.XParagraphCursor xParagraphCursor = null;
91 xParagraphCursor = UnoRuntime.queryInterface(
92 com.sun.star.text.XParagraphCursor.class, xTextRange);
94 xParagraphCursor.gotoStart( false );
95 xParagraphCursor.gotoEndOfParagraph( true );
96 xTextRange = xParagraphCursor.getText().getStart();
98 // create a WordCursor to travel into the paragraph
99 com.sun.star.text.XWordCursor xWordCursor = null;
100 xWordCursor = UnoRuntime.queryInterface(
101 com.sun.star.text.XWordCursor.class, xTextRange);
103 // the PropertySet from the cursor contains the text attributes
104 xPropertySet = UnoRuntime.queryInterface(
105 com.sun.star.beans.XPropertySet.class, xWordCursor);
106 System.out.println(
107 "Parastyle : "
108 +xPropertySet.getPropertyValue("ParaStyleName").toString()
109 + "\nFontname : "
110 + xPropertySet.getPropertyValue("CharFontName").toString()
111 + "\nWeight : "
112 + xPropertySet.getPropertyValue("CharWeight").toString() );
114 xWordCursor.gotoNextWord(false);
115 xWordCursor.gotoNextWord(false);
116 xWordCursor.gotoEndOfWord(true);
118 xPropertySet = UnoRuntime.queryInterface(
119 com.sun.star.beans.XPropertySet.class, xWordCursor);
120 xPropertySet.setPropertyValue("CharWeight",
121 new Float(com.sun.star.awt.FontWeight.BOLD));
122 xPropertySet.setPropertyValue("CharColor", Integer.valueOf( 255 ) );
124 System.out.println(
125 "Parastyle : "
126 + xPropertySet.getPropertyValue("ParaStyleName").toString()
127 + "\nFontname : "
128 + xPropertySet.getPropertyValue("CharFontName").toString()
129 + "\nWeight : "
130 + xPropertySet.getPropertyValue("CharWeight").toString() );
132 // the PropertyState contains information where the attribute is set,
133 // is a text part hard formatted or not.
134 com.sun.star.beans.XPropertyState xPropertyState = null;
135 xPropertyState = UnoRuntime.queryInterface(
136 com.sun.star.beans.XPropertyState.class, xWordCursor);
138 com.sun.star.beans.PropertyState xPropertyStateValue =
139 xPropertyState.getPropertyState("CharWeight");
141 checkPropertyState( xWordCursor, xPropertyStateValue );
143 xWordCursor.goRight( (short) 3 , true );
144 xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
146 System.out.println("Increase the selection with three characters");
147 checkPropertyState(xWordCursor, xPropertyStateValue);
149 xPropertyState.setPropertyToDefault("CharWeight");
151 System.out.println("Set the default value on the selection");
152 xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
153 checkPropertyState(xWordCursor, xPropertyStateValue);
155 // END: 'Hard formatting' Section from the Cookbook
157 catch( Exception e) {
158 e.printStackTrace(System.err);
159 System.exit(1);
163 System.out.println("Done");
165 System.exit(0);
170 public static void checkPropertyState(
171 com.sun.star.text.XWordCursor xWordCursor,
172 com.sun.star.beans.PropertyState xPropertyStateValue )
174 switch( xPropertyStateValue.getValue() ) {
175 case com.sun.star.beans.PropertyState.DIRECT_VALUE_value: {
176 System.out.println( "-> The selection '"
177 + xWordCursor.getString()
178 + "' completely hard formatted" );
179 break;
182 case com.sun.star.beans.PropertyState.DEFAULT_VALUE_value: {
183 System.out.println( "-> The selection '"
184 + xWordCursor.getString()
185 + "' isn't hard formatted" );
186 break;
189 case com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE_value: {
190 System.out.println( "-> The selection '"
191 + xWordCursor.getString()
192 + "' isn't completely hard formatted" );
193 break;
196 default:
197 System.out.println( "No PropertyState found" );
201 public static com.sun.star.frame.XDesktop getDesktop() {
202 com.sun.star.frame.XDesktop xDesktop = null;
203 com.sun.star.lang.XMultiComponentFactory xMCF = null;
205 try {
206 com.sun.star.uno.XComponentContext xContext = null;
208 // get the remote office component context
209 xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
211 // get the remote office service manager
212 xMCF = xContext.getServiceManager();
213 if( xMCF != null ) {
214 System.out.println("Connected to a running office ...");
216 Object oDesktop = xMCF.createInstanceWithContext(
217 "com.sun.star.frame.Desktop", xContext);
218 xDesktop = UnoRuntime.queryInterface(
219 com.sun.star.frame.XDesktop.class, oDesktop);
221 else
222 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
224 catch( Exception e) {
225 e.printStackTrace(System.err);
226 System.exit(1);
230 return xDesktop;
233 public static com.sun.star.text.XTextDocument createTextdocument(
234 com.sun.star.frame.XDesktop xDesktop )
236 com.sun.star.text.XTextDocument aTextDocument = null;
238 try {
239 com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
240 "swriter");
241 aTextDocument = UnoRuntime.queryInterface(
242 com.sun.star.text.XTextDocument.class, xComponent);
244 catch( Exception e) {
245 e.printStackTrace(System.err);
248 return aTextDocument;
252 protected static com.sun.star.lang.XComponent CreateNewDocument(
253 com.sun.star.frame.XDesktop xDesktop,
254 String sDocumentType )
256 String sURL = "private:factory/" + sDocumentType;
258 com.sun.star.lang.XComponent xComponent = null;
259 com.sun.star.frame.XComponentLoader xComponentLoader = null;
260 com.sun.star.beans.PropertyValue xEmptyArgs[] =
261 new com.sun.star.beans.PropertyValue[0];
263 try {
264 xComponentLoader = UnoRuntime.queryInterface(
265 com.sun.star.frame.XComponentLoader.class, xDesktop);
267 xComponent = xComponentLoader.loadComponentFromURL(
268 sURL, "_blank", 0, xEmptyArgs);
270 catch( Exception e) {
271 e.printStackTrace(System.err);
274 return xComponent ;