Ejemplo n.º 1
0
        public static WidgetInstance Load(Database db, System.Data.DataRow row)
        {
            var rowid    = row.GetLong("rowid");
            var fullName = row.GetString("name");

            var type = WidgetManager.GetTypeFromFullName(fullName);

            if (type == null)
            {
                throw new WidgetLoadException(string.Format("A widget with the name '{0}' cannot be found.", fullName));
            }

            var bounds = new Rectangle(row.GetInt("bounds_left"), row.GetInt("bounds_top"),
                                       row.GetInt("bounds_width"), row.GetInt("bounds_height"));

            var settings = new WidgetConfig();

            foreach (System.Data.DataRow settingRow in db.SelectDataTable("select name, value from widget_config where widget_id = @id", "@id", rowid).Rows)
            {
                var name = settingRow.GetString("name");
                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }

                settings.Add(new WidgetConfigItem(name, settingRow.GetString("value")));
            }

            return(new WidgetInstance(type, bounds, settings));
        }
Ejemplo n.º 2
0
        public static WidgetInstance Load(XmlElement element)
        {
            if (element.Name != XmlElementName)
            {
                throw new WidgetLoadException(string.Format("Element name is not '{0}'.", XmlElementName));
            }

            var fullName = element.GetAttribute("Name");

            if (fullName == null)
            {
                throw new WidgetLoadException("'Name' attribute does not exist.");
            }

            var type = WidgetManager.GetTypeFromFullName(fullName);

            if (type == null)
            {
                throw new WidgetLoadException(string.Format("A widget with the name '{0}' cannot be found.", fullName));
            }

            var boundsElement = element.SelectSingleNode("Bounds") as XmlElement;

            if (boundsElement == null)
            {
                throw new WidgetLoadException("'Bounds' element does not exist.");
            }

            var bounds = new Rectangle(int.Parse(boundsElement.GetAttribute("Left")), int.Parse(boundsElement.GetAttribute("Top")),
                                       int.Parse(boundsElement.GetAttribute("Width")), int.Parse(boundsElement.GetAttribute("Height")));

            var settings = new WidgetConfig();

            foreach (var xmlSetting in element.SelectNodes("Config").Cast <XmlElement>())
            {
                var name = xmlSetting.GetAttribute("Name");
                if (string.IsNullOrEmpty(name))
                {
                    throw new WidgetLoadException("Widget config has no 'Name' attribute.");
                }
                settings.Add(new WidgetConfigItem(name, xmlSetting.GetAttribute("Value")));
            }

            return(new WidgetInstance(type, bounds, settings));
        }