1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
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.
20  *
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.
32  *
33  *************************************************************************/
34 
35 //***************************************************************************
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
41 //
42 //          Chapter 4.1.4 Hard formatting
43 //***************************************************************************
44 
45 import com.sun.star.uno.UnoRuntime;
46 
47 public class HardFormatting {
48 
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();
55 
56         try {
57             // create text document
58             com.sun.star.text.XTextDocument xTextDocument = null;
59             xTextDocument = createTextdocument(xDesktop);
60 
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();
65 
66             String sMyText = "A very short paragraph for illustration only";
67 
68             // you can travel with the cursor throught 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 = (com.sun.star.text.XTextCursor)
73                 xTextDocument.getText().createTextCursor();
74 
75             xText.insertString( xTextCursor, "Headline", false );
76             xText.insertControlCharacter(xTextCursor,
77                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
78 
79             xText.insertString(xTextCursor, sMyText, false);
80 
81             com.sun.star.text.XTextRange xTextRange = null;
82             com.sun.star.beans.XPropertySet xPropertySet = null;
83 
84             // BEGIN: 'Hard formating'
85             // the text range not the cursor contains the 'parastyle' property
86             xTextRange = xText.getEnd();
87             xPropertySet = (com.sun.star.beans.XPropertySet)
88                 UnoRuntime.queryInterface(
89                     com.sun.star.beans.XPropertySet.class, xTextRange);
90 
91             // create a paragraph cursor to travel throught the paragraphs
92             com.sun.star.text.XParagraphCursor xParagraphCursor = null;
93             xParagraphCursor = (com.sun.star.text.XParagraphCursor)
94                 UnoRuntime.queryInterface(
95                     com.sun.star.text.XParagraphCursor.class, xTextRange);
96 
97             xParagraphCursor.gotoStart( false );
98             xParagraphCursor.gotoEndOfParagraph( true );
99             xTextRange = xParagraphCursor.getText().getStart();
100 
101             // create a WordCursor to travel into the paragraph
102             com.sun.star.text.XWordCursor xWordCursor = null;
103             xWordCursor = (com.sun.star.text.XWordCursor) UnoRuntime.queryInterface(
104                 com.sun.star.text.XWordCursor.class, xTextRange);
105 
106             // the PropertySet from the cursor contains the text attributes
107             xPropertySet = (com.sun.star.beans.XPropertySet)
108                 UnoRuntime.queryInterface(
109                     com.sun.star.beans.XPropertySet.class, xWordCursor);
110             System.out.println(
111                 "Parastyle : "
112                 +xPropertySet.getPropertyValue("ParaStyleName").toString()
113                 + "\nFontname : "
114                 + xPropertySet.getPropertyValue("CharFontName").toString()
115                 + "\nWeight : "
116                 + xPropertySet.getPropertyValue("CharWeight").toString() );
117 
118             xWordCursor.gotoNextWord(false);
119             xWordCursor.gotoNextWord(false);
120             xWordCursor.gotoEndOfWord(true);
121 
122             xPropertySet = (com.sun.star.beans.XPropertySet)
123                 UnoRuntime.queryInterface(
124                     com.sun.star.beans.XPropertySet.class, xWordCursor);
125             xPropertySet.setPropertyValue("CharWeight",
126                                           new Float(com.sun.star.awt.FontWeight.BOLD));
127             xPropertySet.setPropertyValue("CharColor", new Integer( 255 ) );
128 
129             System.out.println(
130                 "Parastyle : "
131                 + xPropertySet.getPropertyValue("ParaStyleName").toString()
132                 + "\nFontname : "
133                 + xPropertySet.getPropertyValue("CharFontName").toString()
134                 + "\nWeight : "
135                 + xPropertySet.getPropertyValue("CharWeight").toString() );
136 
137             // the PropertyState contains information where the attribute is set,
138             // is a text part hard formated or not.
139             com.sun.star.beans.XPropertyState xPropertyState = null;
140             xPropertyState = (com.sun.star.beans.XPropertyState)
141                 UnoRuntime.queryInterface(
142                     com.sun.star.beans.XPropertyState.class, xWordCursor);
143 
144             com.sun.star.beans.PropertyState xPropertyStateValue =
145                 xPropertyState.getPropertyState("CharWeight");
146 
147             checkPropertyState( xWordCursor, xPropertyStateValue );
148 
149             xWordCursor.goRight( (short) 3 , true );
150             xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
151 
152             System.out.println("Increase the selection with three characters");
153             checkPropertyState(xWordCursor, xPropertyStateValue);
154 
155             xPropertyState.setPropertyToDefault("CharWeight");
156 
157             System.out.println("Set the default value on the selection");
158             xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
159             checkPropertyState(xWordCursor, xPropertyStateValue);
160 
161             // END: 'Hard formating' Section from the Cookbook
162         }
163         catch( Exception e) {
164             e.printStackTrace(System.err);
165             System.exit(1);
166         }
167 
168 
169         System.out.println("Done");
170 
171         System.exit(0);
172 
173     }
174 
175 
176     public static void checkPropertyState(
177         com.sun.star.text.XWordCursor xWordCursor,
178         com.sun.star.beans.PropertyState xPropertyStateValue )
179     {
180         switch( xPropertyStateValue.getValue() ) {
181             case com.sun.star.beans.PropertyState.DIRECT_VALUE_value:  {
182                 System.out.println( "-> The selection '"
183                                     + xWordCursor.getString()
184                                     + "' completly hard formated" );
185                 break;
186             }
187 
188             case com.sun.star.beans.PropertyState.DEFAULT_VALUE_value: {
189                 System.out.println( "-> The selection '"
190                                     + xWordCursor.getString()
191                                     + "' isn't hard formated" );
192                 break;
193             }
194 
195             case com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE_value: {
196                 System.out.println( "-> The selection '"
197                                     + xWordCursor.getString()
198                                     + "' isn't completly hard formated" );
199                 break;
200             }
201 
202             default:
203                 System.out.println( "No PropertyState found" );
204         }
205     }
206 
207     public static com.sun.star.frame.XDesktop getDesktop() {
208         com.sun.star.frame.XDesktop xDesktop = null;
209         com.sun.star.lang.XMultiComponentFactory xMCF = null;
210 
211         try {
212             com.sun.star.uno.XComponentContext xContext = null;
213 
214             // get the remote office component context
215             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
216 
217             // get the remote office service manager
218             xMCF = xContext.getServiceManager();
219             if( xMCF != null ) {
220                 System.out.println("Connected to a running office ...");
221 
222                 Object oDesktop = xMCF.createInstanceWithContext(
223                     "com.sun.star.frame.Desktop", xContext);
224                 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
225                     com.sun.star.frame.XDesktop.class, oDesktop);
226             }
227             else
228                 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
229         }
230         catch( Exception e) {
231             e.printStackTrace(System.err);
232             System.exit(1);
233         }
234 
235 
236         return xDesktop;
237     }
238 
239     public static com.sun.star.text.XTextDocument createTextdocument(
240         com.sun.star.frame.XDesktop xDesktop )
241     {
242         com.sun.star.text.XTextDocument aTextDocument = null;
243 
244         try {
245             com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
246                                                                         "swriter");
247             aTextDocument = (com.sun.star.text.XTextDocument)
248                 UnoRuntime.queryInterface(
249                     com.sun.star.text.XTextDocument.class, xComponent);
250         }
251         catch( Exception e) {
252             e.printStackTrace(System.err);
253         }
254 
255         return aTextDocument;
256     }
257 
258 
259     protected static com.sun.star.lang.XComponent CreateNewDocument(
260         com.sun.star.frame.XDesktop xDesktop,
261         String sDocumentType )
262     {
263         String sURL = "private:factory/" + sDocumentType;
264 
265         com.sun.star.lang.XComponent xComponent = null;
266         com.sun.star.frame.XComponentLoader xComponentLoader = null;
267         com.sun.star.beans.PropertyValue xValues[] =
268             new com.sun.star.beans.PropertyValue[1];
269         com.sun.star.beans.PropertyValue xEmptyArgs[] =
270             new com.sun.star.beans.PropertyValue[0];
271 
272         try {
273             xComponentLoader = (com.sun.star.frame.XComponentLoader)
274                 UnoRuntime.queryInterface(
275                     com.sun.star.frame.XComponentLoader.class, xDesktop);
276 
277             xComponent  = xComponentLoader.loadComponentFromURL(
278                 sURL, "_blank", 0, xEmptyArgs);
279         }
280         catch( Exception e) {
281             e.printStackTrace(System.err);
282         }
283 
284         return xComponent ;
285     }
286 }
287