xref: /AOO41X/main/odk/examples/java/Text/HardFormatting.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir //***************************************************************************
36*cdf0e10cSrcweir // comment: Step 1: get the Desktop object from the office
37*cdf0e10cSrcweir //          Step 2: open an empty text document
38*cdf0e10cSrcweir //          Step 3: enter a example text
39*cdf0e10cSrcweir //          Step 4: get some text attributes
40*cdf0e10cSrcweir //          Step 5: check the PropertyState from the selection
41*cdf0e10cSrcweir //
42*cdf0e10cSrcweir //          Chapter 4.1.4 Hard formatting
43*cdf0e10cSrcweir //***************************************************************************
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir public class HardFormatting {
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     public static void main(String args[]) {
50*cdf0e10cSrcweir         // You need the desktop to create a document
51*cdf0e10cSrcweir         // The getDesktop method does the UNO bootstrapping, gets the
52*cdf0e10cSrcweir         // remote servie manager and the desktop object.
53*cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop = null;
54*cdf0e10cSrcweir         xDesktop = getDesktop();
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir         try {
57*cdf0e10cSrcweir             // create text document
58*cdf0e10cSrcweir             com.sun.star.text.XTextDocument xTextDocument = null;
59*cdf0e10cSrcweir             xTextDocument = createTextdocument(xDesktop);
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir             // the text interface contains all methods and properties to
62*cdf0e10cSrcweir             // manipulate the content from a text document
63*cdf0e10cSrcweir             com.sun.star.text.XText xText = null;
64*cdf0e10cSrcweir             xText = xTextDocument.getText();
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir             String sMyText = "A very short paragraph for illustration only";
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir             // you can travel with the cursor throught the text document.
69*cdf0e10cSrcweir             // you travel only at the model, not at the view. The cursor that you can
70*cdf0e10cSrcweir             // see on the document doesn't change the position
71*cdf0e10cSrcweir             com.sun.star.text.XTextCursor xTextCursor = null;
72*cdf0e10cSrcweir             xTextCursor = (com.sun.star.text.XTextCursor)
73*cdf0e10cSrcweir                 xTextDocument.getText().createTextCursor();
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir             xText.insertString( xTextCursor, "Headline", false );
76*cdf0e10cSrcweir             xText.insertControlCharacter(xTextCursor,
77*cdf0e10cSrcweir                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir             xText.insertString(xTextCursor, sMyText, false);
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir             com.sun.star.text.XTextRange xTextRange = null;
82*cdf0e10cSrcweir             com.sun.star.beans.XPropertySet xPropertySet = null;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir             // BEGIN: 'Hard formating'
85*cdf0e10cSrcweir             // the text range not the cursor contains the 'parastyle' property
86*cdf0e10cSrcweir             xTextRange = xText.getEnd();
87*cdf0e10cSrcweir             xPropertySet = (com.sun.star.beans.XPropertySet)
88*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
89*cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet.class, xTextRange);
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir             // create a paragraph cursor to travel throught the paragraphs
92*cdf0e10cSrcweir             com.sun.star.text.XParagraphCursor xParagraphCursor = null;
93*cdf0e10cSrcweir             xParagraphCursor = (com.sun.star.text.XParagraphCursor)
94*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
95*cdf0e10cSrcweir                     com.sun.star.text.XParagraphCursor.class, xTextRange);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir             xParagraphCursor.gotoStart( false );
98*cdf0e10cSrcweir             xParagraphCursor.gotoEndOfParagraph( true );
99*cdf0e10cSrcweir             xTextRange = xParagraphCursor.getText().getStart();
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir             // create a WordCursor to travel into the paragraph
102*cdf0e10cSrcweir             com.sun.star.text.XWordCursor xWordCursor = null;
103*cdf0e10cSrcweir             xWordCursor = (com.sun.star.text.XWordCursor) UnoRuntime.queryInterface(
104*cdf0e10cSrcweir                 com.sun.star.text.XWordCursor.class, xTextRange);
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir             // the PropertySet from the cursor contains the text attributes
107*cdf0e10cSrcweir             xPropertySet = (com.sun.star.beans.XPropertySet)
108*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
109*cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet.class, xWordCursor);
110*cdf0e10cSrcweir             System.out.println(
111*cdf0e10cSrcweir                 "Parastyle : "
112*cdf0e10cSrcweir                 +xPropertySet.getPropertyValue("ParaStyleName").toString()
113*cdf0e10cSrcweir                 + "\nFontname : "
114*cdf0e10cSrcweir                 + xPropertySet.getPropertyValue("CharFontName").toString()
115*cdf0e10cSrcweir                 + "\nWeight : "
116*cdf0e10cSrcweir                 + xPropertySet.getPropertyValue("CharWeight").toString() );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir             xWordCursor.gotoNextWord(false);
119*cdf0e10cSrcweir             xWordCursor.gotoNextWord(false);
120*cdf0e10cSrcweir             xWordCursor.gotoEndOfWord(true);
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir             xPropertySet = (com.sun.star.beans.XPropertySet)
123*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
124*cdf0e10cSrcweir                     com.sun.star.beans.XPropertySet.class, xWordCursor);
125*cdf0e10cSrcweir             xPropertySet.setPropertyValue("CharWeight",
126*cdf0e10cSrcweir                                           new Float(com.sun.star.awt.FontWeight.BOLD));
127*cdf0e10cSrcweir             xPropertySet.setPropertyValue("CharColor", new Integer( 255 ) );
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir             System.out.println(
130*cdf0e10cSrcweir                 "Parastyle : "
131*cdf0e10cSrcweir                 + xPropertySet.getPropertyValue("ParaStyleName").toString()
132*cdf0e10cSrcweir                 + "\nFontname : "
133*cdf0e10cSrcweir                 + xPropertySet.getPropertyValue("CharFontName").toString()
134*cdf0e10cSrcweir                 + "\nWeight : "
135*cdf0e10cSrcweir                 + xPropertySet.getPropertyValue("CharWeight").toString() );
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir             // the PropertyState contains information where the attribute is set,
138*cdf0e10cSrcweir             // is a text part hard formated or not.
139*cdf0e10cSrcweir             com.sun.star.beans.XPropertyState xPropertyState = null;
140*cdf0e10cSrcweir             xPropertyState = (com.sun.star.beans.XPropertyState)
141*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
142*cdf0e10cSrcweir                     com.sun.star.beans.XPropertyState.class, xWordCursor);
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir             com.sun.star.beans.PropertyState xPropertyStateValue =
145*cdf0e10cSrcweir                 xPropertyState.getPropertyState("CharWeight");
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir             checkPropertyState( xWordCursor, xPropertyStateValue );
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir             xWordCursor.goRight( (short) 3 , true );
150*cdf0e10cSrcweir             xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir             System.out.println("Increase the selection with three characters");
153*cdf0e10cSrcweir             checkPropertyState(xWordCursor, xPropertyStateValue);
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir             xPropertyState.setPropertyToDefault("CharWeight");
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir             System.out.println("Set the default value on the selection");
158*cdf0e10cSrcweir             xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
159*cdf0e10cSrcweir             checkPropertyState(xWordCursor, xPropertyStateValue);
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir             // END: 'Hard formating' Section from the Cookbook
162*cdf0e10cSrcweir         }
163*cdf0e10cSrcweir         catch( Exception e) {
164*cdf0e10cSrcweir             e.printStackTrace(System.err);
165*cdf0e10cSrcweir             System.exit(1);
166*cdf0e10cSrcweir         }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir         System.out.println("Done");
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir         System.exit(0);
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     public static void checkPropertyState(
177*cdf0e10cSrcweir         com.sun.star.text.XWordCursor xWordCursor,
178*cdf0e10cSrcweir         com.sun.star.beans.PropertyState xPropertyStateValue )
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir         switch( xPropertyStateValue.getValue() ) {
181*cdf0e10cSrcweir             case com.sun.star.beans.PropertyState.DIRECT_VALUE_value:  {
182*cdf0e10cSrcweir                 System.out.println( "-> The selection '"
183*cdf0e10cSrcweir                                     + xWordCursor.getString()
184*cdf0e10cSrcweir                                     + "' completly hard formated" );
185*cdf0e10cSrcweir                 break;
186*cdf0e10cSrcweir             }
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir             case com.sun.star.beans.PropertyState.DEFAULT_VALUE_value: {
189*cdf0e10cSrcweir                 System.out.println( "-> The selection '"
190*cdf0e10cSrcweir                                     + xWordCursor.getString()
191*cdf0e10cSrcweir                                     + "' isn't hard formated" );
192*cdf0e10cSrcweir                 break;
193*cdf0e10cSrcweir             }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir             case com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE_value: {
196*cdf0e10cSrcweir                 System.out.println( "-> The selection '"
197*cdf0e10cSrcweir                                     + xWordCursor.getString()
198*cdf0e10cSrcweir                                     + "' isn't completly hard formated" );
199*cdf0e10cSrcweir                 break;
200*cdf0e10cSrcweir             }
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir             default:
203*cdf0e10cSrcweir                 System.out.println( "No PropertyState found" );
204*cdf0e10cSrcweir         }
205*cdf0e10cSrcweir     }
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir     public static com.sun.star.frame.XDesktop getDesktop() {
208*cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop = null;
209*cdf0e10cSrcweir         com.sun.star.lang.XMultiComponentFactory xMCF = null;
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir         try {
212*cdf0e10cSrcweir             com.sun.star.uno.XComponentContext xContext = null;
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir             // get the remote office component context
215*cdf0e10cSrcweir             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir             // get the remote office service manager
218*cdf0e10cSrcweir             xMCF = xContext.getServiceManager();
219*cdf0e10cSrcweir             if( xMCF != null ) {
220*cdf0e10cSrcweir                 System.out.println("Connected to a running office ...");
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir                 Object oDesktop = xMCF.createInstanceWithContext(
223*cdf0e10cSrcweir                     "com.sun.star.frame.Desktop", xContext);
224*cdf0e10cSrcweir                 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
225*cdf0e10cSrcweir                     com.sun.star.frame.XDesktop.class, oDesktop);
226*cdf0e10cSrcweir             }
227*cdf0e10cSrcweir             else
228*cdf0e10cSrcweir                 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
229*cdf0e10cSrcweir         }
230*cdf0e10cSrcweir         catch( Exception e) {
231*cdf0e10cSrcweir             e.printStackTrace(System.err);
232*cdf0e10cSrcweir             System.exit(1);
233*cdf0e10cSrcweir         }
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir         return xDesktop;
237*cdf0e10cSrcweir     }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir     public static com.sun.star.text.XTextDocument createTextdocument(
240*cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop )
241*cdf0e10cSrcweir     {
242*cdf0e10cSrcweir         com.sun.star.text.XTextDocument aTextDocument = null;
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir         try {
245*cdf0e10cSrcweir             com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
246*cdf0e10cSrcweir                                                                         "swriter");
247*cdf0e10cSrcweir             aTextDocument = (com.sun.star.text.XTextDocument)
248*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
249*cdf0e10cSrcweir                     com.sun.star.text.XTextDocument.class, xComponent);
250*cdf0e10cSrcweir         }
251*cdf0e10cSrcweir         catch( Exception e) {
252*cdf0e10cSrcweir             e.printStackTrace(System.err);
253*cdf0e10cSrcweir         }
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir         return aTextDocument;
256*cdf0e10cSrcweir     }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir     protected static com.sun.star.lang.XComponent CreateNewDocument(
260*cdf0e10cSrcweir         com.sun.star.frame.XDesktop xDesktop,
261*cdf0e10cSrcweir         String sDocumentType )
262*cdf0e10cSrcweir     {
263*cdf0e10cSrcweir         String sURL = "private:factory/" + sDocumentType;
264*cdf0e10cSrcweir 
265*cdf0e10cSrcweir         com.sun.star.lang.XComponent xComponent = null;
266*cdf0e10cSrcweir         com.sun.star.frame.XComponentLoader xComponentLoader = null;
267*cdf0e10cSrcweir         com.sun.star.beans.PropertyValue xValues[] =
268*cdf0e10cSrcweir             new com.sun.star.beans.PropertyValue[1];
269*cdf0e10cSrcweir         com.sun.star.beans.PropertyValue xEmptyArgs[] =
270*cdf0e10cSrcweir             new com.sun.star.beans.PropertyValue[0];
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir         try {
273*cdf0e10cSrcweir             xComponentLoader = (com.sun.star.frame.XComponentLoader)
274*cdf0e10cSrcweir                 UnoRuntime.queryInterface(
275*cdf0e10cSrcweir                     com.sun.star.frame.XComponentLoader.class, xDesktop);
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir             xComponent  = xComponentLoader.loadComponentFromURL(
278*cdf0e10cSrcweir                 sURL, "_blank", 0, xEmptyArgs);
279*cdf0e10cSrcweir         }
280*cdf0e10cSrcweir         catch( Exception e) {
281*cdf0e10cSrcweir             e.printStackTrace(System.err);
282*cdf0e10cSrcweir         }
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir         return xComponent ;
285*cdf0e10cSrcweir     }
286*cdf0e10cSrcweir }
287