Пример #1
0
        public static Object GetCustomFieldValue(IXenObject o, CustomFieldDefinition customFieldDefinition)
        {
            Dictionary <String, String> otherConfig = GetOtherConfigCopy(o);

            if (otherConfig == null)
            {
                return(null);
            }

            String key = GetCustomFieldKey(customFieldDefinition);

            if (!otherConfig.ContainsKey(key))
            {
                return(null);
            }

            String value = otherConfig[key];

            if (value == String.Empty)
            {
                return(null);
            }

            return(ParseValue(customFieldDefinition.Type, value));
        }
Пример #2
0
 public static void AddCustomField(Session session, IXenConnection connection, CustomFieldDefinition customField)
 {
     List<CustomFieldDefinition> customFields = customFieldsCache.GetCustomFields(connection);
     if (!customFields.Contains(customField))
     {
         customFields.Add(customField);
         SaveCustomFields(session, connection, customFields);
     }
 }
        private Object GetValue(CustomFieldDefinition definition, Control control)
        {
            switch (definition.Type)
            {
                case CustomFieldDefinition.Types.Date:
                    {
                        DateTimePicker dateControl = (DateTimePicker)control;
                        if (!dateControl.Checked)
                            return null;
                        DateTimePicker timeControl = (DateTimePicker)dateControl.Tag;
                        DateTime date = dateControl.Value;
                        DateTime time = timeControl.Value;

                        return new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second);
                    }

                case CustomFieldDefinition.Types.String:
                    TextBox textBox = control as TextBox;
                    if (textBox == null)
                        return null;

                    string text = textBox.Text;
                    return (text == "" ? null : text);

                default:
                    throw new InvalidEnumArgumentException();
            }
        }
Пример #4
0
        private static void RemoveCustomFieldsFrom(Session session, IEnumerable <IXenObject> os, CustomFieldDefinition customFieldDefinition)
        {
            InvokeHelper.AssertOffEventThread();

            string customFieldKey = GetCustomFieldKey(customFieldDefinition);

            foreach (IXenObject o in os)
            {
                Helpers.RemoveFromOtherConfig(session, o, customFieldKey);
            }
        }
Пример #5
0
 public static string GetCustomFieldKey(CustomFieldDefinition customFieldDefinition)
 {
     return(CUSTOM_FIELD_BASE_KEY + CUSTOM_FIELD_DELIM + customFieldDefinition.Name);
 }
Пример #6
0
        public static void AddCustomField(Session session, IXenConnection connection, CustomFieldDefinition customField)
        {
            List <CustomFieldDefinition> customFields = customFieldsCache.GetCustomFields(connection);

            if (!customFields.Contains(customField))
            {
                customFields.Add(customField);
                SaveCustomFields(session, connection, customFields);
            }
        }
Пример #7
0
        public static void RemoveCustomField(Session session, IXenConnection connection, CustomFieldDefinition definition)
        {
            List <CustomFieldDefinition> customFields = customFieldsCache.GetCustomFields(connection);

            if (customFields.Remove(definition))
            {
                SaveCustomFields(session, connection, customFields);

                // Remove from all Objects
                RemoveCustomFieldsFrom(session, connection.Cache.VMs, definition);
                RemoveCustomFieldsFrom(session, connection.Cache.Hosts, definition);
                RemoveCustomFieldsFrom(session, connection.Cache.Pools, definition);
                RemoveCustomFieldsFrom(session, connection.Cache.SRs, definition);
            }
        }
Пример #8
0
        public static Object GetCustomFieldValue(IXenObject o, CustomFieldDefinition customFieldDefinition)
        {
            Dictionary<String, String> otherConfig = GetOtherConfigCopy(o);
            if (otherConfig == null)
                return null;

            String key = GetCustomFieldKey(customFieldDefinition);
            if (!otherConfig.ContainsKey(key))
                return null;

            String value = otherConfig[key];
            if (value == String.Empty)
                return null;

            return ParseValue(customFieldDefinition.Type, value);
        }
Пример #9
0
        public override bool Equals(object obj)
        {
            CustomFieldDefinition other = obj as CustomFieldDefinition;

            return(other != null && other.name == name && other.type == type);
        }
Пример #10
0
 public CustomField(CustomFieldDefinition definition, Object value)
 {
     this.definition = definition;
     this.value      = value;
 }
Пример #11
0
        private static void RemoveCustomFieldsFrom(Session session, IEnumerable<IXenObject> os, CustomFieldDefinition customFieldDefinition)
        {
            InvokeHelper.AssertOffEventThread();

            string customFieldKey = GetCustomFieldKey(customFieldDefinition);

            foreach (IXenObject o in os)
            {
                Helpers.RemoveFromOtherConfig(session, o, customFieldKey);
            }
        }
Пример #12
0
        private static object ParseValue(CustomFieldDefinition.Types type, string value)
        {
            switch (type)
            {
                case CustomFieldDefinition.Types.Date:
                    DateTime datetime;
                    if (DateTime.TryParse(value, out datetime))
                        return datetime;
                    return null;

                case CustomFieldDefinition.Types.String:
                    return value;

                default:
                    return null;
            }
        }
