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 com.sun.star.beans.*; 26 import com.sun.star.container.*; 27 import com.sun.star.lang.WrappedTargetException; 28 import com.sun.star.lang.XMultiServiceFactory; 29 import com.sun.star.lang.XSingleServiceFactory; 30 import com.sun.star.uno.AnyConverter; 31 import com.sun.star.uno.Exception; 32 import com.sun.star.uno.UnoRuntime; 33 import com.sun.star.lang.Locale; 34 import com.sun.star.util.XChangesBatch; 35 36 /** 37 * This class gives access to the OO configuration api. 38 * It contains 4 get and 4 set convenience methods for getting and settings properties 39 * in the configuration. <br/> 40 * For the get methods, two parameters must be given: name and parent, where name is the 41 * name of the property, parent is a HierarchyElement (::com::sun::star::configuration::HierarchyElement)<br/> 42 * The get and set methods support hieryrchical property names like "options/gridX". <br/> 43 * NOTE: not yet supported, but sometime later, 44 * If you will ommit the "parent" parameter, then the "name" parameter must be in hierarchy form from 45 * the root of the registry. 46 * @author rpiterman 47 */ 48 public abstract class Configuration 49 { 50 getInt(String name, Object parent)51 public static int getInt(String name, Object parent) throws Exception 52 { 53 Object o = getNode(name, parent); 54 if (AnyConverter.isVoid(o)) 55 { 56 return 0; 57 } 58 return AnyConverter.toInt(o); 59 } 60 getShort(String name, Object parent)61 public static short getShort(String name, Object parent) throws Exception 62 { 63 Object o = getNode(name, parent); 64 if (AnyConverter.isVoid(o)) 65 { 66 return (short) 0; 67 } 68 return AnyConverter.toShort(o); 69 } 70 getFloat(String name, Object parent)71 public static float getFloat(String name, Object parent) throws Exception 72 { 73 Object o = getNode(name, parent); 74 if (AnyConverter.isVoid(o)) 75 { 76 return (float) 0; 77 } 78 return AnyConverter.toFloat(o); 79 } 80 getDouble(String name, Object parent)81 public static double getDouble(String name, Object parent) throws Exception 82 { 83 Object o = getNode(name, parent); 84 if (AnyConverter.isVoid(o)) 85 { 86 return (double) 0; 87 } 88 return AnyConverter.toDouble(o); 89 } 90 getString(String name, Object parent)91 public static String getString(String name, Object parent) throws Exception 92 { 93 Object o = getNode(name, parent); 94 if (AnyConverter.isVoid(o)) 95 { 96 return PropertyNames.EMPTY_STRING; 97 } 98 return (String) o; 99 } 100 getBoolean(String name, Object parent)101 public static boolean getBoolean(String name, Object parent) throws Exception 102 { 103 Object o = getNode(name, parent); 104 if (AnyConverter.isVoid(o)) 105 { 106 return false; 107 } 108 return AnyConverter.toBoolean(o); 109 } 110 getNode(String name, Object parent)111 public static Object getNode(String name, Object parent) throws Exception 112 { 113 return UnoRuntime.queryInterface(XNameAccess.class, parent).getByName(name); 114 } 115 set(int value, String name, Object parent)116 public static void set(int value, String name, Object parent) throws Exception 117 { 118 set(new Integer(value), name, parent); 119 } 120 set(short value, String name, Object parent)121 public static void set(short value, String name, Object parent) throws Exception 122 { 123 set(new Short(value), name, parent); 124 } 125 set(String value, String name, Object parent)126 public static void set(String value, String name, Object parent) throws Exception 127 { 128 set((Object) value, name, parent); 129 } 130 set(boolean value, String name, Object parent)131 public static void set(boolean value, String name, Object parent) throws Exception 132 { 133 if (value) 134 { 135 set(Boolean.TRUE, name, parent); 136 } 137 else 138 { 139 set(Boolean.FALSE, name, parent); 140 } 141 } 142 set(Object value, String name, Object parent)143 public static void set(Object value, String name, Object parent) throws com.sun.star.lang.IllegalArgumentException, PropertyVetoException, UnknownPropertyException, WrappedTargetException 144 { 145 UnoRuntime.queryInterface(XHierarchicalPropertySet.class, parent).setHierarchicalPropertyValue(name, value); 146 } 147 148 /** Creates a new instance of RegistryEntry 149 * @param name 150 * @param parent 151 * @return 152 * @throws Exception 153 */ getConfigurationNode(String name, Object parent)154 public static Object getConfigurationNode(String name, Object parent) throws Exception 155 { 156 return UnoRuntime.queryInterface(XNameAccess.class, parent).getByName(name); 157 } 158 getConfigurationRoot(XMultiServiceFactory xmsf, String sPath, boolean updateable)159 public static Object getConfigurationRoot(XMultiServiceFactory xmsf, String sPath, boolean updateable) throws com.sun.star.uno.Exception 160 { 161 162 Object oConfigProvider; 163 oConfigProvider = xmsf.createInstance("com.sun.star.configuration.ConfigurationProvider"); 164 XMultiServiceFactory confMsf = UnoRuntime.queryInterface(XMultiServiceFactory.class, oConfigProvider); 165 166 final String sView = updateable ? "com.sun.star.configuration.ConfigurationUpdateAccess" : "com.sun.star.configuration.ConfigurationAccess"; 167 168 Object args[] = new Object[updateable ? 2 : 1]; 169 170 PropertyValue aPathArgument = new PropertyValue(); 171 aPathArgument.Name = "nodepath"; 172 aPathArgument.Value = sPath; 173 174 args[0] = aPathArgument; 175 176 if (updateable) 177 { 178 179 PropertyValue aModeArgument = new PropertyValue(); 180 aModeArgument.Name = "lazywrite"; 181 aModeArgument.Value = Boolean.FALSE; 182 183 args[1] = aModeArgument; 184 } 185 186 return confMsf.createInstanceWithArguments(sView, args); 187 } 188 getChildrenNames(Object configView)189 public static String[] getChildrenNames(Object configView) 190 { 191 XNameAccess nameAccess = UnoRuntime.queryInterface(XNameAccess.class, configView); 192 return nameAccess.getElementNames(); 193 } 194 getProductName(XMultiServiceFactory xMSF)195 public static String getProductName(XMultiServiceFactory xMSF) 196 { 197 try 198 { 199 Object oProdNameAccess = getConfigurationRoot(xMSF, "org.openoffice.Setup/Product", false); 200 return (String) Helper.getUnoObjectbyName(oProdNameAccess, "ooName"); 201 } 202 catch (Exception exception) 203 { 204 exception.printStackTrace(System.out); 205 return null; 206 } 207 } 208 getOfficeLocaleString(XMultiServiceFactory xMSF)209 public static String getOfficeLocaleString(XMultiServiceFactory xMSF) 210 { 211 String sLocale = PropertyNames.EMPTY_STRING; 212 try 213 { 214 Locale aLocLocale = new Locale(); 215 Object oMasterKey = getConfigurationRoot(xMSF, "org.openoffice.Setup/L10N/", false); 216 sLocale = (String) Helper.getUnoObjectbyName(oMasterKey, "ooLocale"); 217 } 218 catch (Exception exception) 219 { 220 exception.printStackTrace(System.out); 221 } 222 return sLocale; 223 } 224 getOfficeLocale(XMultiServiceFactory xMSF)225 public static Locale getOfficeLocale(XMultiServiceFactory xMSF) 226 { 227 Locale aLocLocale = new Locale(); 228 // Object oMasterKey = getConfigurationRoot(xMSF, "org.openoffice.Setup/L10N/", false); 229 // String sLocale = (String) Helper.getUnoObjectbyName(oMasterKey, "ooLocale"); 230 String sLocale = getOfficeLocaleString(xMSF); 231 String[] sLocaleList = JavaTools.ArrayoutofString(sLocale, "-"); 232 aLocLocale.Language = sLocaleList[0]; 233 if (sLocaleList.length > 1) 234 { 235 aLocLocale.Country = sLocaleList[1]; 236 } 237 return aLocLocale; 238 } 239 getOfficeLinguistic(XMultiServiceFactory xMSF)240 public static String getOfficeLinguistic(XMultiServiceFactory xMSF) 241 { 242 try 243 { 244 Object oMasterKey = getConfigurationRoot(xMSF, "org.openoffice.Setup/L10N/", false); 245 return (String) Helper.getUnoObjectbyName(oMasterKey, "ooLocale"); 246 } 247 catch (Exception exception) 248 { 249 exception.printStackTrace(); 250 return null; 251 } 252 } 253 254 /** 255 * This method creates a new configuration node and adds it 256 * to the given view. Note that if a node with the given name 257 * already exists it will be completely removed from 258 * the configuration. 259 * @param configView 260 * @param name 261 * @return the new created configuration node. 262 * @throws com.sun.star.lang.WrappedTargetException 263 * @throws ElementExistException 264 * @throws NoSuchElementException 265 * @throws com.sun.star.uno.Exception 266 */ addConfigNode(Object configView, String name)267 public static Object addConfigNode(Object configView, String name) throws com.sun.star.lang.WrappedTargetException, ElementExistException, NoSuchElementException, com.sun.star.uno.Exception 268 { 269 270 XNameContainer xNameContainer = UnoRuntime.queryInterface(XNameContainer.class, configView); 271 272 if (xNameContainer == null) 273 { 274 XNameReplace xNameReplace = UnoRuntime.queryInterface(XNameReplace.class, configView); 275 return xNameReplace.getByName(name); 276 } 277 else 278 { 279 280 /*if (xNameContainer.hasByName(name)) 281 xNameContainer.removeByName(name);*/ 282 283 // create a new detached set element (instance of DataSourceDescription) 284 XSingleServiceFactory xElementFactory = UnoRuntime.queryInterface(XSingleServiceFactory.class, configView); 285 286 // the new element is the result ! 287 Object newNode = xElementFactory.createInstance(); 288 // insert it - this also names the element 289 xNameContainer.insertByName(name, newNode); 290 291 return newNode; 292 } 293 } 294 removeNode(Object configView, String name)295 public static void removeNode(Object configView, String name) throws NoSuchElementException, WrappedTargetException 296 { 297 XNameContainer xNameContainer = UnoRuntime.queryInterface(XNameContainer.class, configView); 298 299 if (xNameContainer.hasByName(name)) 300 { 301 xNameContainer.removeByName(name); 302 } 303 } 304 commit(Object configView)305 public static void commit(Object configView) throws WrappedTargetException 306 { 307 XChangesBatch xUpdateControl = UnoRuntime.queryInterface(XChangesBatch.class, configView); 308 xUpdateControl.commitChanges(); 309 } 310 updateConfiguration(XMultiServiceFactory xmsf, String path, String name, ConfigNode node, Object param)311 public static void updateConfiguration(XMultiServiceFactory xmsf, String path, String name, ConfigNode node, Object param) throws com.sun.star.uno.Exception, com.sun.star.container.ElementExistException, NoSuchElementException, WrappedTargetException 312 { 313 Object view = Configuration.getConfigurationRoot(xmsf, path, true); 314 addConfigNode(path, name); 315 node.writeConfiguration(view, param); 316 XChangesBatch xUpdateControl = UnoRuntime.queryInterface(XChangesBatch.class, view); 317 xUpdateControl.commitChanges(); 318 } 319 removeNode(XMultiServiceFactory xmsf, String path, String name)320 public static void removeNode(XMultiServiceFactory xmsf, String path, String name) throws com.sun.star.uno.Exception, com.sun.star.container.ElementExistException, NoSuchElementException, WrappedTargetException 321 { 322 Object view = Configuration.getConfigurationRoot(xmsf, path, true); 323 removeNode(view, name); 324 XChangesBatch xUpdateControl = UnoRuntime.queryInterface(XChangesBatch.class, view); 325 xUpdateControl.commitChanges(); 326 } 327 getNodeDisplayNames(XNameAccess _xNameAccessNode)328 public static String[] getNodeDisplayNames(XNameAccess _xNameAccessNode) 329 { 330 String[] snames = null; 331 return getNodeChildNames(_xNameAccessNode, PropertyNames.PROPERTY_NAME); 332 } 333 getNodeChildNames(XNameAccess xNameAccessNode, String _schildname)334 public static String[] getNodeChildNames(XNameAccess xNameAccessNode, String _schildname) 335 { 336 String[] snames = null; 337 try 338 { 339 snames = xNameAccessNode.getElementNames(); 340 String[] sdisplaynames = new String[snames.length]; 341 for (int i = 0; i < snames.length; i++) 342 { 343 Object oContent = Helper.getUnoPropertyValue(xNameAccessNode.getByName(snames[i]), _schildname); 344 if (!AnyConverter.isVoid(oContent)) 345 { 346 sdisplaynames[i] = (String) Helper.getUnoPropertyValue(xNameAccessNode.getByName(snames[i]), _schildname); 347 } 348 else 349 { 350 sdisplaynames[i] = snames[i]; 351 } 352 } 353 return sdisplaynames; 354 } 355 catch (Exception e) 356 { 357 e.printStackTrace(System.out); 358 return snames; 359 } 360 } 361 getChildNodebyIndex(XNameAccess _xNameAccess, int _index)362 public static XNameAccess getChildNodebyIndex(XNameAccess _xNameAccess, int _index) 363 { 364 try 365 { 366 String[] snames = _xNameAccess.getElementNames(); 367 Object oNode = _xNameAccess.getByName(snames[_index]); 368 return UnoRuntime.queryInterface(XNameAccess.class, oNode); 369 } 370 catch (Exception e) 371 { 372 e.printStackTrace(System.out); 373 return null; 374 } 375 } 376 getChildNodebyName(XNameAccess _xNameAccessNode, String _SubNodeName)377 public static XNameAccess getChildNodebyName(XNameAccess _xNameAccessNode, String _SubNodeName) 378 { 379 try 380 { 381 if (_xNameAccessNode.hasByName(_SubNodeName)) 382 { 383 return UnoRuntime.queryInterface(XNameAccess.class, _xNameAccessNode.getByName(_SubNodeName)); 384 } 385 } 386 catch (Exception e) 387 { 388 e.printStackTrace(System.out); 389 } 390 return null; 391 } 392 getChildNodebyDisplayName(XNameAccess _xNameAccessNode, String _displayname)393 public static XNameAccess getChildNodebyDisplayName(XNameAccess _xNameAccessNode, String _displayname) 394 { 395 String[] snames = null; 396 return getChildNodebyDisplayName(_xNameAccessNode, _displayname, PropertyNames.PROPERTY_NAME); 397 } 398 getChildNodebyDisplayName(XNameAccess _xNameAccessNode, String _displayname, String _nodename)399 public static XNameAccess getChildNodebyDisplayName(XNameAccess _xNameAccessNode, String _displayname, String _nodename) 400 { 401 String[] snames = null; 402 try 403 { 404 snames = _xNameAccessNode.getElementNames(); 405 String[] sdisplaynames = new String[snames.length]; 406 for (int i = 0; i < snames.length; i++) 407 { 408 String curdisplayname = (String) Helper.getUnoPropertyValue(_xNameAccessNode.getByName(snames[i]), _nodename); 409 if (curdisplayname.equals(_displayname)) 410 { 411 return UnoRuntime.queryInterface(XNameAccess.class, _xNameAccessNode.getByName(snames[i])); 412 } 413 } 414 } 415 catch (Exception e) 416 { 417 e.printStackTrace(System.out); 418 } 419 return null; 420 } 421 getChildNodebyDisplayName(XMultiServiceFactory _xMSF, Locale _aLocale, XNameAccess _xNameAccessNode, String _displayname, String _nodename, int _nmaxcharcount)422 public static XNameAccess getChildNodebyDisplayName(XMultiServiceFactory _xMSF, Locale _aLocale, XNameAccess _xNameAccessNode, String _displayname, String _nodename, int _nmaxcharcount) 423 { 424 String[] snames = null; 425 try 426 { 427 snames = _xNameAccessNode.getElementNames(); 428 String[] sdisplaynames = new String[snames.length]; 429 for (int i = 0; i < snames.length; i++) 430 { 431 String curdisplayname = (String) Helper.getUnoPropertyValue(_xNameAccessNode.getByName(snames[i]), _nodename); 432 if ((_nmaxcharcount > 0) && (_nmaxcharcount < curdisplayname.length())) 433 { 434 curdisplayname = curdisplayname.substring(0, _nmaxcharcount); 435 } 436 curdisplayname = Desktop.removeSpecialCharacters(_xMSF, _aLocale, curdisplayname); 437 438 if (curdisplayname.equals(_displayname)) 439 { 440 return UnoRuntime.queryInterface(XNameAccess.class, _xNameAccessNode.getByName(snames[i])); 441 } 442 } 443 } 444 catch (Exception e) 445 { 446 e.printStackTrace(System.out); 447 } 448 return null; 449 } 450 } 451