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