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.common; 28 29 // import java.util.Date; 30 31 // import com.sun.star.awt.XToolkit; 32 import com.sun.star.beans.PropertyValue; 33 // import com.sun.star.frame.XDesktop; 34 // import com.sun.star.frame.XFrame; 35 // import com.sun.star.frame.XFramesSupplier; 36 37 import com.sun.star.lang.WrappedTargetException; 38 import com.sun.star.lang.XComponent; 39 import com.sun.star.lang.XMultiComponentFactory; 40 import com.sun.star.lang.XMultiServiceFactory; 41 import com.sun.star.sheet.XSpreadsheetDocument; 42 import com.sun.star.text.XTextDocument; 43 import com.sun.star.uno.Any; 44 import com.sun.star.uno.UnoRuntime; 45 import com.sun.star.uno.XComponentContext; 46 import com.sun.star.uno.XNamingService; 47 import com.sun.star.util.XURLTransformer; 48 import com.sun.star.lang.Locale; 49 import com.sun.star.uno.XInterface; 50 import com.sun.star.bridge.XUnoUrlResolver; 51 import com.sun.star.comp.helper.Bootstrap; 52 import com.sun.star.container.NoSuchElementException; 53 import com.sun.star.container.XEnumeration; 54 import com.sun.star.container.XHierarchicalNameAccess; 55 import com.sun.star.container.XNameAccess; 56 import com.sun.star.util.XStringSubstitution; 57 import com.sun.star.frame.*; 58 import com.sun.star.i18n.KParseType; 59 import com.sun.star.i18n.ParseResult; 60 import com.sun.star.i18n.XCharacterClassification; 61 62 public class Desktop 63 { 64 65 /** Creates a new instance of Desktop */ 66 public Desktop() 67 { 68 } 69 70 public static XDesktop getDesktop(XMultiServiceFactory xMSF) 71 { 72 com.sun.star.uno.XInterface xInterface = null; 73 XDesktop xDesktop = null; 74 if (xMSF != null) 75 { 76 try 77 { 78 xInterface = (com.sun.star.uno.XInterface) xMSF.createInstance("com.sun.star.frame.Desktop"); 79 xDesktop = UnoRuntime.queryInterface(XDesktop.class, xInterface); 80 } 81 catch (Exception exception) 82 { 83 exception.printStackTrace(System.out); 84 } 85 } 86 else 87 { 88 System.out.println("Can't create a desktop. null pointer !"); 89 } 90 return xDesktop; 91 } 92 93 public static XFrame getActiveFrame(XMultiServiceFactory xMSF) 94 { 95 XDesktop xDesktop = getDesktop(xMSF); 96 XFramesSupplier xFrameSuppl = UnoRuntime.queryInterface(XFramesSupplier.class, xDesktop); 97 return xFrameSuppl.getActiveFrame(); 98 } 99 100 public static XComponent getActiveComponent(XMultiServiceFactory _xMSF) 101 { 102 XFrame xFrame = getActiveFrame(_xMSF); 103 return UnoRuntime.queryInterface(XComponent.class, xFrame.getController().getModel()); 104 } 105 106 public static XTextDocument getActiveTextDocument(XMultiServiceFactory _xMSF) 107 { 108 XComponent xComponent = getActiveComponent(_xMSF); 109 return UnoRuntime.queryInterface(XTextDocument.class, xComponent); 110 } 111 112 public static XSpreadsheetDocument getActiveSpreadsheetDocument(XMultiServiceFactory _xMSF) 113 { 114 XComponent xComponent = getActiveComponent(_xMSF); 115 return UnoRuntime.queryInterface(XSpreadsheetDocument.class, xComponent); 116 } 117 118 public static XDispatch getDispatcher(XMultiServiceFactory xMSF, XFrame xFrame, String _stargetframe, com.sun.star.util.URL oURL) 119 { 120 try 121 { 122 com.sun.star.util.URL[] oURLArray = new com.sun.star.util.URL[1]; 123 oURLArray[0] = oURL; 124 XDispatchProvider xDispatchProvider = UnoRuntime.queryInterface(XDispatchProvider.class, xFrame); 125 return xDispatchProvider.queryDispatch(oURLArray[0], _stargetframe, FrameSearchFlag.ALL); // "_self" 126 } 127 catch (Exception e) 128 { 129 e.printStackTrace(System.out); 130 } 131 return null; 132 } 133 134 public static com.sun.star.util.URL getDispatchURL(XMultiServiceFactory xMSF, String _sURL) 135 { 136 try 137 { 138 Object oTransformer = xMSF.createInstance("com.sun.star.util.URLTransformer"); 139 XURLTransformer xTransformer = UnoRuntime.queryInterface(XURLTransformer.class, oTransformer); 140 com.sun.star.util.URL[] oURL = new com.sun.star.util.URL[1]; 141 oURL[0] = new com.sun.star.util.URL(); 142 oURL[0].Complete = _sURL; 143 xTransformer.parseStrict(oURL); 144 return oURL[0]; 145 } 146 catch (Exception e) 147 { 148 e.printStackTrace(System.out); 149 } 150 return null; 151 } 152 153 public static void dispatchURL(XMultiServiceFactory xMSF, String sURL, XFrame xFrame, String _stargetframe) 154 { 155 com.sun.star.util.URL oURL = getDispatchURL(xMSF, sURL); 156 XDispatch xDispatch = getDispatcher(xMSF, xFrame, _stargetframe, oURL); 157 dispatchURL(xDispatch, oURL); 158 } 159 160 public static void dispatchURL(XMultiServiceFactory xMSF, String sURL, XFrame xFrame) 161 { 162 dispatchURL(xMSF, sURL, xFrame, PropertyNames.EMPTY_STRING); 163 } 164 165 public static void dispatchURL(XDispatch _xDispatch, com.sun.star.util.URL oURL) 166 { 167 PropertyValue[] oArg = new PropertyValue[0]; 168 _xDispatch.dispatch(oURL, oArg); 169 } 170 171 public static XMultiComponentFactory getMultiComponentFactory() throws com.sun.star.uno.Exception, RuntimeException, java.lang.Exception 172 { 173 XComponentContext xcomponentcontext = Bootstrap.createInitialComponentContext(null); 174 // initial serviceManager 175 return xcomponentcontext.getServiceManager(); 176 } 177 178 public static XMultiServiceFactory connect(String connectStr) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException, Exception 179 { 180 XMultiComponentFactory componentFactory = getMultiComponentFactory(); 181 Object xUrlResolver = componentFactory.createInstanceWithContext( "com.sun.star.bridge.UnoUrlResolver", null ); 182 XUnoUrlResolver urlResolver = UnoRuntime.queryInterface(XUnoUrlResolver.class, xUrlResolver); 183 return UnoRuntime.queryInterface(XMultiServiceFactory.class, urlResolver.resolve( connectStr ) ); 184 } 185 186 public static String getIncrementSuffix(XNameAccess xElementContainer, String ElementName) 187 { 188 boolean bElementexists = true; 189 int i = 1; 190 String sIncSuffix = PropertyNames.EMPTY_STRING; 191 String BaseName = ElementName; 192 while (bElementexists) 193 { 194 bElementexists = xElementContainer.hasByName(ElementName); 195 if (bElementexists) 196 { 197 i += 1; 198 ElementName = BaseName + Integer.toString(i); 199 } 200 } 201 if (i > 1) 202 { 203 sIncSuffix = Integer.toString(i); 204 } 205 return sIncSuffix; 206 } 207 208 public static String getIncrementSuffix(XHierarchicalNameAccess xElementContainer, String ElementName) 209 { 210 boolean bElementexists = true; 211 int i = 1; 212 String sIncSuffix = PropertyNames.EMPTY_STRING; 213 String BaseName = ElementName; 214 while (bElementexists) 215 { 216 bElementexists = xElementContainer.hasByHierarchicalName(ElementName); 217 if (bElementexists) 218 { 219 i += 1; 220 ElementName = BaseName + Integer.toString(i); 221 } 222 } 223 if (i > 1) 224 { 225 sIncSuffix = Integer.toString(i); 226 } 227 return sIncSuffix; 228 } 229 230 public static int checkforfirstSpecialCharacter(XMultiServiceFactory _xMSF, String _sString, Locale _aLocale) 231 { 232 try 233 { 234 int nStartFlags = com.sun.star.i18n.KParseTokens.ANY_LETTER_OR_NUMBER + com.sun.star.i18n.KParseTokens.ASC_UNDERSCORE; 235 Object ocharservice = _xMSF.createInstance("com.sun.star.i18n.CharacterClassification"); 236 XCharacterClassification xCharacterClassification = UnoRuntime.queryInterface(XCharacterClassification.class, ocharservice); 237 ParseResult aResult = xCharacterClassification.parsePredefinedToken(KParseType.IDENTNAME, _sString, 0, _aLocale, nStartFlags, PropertyNames.EMPTY_STRING, nStartFlags, PropertyNames.SPACE); 238 return aResult.EndPos; 239 } 240 catch (Exception e) 241 { 242 e.printStackTrace(System.out); 243 return -1; 244 } 245 } 246 247 public static String removeSpecialCharacters(XMultiServiceFactory _xMSF, Locale _aLocale, String _sname) 248 { 249 String snewname = _sname; 250 int i = 0; 251 while (i < snewname.length()) 252 { 253 i = Desktop.checkforfirstSpecialCharacter(_xMSF, snewname, _aLocale); 254 if (i < snewname.length()) 255 { 256 String sspecialchar = snewname.substring(i, i + 1); 257 snewname = JavaTools.replaceSubString(snewname, PropertyNames.EMPTY_STRING, sspecialchar); 258 } 259 } 260 return snewname; 261 } 262 263 /** 264 * Checks if the passed Element Name already exists in the ElementContainer. If yes it appends a 265 * suffix to make it unique 266 * @param xElementContainer 267 * @param sElementName 268 * @return a unique Name ready to be added to the container. 269 */ 270 public static String getUniqueName(XNameAccess xElementContainer, String sElementName) 271 { 272 String sIncSuffix = getIncrementSuffix(xElementContainer, sElementName); 273 return sElementName + sIncSuffix; 274 } 275 276 /** 277 * Checks if the passed Element Name already exists in the ElementContainer. If yes it appends a 278 * suffix to make it unique 279 * @param xElementContainer 280 * @param sElementName 281 * @return a unique Name ready to be added to the container. 282 */ 283 public static String getUniqueName(XHierarchicalNameAccess xElementContainer, String sElementName) 284 { 285 String sIncSuffix = getIncrementSuffix(xElementContainer, sElementName); 286 return sElementName + sIncSuffix; 287 } 288 289 /** 290 * Checks if the passed Element Name already exists in the list If yes it appends a 291 * suffix to make it unique 292 * @param _slist 293 * @param _sElementName 294 * @param _sSuffixSeparator 295 * @return a unique Name not being in the passed list. 296 */ 297 public static String getUniqueName(String[] _slist, String _sElementName, String _sSuffixSeparator) 298 { 299 int a = 2; 300 String scompname = _sElementName; 301 boolean bElementexists = true; 302 if (_slist == null) 303 { 304 return _sElementName; 305 } 306 if (_slist.length == 0) 307 { 308 return _sElementName; 309 } 310 while (bElementexists) 311 { 312 for (int i = 0; i < _slist.length; i++) 313 { 314 if (JavaTools.FieldInList(_slist, scompname) == -1) 315 { 316 return scompname; 317 } 318 } 319 scompname = _sElementName + _sSuffixSeparator + a++; 320 } 321 return PropertyNames.EMPTY_STRING; 322 } 323 324 /** 325 * @deprecated use Configuration.getConfigurationRoot() with the same parameters instead 326 * @param xMSF 327 * @param KeyName 328 * @param bForUpdate 329 * @return 330 */ 331 public static XInterface getRegistryKeyContent(XMultiServiceFactory xMSF, String KeyName, boolean bForUpdate) 332 { 333 try 334 { 335 Object oConfigProvider; 336 PropertyValue[] aNodePath = new PropertyValue[1]; 337 oConfigProvider = xMSF.createInstance("com.sun.star.configuration.ConfigurationProvider"); 338 aNodePath[0] = new PropertyValue(); 339 aNodePath[0].Name = "nodepath"; 340 aNodePath[0].Value = KeyName; 341 XMultiServiceFactory xMSFConfig = UnoRuntime.queryInterface(XMultiServiceFactory.class, oConfigProvider); 342 if (bForUpdate) 343 { 344 return (XInterface) xMSFConfig.createInstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess", aNodePath); 345 } 346 else 347 { 348 return (XInterface) xMSFConfig.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", aNodePath); 349 } 350 } 351 catch (Exception exception) 352 { 353 exception.printStackTrace(System.out); 354 return null; 355 } 356 } 357 358 /** 359 * @deprecated used to retrieve the most common paths used in the office application 360 * @author bc93774 361 * 362 */ 363 public class OfficePathRetriever 364 { 365 366 public String TemplatePath; 367 public String BitmapPath; 368 public String UserTemplatePath; 369 public String WorkPath; 370 371 public OfficePathRetriever(XMultiServiceFactory xMSF) 372 { 373 try 374 { 375 TemplatePath = FileAccess.getOfficePath(xMSF, "Template", "share", "/wizard"); 376 UserTemplatePath = FileAccess.getOfficePath(xMSF, "Template", "user", PropertyNames.EMPTY_STRING); 377 BitmapPath = FileAccess.combinePaths(xMSF, TemplatePath, "/../wizard/bitmap"); 378 WorkPath = FileAccess.getOfficePath(xMSF, "Work", PropertyNames.EMPTY_STRING, PropertyNames.EMPTY_STRING); 379 } 380 catch (NoValidPathException nopathexception) 381 { 382 } 383 } 384 } 385 386 public static String getTemplatePath(XMultiServiceFactory _xMSF) 387 { 388 try 389 { 390 return FileAccess.getOfficePath(_xMSF, "Template", "share", "/wizard"); 391 } 392 catch (NoValidPathException nopathexception) 393 { 394 } 395 return PropertyNames.EMPTY_STRING; 396 } 397 398 public static String getUserTemplatePath(XMultiServiceFactory _xMSF) 399 { 400 try 401 { 402 return FileAccess.getOfficePath(_xMSF, "Template", "user", PropertyNames.EMPTY_STRING); 403 } 404 catch (NoValidPathException nopathexception) 405 { 406 } 407 return PropertyNames.EMPTY_STRING; 408 } 409 410 public static String getBitmapPath(XMultiServiceFactory _xMSF) 411 { 412 try 413 { 414 return FileAccess.combinePaths(_xMSF, getTemplatePath(_xMSF), "/../wizard/bitmap"); 415 } 416 catch (NoValidPathException nopathexception) 417 { 418 } 419 return PropertyNames.EMPTY_STRING; 420 } 421 422 public static String getWorkPath(XMultiServiceFactory _xMSF) 423 { 424 try 425 { 426 return FileAccess.getOfficePath(_xMSF, "Work", PropertyNames.EMPTY_STRING, PropertyNames.EMPTY_STRING); 427 } 428 catch (NoValidPathException nopathexception) 429 { 430 } 431 return PropertyNames.EMPTY_STRING; 432 } 433 434 public static XStringSubstitution createStringSubstitution(XMultiServiceFactory xMSF) 435 { 436 Object xPathSubst = null; 437 try 438 { 439 xPathSubst = xMSF.createInstance("com.sun.star.util.PathSubstitution"); 440 } 441 catch (com.sun.star.uno.Exception e) 442 { 443 e.printStackTrace(); 444 } 445 if (xPathSubst != null) 446 { 447 return UnoRuntime.queryInterface(XStringSubstitution.class, xPathSubst); 448 } 449 else 450 { 451 return null; 452 } 453 } 454 455 /** 456 * This method searches (and hopefully finds...) a frame 457 * with a componentWindow. 458 * It does it in three phases: 459 * 1. Check if the given desktop argument has a componentWindow. 460 * If it is null, the myFrame argument is taken. 461 * 2. Go up the tree of frames and search a frame with a component window. 462 * 3. Get from the desktop all the components, and give the first one 463 * which has a frame. 464 * @param xMSF 465 * @param myFrame 466 * @param desktop 467 * @return 468 * @throws NoSuchElementException 469 * @throws WrappedTargetException 470 */ 471 public static XFrame findAFrame(XMultiServiceFactory xMSF, XFrame myFrame, XFrame desktop) 472 throws NoSuchElementException, 473 WrappedTargetException 474 { 475 if (desktop == null) 476 { 477 desktop = myFrame; // we go up in the tree... 478 } 479 while (desktop != null && desktop.getComponentWindow() == null) 480 { 481 desktop = desktop.findFrame("_parent", FrameSearchFlag.PARENT); 482 } 483 if (desktop == null) 484 { 485 486 for (XEnumeration e = Desktop.getDesktop(xMSF).getComponents().createEnumeration(); e.hasMoreElements();) 487 { 488 489 Object comp = ((Any) e.nextElement()).getObject(); 490 XModel xModel = UnoRuntime.queryInterface(XModel.class, comp); 491 XFrame xFrame = xModel.getCurrentController().getFrame(); 492 493 if (xFrame != null && xFrame.getComponentWindow() != null) 494 { 495 return xFrame; 496 } 497 } 498 } 499 return desktop; 500 } 501 } 502