Esempio n. 1
0
        /// <summary>
        /// Sets a property.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="propName">Name of the property.</param>
        /// <param name="value">The value of the property.</param>
        /// <returns><c>true</c> if the property could been set without an error, otherwise <c>false</c></returns>
        internal static bool SetProperty(XPropertySet obj, String propName, Object value)
        {
            if (obj != null)
            {
                //var proInfo = obj.getPropertySetInfo();
                //var properties = proInfo.getProperties();
                //List<String> props = new List<String>();
                //foreach (var prop in properties)
                //{
                //    props.Add(prop.Name);
                //}

                if (obj.getPropertySetInfo() != null && obj.getPropertySetInfo().hasPropertyByName(propName))
                {
                    try
                    {
                        obj.setPropertyValue(propName, Any.Get(value));
                        return(true);
                    }
                    catch (unoidl.com.sun.star.beans.PropertyVetoException)
                    {
                        System.Diagnostics.Debug.WriteLine("Property value is unacceptable");
                    }
                    catch (unoidl.com.sun.star.uno.RuntimeException)
                    {
                        System.Diagnostics.Debug.WriteLine("Property value set cause internal error");
                        Logger.Instance.Log(LogPriority.IMPORTANT, "OoUtils", "[FATAL ERROR] Cannot set property '" + propName + "' to value: '" + (value != null ? value.ToString() : "NULL") + "' for the current object");
                    }
                }
                else
                {
                    XPropertyContainer pc = obj as XPropertyContainer;
                    if (pc != null)
                    {
                        try
                        {
                            pc.addProperty(propName, (short)2, Any.Get(value));
                            return(true);
                        }
                        catch { }
                    }
                }
            }
            return(false);
        }
        /// <summary>
        /// Gets all properties of the object.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="debug">if set to <c>true</c> the properties will be printed
        /// to the System.Diagnostics.Debug output.</param>
        /// <returns>A dictionary with the properties. The name is the key an the value
        /// is set as uno.Any. You can access the real value by using uno.Any.Value</returns>
        public static Dictionary <String, uno.Any> GetAllProperties(XPropertySet obj, bool debug = true)
        {
            String output = "";

            if (obj != null)
            {
                try
                {
                    var b = obj.getPropertySetInfo();
                    if (b != null)
                    {
                        Property[] a = b.getProperties();
                        if (debug)
                        {
                            System.Diagnostics.Debug.WriteLine("Object [" + obj + "] has " + a.Length + " Properties:");
                        }

                        var properties = new Dictionary <String, uno.Any>();

                        foreach (var item in a)
                        {
                            try
                            {
                                if (obj != null && item != null && item.Name != null && obj.getPropertyValue(item.Name).hasValue())
                                {
                                    if (debug)
                                    {
                                        output += "\n" + ("\tProperty: " + item.Name + " = " + obj.getPropertyValue(item.Name));
                                    }
                                    properties.Add(item.Name, obj.getPropertyValue(item.Name));
                                }
                                else
                                {
                                    output += "\n" + "Can't get property - " + item.Name;
                                }
                            }
                            catch (Exception)
                            {
                                output += "\n" + "Can't get property - " + item.Name;
                            }
                        }
                        System.Diagnostics.Debug.WriteLine(output);
                        return(properties);
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("Can't get properties of object: " + e);
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("object is null and therefore not of type XPropertySet and no properties could be displayed! ");
            }
            return(new Dictionary <String, uno.Any>());
        }
Esempio n. 3
0
 /// <summary>
 /// Gets a property value.
 /// </summary>
 /// <param name="obj">The obj.</param>
 /// <param name="propName">Name of the property.</param>
 /// <returns>
 /// the property form the property set or null
 /// </returns>
 internal static Object GetProperty(XPropertySet obj, String propName)
 {
     try
     {
         if (obj != null)
         {
             XPropertySetInfo propInfo = obj.getPropertySetInfo();
             if (propInfo != null && propInfo.hasPropertyByName(propName))
             {
                 return(obj.getPropertyValue(propName).Value);
             }
         }
     }
     catch { }
     return(null);
 }
        /// <summary>
        /// Sets a property if possible.
        /// </summary>
        /// <param name="propertySet">The property set.</param>
        /// <param name="name">The name of the property.</param>
        /// <param name="value">The value.</param>
        /// <returns><c>true</c> if the property could be set otherwise <c>false</c></returns>
        public bool SetProperty(object propertySet, string name, object value)
        {
            if (propertySet != null)
            {
                XPropertySet ps = null;
                if (propertySet is XPropertySet)
                {
                    ps = propertySet as XPropertySet;
                }
                else if (propertySet is XControl)
                { // for a XControll you have to get the model to change or get the properties
                    XControlModel model = ((XControl)propertySet).getModel();
                    if (model != null && model is XPropertySet)
                    {
                        ps = model as XPropertySet;
                    }
                }

                if (ps != null)
                {
                    var  psInfo = ps.getPropertySetInfo();
                    bool exist  = psInfo.hasPropertyByName(name);

                    //util.Debug.GetAllProperties(ps);

                    if (exist)
                    {
                        ps.setPropertyValue(name, Any.Get(value));
                        return(true);
                    }
                    else
                    {
                    }
                }
            }
            return(false);
        }
        /// <summary>
        /// Gets all properties of the object.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="debug">if set to <c>true</c> the properties will be printed
        /// to the System.Diagnostics.Debug output.</param>
        /// <returns>A dictionary with the properties. The name is the key an the value 
        /// is set as uno.Any. You can access the real value by using uno.Any.Value</returns>
        public static Dictionary<String, uno.Any> GetAllProperties(XPropertySet obj, bool debug = true)
        {
            String output = "";
            if (obj != null)
            {
                try
                {
                    var b = obj.getPropertySetInfo();
                    if (b != null)
                    {
                        Property[] a = b.getProperties();
                        if (debug)
                            System.Diagnostics.Debug.WriteLine("Object [" + obj + "] has " + a.Length + " Properties:");

                        var properties = new Dictionary<String, uno.Any>();

                        foreach (var item in a)
                        {
                            try
                            {
                                if (obj != null && item != null && item.Name != null && obj.getPropertyValue(item.Name).hasValue())
                                {
                                    if (debug)
                                        output += "\n" + ("\tProperty: " + item.Name + " = " + obj.getPropertyValue(item.Name));
                                    properties.Add(item.Name, obj.getPropertyValue(item.Name));
                                }
                                else
                                {
                                    output += "\n" + "Can't get property - " + item.Name;
                                }
                                
                            }
                            catch (Exception)
                            {
                                output += "\n" + "Can't get property - " + item.Name;
                            }
                        }
                        System.Diagnostics.Debug.WriteLine(output);
                        return properties;
                    }
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine("Can't get properties of object: " + e);
                }
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("object is null and therefore not of type XPropertySet and no properties could be displayed! ");
            }
            return new Dictionary<String, uno.Any>();
        }
 /// <summary>
 /// Gets a property value.
 /// </summary>
 /// <param name="obj">The obj.</param>
 /// <param name="propName">Name of the property.</param>
 /// <returns>
 /// the property form the property set or null
 /// </returns>
 public static Object GetProperty(XPropertySet obj, String propName)
 {
     try
     {
         if (obj != null)
         {
             XPropertySetInfo propInfo = obj.getPropertySetInfo();
             if (propInfo != null && propInfo.hasPropertyByName(propName))
             {
                 return obj.getPropertyValue(propName).Value;
             }
         }
     }
     catch { }
     return null;
 }
        /// <summary>
        /// Sets a property.
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="propName">Name of the property.</param>
        /// <param name="value">The value of the property.</param>
        /// <returns><c>true</c> if the property could been set without an error, otherwise <c>false</c></returns>
        public static bool SetProperty(XPropertySet obj, String propName, Object value)
        {
            if (obj != null)
            {
                //var proInfo = obj.getPropertySetInfo();
                //var properties = proInfo.getProperties();
                //List<String> props = new List<String>();
                //foreach (var prop in properties)
                //{
                //    props.Add(prop.Name);
                //}                

                if (obj.getPropertySetInfo() != null && obj.getPropertySetInfo().hasPropertyByName(propName))
                {
                    try
                    {
                        obj.setPropertyValue(propName, Any.Get(value));
                        return true;
                    }
                    catch (unoidl.com.sun.star.beans.PropertyVetoException)
                    {
                        System.Diagnostics.Debug.WriteLine("Property value is unacceptable");
                    }
                    catch (unoidl.com.sun.star.uno.RuntimeException)
                    {
                        System.Diagnostics.Debug.WriteLine("Property value set cause internal error");
                        Logger.Instance.Log(LogPriority.IMPORTANT, "OoUtils", "[FATAL ERROR] Cannot set property '" + propName + "' to value: '" + value.ToString() + "' for the current object");
                    }
                }
                else
                {
                    XPropertyContainer pc = obj as XPropertyContainer;
                    if (pc != null)
                    {
                        try
                        {
                            pc.addProperty(propName, (short)2, Any.Get(value));
                            return true;
                        }
                        catch { }
                    }
                }
            }
            return false;
        }