- Timestamp:
- 03/04/10 04:52:28 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Parameters/3.3/ConstrainedValueParameter.cs
r2852 r2924 21 21 22 22 using System; 23 using HeuristicLab.Common; 23 using System.Linq; 24 using HeuristicLab.Collections; 24 25 using HeuristicLab.Core; 25 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 27 28 namespace HeuristicLab.Parameters { 28 29 /// <summary> 29 /// A parameter whose value has to be chosen from a set of valid values which is defined it the parameter itself.30 /// A parameter whose value has to be chosen from a set of valid values. 30 31 /// </summary> 31 [Item("ConstrainedValueParameter<T>", "A parameter whose value has to be chosen from a set of valid values which is defined it the parameter itself.")] 32 public sealed class ConstrainedValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem { 33 private ItemSet<T> validValues; 34 [Storable] 35 public ItemSet<T> ValidValues { 36 get { return validValues; } 37 private set { 38 DeregisterValidValuesEvents(); 39 validValues = value; 40 RegisterValidValuesEvents(); 32 [Item("ConstrainedValueParameter<T>", "A parameter whose value has to be chosen from a set of valid values.")] 33 [EmptyStorableClass] 34 public class ConstrainedValueParameter<T> : OptionalConstrainedValueParameter<T> where T : class, IItem { 35 public override T Value { 36 get { return base.Value; } 37 set { 38 if ((value == null) && (ValidValues.Count > 0)) throw new ArgumentNullException(); 39 base.Value = value; 41 40 } 42 41 } 43 42 44 private T value; 45 [Storable] 46 public T Value { 47 get { return this.value; } 48 set { 49 if (value != this.value) { 50 if ((value != null) && !validValues.Contains(value)) throw new ArgumentException("Invalid value."); 51 this.value = value; 52 OnValueChanged(); 53 } 54 } 43 public ConstrainedValueParameter() : base() { } 44 public ConstrainedValueParameter(string name) : base(name) { } 45 public ConstrainedValueParameter(string name, ItemSet<T> validValues) : base(name, validValues) { } 46 public ConstrainedValueParameter(string name, ItemSet<T> validValues, T value) : base(name, validValues, value) { } 47 public ConstrainedValueParameter(string name, string description) : base(name, description) { } 48 public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues) : base(name, description, validValues) { } 49 public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value) : base(name, description, validValues, value) { } 50 51 protected override void validValues_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) { 52 if (Value == null) Value = ValidValues.First(); 55 53 } 56 IItem IValueParameter.Value { 57 get { return Value; } 58 set { 59 T val = value as T; 60 if ((value != null) && (val == null)) 61 throw new InvalidOperationException( 62 string.Format("Type mismatch. Value is not a \"{0}\".", 63 typeof(T).GetPrettyName()) 64 ); 65 Value = val; 66 } 54 protected override void ValidValues_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) { 55 if ((Value != null) && !ValidValues.Contains(Value)) Value = ValidValues.FirstOrDefault(); 67 56 } 68 69 public ConstrainedValueParameter() 70 : base("Anonymous", typeof(T)) { 71 ValidValues = new ItemSet<T>(); 72 } 73 public ConstrainedValueParameter(string name) 74 : base(name, typeof(T)) { 75 ValidValues = new ItemSet<T>(); 76 } 77 public ConstrainedValueParameter(string name, ItemSet<T> validValues) 78 : base(name, typeof(T)) { 79 ValidValues = validValues; 80 } 81 public ConstrainedValueParameter(string name, ItemSet<T> validValues, T value) 82 : base(name, typeof(T)) { 83 ValidValues = validValues; 84 Value = value; 85 } 86 public ConstrainedValueParameter(string name, string description) 87 : base(name, description, typeof(T)) { 88 ValidValues = new ItemSet<T>(); 89 } 90 public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues) 91 : base(name, description, typeof(T)) { 92 ValidValues = validValues; 93 } 94 public ConstrainedValueParameter(string name, string description, ItemSet<T> validValues, T value) 95 : base(name, description, typeof(T)) { 96 ValidValues = validValues; 97 Value = value; 98 } 99 100 public override IDeepCloneable Clone(Cloner cloner) { 101 ConstrainedValueParameter<T> clone = (ConstrainedValueParameter<T>)base.Clone(cloner); 102 clone.ValidValues = (ItemSet<T>)cloner.Clone(validValues); 103 clone.Value = (T)cloner.Clone(value); 104 return clone; 105 } 106 107 public override string ToString() { 108 return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : "null", DataType.GetPrettyName()); 109 } 110 111 protected override IItem GetActualValue() { 112 return Value; 113 } 114 protected override void SetActualValue(IItem value) { 115 ((IValueParameter)this).Value = value; 116 } 117 118 public event EventHandler ValueChanged; 119 private void OnValueChanged() { 120 if (ValueChanged != null) 121 ValueChanged(this, EventArgs.Empty); 122 OnChanged(); 123 } 124 125 private void RegisterValidValuesEvents() { 126 if (validValues != null) { 127 validValues.ItemsRemoved += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved); 128 validValues.CollectionReset += new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset); 129 validValues.Changed += new ChangedEventHandler(ValidValues_Changed); 130 } 131 } 132 private void DeregisterValidValuesEvents() { 133 if (validValues != null) { 134 validValues.ItemsRemoved -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_ItemsRemoved); 135 validValues.CollectionReset -= new HeuristicLab.Collections.CollectionItemsChangedEventHandler<T>(ValidValues_CollectionReset); 136 validValues.Changed -= new ChangedEventHandler(ValidValues_Changed); 137 } 138 } 139 140 private void ValidValues_ItemsRemoved(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) { 141 if ((Value != null) && !validValues.Contains(Value)) Value = null; 142 } 143 private void ValidValues_CollectionReset(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<T> e) { 144 if ((Value != null) && !validValues.Contains(Value)) Value = null; 145 } 146 private void ValidValues_Changed(object sender, ChangedEventArgs e) { 147 OnChanged(e); 57 protected override void ValidValues_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) { 58 if ((Value != null) && !ValidValues.Contains(Value)) Value = ValidValues.FirstOrDefault(); 148 59 } 149 60 }
Note: See TracChangeset
for help on using the changeset viewer.