Пример #13
0
        public static void RemoveCustomField(Session session, IXenConnection connection, CustomFieldDefinition definition)
        {
            List<CustomFieldDefinition> customFields = customFieldsCache.GetCustomFields(connection);
            if (customFields.Remove(definition))
            {
                SaveCustomFields(session, connection, customFields);

                // Remove from all Objects
                RemoveCustomFieldsFrom(session, connection.Cache.VMs, definition);
                RemoveCustomFieldsFrom(session, connection.Cache.Hosts, definition);
                RemoveCustomFieldsFrom(session, connection.Cache.Pools, definition);
                RemoveCustomFieldsFrom(session, connection.Cache.SRs, definition);
            }
        }
        private void Rebuild(bool revertValues)
        {
            CustomFieldDefinition[] customFieldDefinitions = CustomFieldsManager.GetCustomFields(xenObject.Connection).ToArray();

            tableLayoutPanel.SuspendLayout();

            // Add new custom fields
            foreach (CustomFieldDefinition customFieldDefinition in customFieldDefinitions)
            {
                Object value = CustomFieldsManager.GetCustomFieldValue(xenObject, customFieldDefinition);

                if (!controls.ContainsKey(customFieldDefinition))
                {
                    // Create the display label
                    Label lblKey = new Label();
                    lblKey.Text = customFieldDefinition.Name;
                    lblKey.Margin = new Padding(3, 7, 3, 3);
                    lblKey.Font = Program.DefaultFont;
                    lblKey.Width = (int)tableLayoutPanel.ColumnStyles[0].Width;
                    lblKey.AutoEllipsis = true;
                    lblKey.AutoSize = false;

                    tableLayoutPanel.Controls.Add(lblKey);

                    // Create value field
                    Control control;

                    switch (customFieldDefinition.Type)
                    {
                        case CustomFieldDefinition.Types.String:
                            TextBox textBox = new TextBox();
                            textBox.Text = (String)value;

                            tableLayoutPanel.Controls.Add(textBox);
                            tableLayoutPanel.SetColumnSpan(textBox, 2);
                            textBox.Dock = DockStyle.Fill;
                            control = textBox;
                            break;

                        case CustomFieldDefinition.Types.Date:
                            DateTimePicker date = new DateTimePicker();
                            date.MinDate = DateTime.MinValue;
                            date.MaxDate = DateTime.MaxValue;
                            date.Dock = DockStyle.Fill;
                            date.MinimumSize = new Size(0, 24);
                            date.ShowCheckBox = true;
                            date.Format = DateTimePickerFormat.Long;
                            if (value != null)
                            {
                                date.Value = (DateTime)value;
                                date.Checked = true;
                            }
                            else
                                date.Checked = false;
                            tableLayoutPanel.Controls.Add(date);

                            DateTimePicker time = new DateTimePicker();
                            time.MinDate = DateTime.MinValue;
                            time.MaxDate = DateTime.MaxValue;
                            time.Dock = DockStyle.Fill;
                            time.MinimumSize = new Size(0, 24);
                            time.Format = DateTimePickerFormat.Time;
                            time.ShowUpDown = true;
                            if (value != null)
                            {
                                time.Value = (DateTime)value;
                                time.Enabled = true;
                            }
                            else
                                time.Enabled = false;
                            tableLayoutPanel.Controls.Add(time);
                            // Tag so we can remove this control later
                            date.Tag = time;
                            date.ValueChanged += delegate(Object sender, EventArgs e)
                            {
                                time.Enabled = date.Checked;
                            };

                            control = date;
                            break;

                        default:
                            throw new InvalidEnumArgumentException();
                    }

                    controls[customFieldDefinition] = new KeyValuePair<Label, Control>(lblKey, control);
                }
                else if (revertValues)
                {
                    KeyValuePair<Label, Control> kvp = controls[customFieldDefinition];

                    SetValue(customFieldDefinition, kvp.Value, value);
                }
            }

            // Remove old ones
            CustomFieldDefinition[] definitions = new CustomFieldDefinition[controls.Keys.Count];
            controls.Keys.CopyTo(definitions, 0);

            foreach (CustomFieldDefinition definition in definitions)
            {
                if (Array.IndexOf<CustomFieldDefinition>(customFieldDefinitions, definition) > -1)
                    continue;

                KeyValuePair<Label, Control> kvp = controls[definition];

                tableLayoutPanel.Controls.Remove(kvp.Value);
                tableLayoutPanel.Controls.Remove(kvp.Key);

                DateTimePicker timeControl = kvp.Value.Tag as DateTimePicker;
                if (timeControl != null)
                {
                    tableLayoutPanel.Controls.Remove(timeControl);
                }

                controls.Remove(definition);

                kvp.Key.Dispose();
                kvp.Value.Dispose();
            }

            tableLayoutPanel.ResumeLayout();
        }
Пример #15
0
 public CustomField(CustomFieldDefinition definition, Object value)
 {
     this.definition = definition;
     this.value = value;
 }
        private void SetValue(CustomFieldDefinition definition, Control control, Object value)
        {
            switch (definition.Type)
            {
                case CustomFieldDefinition.Types.Date:
                    {
                        DateTimePicker dateControl = (DateTimePicker)control;
                        DateTimePicker timeControl = (DateTimePicker)dateControl.Tag;

                        if (value != null)
                        {
                            dateControl.Checked = true;
                            dateControl.Value = (DateTime)value;
                            timeControl.Value = (DateTime)value;
                        }
                        else
                            dateControl.Checked = false;
                    }
                    break;

                case CustomFieldDefinition.Types.String:
                    TextBox textBox = control as TextBox;
                    if (textBox == null)
                        return;

                    textBox.Text = (String)value;
                    break;
            }
        }
Пример #17
0
 public static string GetCustomFieldKey(CustomFieldDefinition customFieldDefinition)
 {
     return CUSTOM_FIELD_BASE_KEY + CUSTOM_FIELD_DELIM + customFieldDefinition.Name;
 }