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 package util; 24 25 import java.util.Hashtable; 26 // access the implementations via names 27 import com.sun.star.uno.XInterface; 28 import com.sun.star.lang.XMultiServiceFactory; 29 30 import com.sun.star.uno.UnoRuntime; 31 // staroffice interfaces to provide desktop and componentloader 32 // and components i.e. spreadsheets, writerdocs etc. 33 import com.sun.star.frame.XDesktop; 34 import com.sun.star.frame.XComponentLoader; 35 import com.sun.star.lang.XComponent; 36 import com.sun.star.lang.XServiceInfo; 37 38 // name - value pair 39 import com.sun.star.beans.PropertyValue; 40 import com.sun.star.beans.PropertyState; 41 42 // additional classes required for testcase 43 import com.sun.star.sheet.*; 44 import com.sun.star.text.*; 45 import com.sun.star.container.*; 46 import com.sun.star.chart.*; 47 import com.sun.star.drawing.*; 48 import com.sun.star.awt.*; 49 50 public class SOfficeFactory { 51 52 private static Hashtable lookup = new Hashtable(10); 53 protected XComponentLoader oCLoader; 54 55 private SOfficeFactory(XMultiServiceFactory xMSF) { 56 // get XInterface of Desktop service 57 Object oInterface; 58 try { 59 oInterface = xMSF.createInstance("com.sun.star.frame.Desktop"); 60 } catch (com.sun.star.uno.Exception e) { 61 throw new IllegalArgumentException( 62 "Desktop Service not available"); 63 } 64 65 // query the desktop interface and then it's componentloader 66 XDesktop oDesktop = (XDesktop) UnoRuntime.queryInterface( 67 XDesktop.class, oInterface); 68 69 oCLoader = (XComponentLoader) UnoRuntime.queryInterface( 70 XComponentLoader.class, oDesktop); 71 } 72 73 public static SOfficeFactory getFactory(XMultiServiceFactory xMSF) { 74 75 SOfficeFactory soFactory = (SOfficeFactory) lookup.get(new Integer(xMSF.hashCode()).toString()); 76 77 if (soFactory == null) { 78 soFactory = new SOfficeFactory(xMSF); 79 lookup.put(new Integer(xMSF.hashCode()).toString(), soFactory); 80 } 81 82 return soFactory; 83 } 84 85 // ********************************************************* 86 // Document creation. The documents needed are created here. 87 // ********************************************************* 88 /** 89 * method which opens a new TextDocument 90 * 91 * @see XTextDocument 92 */ 93 public XTextDocument createTextDoc(String frameName) 94 throws com.sun.star.uno.Exception { 95 96 XComponent oDoc = openDoc("swriter", frameName); 97 98 if (oDoc != null) { 99 DesktopTools.bringWindowToFront(oDoc); 100 return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, oDoc); 101 } else { 102 return null; 103 } 104 105 } // finished createTextDoc 106 107 /** 108 * method which opens a new TextDocument 109 * 110 * @see XTextDocument 111 */ 112 public XTextDocument createTextDoc(String frameName, PropertyValue[] mediaDescriptor) 113 throws com.sun.star.uno.Exception { 114 115 XComponent oDoc = openDoc("swriter", frameName, mediaDescriptor); 116 117 if (oDoc != null) { 118 DesktopTools.bringWindowToFront(oDoc); 119 return (XTextDocument) UnoRuntime.queryInterface(XTextDocument.class, oDoc); 120 } else { 121 return null; 122 } 123 } // finished createTextDoc 124 125 /** 126 * method which opens a new SpreadsheetDocument 127 * 128 * @see XSpreadsheetDocument 129 */ 130 public XSpreadsheetDocument createCalcDoc(String frameName) 131 throws com.sun.star.uno.Exception { 132 133 XComponent oDoc = openDoc("scalc", frameName); 134 135 if (oDoc != null) { 136 DesktopTools.bringWindowToFront(oDoc); 137 return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, oDoc); 138 } else { 139 return null; 140 } 141 } // finished createCalcDoc 142 143 /** 144 * method which opens a new SpreadsheetDocument 145 * 146 * @see XSpreadsheetDocument 147 */ 148 public XSpreadsheetDocument createCalcDoc(String frameName, PropertyValue[] mediaDescriptor) 149 throws com.sun.star.uno.Exception { 150 151 XComponent oDoc = openDoc("scalc", frameName, mediaDescriptor); 152 153 if (oDoc != null) { 154 DesktopTools.bringWindowToFront(oDoc); 155 return (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, oDoc); 156 } else { 157 return null; 158 } 159 } // finished createCalcDoc 160 161 /** 162 * method which opens a new DrawDocument 163 */ 164 public XComponent createDrawDoc(String frameName) 165 throws com.sun.star.uno.Exception { 166 167 return openDoc("sdraw", frameName); 168 } // finished createDrawDoc 169 170 /** 171 * method which opens a new ImpressDocument 172 */ 173 /** 174 * method which opens a new DrawDocument 175 */ 176 public XComponent createDrawDoc(String frameName, PropertyValue[] mediaDescriptor) 177 throws com.sun.star.uno.Exception { 178 179 return openDoc("sdraw", frameName, mediaDescriptor); 180 } // finished createDrawDoc 181 182 /** 183 * method which opens a new ImpressDocument 184 */ 185 public XComponent createImpressDoc(String frameName) 186 throws com.sun.star.uno.Exception { 187 188 return openDoc("simpress", frameName); 189 } // finished createImpressDoc 190 191 /** 192 * method which opens a new ImpressDocument 193 */ 194 public XComponent createImpressDoc(String frameName, PropertyValue[] mediaDescriptor) 195 throws com.sun.star.uno.Exception { 196 197 return openDoc("simpress", frameName, mediaDescriptor); 198 } // finished createImpressDoc 199 200 /** 201 * method which opens a new MathDocument 202 */ 203 public XComponent createMathDoc(String frameName) 204 throws com.sun.star.uno.Exception { 205 206 return openDoc("smath", frameName); 207 } // finished createMathDoc 208 209 /** 210 * method which opens a new MathDocument 211 */ 212 public XComponent createMathDoc(String frameName, PropertyValue[] mediaDescriptor) 213 throws com.sun.star.uno.Exception { 214 215 return openDoc("smath", frameName, mediaDescriptor); 216 } // finished createMathDoc 217 218 /** 219 * method which opens a new ChartDocument 220 * 221 * @see XChartDocument 222 */ 223 public XChartDocument createChartDoc(String frameName) 224 throws com.sun.star.uno.Exception { 225 226 // XComponent oDoc = loadDocument( 227 // util.utils.getFullTestURL("emptyChart.sds")); 228 229 XComponent oDoc = loadDocument("private:factory/schart"); 230 231 if (oDoc != null) { 232 DesktopTools.bringWindowToFront(oDoc); 233 return (XChartDocument) UnoRuntime.queryInterface(XChartDocument.class, oDoc); 234 } else { 235 return null; 236 } 237 238 } // finished createChartDoc 239 240 /** 241 * creates a simple TextTable defaultet to 2 rows and 2 columns 242 */ 243 public static XTextTable createTextTable(XTextDocument xTextDoc) 244 throws com.sun.star.uno.Exception { 245 246 TableDsc tDsc = new TableDsc(); 247 InstCreator instCreate = new InstCreator(xTextDoc, tDsc); 248 249 XTextTable oTable = (XTextTable) instCreate.getInstance(); 250 return oTable; 251 } 252 253 /** 254 * creates a TextTable with a specified count of rows and columns 255 */ 256 public static XTextTable createTextTable(XTextDocument xTextDoc, 257 int rows, int columns) 258 throws com.sun.star.uno.Exception { 259 260 TableDsc tDsc = new TableDsc(rows, columns); 261 InstCreator instCreate = new InstCreator(xTextDoc, tDsc); 262 263 XTextTable oTable = (XTextTable) instCreate.getInstance(); 264 return oTable; 265 } 266 267 /** 268 * creates a simple TextFrame 269 * ... to be continued 270 */ 271 public static XTextFrame createTextFrame(XTextDocument xTextDoc) 272 throws com.sun.star.uno.Exception { 273 274 FrameDsc tDsc = new FrameDsc(); 275 InstCreator instCreate = new InstCreator(xTextDoc, tDsc); 276 277 XTextFrame oFrame = (XTextFrame) instCreate.getInstance(); 278 return oFrame; 279 } 280 281 /** 282 * creates a simple TextFrame 283 * ... to be continued 284 */ 285 public static XTextFrame createTextFrame(XTextDocument xTextDoc, 286 int height, int width) { 287 288 FrameDsc tDsc = new FrameDsc(height, width); 289 InstCreator instCreate = new InstCreator(xTextDoc, tDsc); 290 291 XTextFrame oFrame = (XTextFrame) instCreate.getInstance(); 292 return oFrame; 293 } 294 295 public static void insertString(XTextDocument xTextDoc, String cString) 296 throws com.sun.star.uno.Exception { 297 XText xText = xTextDoc.getText(); 298 XText oText = (XText) UnoRuntime.queryInterface( 299 XText.class, xText); 300 301 XTextCursor oCursor = oText.createTextCursor(); 302 oText.insertString(oCursor, cString, false); 303 } 304 305 public static void insertTextContent(XTextDocument xTextDoc, 306 XTextContent xCont) 307 throws com.sun.star.lang.IllegalArgumentException { 308 XText xText = xTextDoc.getText(); 309 XText oText = (XText) UnoRuntime.queryInterface( 310 XText.class, xText); 311 312 XTextCursor oCursor = oText.createTextCursor(); 313 oText.insertTextContent(oCursor, xCont, false); 314 } 315 316 public static com.sun.star.table.XCell getFirstTableCell( 317 XTextContent oTable) { 318 319 String CellNames[] = ((XTextTable) oTable).getCellNames(); 320 321 com.sun.star.table.XCell oCell = ((XTextTable) oTable).getCellByName( 322 CellNames[0]); 323 return oCell; 324 325 } 326 327 /** 328 * the method createBookmark 329 */ 330 public static XTextContent createBookmark(XTextDocument xTextDoc) 331 throws com.sun.star.uno.Exception { 332 333 BookmarkDsc tDsc = new BookmarkDsc(); 334 InstCreator instCreate = new InstCreator(xTextDoc, tDsc); 335 336 XTextContent oBookmark = (XTextContent) instCreate.getInstance(); 337 return oBookmark; 338 339 } /// finish createBookmark 340 341 /** 342 * the method createReferenceMark 343 */ 344 public static XTextContent createReferenceMark(XTextDocument xTextDoc) 345 throws com.sun.star.uno.Exception { 346 347 ReferenceMarkDsc tDsc = new ReferenceMarkDsc(); 348 InstCreator instCreate = new InstCreator(xTextDoc, tDsc); 349 350 XTextContent oReferenceMark = (XTextContent) instCreate.getInstance(); 351 return oReferenceMark; 352 353 } /// finish createReferenceMark 354 355 /** 356 * the method createFootnote 357 */ 358 public static XTextContent createFootnote(XTextDocument xTextDoc) 359 throws com.sun.star.uno.Exception { 360 361 FootnoteDsc tDsc = new FootnoteDsc(); 362 InstCreator instCreate = new InstCreator(xTextDoc, tDsc); 363 364 XTextContent oFootnote = (XTextContent) instCreate.getInstance(); 365 return oFootnote; 366 367 } /// finish createFootnote 368 369 /** 370 * the method create Index 371 */ 372 public static XTextContent createIndex(XTextDocument xTextDoc, String kind) 373 throws com.sun.star.uno.Exception { 374 375 XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, 376 xTextDoc); 377 378 Object oInt = oDocMSF.createInstance(kind); 379 380 XTextContent xTC = (XTextContent) UnoRuntime.queryInterface(XDocumentIndex.class, oInt); 381 382 return xTC; 383 384 } 385 386 public static XSpreadsheet createSpreadsheet(XSpreadsheetDocument oDoc) 387 throws com.sun.star.uno.Exception { 388 389 XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc); 390 391 Object oInt = oDocMSF.createInstance( 392 "com.sun.star.sheet.Spreadsheet"); 393 394 XSpreadsheet oSpreadsheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, oInt); 395 396 return oSpreadsheet; 397 } 398 399 public static XIndexAccess getTableCollection(XTextDocument oDoc) { 400 401 XTextTablesSupplier oTTS = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, oDoc); 402 403 XNameAccess oNA = oTTS.getTextTables(); 404 XIndexAccess oIA = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, oNA); 405 406 return oIA; 407 } 408 409 public static String getUniqueName(XInterface oInterface, String prefix) { 410 XNameAccess oNameAccess = (XNameAccess) UnoRuntime.queryInterface(XNameAccess.class, oInterface); 411 if (oNameAccess == null) { 412 return null; 413 } 414 int i; 415 for (i = 0; oNameAccess.hasByName(prefix + i); i++) { 416 } 417 ; 418 return prefix + i; 419 } 420 421 public XShape createShape(XComponent oDoc, int height, int width, int x, int y, String kind) { 422 //possible values for kind are 'Ellipse', 'Line' and 'Rectangle' 423 424 ShapeDsc sDsc = new ShapeDsc(height, width, x, y, kind); 425 InstCreator instCreate = new InstCreator(oDoc, sDsc); 426 427 XShape oShape = (XShape) instCreate.getInstance(); 428 429 return oShape; 430 431 } 432 433 /** 434 * creates a Diagram wich specified in kind(String) 435 */ 436 public XDiagram createDiagram(XComponent oDoc, String kind) { 437 XInterface oInterface = null; 438 XDiagram oDiagram = null; 439 440 //get LineDiagram 441 XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc); 442 443 try { 444 oInterface = (XInterface) oDocMSF.createInstance("com.sun.star.chart." + kind); 445 oDiagram = (XDiagram) UnoRuntime.queryInterface(XDiagram.class, oInterface); 446 } catch (Exception e) { 447 // Some exception occures.FAILED 448 System.out.println("Couldn't create " + kind + "-Diagram " + e); 449 } 450 return oDiagram; 451 } 452 453 /* 454 // create a Control-Instance which specified in kind(String) 455 */ 456 public XInterface createControl(XComponent oDoc, String kind) { 457 458 XInterface oControl = null; 459 460 XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc); 461 462 try { 463 oControl = (XInterface) oDocMSF.createInstance("com.sun.star.form.component." + kind); 464 } catch (Exception e) { 465 // Some exception occures.FAILED 466 System.out.println("Couldn't create instance " + kind + ": " + e); 467 } 468 return oControl; 469 } 470 471 /* 472 // create an Instance which is specified in kind(String) 473 */ 474 public Object createInstance(XComponent oDoc, String kind) { 475 476 Object oInstance = null; 477 478 XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc); 479 480 try { 481 oInstance = (Object) oDocMSF.createInstance(kind); 482 } catch (Exception e) { 483 // Some exception occures.FAILED 484 System.out.println("Couldn't create instance " + kind + ": " + e); 485 } 486 return oInstance; 487 } 488 489 public XControlShape createControlShape(XComponent oDoc, int height, int width, int x, int y, String kind) { 490 491 Size size = new Size(); 492 Point position = new Point(); 493 XControlShape oCShape = null; 494 XControlModel aControl = null; 495 496 //get MSF 497 XMultiServiceFactory oDocMSF = (XMultiServiceFactory) UnoRuntime.queryInterface(XMultiServiceFactory.class, oDoc); 498 499 try { 500 Object oInt = oDocMSF.createInstance("com.sun.star.drawing.ControlShape"); 501 Object aCon = oDocMSF.createInstance("com.sun.star.form.component." + kind); 502 aControl = (XControlModel) UnoRuntime.queryInterface(XControlModel.class, aCon); 503 oCShape = (XControlShape) UnoRuntime.queryInterface(XControlShape.class, oInt); 504 size.Height = height; 505 size.Width = width; 506 position.X = x; 507 position.Y = y; 508 oCShape.setSize(size); 509 oCShape.setPosition(position); 510 511 512 } catch (Exception e) { 513 // Some exception occures.FAILED 514 System.out.println("Couldn't create instance " + e); 515 } 516 517 try { 518 oCShape.setControl(aControl); 519 } catch (Exception e) { 520 // Some exception occures.FAILED 521 System.out.println("Couldn't get Control " + e); 522 } 523 524 525 return oCShape; 526 527 } 528 529 public XComponent loadDocument(String fileName) 530 throws com.sun.star.lang.IllegalArgumentException, 531 com.sun.star.io.IOException, 532 com.sun.star.uno.Exception { 533 534 // that noargs thing for load attributes 535 PropertyValue[] szEmptyArgs = new PropertyValue[0]; 536 String frameName = "_blank"; 537 538 XComponent oDoc = oCLoader.loadComponentFromURL( 539 fileName, frameName, 0, szEmptyArgs); 540 541 if (oDoc == null) { 542 return null; 543 } 544 DesktopTools.bringWindowToFront(oDoc); 545 return oDoc; 546 } 547 548 public XComponent loadDocument(String fileName, PropertyValue[] Args) 549 throws com.sun.star.lang.IllegalArgumentException, 550 com.sun.star.io.IOException, 551 com.sun.star.uno.Exception { 552 553 // that noargs thing for load attributes 554 String frameName = "_blank"; 555 556 XComponent oDoc = oCLoader.loadComponentFromURL( 557 fileName, frameName, 0, Args); 558 559 if (oDoc == null) { 560 return null; 561 } 562 DesktopTools.bringWindowToFront(oDoc); 563 564 return oDoc; 565 } 566 567 public XComponent openDoc(String kind, String frameName) 568 throws com.sun.star.lang.IllegalArgumentException, 569 com.sun.star.io.IOException, 570 com.sun.star.uno.Exception { 571 572 // that noargs thing for load attributes 573 PropertyValue[] Args = null; 574 if (kind.equals("simpress")) { 575 Args = new PropertyValue[1]; 576 PropertyValue Arg = new PropertyValue(); 577 Arg.Name = "OpenFlags"; 578 Arg.Value = "S"; 579 Arg.Handle = -1; 580 Arg.State = PropertyState.DEFAULT_VALUE; 581 Args[0] = Arg; 582 } else { 583 Args = new PropertyValue[0]; 584 } 585 586 if (frameName == null) { 587 frameName = "_blank"; 588 } 589 // load a blank a doc 590 XComponent oDoc = oCLoader.loadComponentFromURL("private:factory/" + kind, frameName, 40, Args); 591 DesktopTools.bringWindowToFront(oDoc); 592 593 return oDoc; 594 595 } // finished openDoc 596 597 public XComponent openDoc(String kind, String frameName, PropertyValue[] mediaDescriptor) 598 throws com.sun.star.lang.IllegalArgumentException, 599 com.sun.star.io.IOException, 600 com.sun.star.uno.Exception { 601 602 if (frameName == null) { 603 frameName = "_blank"; 604 } 605 // load a blank a doc 606 XComponent oDoc = oCLoader.loadComponentFromURL( 607 "private:factory/" + kind, frameName, 40, mediaDescriptor); 608 DesktopTools.bringWindowToFront(oDoc); 609 610 return oDoc; 611 612 } // finished openDoc 613 614 // query for XServiceInfo 615 public Object queryXServiceInfo(Object oObj) { 616 if (oObj != null) { 617 XServiceInfo oInfo = (XServiceInfo) UnoRuntime.queryInterface( 618 XServiceInfo.class, oObj); 619 System.out.println("!!!! XServiceInfo n.a. !!!! "); 620 } else { 621 System.out.println("Object is empty!!!! "); 622 } 623 return null; 624 } // finish queryXServiceInfo 625 }