1 /* 2 ************************************************************************ 3 * 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * Copyright 2000, 2010 Oracle and/or its affiliates. 7 * 8 * OpenOffice.org - a multi-platform office productivity suite 9 * 10 * This file is part of OpenOffice.org. 11 * 12 * OpenOffice.org is free software: you can redistribute it and/or modify 13 * it under the terms of the GNU Lesser General Public License version 3 14 * only, as published by the Free Software Foundation. 15 * 16 * OpenOffice.org is distributed in the hope that it will be useful, 17 * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 * GNU Lesser General Public License version 3 for more details 20 * (a copy is included in the LICENSE file that accompanied this code). 21 * 22 * You should have received a copy of the GNU Lesser General Public License 23 * version 3 along with OpenOffice.org. If not, see 24 * <http://www.openoffice.org/license.html> 25 * for a copy of the LGPLv3 License. 26 * 27 ************************************************************************/ 28 package com.sun.star.wizards.common; 29 30 import com.sun.star.beans.Property; 31 import com.sun.star.uno.UnoRuntime; 32 import com.sun.star.beans.XPropertySet; 33 import com.sun.star.beans.XPropertySetInfo; 34 import com.sun.star.uno.AnyConverter; 35 import com.sun.star.lang.XServiceInfo; 36 37 // import com.sun.star.container.XNameAccess; 38 import java.util.HashMap; 39 40 public class PropertySetHelper 41 { 42 43 protected XPropertySet m_xPropertySet; 44 private HashMap<String, Object> m_aHashMap; 45 46 public PropertySetHelper(Object _aObj) 47 { 48 if (_aObj == null) 49 { 50 return; 51 } 52 m_xPropertySet = UnoRuntime.queryInterface(XPropertySet.class, _aObj); 53 } 54 55 private HashMap<String, Object> getHashMap() 56 { 57 if (m_aHashMap == null) 58 { 59 m_aHashMap = new HashMap<String, Object>(); 60 } 61 return m_aHashMap; 62 } 63 64 /** 65 set a property, don't throw any exceptions, they will only write down as a hint in the helper debug output 66 @param _sName name of the property to set 67 @param _aValue property value as object 68 */ 69 public void setPropertyValueDontThrow(String _sName, Object _aValue) 70 { 71 try 72 { 73 setPropertyValue(_sName, _aValue); 74 } 75 catch (Exception e) 76 { 77 DebugHelper.writeInfo("Don't throw the exception with property name(" + _sName + " ) : " + e.getMessage()); 78 } 79 } 80 81 /** 82 set a property, 83 @param _sName name of the property to set 84 @param _aValue property value as object 85 * @throws java.lang.Exception 86 */ 87 public void setPropertyValue(String _sName, Object _aValue) throws java.lang.Exception 88 { 89 if (m_xPropertySet != null) 90 { 91 try 92 { 93 m_xPropertySet.setPropertyValue(_sName, _aValue); 94 } 95 // Exceptions are not from interest 96 catch (com.sun.star.beans.UnknownPropertyException e) 97 { 98 DebugHelper.writeInfo(e.getMessage()); 99 DebugHelper.exception(e); 100 } 101 catch (com.sun.star.beans.PropertyVetoException e) 102 { 103 DebugHelper.writeInfo(e.getMessage()); 104 DebugHelper.exception(e); 105 } 106 catch (com.sun.star.lang.IllegalArgumentException e) 107 { 108 DebugHelper.writeInfo(e.getMessage()); 109 DebugHelper.exception(e); 110 } 111 catch (com.sun.star.lang.WrappedTargetException e) 112 { 113 DebugHelper.writeInfo(e.getMessage()); 114 DebugHelper.exception(e); 115 } 116 } 117 else 118 { 119 // DebugHelper.writeInfo("PropertySetHelper.setProperty() can't get XPropertySet"); 120 getHashMap().put(_sName, _aValue); 121 } 122 } 123 124 /** 125 get a property and convert it to a int value 126 @param _sName the string name of the property 127 @param _nDefault if an error occur, return this value 128 @return the int value of the property 129 */ 130 public int getPropertyValueAsInteger(String _sName, int _nDefault) 131 { 132 Object aObject = null; 133 int nValue = _nDefault; 134 135 if (m_xPropertySet != null) 136 { 137 try 138 { 139 aObject = m_xPropertySet.getPropertyValue(_sName); 140 } 141 catch (com.sun.star.beans.UnknownPropertyException e) 142 { 143 DebugHelper.writeInfo(e.getMessage()); 144 } 145 catch (com.sun.star.lang.WrappedTargetException e) 146 { 147 DebugHelper.writeInfo(e.getMessage()); 148 } 149 } 150 if (aObject != null) 151 { 152 try 153 { 154 nValue = NumericalHelper.toInt(aObject); 155 } 156 catch (com.sun.star.lang.IllegalArgumentException e) 157 { 158 DebugHelper.writeInfo("can't convert a object to integer."); 159 } 160 } 161 return nValue; 162 } 163 164 /** 165 get a property and convert it to a short value 166 @param _sName the string name of the property 167 @param _nDefault if an error occur, return this value 168 @return the int value of the property 169 */ 170 public short getPropertyValueAsShort(String _sName, short _nDefault) 171 { 172 Object aObject = null; 173 short nValue = _nDefault; 174 175 if (m_xPropertySet != null) 176 { 177 try 178 { 179 aObject = m_xPropertySet.getPropertyValue(_sName); 180 } 181 catch (com.sun.star.beans.UnknownPropertyException e) 182 { 183 DebugHelper.writeInfo(e.getMessage()); 184 } 185 catch (com.sun.star.lang.WrappedTargetException e) 186 { 187 DebugHelper.writeInfo(e.getMessage()); 188 } 189 } 190 if (aObject != null) 191 { 192 try 193 { 194 nValue = NumericalHelper.toShort(aObject); 195 } 196 catch (com.sun.star.lang.IllegalArgumentException e) 197 { 198 DebugHelper.writeInfo("can't convert a object to short."); 199 } 200 } 201 return nValue; 202 } 203 204 /** 205 get a property and convert it to a double value 206 @param _sName the string name of the property 207 @param _nDefault if an error occur, return this value 208 @return the int value of the property 209 */ 210 public double getPropertyValueAsDouble(String _sName, double _nDefault) 211 { 212 Object aObject = null; 213 double nValue = _nDefault; 214 215 if (m_xPropertySet != null) 216 { 217 try 218 { 219 aObject = m_xPropertySet.getPropertyValue(_sName); 220 } 221 catch (com.sun.star.beans.UnknownPropertyException e) 222 { 223 DebugHelper.writeInfo(e.getMessage()); 224 } 225 catch (com.sun.star.lang.WrappedTargetException e) 226 { 227 DebugHelper.writeInfo(e.getMessage()); 228 } 229 } 230 if (aObject == null) 231 { 232 if (getHashMap().containsKey(_sName)) 233 { 234 aObject = getHashMap().get(_sName); 235 } 236 } 237 if (aObject != null) 238 { 239 try 240 { 241 nValue = NumericalHelper.toDouble(aObject); 242 } 243 catch (com.sun.star.lang.IllegalArgumentException e) 244 { 245 DebugHelper.writeInfo("can't convert a object to integer."); 246 } 247 } 248 return nValue; 249 } 250 251 /** 252 get a property and convert it to a boolean value 253 @param _sName the string name of the property 254 @param _bDefault if an error occur, return this value 255 @return the boolean value of the property 256 */ 257 public boolean getPropertyValueAsBoolean(String _sName, boolean _bDefault) 258 { 259 Object aObject = null; 260 boolean bValue = _bDefault; 261 262 if (m_xPropertySet != null) 263 { 264 try 265 { 266 aObject = m_xPropertySet.getPropertyValue(_sName); 267 } 268 catch (com.sun.star.beans.UnknownPropertyException e) 269 { 270 DebugHelper.writeInfo(e.getMessage()); 271 DebugHelper.writeInfo("UnknownPropertyException caught: Name:=" + _sName); 272 } 273 catch (com.sun.star.lang.WrappedTargetException e) 274 { 275 DebugHelper.writeInfo(e.getMessage()); 276 } 277 } 278 if (aObject != null) 279 { 280 try 281 { 282 bValue = NumericalHelper.toBoolean(aObject); 283 } 284 catch (com.sun.star.lang.IllegalArgumentException e) 285 { 286 DebugHelper.writeInfo("can't convert a object to boolean."); 287 } 288 } 289 return bValue; 290 } 291 292 /** 293 get a property and convert it to a string value 294 @param _sName the string name of the property 295 @param _sDefault if an error occur, return this value 296 @return the string value of the property 297 */ 298 public String getPropertyValueAsString(String _sName, String _sDefault) 299 { 300 Object aObject = null; 301 String sValue = _sDefault; 302 303 if (m_xPropertySet != null) 304 { 305 try 306 { 307 aObject = m_xPropertySet.getPropertyValue(_sName); 308 } 309 catch (com.sun.star.beans.UnknownPropertyException e) 310 { 311 DebugHelper.writeInfo(e.getMessage()); 312 } 313 catch (com.sun.star.lang.WrappedTargetException e) 314 { 315 DebugHelper.writeInfo(e.getMessage()); 316 } 317 } 318 if (aObject != null) 319 { 320 try 321 { 322 sValue = AnyConverter.toString(aObject); 323 } 324 catch (com.sun.star.lang.IllegalArgumentException e) 325 { 326 DebugHelper.writeInfo("can't convert a object to string."); 327 } 328 } 329 return sValue; 330 } 331 332 /** 333 get a property and don't convert it 334 @param _sName the string name of the property 335 @return the object value of the property without any conversion 336 */ 337 public Object getPropertyValueAsObject(String _sName) 338 { 339 Object aObject = null; 340 341 if (m_xPropertySet != null) 342 { 343 try 344 { 345 aObject = m_xPropertySet.getPropertyValue(_sName); 346 } 347 catch (com.sun.star.beans.UnknownPropertyException e) 348 { 349 DebugHelper.writeInfo(e.getMessage()); 350 } 351 catch (com.sun.star.lang.WrappedTargetException e) 352 { 353 DebugHelper.writeInfo(e.getMessage()); 354 } 355 } 356 return aObject; 357 } 358 359 /** 360 * Debug helper, to show all properties which are available in the given object. 361 * @param _xObj the object of which the properties should shown 362 */ 363 public static void showProperties(Object _xObj) 364 { 365 PropertySetHelper aHelper = new PropertySetHelper(_xObj); 366 aHelper.showProperties(); 367 } 368 369 /** 370 Debug helper, to show all properties which are available in the current object. 371 */ 372 public void showProperties() 373 { 374 String sName = PropertyNames.EMPTY_STRING; 375 376 if (m_xPropertySet != null) 377 { 378 XServiceInfo xServiceInfo = UnoRuntime.queryInterface(XServiceInfo.class, m_xPropertySet); 379 if (xServiceInfo != null) 380 { 381 sName = xServiceInfo.getImplementationName(); 382 } 383 XPropertySetInfo xInfo = m_xPropertySet.getPropertySetInfo(); 384 Property[] aAllProperties = xInfo.getProperties(); 385 DebugHelper.writeInfo("Show all properties of Implementation of :'" + sName + "'"); 386 for (int i = 0; i < aAllProperties.length; i++) 387 { 388 DebugHelper.writeInfo(" - " + aAllProperties[i].Name); 389 } 390 } 391 else 392 { 393 DebugHelper.writeInfo("The given object don't support XPropertySet interface."); 394 } 395 } 396 } 397