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