1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 //***************************************************************************
25 // comment: Step 1: get the Desktop object from the office
26 //          Step 2: open an empty text document
27 //          Step 3: enter a example text
28 //          Step 4: get some text attributes
29 //          Step 5: check the PropertyState from the selection
30 //
31 //          Chapter 4.1.4 Hard formatting
32 //***************************************************************************
33 
34 import com.sun.star.uno.UnoRuntime;
35 
36 public class HardFormatting {
37 
38     public static void main(String args[]) {
39         // You need the desktop to create a document
40         // The getDesktop method does the UNO bootstrapping, gets the
41         // remote servie manager and the desktop object.
42         com.sun.star.frame.XDesktop xDesktop = null;
43         xDesktop = getDesktop();
44 
45         try {
46             // create text document
47             com.sun.star.text.XTextDocument xTextDocument = null;
48             xTextDocument = createTextdocument(xDesktop);
49 
50             // the text interface contains all methods and properties to
51             // manipulate the content from a text document
52             com.sun.star.text.XText xText = null;
53             xText = xTextDocument.getText();
54 
55             String sMyText = "A very short paragraph for illustration only";
56 
57             // you can travel with the cursor throught the text document.
58             // you travel only at the model, not at the view. The cursor that you can
59             // see on the document doesn't change the position
60             com.sun.star.text.XTextCursor xTextCursor = null;
61             xTextCursor = (com.sun.star.text.XTextCursor)
62                 xTextDocument.getText().createTextCursor();
63 
64             xText.insertString( xTextCursor, "Headline", false );
65             xText.insertControlCharacter(xTextCursor,
66                       com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, false);
67 
68             xText.insertString(xTextCursor, sMyText, false);
69 
70             com.sun.star.text.XTextRange xTextRange = null;
71             com.sun.star.beans.XPropertySet xPropertySet = null;
72 
73             // BEGIN: 'Hard formatting'
74             // the text range not the cursor contains the 'parastyle' property
75             xTextRange = xText.getEnd();
76             xPropertySet = (com.sun.star.beans.XPropertySet)
77                 UnoRuntime.queryInterface(
78                     com.sun.star.beans.XPropertySet.class, xTextRange);
79 
80             // create a paragraph cursor to travel throught the paragraphs
81             com.sun.star.text.XParagraphCursor xParagraphCursor = null;
82             xParagraphCursor = (com.sun.star.text.XParagraphCursor)
83                 UnoRuntime.queryInterface(
84                     com.sun.star.text.XParagraphCursor.class, xTextRange);
85 
86             xParagraphCursor.gotoStart( false );
87             xParagraphCursor.gotoEndOfParagraph( true );
88             xTextRange = xParagraphCursor.getText().getStart();
89 
90             // create a WordCursor to travel into the paragraph
91             com.sun.star.text.XWordCursor xWordCursor = null;
92             xWordCursor = (com.sun.star.text.XWordCursor) UnoRuntime.queryInterface(
93                 com.sun.star.text.XWordCursor.class, xTextRange);
94 
95             // the PropertySet from the cursor contains the text attributes
96             xPropertySet = (com.sun.star.beans.XPropertySet)
97                 UnoRuntime.queryInterface(
98                     com.sun.star.beans.XPropertySet.class, xWordCursor);
99             System.out.println(
100                 "Parastyle : "
101                 +xPropertySet.getPropertyValue("ParaStyleName").toString()
102                 + "\nFontname : "
103                 + xPropertySet.getPropertyValue("CharFontName").toString()
104                 + "\nWeight : "
105                 + xPropertySet.getPropertyValue("CharWeight").toString() );
106 
107             xWordCursor.gotoNextWord(false);
108             xWordCursor.gotoNextWord(false);
109             xWordCursor.gotoEndOfWord(true);
110 
111             xPropertySet = (com.sun.star.beans.XPropertySet)
112                 UnoRuntime.queryInterface(
113                     com.sun.star.beans.XPropertySet.class, xWordCursor);
114             xPropertySet.setPropertyValue("CharWeight",
115                                           new Float(com.sun.star.awt.FontWeight.BOLD));
116             xPropertySet.setPropertyValue("CharColor", new Integer( 255 ) );
117 
118             System.out.println(
119                 "Parastyle : "
120                 + xPropertySet.getPropertyValue("ParaStyleName").toString()
121                 + "\nFontname : "
122                 + xPropertySet.getPropertyValue("CharFontName").toString()
123                 + "\nWeight : "
124                 + xPropertySet.getPropertyValue("CharWeight").toString() );
125 
126             // the PropertyState contains information where the attribute is set,
127             // is a text part hard formated or not.
128             com.sun.star.beans.XPropertyState xPropertyState = null;
129             xPropertyState = (com.sun.star.beans.XPropertyState)
130                 UnoRuntime.queryInterface(
131                     com.sun.star.beans.XPropertyState.class, xWordCursor);
132 
133             com.sun.star.beans.PropertyState xPropertyStateValue =
134                 xPropertyState.getPropertyState("CharWeight");
135 
136             checkPropertyState( xWordCursor, xPropertyStateValue );
137 
138             xWordCursor.goRight( (short) 3 , true );
139             xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
140 
141             System.out.println("Increase the selection with three characters");
142             checkPropertyState(xWordCursor, xPropertyStateValue);
143 
144             xPropertyState.setPropertyToDefault("CharWeight");
145 
146             System.out.println("Set the default value on the selection");
147             xPropertyStateValue = xPropertyState.getPropertyState("CharWeight");
148             checkPropertyState(xWordCursor, xPropertyStateValue);
149 
150             // END: 'Hard formatting' Section from the Cookbook
151         }
152         catch( Exception e) {
153             e.printStackTrace(System.err);
154             System.exit(1);
155         }
156 
157 
158         System.out.println("Done");
159 
160         System.exit(0);
161 
162     }
163 
164 
165     public static void checkPropertyState(
166         com.sun.star.text.XWordCursor xWordCursor,
167         com.sun.star.beans.PropertyState xPropertyStateValue )
168     {
169         switch( xPropertyStateValue.getValue() ) {
170             case com.sun.star.beans.PropertyState.DIRECT_VALUE_value:  {
171                 System.out.println( "-> The selection '"
172                                     + xWordCursor.getString()
173                                     + "' completly hard formated" );
174                 break;
175             }
176 
177             case com.sun.star.beans.PropertyState.DEFAULT_VALUE_value: {
178                 System.out.println( "-> The selection '"
179                                     + xWordCursor.getString()
180                                     + "' isn't hard formated" );
181                 break;
182             }
183 
184             case com.sun.star.beans.PropertyState.AMBIGUOUS_VALUE_value: {
185                 System.out.println( "-> The selection '"
186                                     + xWordCursor.getString()
187                                     + "' isn't completly hard formated" );
188                 break;
189             }
190 
191             default:
192                 System.out.println( "No PropertyState found" );
193         }
194     }
195 
196     public static com.sun.star.frame.XDesktop getDesktop() {
197         com.sun.star.frame.XDesktop xDesktop = null;
198         com.sun.star.lang.XMultiComponentFactory xMCF = null;
199 
200         try {
201             com.sun.star.uno.XComponentContext xContext = null;
202 
203             // get the remote office component context
204             xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
205 
206             // get the remote office service manager
207             xMCF = xContext.getServiceManager();
208             if( xMCF != null ) {
209                 System.out.println("Connected to a running office ...");
210 
211                 Object oDesktop = xMCF.createInstanceWithContext(
212                     "com.sun.star.frame.Desktop", xContext);
213                 xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(
214                     com.sun.star.frame.XDesktop.class, oDesktop);
215             }
216             else
217                 System.out.println( "Can't create a desktop. No connection, no remote office servicemanager available!" );
218         }
219         catch( Exception e) {
220             e.printStackTrace(System.err);
221             System.exit(1);
222         }
223 
224 
225         return xDesktop;
226     }
227 
228     public static com.sun.star.text.XTextDocument createTextdocument(
229         com.sun.star.frame.XDesktop xDesktop )
230     {
231         com.sun.star.text.XTextDocument aTextDocument = null;
232 
233         try {
234             com.sun.star.lang.XComponent xComponent = CreateNewDocument(xDesktop,
235                                                                         "swriter");
236             aTextDocument = (com.sun.star.text.XTextDocument)
237                 UnoRuntime.queryInterface(
238                     com.sun.star.text.XTextDocument.class, xComponent);
239         }
240         catch( Exception e) {
241             e.printStackTrace(System.err);
242         }
243 
244         return aTextDocument;
245     }
246 
247 
248     protected static com.sun.star.lang.XComponent CreateNewDocument(
249         com.sun.star.frame.XDesktop xDesktop,
250         String sDocumentType )
251     {
252         String sURL = "private:factory/" + sDocumentType;
253 
254         com.sun.star.lang.XComponent xComponent = null;
255         com.sun.star.frame.XComponentLoader xComponentLoader = null;
256         com.sun.star.beans.PropertyValue xValues[] =
257             new com.sun.star.beans.PropertyValue[1];
258         com.sun.star.beans.PropertyValue xEmptyArgs[] =
259             new com.sun.star.beans.PropertyValue[0];
260 
261         try {
262             xComponentLoader = (com.sun.star.frame.XComponentLoader)
263                 UnoRuntime.queryInterface(
264                     com.sun.star.frame.XComponentLoader.class, xDesktop);
265 
266             xComponent  = xComponentLoader.loadComponentFromURL(
267                 sURL, "_blank", 0, xEmptyArgs);
268         }
269         catch( Exception e) {
270             e.printStackTrace(System.err);
271         }
272 
273         return xComponent ;
274     }
275 }
276