I've included the classes I'm mapping along with the intermediate classes in
the inheritance hierarchy.

    public abstract class ColorSource
    {
        public ColorSource()
        {
        }

        public abstract Color GetColorFrom(IRecord record);
    }

    public class ConstantColorSource : ColorSource
    {
        public const string ColorPartOutOfRangeMessage = "{0} is less than 0
or greater than 255.";

        protected internal ConstantColorSource()
        {
        }

        internal ConstantColorSource(int alpha, int red, int green, int
blue)
        {
            if (!WholeNumber.IsByteValue(alpha))
                throw new
ArgumentException(String.Format(ColorPartOutOfRangeMessage, "alpha"),
"alpha");
            if (!WholeNumber.IsByteValue(red))
                throw new
ArgumentException(String.Format(ColorPartOutOfRangeMessage, "red"), "red");
            if (!WholeNumber.IsByteValue(green))
                throw new
ArgumentException(String.Format(ColorPartOutOfRangeMessage, "green"),
"green");
            if (!WholeNumber.IsByteValue(blue))
                throw new
ArgumentException(String.Format(ColorPartOutOfRangeMessage, "blue"),
"blue");

            this.Alpha = alpha;
            this.Blue = blue;
            this.Green = green;
            this.Red = red;
        }

        internal ConstantColorSource(int red, int green, int blue)
            : this(Byte.MaxValue, red, green, blue)
        {
        }

        internal ConstantColorSource(Color color)
        {
            this.Alpha = color.A;
            this.Red = color.R;
            this.Green = color.G;
            this.Blue = color.B;
        }

        public virtual int Alpha { get; private set; }

        public virtual int Blue { get; private set; }

        public virtual int Green { get; private set; }

        public virtual int Red { get; private set; }

        public override Color GetColorFrom(IRecord record)
        {
            return Color.FromArgb(this.Alpha, this.Red, this.Green,
this.Blue);
        }
    }

    public abstract class DataBoundColorSource : ColorSource
    {
        protected internal DataBoundColorSource()
        {
        }

        public DataBoundColorSource(RecordAccessor recordAccessor)
        {
            if (recordAccessor == null)
                throw new ArgumentNullException("recordAccessor",
"recordAccessor is null.");
            this.RecordAccessor = recordAccessor;
        }

        public virtual RecordAccessor RecordAccessor { get; private set; }
    }

    public abstract class ColorMapColorSource : DataBoundColorSource
    {
        protected internal ColorMapColorSource()
        {
        }

        public ColorMapColorSource(ColorMap colorMap, RecordAccessor
recordAccessor)
            : base(recordAccessor)
        {
            if (colorMap == null)
                throw new ArgumentNullException("colorMap", "colorMap is
null.");
            this.ColorMap = colorMap;
        }

        public virtual ColorMap ColorMap { get; private set; }
    }

    public class SubstringColorSource : ColorMapColorSource
    {
        public const string ColorMappingExceptionMessage = "An error was
encountered while attempting to locate a color for the value '{0}' in the
'{1}' color map.";
        public const string RecordAccessExceptionMessage = "An error was
encountered while attempting to access the supplied record ({0}).";
        public const string ValueNotMappedToValidColorExceptionMessage =
"The value '{0}' is not mapped to a valid color in the {1} color map.";

        private static readonly TextSelector TextSelector = new
TextSelector();

        internal SubstringColorSource(int start, int length, ColorMap
colorMap, RecordAccessor recordAccessor)
            : base(colorMap, recordAccessor)
        {
            this.Start = start;
            this.Length = length;
        }

        protected internal SubstringColorSource()
        {
        }

        public virtual int Length { get; private set; }

        public virtual int Start { get; private set; }

        public override Color GetColorFrom(IRecord record)
        {
            if (record == null)
                throw new ArgumentNullException("record", "record is
null.");

            var value = TextSelector.Select(getValue(record), this.Start,
this.Length);
            var color = getColor(value);

            if (color.ToArgb() == Color.Empty.ToArgb())
            {
                throw new ApplicationException(String.Format(
                    ValueNotMappedToValidColorExceptionMessage,
                    value,
                    this.ColorMap.Name
                    ));
            }

            return color;
        }

        private Color getColor(string value)
        {
            try
            {
                return this.ColorMap.FindColorBy(value);
            }
            catch (Exception exception)
            {
                throw new ApplicationException(
                    String.Format(ColorMappingExceptionMessage, value,
this.ColorMap.Name),
                    exception
                    );
            }
        }

        private string getValue(IRecord record)
        {
            try
            {
                return this.RecordAccessor.GetValueFrom(record);
            }
            catch (Exception exception)
            {
                throw new ApplicationException(
                    String.Format(RecordAccessExceptionMessage,
record.Schema.ToString()),
                    exception
                    );
            }
        }
    }

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to