1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 package com.sun.star.wizards.document; 28 29 import com.sun.star.lang.IllegalArgumentException; 30 import com.sun.star.lang.XComponent; 31 import com.sun.star.lang.XMultiServiceFactory; 32 import com.sun.star.container.XNameAccess; 33 import com.sun.star.document.XDocumentProperties; 34 import com.sun.star.document.XDocumentPropertiesSupplier; 35 import com.sun.star.document.XEventsSupplier; 36 import com.sun.star.document.XTypeDetection; 37 import com.sun.star.drawing.XDrawPagesSupplier; 38 import com.sun.star.wizards.common.*; 39 import com.sun.star.awt.Rectangle; 40 import com.sun.star.awt.VclWindowPeerAttribute; 41 import com.sun.star.awt.WindowAttribute; 42 import com.sun.star.awt.WindowDescriptor; 43 import com.sun.star.awt.XToolkit; 44 import com.sun.star.awt.XWindow; 45 import com.sun.star.awt.XWindowPeer; 46 import com.sun.star.beans.PropertyValue; 47 import com.sun.star.beans.PropertyVetoException; 48 import com.sun.star.sheet.XCellRangeData; 49 import com.sun.star.sheet.XSpreadsheetDocument; 50 import com.sun.star.table.XCellRange; 51 import com.sun.star.task.XInteractionHandler; 52 import com.sun.star.text.XTextDocument; 53 import com.sun.star.uno.Exception; 54 import com.sun.star.uno.UnoRuntime; 55 import com.sun.star.uno.XInterface; 56 import com.sun.star.lang.XServiceInfo; 57 import com.sun.star.frame.XComponentLoader; 58 import com.sun.star.frame.XDesktop; 59 import com.sun.star.frame.XFrame; 60 import com.sun.star.frame.XFrames; 61 import com.sun.star.frame.XFramesSupplier; 62 import com.sun.star.frame.XModel; 63 import com.sun.star.frame.XStorable; 64 import com.sun.star.frame.XTerminateListener; 65 import com.sun.star.util.XCloseable; 66 import com.sun.star.util.XModifiable; 67 68 public class OfficeDocument 69 { 70 71 private XWindowPeer xWindowPeer; 72 private XMultiServiceFactory xMSF; 73 74 /** Creates a new instance of OfficeDocument */ 75 public OfficeDocument(XMultiServiceFactory _xMSF) 76 { 77 xMSF = _xMSF; 78 } 79 80 public static void attachEventCall(XComponent xComponent, String EventName, String EventType, String EventURL) 81 { 82 try 83 { 84 XEventsSupplier xEventsSuppl = UnoRuntime.queryInterface(XEventsSupplier.class, xComponent); 85 PropertyValue[] oEventProperties = new PropertyValue[2]; 86 oEventProperties[0] = new PropertyValue(); 87 oEventProperties[0].Name = "EventType"; 88 oEventProperties[0].Value = EventType; // "Service", "StarBasic" 89 oEventProperties[1] = new PropertyValue(); 90 oEventProperties[1].Name = "Script"; //PropertyNames.URL; 91 oEventProperties[1].Value = EventURL; 92 xEventsSuppl.getEvents().replaceByName(EventName, oEventProperties); 93 } 94 catch (Exception exception) 95 { 96 exception.printStackTrace(System.out); 97 } 98 } 99 100 public static void dispose(XMultiServiceFactory xMSF, XComponent xComponent) 101 { 102 try 103 { 104 if (xComponent != null) 105 { 106 XModifiable xModified = UnoRuntime.queryInterface(XModifiable.class, xComponent); 107 XModel xModel = UnoRuntime.queryInterface(XModel.class, xComponent); 108 XFrame xFrame = xModel.getCurrentController().getFrame(); 109 if (xModified.isModified()) 110 { 111 xModified.setModified(false); 112 } 113 Desktop.dispatchURL(xMSF, ".uno:CloseDoc", xFrame); 114 } 115 } 116 catch (PropertyVetoException exception) 117 { 118 exception.printStackTrace(System.out); 119 } 120 } 121 122 /** 123 * Create a new office document, attached to the given frame. 124 * @param desktop 125 * @param frame 126 * @param sDocumentType e.g. swriter, scalc, ( simpress, scalc : not tested) 127 * @return the document Component (implements XComponent) object ( XTextDocument, or XSpreadsheedDocument ) 128 */ 129 public static Object createNewDocument(XFrame frame, String sDocumentType, boolean preview, boolean readonly) 130 { 131 132 PropertyValue[] loadValues = new PropertyValue[2]; 133 loadValues[0] = new PropertyValue(); 134 loadValues[0].Name = PropertyNames.READ_ONLY; 135 loadValues[0].Value = readonly ? Boolean.TRUE : Boolean.FALSE; 136 loadValues[1] = new PropertyValue(); 137 loadValues[1].Name = "Preview"; 138 loadValues[1].Value = preview ? Boolean.TRUE : Boolean.FALSE; 139 140 Object oDocument = null; 141 com.sun.star.frame.XComponentLoader xComponentLoader = null; 142 XInterface xInterface = null; 143 String sURL = "private:factory/" + sDocumentType; 144 145 try 146 { 147 xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, frame); 148 /*if (frame.getName() == null || frame.getName().equals(PropertyNames.EMPTY_STRING)); 149 frame.setName("T" + System.currentTimeMillis());*/ 150 XComponent xComponent = xComponentLoader.loadComponentFromURL(sURL, "_self", 0, loadValues); 151 152 if (sDocumentType.equals("swriter")) 153 { 154 oDocument = UnoRuntime.queryInterface(XTextDocument.class, xComponent); 155 } 156 else if (sDocumentType.equals("scalc")) 157 { 158 oDocument = UnoRuntime.queryInterface(XSpreadsheetDocument.class, xComponent); 159 //TODO: 160 // else if (sDocumentType == "simpress") 161 // else if (sDocumentType == "sdraw") 162 } 163 } 164 catch (Exception exception) 165 { 166 exception.printStackTrace(System.out); 167 } 168 return oDocument; 169 } 170 171 public static XFrame createNewFrame(XMultiServiceFactory xMSF, XTerminateListener listener) 172 { 173 return createNewFrame(xMSF, listener, "_blank"); 174 } 175 176 public static XFrame createNewFrame(XMultiServiceFactory xMSF, XTerminateListener listener, String FrameName) 177 { 178 XFrame xFrame = null; 179 if (FrameName.equalsIgnoreCase("WIZARD_LIVE_PREVIEW")) 180 { 181 xFrame = createNewPreviewFrame(xMSF, listener); 182 } 183 else 184 { 185 XFrame xF = UnoRuntime.queryInterface(XFrame.class, Desktop.getDesktop(xMSF)); 186 xFrame = xF.findFrame(FrameName, 0); 187 if (listener != null) 188 { 189 XFramesSupplier xFS = UnoRuntime.queryInterface(XFramesSupplier.class, xF); 190 XFrames xFF = xFS.getFrames(); 191 xFF.remove(xFrame); 192 XDesktop xDesktop = UnoRuntime.queryInterface(XDesktop.class, xF); 193 xDesktop.addTerminateListener(listener); 194 } 195 } 196 return xFrame; 197 } 198 199 public static XFrame createNewPreviewFrame(XMultiServiceFactory xMSF, XTerminateListener listener) 200 { 201 XToolkit xToolkit = null; 202 try 203 { 204 xToolkit = UnoRuntime.queryInterface(XToolkit.class, xMSF.createInstance("com.sun.star.awt.Toolkit")); 205 } 206 catch (Exception e) 207 { 208 // TODO Auto-generated catch block 209 e.printStackTrace(); 210 } 211 212 //describe the window and its properties 213 WindowDescriptor aDescriptor = new WindowDescriptor(); 214 aDescriptor.Type = com.sun.star.awt.WindowClass.TOP; 215 aDescriptor.WindowServiceName = "window"; 216 aDescriptor.ParentIndex = -1; 217 aDescriptor.Parent = null; 218 aDescriptor.Bounds = new Rectangle(10, 10, 640, 480); 219 aDescriptor.WindowAttributes = WindowAttribute.BORDER | 220 WindowAttribute.MOVEABLE | 221 WindowAttribute.SIZEABLE | 222 //WindowAttribute.CLOSEABLE | 223 VclWindowPeerAttribute.CLIPCHILDREN; 224 225 //create a new blank container window 226 XWindowPeer xPeer = null; 227 try 228 { 229 xPeer = UnoRuntime.queryInterface(XWindowPeer.class, xToolkit.createWindow(aDescriptor)); 230 } 231 catch (IllegalArgumentException e) 232 { 233 // TODO Auto-generated catch block 234 e.printStackTrace(); 235 } 236 XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, xPeer); 237 238 //define some further properties of the frame window 239 //if it's needed .-) 240 //xPeer->setBackground(...); 241 242 //create new empty frame and set window on it 243 XFrame xFrame = null; 244 try 245 { 246 xFrame = UnoRuntime.queryInterface(XFrame.class, xMSF.createInstance("com.sun.star.frame.Frame")); 247 } 248 catch (Exception e) 249 { 250 // TODO Auto-generated catch block 251 e.printStackTrace(); 252 } 253 xFrame.initialize(xWindow); 254 255 //from now this frame is useable ... 256 //and not part of the desktop tree. 257 //You are alone with him .-) 258 259 if (listener != null) 260 { 261 Desktop.getDesktop(xMSF).addTerminateListener(listener); 262 } 263 264 return xFrame; 265 266 } 267 268 public static Object load(XInterface xInterface, String sURL, String sFrame, PropertyValue[] xValues) 269 { 270 // XComponent xComponent = null; 271 Object oDocument = null; 272 com.sun.star.frame.XComponentLoader xComponentLoader = null; 273 //XInterface xInterface = null; 274 try 275 { 276 xComponentLoader = UnoRuntime.queryInterface(XComponentLoader.class, xInterface); 277 com.sun.star.lang.XComponent xComponent = xComponentLoader.loadComponentFromURL(sURL, sFrame, 0, xValues); 278 279 XServiceInfo xComponentService = UnoRuntime.queryInterface(XServiceInfo.class, xComponent); 280 if (xComponentService.supportsService("com.sun.star.text.TextDocument")) 281 { 282 oDocument = UnoRuntime.queryInterface(XTextDocument.class, xComponent); //TODO: write if clauses for Calc, Impress and Draw 283 } 284 } 285 catch (Exception exception) 286 { 287 exception.printStackTrace(System.out); 288 } 289 return oDocument; 290 } 291 292 public static boolean store(XMultiServiceFactory xMSF, XComponent xComponent, String StorePath, String FilterName, boolean bStoreToUrl) 293 { 294 try 295 { 296 XStorable xStoreable = UnoRuntime.queryInterface(XStorable.class, xComponent); 297 PropertyValue[] oStoreProperties; 298 if (FilterName.length() > 0) 299 { 300 oStoreProperties = new PropertyValue[2]; 301 oStoreProperties[0] = new PropertyValue(); 302 oStoreProperties[0].Name = "FilterName"; 303 oStoreProperties[0].Value = FilterName; 304 oStoreProperties[1] = new PropertyValue(); 305 oStoreProperties[1].Name = "InteractionHandler"; 306 oStoreProperties[1].Value = UnoRuntime.queryInterface(XInteractionHandler.class, xMSF.createInstance("com.sun.star.comp.uui.UUIInteractionHandler")); 307 } 308 else 309 { 310 oStoreProperties = new PropertyValue[0]; 311 } 312 if (bStoreToUrl) 313 { 314 xStoreable.storeToURL(StorePath, oStoreProperties); 315 } 316 else 317 { 318 xStoreable.storeAsURL(StorePath, oStoreProperties); 319 } 320 return true; 321 } 322 catch (Exception exception) 323 { 324 325 exception.printStackTrace(System.out); 326 return false; 327 } 328 } 329 330 public static boolean close(XComponent xComponent) 331 { 332 boolean bState = false; 333 XModel xModel = UnoRuntime.queryInterface(XModel.class, xComponent); 334 335 if (xModel != null) 336 { 337 XCloseable xCloseable = UnoRuntime.queryInterface(XCloseable.class, xModel); 338 339 if (xCloseable != null) 340 { 341 try 342 { 343 xCloseable.close(true); 344 bState = true; 345 } 346 catch (com.sun.star.util.CloseVetoException exCloseVeto) 347 { 348 System.out.println("could not close doc"); 349 bState = false; 350 } 351 } 352 else 353 { 354 XComponent xDisposeable = UnoRuntime.queryInterface(XComponent.class, xModel); 355 xDisposeable.dispose(); 356 bState = true; 357 } 358 } 359 return bState; 360 } 361 362 public static void ArraytoCellRange(Object[][] datalist, Object oTable, int xpos, int ypos) 363 { 364 try 365 { 366 int rowcount = datalist.length; 367 if (rowcount > 0) 368 { 369 int colcount = datalist[0].length; 370 if (colcount > 0) 371 { 372 XCellRange xCellRange = UnoRuntime.queryInterface(XCellRange.class, oTable); 373 XCellRange xNewRange = xCellRange.getCellRangeByPosition(xpos, ypos, (colcount + xpos) - 1, (rowcount + ypos) - 1); 374 XCellRangeData xDataArray = UnoRuntime.queryInterface(XCellRangeData.class, xNewRange); 375 xDataArray.setDataArray(datalist); 376 } 377 } 378 } 379 catch (Exception e) 380 { 381 e.printStackTrace(); 382 } 383 } 384 385 public static PropertyValue[] getFileMediaDecriptor(XMultiServiceFactory xmsf, String url) 386 throws Exception 387 { 388 Object typeDetect = xmsf.createInstance("com.sun.star.document.TypeDetection"); 389 390 PropertyValue[][] mediaDescr = new PropertyValue[1][1]; 391 mediaDescr[0][0] = new PropertyValue(); 392 mediaDescr[0][0].Name = PropertyNames.URL; 393 mediaDescr[0][0].Value = url; 394 395 String type = UnoRuntime.queryInterface(XTypeDetection.class, typeDetect).queryTypeByDescriptor(mediaDescr, true); 396 397 XNameAccess xNameAccess = UnoRuntime.queryInterface(XNameAccess.class, typeDetect); 398 if (type.equals(PropertyNames.EMPTY_STRING)) 399 { 400 return null; 401 } 402 else 403 { 404 return (PropertyValue[]) xNameAccess.getByName(type); 405 } 406 } 407 408 public static PropertyValue[] getTypeMediaDescriptor(XMultiServiceFactory xmsf, String type) 409 throws Exception 410 { 411 Object typeDetect = xmsf.createInstance("com.sun.star.document.TypeDetection"); 412 XNameAccess xNameAccess = UnoRuntime.queryInterface(XNameAccess.class, typeDetect); 413 return (PropertyValue[]) xNameAccess.getByName(type); 414 } 415 416 /** 417 * returns the count of slides in a presentation, 418 * or the count of pages in a draw document. 419 * @param model a presentation or a draw document 420 * @return the number of slides/pages in the given document. 421 */ 422 public static int getSlideCount(Object model) 423 { 424 XDrawPagesSupplier xDrawPagesSupplier = UnoRuntime.queryInterface(XDrawPagesSupplier.class, model); 425 return xDrawPagesSupplier.getDrawPages().getCount(); 426 } 427 428 public static XDocumentProperties getDocumentProperties(Object document) 429 { 430 XDocumentPropertiesSupplier xDocumentPropertiesSupplier = UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, document); 431 return xDocumentPropertiesSupplier.getDocumentProperties(); 432 } 433 434 public static int showMessageBox(XMultiServiceFactory xMSF, String windowServiceName, int windowAttribute, String MessageText) 435 { 436 // if (getWindowPeer() != null) 437 // return SystemDialog.showMessageBox(xMSF, xWindowPeer, windowServiceName, windowAttribute, MessageText); 438 // else 439 return SystemDialog.showMessageBox(xMSF, windowServiceName, windowAttribute, MessageText); 440 } 441 442 /** 443 * @return Returns the xWindowPeer. 444 */ 445 public XWindowPeer getWindowPeer() 446 { 447 return xWindowPeer; 448 } 449 450 /** 451 * @param windowPeer The xWindowPeer to set. 452 * Should be called as soon as a Windowpeer of a wizard dialog is available 453 * The windowpeer is needed to call a Messagebox 454 */ 455 public void setWindowPeer(XWindowPeer windowPeer) 456 { 457 xWindowPeer = windowPeer; 458 } 459 } 460