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 import com.sun.star.lang.XComponent; 36*cdf0e10cSrcweir import com.sun.star.uno.XComponentContext; 37*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 38*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter; 39*cdf0e10cSrcweir import com.sun.star.bridge.XUnoUrlResolver; 40*cdf0e10cSrcweir import com.sun.star.frame.XComponentLoader; 41*cdf0e10cSrcweir import com.sun.star.lang.XMultiComponentFactory; 42*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory; 43*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo; 44*cdf0e10cSrcweir import com.sun.star.beans.XPropertySet; 45*cdf0e10cSrcweir import com.sun.star.beans.XPropertySetInfo; 46*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue; 47*cdf0e10cSrcweir import com.sun.star.beans.UnknownPropertyException; 48*cdf0e10cSrcweir import com.sun.star.beans.PropertyVetoException; 49*cdf0e10cSrcweir import com.sun.star.text.XTextDocument; 50*cdf0e10cSrcweir import com.sun.star.text.XText; 51*cdf0e10cSrcweir import com.sun.star.text.XTextCursor; 52*cdf0e10cSrcweir import com.sun.star.text.XWordCursor; 53*cdf0e10cSrcweir import com.sun.star.text.XTextContent; 54*cdf0e10cSrcweir import com.sun.star.text.XTextTable; 55*cdf0e10cSrcweir import com.sun.star.text.XTextTableCursor; 56*cdf0e10cSrcweir import com.sun.star.table.XTableRows; 57*cdf0e10cSrcweir import com.sun.star.table.XCellRange; 58*cdf0e10cSrcweir import com.sun.star.table.XCell; 59*cdf0e10cSrcweir import com.sun.star.table.XCellCursor; 60*cdf0e10cSrcweir import com.sun.star.table.TableBorder; 61*cdf0e10cSrcweir import com.sun.star.table.BorderLine; 62*cdf0e10cSrcweir import com.sun.star.drawing.XShape; 63*cdf0e10cSrcweir import com.sun.star.awt.Size; 64*cdf0e10cSrcweir import com.sun.star.awt.Point; 65*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument; 66*cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet; 67*cdf0e10cSrcweir import com.sun.star.sheet.XSheetCellCursor; 68*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess; 69*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPagesSupplier; 70*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPageSupplier; 71*cdf0e10cSrcweir import com.sun.star.drawing.XDrawPage; 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir import com.sun.star.text.XTextTablesSupplier; 74*cdf0e10cSrcweir import com.sun.star.container.XNameAccess; 75*cdf0e10cSrcweir import com.sun.star.container.XNamed; 76*cdf0e10cSrcweir import com.sun.star.text.XBookmarksSupplier; 77*cdf0e10cSrcweir import com.sun.star.text.XTextRange; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir /** 80*cdf0e10cSrcweir * 81*cdf0e10cSrcweir * @author dschulten 82*cdf0e10cSrcweir */ 83*cdf0e10cSrcweir public class HelloTextTableShape { 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir private XComponentContext xRemoteContext = null; 86*cdf0e10cSrcweir private XMultiComponentFactory xRemoteServiceManager = null; 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir /** Creates a new instance of HelloTextTableShape */ 89*cdf0e10cSrcweir public HelloTextTableShape() { 90*cdf0e10cSrcweir } 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir /** 93*cdf0e10cSrcweir * @param args the command line arguments 94*cdf0e10cSrcweir */ 95*cdf0e10cSrcweir public static void main(String[] args) { 96*cdf0e10cSrcweir HelloTextTableShape helloTextTableShape1 = new HelloTextTableShape(); 97*cdf0e10cSrcweir try { 98*cdf0e10cSrcweir helloTextTableShape1.useDocuments(); 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir catch (java.lang.Exception e){ 101*cdf0e10cSrcweir System.err.println(e.getMessage()); 102*cdf0e10cSrcweir e.printStackTrace(); 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir finally { 105*cdf0e10cSrcweir System.exit(0); 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir protected void useDocuments() throws java.lang.Exception { 111*cdf0e10cSrcweir useWriter(); 112*cdf0e10cSrcweir useCalc(); 113*cdf0e10cSrcweir useDraw(); 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir protected void useWriter() throws java.lang.Exception { 117*cdf0e10cSrcweir try { 118*cdf0e10cSrcweir // create new writer document and get text, then manipulate text 119*cdf0e10cSrcweir XComponent xWriterComponent = newDocComponent("swriter"); 120*cdf0e10cSrcweir XTextDocument xTextDocument = (XTextDocument)UnoRuntime.queryInterface( 121*cdf0e10cSrcweir XTextDocument.class, xWriterComponent); 122*cdf0e10cSrcweir XText xText = xTextDocument.getText(); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir manipulateText(xText); 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir // get internal service factory of the document 127*cdf0e10cSrcweir XMultiServiceFactory xWriterFactory = (XMultiServiceFactory)UnoRuntime.queryInterface( 128*cdf0e10cSrcweir XMultiServiceFactory.class, xWriterComponent); 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir // insert TextTable and get cell text, then manipulate text in cell 131*cdf0e10cSrcweir Object table = xWriterFactory.createInstance("com.sun.star.text.TextTable"); 132*cdf0e10cSrcweir XTextContent xTextContentTable = (XTextContent)UnoRuntime.queryInterface( 133*cdf0e10cSrcweir XTextContent.class, table); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir xText.insertTextContent(xText.getEnd(), xTextContentTable, false); 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir XCellRange xCellRange = (XCellRange)UnoRuntime.queryInterface( 138*cdf0e10cSrcweir XCellRange.class, table); 139*cdf0e10cSrcweir XCell xCell = xCellRange.getCellByPosition(0, 1); 140*cdf0e10cSrcweir XText xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell); 141*cdf0e10cSrcweir 142*cdf0e10cSrcweir manipulateText(xCellText); 143*cdf0e10cSrcweir manipulateTable(xCellRange); 144*cdf0e10cSrcweir 145*cdf0e10cSrcweir // insert RectangleShape and get shape text, then manipulate text 146*cdf0e10cSrcweir Object writerShape = xWriterFactory.createInstance( 147*cdf0e10cSrcweir "com.sun.star.drawing.RectangleShape"); 148*cdf0e10cSrcweir XShape xWriterShape = (XShape)UnoRuntime.queryInterface( 149*cdf0e10cSrcweir XShape.class, writerShape); 150*cdf0e10cSrcweir xWriterShape.setSize(new Size(10000, 10000)); 151*cdf0e10cSrcweir XTextContent xTextContentShape = (XTextContent)UnoRuntime.queryInterface( 152*cdf0e10cSrcweir XTextContent.class, writerShape); 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir xText.insertTextContent(xText.getEnd(), xTextContentShape, false); 155*cdf0e10cSrcweir 156*cdf0e10cSrcweir XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface( 157*cdf0e10cSrcweir XPropertySet.class, writerShape); 158*cdf0e10cSrcweir // wrap text inside shape 159*cdf0e10cSrcweir xShapeProps.setPropertyValue("TextContourFrame", new Boolean(true)); 160*cdf0e10cSrcweir 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, writerShape); 163*cdf0e10cSrcweir 164*cdf0e10cSrcweir manipulateText(xShapeText); 165*cdf0e10cSrcweir manipulateShape(xWriterShape); 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir /* more code snippets used in the manual: 168*cdf0e10cSrcweir 169*cdf0e10cSrcweir Object bookmark = xWriterFactory.createInstance ( "com.sun.star.text.Bookmark" ); 170*cdf0e10cSrcweir // name the bookmark 171*cdf0e10cSrcweir XNamed xNamed = (XNamed) UnoRuntime.queryInterface ( 172*cdf0e10cSrcweir XNamed.class, bookmark ); 173*cdf0e10cSrcweir xNamed.setName("MyUniqueBookmarkName"); 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir // get XTextContent interface and insert it at the end of the document 176*cdf0e10cSrcweir XTextContent xTextContent = (XTextContent) UnoRuntime.queryInterface ( 177*cdf0e10cSrcweir XTextContent.class, bookmark ); 178*cdf0e10cSrcweir //mxDocText.insertTextContent ( mxDocText.getEnd(), xTextContent, false ); 179*cdf0e10cSrcweir xText.insertTextContent ( xText.getEnd(), xTextContent, false ); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir //query BookmarksSupplier 182*cdf0e10cSrcweir XBookmarksSupplier xBookmarksSupplier = (XBookmarksSupplier)UnoRuntime.queryInterface( 183*cdf0e10cSrcweir XBookmarksSupplier.class, xWriterComponent); 184*cdf0e10cSrcweir XNameAccess xNamedBookmarks = xBookmarksSupplier.getBookmarks(); 185*cdf0e10cSrcweir Object foundBookmark = xNamedBookmarks.getByName("MyUniqueBookmarkName"); 186*cdf0e10cSrcweir XTextContent xFoundBookmark = (XTextContent)UnoRuntime.queryInterface(XTextContent.class, foundBookmark); 187*cdf0e10cSrcweir XTextRange xFound = xFoundBookmark.getAnchor(); 188*cdf0e10cSrcweir xFound.setString(" The throat mike, glued to her neck, " 189*cdf0e10cSrcweir + "looked as much as possible like an analgesic dermadisk."); 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir // first query the XTextTablesSupplier interface from our document 195*cdf0e10cSrcweir XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface( 196*cdf0e10cSrcweir XTextTablesSupplier.class, xWriterComponent); 197*cdf0e10cSrcweir // get the tables collection 198*cdf0e10cSrcweir XNameAccess xNamedTables = xTablesSupplier.getTextTables(); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir // now query the XIndexAccess from the tables collection 201*cdf0e10cSrcweir XIndexAccess xIndexedTables = (XIndexAccess) UnoRuntime.queryInterface( 202*cdf0e10cSrcweir XIndexAccess.class, xNamedTables); 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir // we need properties 205*cdf0e10cSrcweir XPropertySet xTableProps = null; 206*cdf0e10cSrcweir 207*cdf0e10cSrcweir // get the tables 208*cdf0e10cSrcweir for (int i = 0; i < xIndexedTables.getCount(); i++) { 209*cdf0e10cSrcweir //Object table = xIndexedTables.getByIndex(i); 210*cdf0e10cSrcweir table = xIndexedTables.getByIndex(i); 211*cdf0e10cSrcweir xTableProps = (XPropertySet) UnoRuntime.queryInterface( 212*cdf0e10cSrcweir XPropertySet.class, table); 213*cdf0e10cSrcweir xTableProps.setPropertyValue("BackColor", new Integer(0xC8FFB9)); 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir */ 216*cdf0e10cSrcweir } 217*cdf0e10cSrcweir catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1 218*cdf0e10cSrcweir xRemoteContext = null; 219*cdf0e10cSrcweir throw e; 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir protected void useCalc() throws java.lang.Exception { 225*cdf0e10cSrcweir try { 226*cdf0e10cSrcweir // create new calc document and manipulate cell text 227*cdf0e10cSrcweir XComponent xCalcComponent = newDocComponent("scalc"); 228*cdf0e10cSrcweir XSpreadsheetDocument xSpreadsheetDocument = 229*cdf0e10cSrcweir (XSpreadsheetDocument)UnoRuntime.queryInterface( 230*cdf0e10cSrcweir XSpreadsheetDocument .class, xCalcComponent); 231*cdf0e10cSrcweir Object sheets = xSpreadsheetDocument.getSheets(); 232*cdf0e10cSrcweir XIndexAccess xIndexedSheets = (XIndexAccess)UnoRuntime.queryInterface( 233*cdf0e10cSrcweir XIndexAccess.class, sheets); 234*cdf0e10cSrcweir Object sheet = xIndexedSheets.getByIndex(0); 235*cdf0e10cSrcweir 236*cdf0e10cSrcweir //get cell A2 in first sheet 237*cdf0e10cSrcweir XCellRange xSpreadsheetCells = (XCellRange)UnoRuntime.queryInterface( 238*cdf0e10cSrcweir XCellRange.class, sheet); 239*cdf0e10cSrcweir XCell xCell = xSpreadsheetCells.getCellByPosition(0,1); 240*cdf0e10cSrcweir XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface( 241*cdf0e10cSrcweir XPropertySet.class, xCell); 242*cdf0e10cSrcweir xCellProps.setPropertyValue("IsTextWrapped", new Boolean(true)); 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir XText xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell); 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir manipulateText(xCellText); 247*cdf0e10cSrcweir manipulateTable(xSpreadsheetCells); 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir // get internal service factory of the document 250*cdf0e10cSrcweir XMultiServiceFactory xCalcFactory = (XMultiServiceFactory)UnoRuntime.queryInterface( 251*cdf0e10cSrcweir XMultiServiceFactory.class, xCalcComponent); 252*cdf0e10cSrcweir // get Drawpage 253*cdf0e10cSrcweir XDrawPageSupplier xDrawPageSupplier = (XDrawPageSupplier)UnoRuntime.queryInterface(XDrawPageSupplier.class, sheet); 254*cdf0e10cSrcweir XDrawPage xDrawPage = xDrawPageSupplier.getDrawPage(); 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir // create and insert RectangleShape and get shape text, then manipulate text 257*cdf0e10cSrcweir Object calcShape = xCalcFactory.createInstance( 258*cdf0e10cSrcweir "com.sun.star.drawing.RectangleShape"); 259*cdf0e10cSrcweir XShape xCalcShape = (XShape)UnoRuntime.queryInterface( 260*cdf0e10cSrcweir XShape.class, calcShape); 261*cdf0e10cSrcweir xCalcShape.setSize(new Size(10000, 10000)); 262*cdf0e10cSrcweir xCalcShape.setPosition(new Point(7000, 3000)); 263*cdf0e10cSrcweir 264*cdf0e10cSrcweir xDrawPage.add(xCalcShape); 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface( 267*cdf0e10cSrcweir XPropertySet.class, calcShape); 268*cdf0e10cSrcweir // wrap text inside shape 269*cdf0e10cSrcweir xShapeProps.setPropertyValue("TextContourFrame", new Boolean(true)); 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, calcShape); 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir manipulateText(xShapeText); 275*cdf0e10cSrcweir manipulateShape(xCalcShape); 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir } 278*cdf0e10cSrcweir catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1 279*cdf0e10cSrcweir xRemoteContext = null; 280*cdf0e10cSrcweir throw e; 281*cdf0e10cSrcweir } 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir 285*cdf0e10cSrcweir protected void useDraw() throws java.lang.Exception { 286*cdf0e10cSrcweir try { 287*cdf0e10cSrcweir //create new draw document and insert ractangle shape 288*cdf0e10cSrcweir XComponent xDrawComponent = newDocComponent("sdraw"); 289*cdf0e10cSrcweir XDrawPagesSupplier xDrawPagesSupplier = 290*cdf0e10cSrcweir (XDrawPagesSupplier)UnoRuntime.queryInterface( 291*cdf0e10cSrcweir XDrawPagesSupplier.class, xDrawComponent); 292*cdf0e10cSrcweir 293*cdf0e10cSrcweir Object drawPages = xDrawPagesSupplier.getDrawPages(); 294*cdf0e10cSrcweir XIndexAccess xIndexedDrawPages = (XIndexAccess)UnoRuntime.queryInterface( 295*cdf0e10cSrcweir XIndexAccess.class, drawPages); 296*cdf0e10cSrcweir Object drawPage = xIndexedDrawPages.getByIndex(0); 297*cdf0e10cSrcweir XDrawPage xDrawPage = (XDrawPage)UnoRuntime.queryInterface(XDrawPage.class, drawPage); 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir // get internal service factory of the document 300*cdf0e10cSrcweir XMultiServiceFactory xDrawFactory = 301*cdf0e10cSrcweir (XMultiServiceFactory)UnoRuntime.queryInterface( 302*cdf0e10cSrcweir XMultiServiceFactory.class, xDrawComponent); 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir Object drawShape = xDrawFactory.createInstance( 305*cdf0e10cSrcweir "com.sun.star.drawing.RectangleShape"); 306*cdf0e10cSrcweir XShape xDrawShape = (XShape)UnoRuntime.queryInterface(XShape.class, drawShape); 307*cdf0e10cSrcweir xDrawShape.setSize(new Size(10000, 20000)); 308*cdf0e10cSrcweir xDrawShape.setPosition(new Point(5000, 5000)); 309*cdf0e10cSrcweir xDrawPage.add(xDrawShape); 310*cdf0e10cSrcweir 311*cdf0e10cSrcweir XText xShapeText = (XText)UnoRuntime.queryInterface(XText.class, drawShape); 312*cdf0e10cSrcweir XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface( 313*cdf0e10cSrcweir XPropertySet.class, drawShape); 314*cdf0e10cSrcweir 315*cdf0e10cSrcweir // wrap text inside shape 316*cdf0e10cSrcweir xShapeProps.setPropertyValue("TextContourFrame", new Boolean(true)); 317*cdf0e10cSrcweir 318*cdf0e10cSrcweir manipulateText(xShapeText); 319*cdf0e10cSrcweir manipulateShape(xDrawShape); 320*cdf0e10cSrcweir } 321*cdf0e10cSrcweir catch( com.sun.star.lang.DisposedException e ) { //works from Patch 1 322*cdf0e10cSrcweir xRemoteContext = null; 323*cdf0e10cSrcweir throw e; 324*cdf0e10cSrcweir } 325*cdf0e10cSrcweir 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir } 328*cdf0e10cSrcweir 329*cdf0e10cSrcweir protected void manipulateText(XText xText) throws com.sun.star.uno.Exception { 330*cdf0e10cSrcweir // simply set whole text as one string 331*cdf0e10cSrcweir xText.setString("He lay flat on the brown, pine-needled floor of the forest, " 332*cdf0e10cSrcweir + "his chin on his folded arms, and high overhead the wind blew in the tops " 333*cdf0e10cSrcweir + "of the pine trees."); 334*cdf0e10cSrcweir 335*cdf0e10cSrcweir // create text cursor for selecting and formatting 336*cdf0e10cSrcweir XTextCursor xTextCursor = xText.createTextCursor(); 337*cdf0e10cSrcweir XPropertySet xCursorProps = (XPropertySet)UnoRuntime.queryInterface( 338*cdf0e10cSrcweir XPropertySet.class, xTextCursor); 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir // use cursor to select "He lay" and apply bold italic 341*cdf0e10cSrcweir xTextCursor.gotoStart(false); 342*cdf0e10cSrcweir xTextCursor.goRight((short)6, true); 343*cdf0e10cSrcweir // from CharacterProperties 344*cdf0e10cSrcweir xCursorProps.setPropertyValue("CharPosture", 345*cdf0e10cSrcweir com.sun.star.awt.FontSlant.ITALIC); 346*cdf0e10cSrcweir xCursorProps.setPropertyValue("CharWeight", 347*cdf0e10cSrcweir new Float(com.sun.star.awt.FontWeight.BOLD)); 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir // add more text at the end of the text using insertString 350*cdf0e10cSrcweir xTextCursor.gotoEnd(false); 351*cdf0e10cSrcweir xText.insertString(xTextCursor, " The mountainside sloped gently where he lay; " 352*cdf0e10cSrcweir + "but below it was steep and he could see the dark of the oiled road " 353*cdf0e10cSrcweir + "winding through the pass. There was a stream alongside the road " 354*cdf0e10cSrcweir + "and far down the pass he saw a mill beside the stream and the falling water " 355*cdf0e10cSrcweir + "of the dam, white in the summer sunlight.", false); 356*cdf0e10cSrcweir // after insertString the cursor is behind the inserted text, insert more text 357*cdf0e10cSrcweir xText.insertString(xTextCursor, "\n \"Is that the mill?\" he asked.", false); 358*cdf0e10cSrcweir } 359*cdf0e10cSrcweir 360*cdf0e10cSrcweir protected void manipulateTable(XCellRange xCellRange) throws com.sun.star.uno.Exception { 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir String backColorPropertyName = ""; 363*cdf0e10cSrcweir XPropertySet xTableProps = null; 364*cdf0e10cSrcweir 365*cdf0e10cSrcweir // enter column titles and a cell value 366*cdf0e10cSrcweir XCell xCell = xCellRange.getCellByPosition(0,0); 367*cdf0e10cSrcweir XText xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell); 368*cdf0e10cSrcweir xCellText.setString("Quotation"); 369*cdf0e10cSrcweir xCell = xCellRange.getCellByPosition(1,0); 370*cdf0e10cSrcweir xCellText = (XText)UnoRuntime.queryInterface(XText.class, xCell); 371*cdf0e10cSrcweir xCellText.setString("Year"); 372*cdf0e10cSrcweir xCell = xCellRange.getCellByPosition(1,1); 373*cdf0e10cSrcweir xCell.setValue(1940); 374*cdf0e10cSrcweir XCellRange xSelectedCells = xCellRange.getCellRangeByName("A1:B1"); 375*cdf0e10cSrcweir XPropertySet xCellProps = (XPropertySet)UnoRuntime.queryInterface( 376*cdf0e10cSrcweir XPropertySet.class, xSelectedCells); 377*cdf0e10cSrcweir 378*cdf0e10cSrcweir // format table headers and table borders 379*cdf0e10cSrcweir // we need to distinguish text and sheet tables: 380*cdf0e10cSrcweir // property name for cell colors is different in text and sheet cells 381*cdf0e10cSrcweir // we want to apply TableBorder to whole text table, but only to sheet cells with content 382*cdf0e10cSrcweir XServiceInfo xServiceInfo = (XServiceInfo)UnoRuntime.queryInterface( 383*cdf0e10cSrcweir XServiceInfo.class, xCellRange); 384*cdf0e10cSrcweir if (xServiceInfo.supportsService("com.sun.star.sheet.Spreadsheet")) { 385*cdf0e10cSrcweir backColorPropertyName = "CellBackColor"; 386*cdf0e10cSrcweir xSelectedCells = xCellRange.getCellRangeByName("A1:B2"); 387*cdf0e10cSrcweir xTableProps = (XPropertySet)UnoRuntime.queryInterface( 388*cdf0e10cSrcweir XPropertySet.class, xSelectedCells); 389*cdf0e10cSrcweir } 390*cdf0e10cSrcweir else if (xServiceInfo.supportsService("com.sun.star.text.TextTable")) { 391*cdf0e10cSrcweir backColorPropertyName = "BackColor"; 392*cdf0e10cSrcweir xTableProps = (XPropertySet)UnoRuntime.queryInterface( 393*cdf0e10cSrcweir XPropertySet.class, xCellRange); 394*cdf0e10cSrcweir } 395*cdf0e10cSrcweir // set cell background color 396*cdf0e10cSrcweir xCellProps.setPropertyValue(backColorPropertyName, new Integer(0x99CCFF)); 397*cdf0e10cSrcweir 398*cdf0e10cSrcweir // set table borders 399*cdf0e10cSrcweir // create description for blue line, width 10 400*cdf0e10cSrcweir BorderLine theLine = new BorderLine(); 401*cdf0e10cSrcweir theLine.Color = 0x000099; 402*cdf0e10cSrcweir theLine.OuterLineWidth = 10; 403*cdf0e10cSrcweir // apply line description to all border lines and make them valid 404*cdf0e10cSrcweir TableBorder bord = new TableBorder(); 405*cdf0e10cSrcweir bord.VerticalLine = bord.HorizontalLine = 406*cdf0e10cSrcweir bord.LeftLine = bord.RightLine = 407*cdf0e10cSrcweir bord.TopLine = bord.BottomLine = 408*cdf0e10cSrcweir theLine; 409*cdf0e10cSrcweir bord.IsVerticalLineValid = bord.IsHorizontalLineValid = 410*cdf0e10cSrcweir bord.IsLeftLineValid = bord.IsRightLineValid = 411*cdf0e10cSrcweir bord.IsTopLineValid = bord.IsBottomLineValid = 412*cdf0e10cSrcweir true; 413*cdf0e10cSrcweir 414*cdf0e10cSrcweir xTableProps.setPropertyValue("TableBorder", bord); 415*cdf0e10cSrcweir 416*cdf0e10cSrcweir bord = (TableBorder)xTableProps.getPropertyValue("TableBorder"); 417*cdf0e10cSrcweir theLine = bord.TopLine; 418*cdf0e10cSrcweir int col = theLine.Color; 419*cdf0e10cSrcweir System.out.println(col); 420*cdf0e10cSrcweir } 421*cdf0e10cSrcweir 422*cdf0e10cSrcweir protected void manipulateShape(XShape xShape) throws com.sun.star.uno.Exception { 423*cdf0e10cSrcweir XPropertySet xShapeProps = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xShape); 424*cdf0e10cSrcweir xShapeProps.setPropertyValue("FillColor", new Integer(0x99CCFF)); 425*cdf0e10cSrcweir xShapeProps.setPropertyValue("LineColor", new Integer(0x000099)); 426*cdf0e10cSrcweir xShapeProps.setPropertyValue("RotateAngle", new Integer(3000)); 427*cdf0e10cSrcweir 428*cdf0e10cSrcweir xShapeProps.setPropertyValue("TextLeftDistance", new Integer(0)); 429*cdf0e10cSrcweir xShapeProps.setPropertyValue("TextRightDistance", new Integer(0)); 430*cdf0e10cSrcweir xShapeProps.setPropertyValue("TextUpperDistance", new Integer(0)); 431*cdf0e10cSrcweir xShapeProps.setPropertyValue("TextLowerDistance", new Integer(0)); 432*cdf0e10cSrcweir } 433*cdf0e10cSrcweir 434*cdf0e10cSrcweir 435*cdf0e10cSrcweir protected XComponent newDocComponent(String docType) throws java.lang.Exception { 436*cdf0e10cSrcweir String loadUrl = "private:factory/" + docType; 437*cdf0e10cSrcweir xRemoteServiceManager = this.getRemoteServiceManager(); 438*cdf0e10cSrcweir Object desktop = xRemoteServiceManager.createInstanceWithContext( 439*cdf0e10cSrcweir "com.sun.star.frame.Desktop", xRemoteContext); 440*cdf0e10cSrcweir XComponentLoader xComponentLoader = (XComponentLoader)UnoRuntime.queryInterface( 441*cdf0e10cSrcweir XComponentLoader.class, desktop); 442*cdf0e10cSrcweir PropertyValue[] loadProps = new PropertyValue[0]; 443*cdf0e10cSrcweir return xComponentLoader.loadComponentFromURL(loadUrl, "_blank", 0, loadProps); 444*cdf0e10cSrcweir } 445*cdf0e10cSrcweir 446*cdf0e10cSrcweir protected XMultiComponentFactory getRemoteServiceManager() throws java.lang.Exception { 447*cdf0e10cSrcweir if (xRemoteContext == null && xRemoteServiceManager == null) { 448*cdf0e10cSrcweir try { 449*cdf0e10cSrcweir // First step: get the remote office component context 450*cdf0e10cSrcweir xRemoteContext = com.sun.star.comp.helper.Bootstrap.bootstrap(); 451*cdf0e10cSrcweir System.out.println("Connected to a running office ..."); 452*cdf0e10cSrcweir 453*cdf0e10cSrcweir xRemoteServiceManager = xRemoteContext.getServiceManager(); 454*cdf0e10cSrcweir } 455*cdf0e10cSrcweir catch( Exception e) { 456*cdf0e10cSrcweir e.printStackTrace(); 457*cdf0e10cSrcweir System.exit(1); 458*cdf0e10cSrcweir } 459*cdf0e10cSrcweir } 460*cdf0e10cSrcweir return xRemoteServiceManager; 461*cdf0e10cSrcweir } 462*cdf0e10cSrcweir } 463