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