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