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