private void Populate()
        {
            if (Table == null || BusyPopulating)
                return;

            try
            {
                BusyPopulating = true;
                Slyce.Common.Utility.SuspendPainting(this);
                bool hasMultiSchemas = Table.Database.GetSchemas().Count() > 1;
                //HideAllFloatingToolstrips();
                Cursor = Cursors.WaitCursor;
                Font boldFont = new Font(Font, FontStyle.Bold);
                Font subTextFont = new Font(Font.FontFamily, Font.Size - 0.6F, FontStyle.Regular);
                MainShape = new RawTable(shapeCanvas1, Table.Name, boldFont, CategoryFont, PropertyFont, Table);

                if (hasMultiSchemas)
                    MainShape.SubText = string.Format("  ({0}) ", Table.Schema);

                MainShape.FontSubText = subTextFont;
                MainShape.Icon = Table.IsView ? ViewImage : TableImage;
                //MainShape.Enter += new EventHandler(centreShape_Enter);
                //MainShape.Leave += new EventHandler(centreShape_Leave);
                MainShape.MouseClick += new MouseEventHandler(centreShape_MouseClick);
                CategoryColumns.Parent = MainShape;
                CategoryColumns.Properties.Clear();

                MainShape.Categories.Add(CategoryColumns);

                if (Table.Columns.Count > 0)
                {
                    foreach (var column in Table.Columns)
                    {
                        RawProperty prop = new RawProperty(column.Name, column);

                        if (column.InPrimaryKey)
                        {
                            prop.ImageColor = Color.OrangeRed;
                            prop.ImageType = RawProperty.ImageTypes.Key;
                        }
                        prop.Click += new MouseEventHandler(Column_Click);
                        prop.DoubleClick += new MouseEventHandler(Column_DoubleClick);
                        CategoryColumns.Properties.Add(prop);
                    }
                }

                List<RawShape> bottomLevelShapes = new List<RawShape>();
                List<RawShape> rightAlignedShapes = new List<RawShape>();
                Font boldUnderlineFont = new Font(Font, FontStyle.Bold | FontStyle.Underline);

                //TheEntity.Discriminator.RootGrouping.
                #region Mapped Entities

                if (ShowMappedEntities)
                {
                    foreach (EntityImpl entity in MappedEntities.OrderBy(e => e.Name))
                    {
                        RawEntity entityShape = new RawEntity(shapeCanvas1, entity.Name, boldFont, entity);

                        CustomLineCap startCap = LineCaps.None;//.SolidArrow;
                        CustomLineCap endCap = LineCaps.None;
                        entityShape.OriginatingLineStyle.LineStyle = DashStyle.Solid;
                        entityShape.Cursor = Cursors.Hand;
                        //entityShape.OriginatingLineStyle.MiddleText = GetDiscriminatorText(entityShape);
                        entityShape.OriginatingLineStyle.StartCap = startCap;
                        entityShape.OriginatingLineStyle.EndCap = endCap;
                        //entityShape.BackColor1 = Color.Blue;
                        //entityShape.BackColor2 = Color.DarkBlue;
                        //entityShape.RoundedCorners = false;
                        entityShape.Tag = entity;
                        entityShape.Icon = EntityImage;
                        string discriminatorText = GetDiscriminatorText(entity);
                        entityShape.OriginatingLineStyle.MiddleText = discriminatorText;

                        if (string.IsNullOrEmpty(discriminatorText))
                        {
                            entityShape.OriginatingLineStyle.MiddleImage = FilterImageDisabled;
                            entityShape.OriginatingLineStyle.MiddleImageFocused = FilterImageDisabled;
                        }
                        else
                        {
                            entityShape.OriginatingLineStyle.MiddleImage = FilterImage;
                            entityShape.OriginatingLineStyle.MiddleImageFocused = FilterImageFocused;
                        }
                        entityShape.OriginatingLineStyle.MouseClick += new MouseEventHandler(OriginatingLineStyle_MouseClick);
                        entityShape.MouseClick += new MouseEventHandler(entityShape_MouseClick);
                        entityShape.OriginatingLineStyle.MiddleImageClick += new MouseEventHandler(MapLine_MiddleImageClick);
                        entityShape.OriginatingLineStyle.ForeColor = Color.White;
                        entityShape.OriginatingLineStyle.DataObject = entity;
                        entityShape.OriginatingLineStyle.ShowMiddleTextOnlyWhenFocused = true;
                        //entityShape.Enter += new EventHandler(entityShape_Enter);
                        //entityShape.Leave += new EventHandler(entityShape_Leave);
                        entityShape.MouseDoubleClick += new MouseEventHandler(entityShape_MouseDoubleClick);

                        //#region Setup discriminator
                        //if (Table.Discriminator != null && Table.Discriminator.RootGrouping != null)
                        //{
                        //    ArchAngel.Providers.EntityModel.Model.DatabaseLayer.Discrimination.Condition firstCondition = Table.Discriminator.RootGrouping.Conditions.ElementAtOrDefault(0);

                        //    if (firstCondition != null)
                        //    {
                        //        IColumn column = firstCondition.Column;
                        //        ArchAngel.Providers.EntityModel.Model.DatabaseLayer.Discrimination.Operator op = firstCondition.Operator;
                        //        string exprText = firstCondition.ExpressionValue.Value;
                        //    }
                        //}
                        //#endregion

                        //tableShape.OriginatingLineStyle.MiddleImageMouseOver += new EventHandler(OriginatingLineStyle_MiddleImageMouseOver);
                        rightAlignedShapes.Add(entityShape);
                    }
                    #region Add empty entity
                    Slyce.Common.Controls.Diagramming.Shapes.RawShape emptyEntity = new Slyce.Common.Controls.Diagramming.Shapes.RawShape(shapeCanvas1, "Map entity...", boldUnderlineFont)
                    {
                        BackColor1 = Color.White,
                        BackColor2 = Color.White,
                        BorderColor = Color.Gray,
                        ForeColor = Color.Gray,
                        FocusForeColor = Color.Blue,
                        FocusBackColor1 = Color.WhiteSmoke,
                        FocusBackColor2 = Color.White,
                        FocusBorderColor = Color.DarkGray,
                        Cursor = Cursors.Hand,
                        Tag = null,
                        RoundedCorners = false,
                        OriginatingLineStyle = null //new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow)
                    };
                    emptyEntity.MouseClick += new MouseEventHandler(emptyEntity_MouseClick);
                    //emptyTable.OriginatingLineStyle.MouseClick += new MouseEventHandler(OriginatingLineStyle_MouseClick);
                    rightAlignedShapes.Add(emptyEntity);
                    #endregion
                }
                #endregion

                #region Relationships
                if (ShowRelatedtables)
                {
                    foreach (Relationship relationship in Table.Relationships.OrderBy(r => r.PrimaryTable == Table ? r.ForeignTable.Name : r.PrimaryTable.Name))
                    {
                        RawShape relatedTableShape;
                        CustomLineCap startCap;
                        CustomLineCap endCap;

                        if (relationship.PrimaryTable == Table)
                        {
                            relatedTableShape = new RawTable(shapeCanvas1, relationship.ForeignTable.Name, boldFont, relationship.ForeignTable);

                            if (hasMultiSchemas)
                                relatedTableShape.SubText = string.Format("  ({0}) ", relationship.ForeignTable.Schema);

                            string end1 = relationship.PrimaryKey.IsUnique ? "1" : "m";
                            endCap = relationship.ForeignKey.IsUnique ? LineCaps.One : LineCaps.Many;
                            startCap = relationship.PrimaryKey.IsUnique ? LineCaps.One : LineCaps.Many;
                            relatedTableShape.OriginatingLineStyle.MiddleImageClick += new MouseEventHandler(OriginatingLineStyle_MiddleImageClick2);
                            relatedTableShape.Icon = relationship.ForeignTable.IsView ? ViewImage : TableImage;
                        }
                        else if (relationship.ForeignTable == Table)
                        {
                            relatedTableShape = new RawTable(shapeCanvas1, relationship.PrimaryTable.Name, boldFont, relationship.PrimaryTable);

                            if (hasMultiSchemas)
                                relatedTableShape.SubText = string.Format("  ({0}) ", relationship.PrimaryTable.Schema);

                            string end1 = relationship.ForeignKey.IsUnique ? "1" : "m";
                            endCap = relationship.PrimaryKey.IsUnique ? LineCaps.One : LineCaps.Many;
                            startCap = relationship.ForeignKey.IsUnique ? LineCaps.One : LineCaps.Many;
                            relatedTableShape.OriginatingLineStyle.MiddleImageClick += new MouseEventHandler(OriginatingLineStyle_MiddleImageClick2);
                            relatedTableShape.Icon = relationship.PrimaryTable.IsView ? ViewImage : TableImage;
                        }
                        else
                            throw new Exception("What the...??!");

                        relatedTableShape.FontSubText = subTextFont;

                        if (relationship.IsUserDefined)
                        {
                            relatedTableShape.OriginatingLineStyle.LineStyle = DashStyle.Dash;
                            relatedTableShape.OriginatingLineStyle.LineColor = Color.Yellow;
                        }
                        else
                        {
                            relatedTableShape.OriginatingLineStyle.LineStyle = DashStyle.Solid;
                        }
                        relatedTableShape.Cursor = Cursors.Hand;
                        relatedTableShape.OriginatingLineStyle.StartCap = startCap;
                        relatedTableShape.OriginatingLineStyle.EndCap = endCap;
                        relatedTableShape.OriginatingLineStyle.DataObject = relationship;
                        relatedTableShape.OriginatingLineStyle.ForeColor = Color.White;
                        relatedTableShape.OriginatingLineStyle.MiddleImage = InfoImage;
                        relatedTableShape.MouseClick += new MouseEventHandler(referencedEntityShape_MouseClick);
                        relatedTableShape.MouseDoubleClick += new MouseEventHandler(referencedEntityShape_MouseDoubleClick);
                        bottomLevelShapes.Add(relatedTableShape);
                    }
                    // Add empty relationship
                    RawShape emptyTable = new RawShape(shapeCanvas1, "Add relationship...", boldUnderlineFont)
                    {
                        BackColor1 = Color.White,
                        BackColor2 = Color.White,
                        BorderColor = Color.Gray,
                        ForeColor = Color.Gray,
                        FocusForeColor = Color.Blue,
                        FocusBackColor1 = Color.WhiteSmoke,
                        FocusBackColor2 = Color.White,
                        FocusBorderColor = Color.DarkGray,
                        Cursor = Cursors.Hand,
                        OriginatingLineStyle = null,
                        Tag = null
                    };
                    emptyTable.MouseClick += new MouseEventHandler(emptyTable_MouseClick);
                    bottomLevelShapes.Add(emptyTable);
                }
                #endregion

                shapeCanvas1.SwimLane1 = new ShapeCanvas.SwimLaneStyle(Color.FromArgb(79, 124, 205), Color.Black, Color.White, 0F, "Related Tables", ShapeCanvas.SwimLaneStyle.Styles.Fill);
                shapeCanvas1.SwimLane3 = new ShapeCanvas.SwimLaneStyle(Color.FromArgb(79, 124, 205), Color.Black, Color.White, 180F, "Mapped Entities", ShapeCanvas.SwimLaneStyle.Styles.Fill);
                shapeCanvas1.SwimLane4 = new ShapeCanvas.SwimLaneStyle(Color.FromArgb(79, 124, 205), Color.Black, Color.White, 90F, "Mapped Entities", ShapeCanvas.SwimLaneStyle.Styles.Fill);

                shapeCanvas1.BackColor = this.BackColor;
                //shapeCanvas1.DrawStar(centreShape, outerShapes);
                //shapeCanvas1.DrawThreeLayerHorizontal(centreShape, null, outerShapes, false);
                shapeCanvas1.Height = this.Height;
                //shapeCanvas1.DrawThreeLayerVertical(centreShape, null, bottomLevelShapes, rightAlignedShapes);
                shapeCanvas1.KeepMainShapeCentered = true;
                shapeCanvas1.DrawThreeLayerHorizontal(MainShape, bottomLevelShapes, rightAlignedShapes, KeepMainShapeFull);
                //shapeCanvas1.Focus();
            }
            finally
            {
                this.AutoScrollPosition = ScrollPosition;
                Slyce.Common.Utility.ResumePainting(this);
                Cursor = Cursors.Default;
                BusyPopulating = false;
                this.Focus();
                shapeCanvas1.Focus();
            }
        }
        private void DrawThreeLayers()
        {
            if (Table == null)
                return;

            Cursor = Cursors.WaitCursor;
            Font boldFont = new Font(Font, FontStyle.Bold);
            Font boldUnderlineFont = new Font(Font, FontStyle.Bold | FontStyle.Underline);

            RawTable centreShape = new RawTable(Table.Name, boldFont, Table);
            centreShape.Icon = TableImage;
            centreShape.MouseClick += new MouseEventHandler(centreShape_MouseClick);

            List<RawShape> topLevelShapes = new List<RawShape>();
            List<RawShape> bottomLevelShapes = new List<RawShape>();

            foreach (Relationship relationship in Table.Relationships)
            {
                RawShape referencedEntityShape;
                CustomLineCap startCap;
                CustomLineCap endCap;

                if (relationship.PrimaryTable == Table)
                {
                    referencedEntityShape = new RawTable(relationship.ForeignTable.Name, boldFont, relationship.ForeignTable);
                    string end1 = relationship.PrimaryKey.IsUnique ? "1" : "m";
                    startCap = relationship.ForeignKey.IsUnique ? LineCaps.One : LineCaps.Many;
                    endCap = relationship.PrimaryKey.IsUnique ? LineCaps.One : LineCaps.Many;
                }
                else if (relationship.ForeignTable == Table)
                {
                    referencedEntityShape = new RawTable(relationship.PrimaryTable.Name, boldFont, relationship.PrimaryTable);
                    string end1 = relationship.ForeignKey.IsUnique ? "1" : "m";
                    startCap = relationship.PrimaryKey.IsUnique ? LineCaps.One : LineCaps.Many;
                    endCap = relationship.ForeignKey.IsUnique ? LineCaps.One : LineCaps.Many;
                }
                else
                    throw new Exception("What the...??!");

                referencedEntityShape.Icon = TableImage;
                referencedEntityShape.OriginatingLineStyle.LineStyle = DashStyle.Solid;
                referencedEntityShape.OriginatingLineStyle.StartCap = startCap;
                referencedEntityShape.OriginatingLineStyle.EndCap = endCap;
                referencedEntityShape.OriginatingLineStyle.DataObject = relationship;
                referencedEntityShape.OriginatingLineStyle.ForeColor = Color.White;
                referencedEntityShape.OriginatingLineStyle.MiddleImage = InfoImage;
                referencedEntityShape.OriginatingLineStyle.MiddleImageClick += new MouseEventHandler(OriginatingLineStyle_MiddleImageClick);
                bottomLevelShapes.Add(referencedEntityShape);
            }
            //// Add empty reference
            //RawShape emptyReference = new RawShape("Add relationship...", boldUnderlineFont)
            //{
            //    BackColor1 = Color.White,
            //    BackColor2 = Color.White,
            //    BorderColor = Color.Gray,
            //    ForeColor = Color.Gray,
            //    FocusForeColor = Color.Blue,
            //    FocusBackColor1 = Color.WhiteSmoke,
            //    FocusBackColor2 = Color.White,
            //    FocusBorderColor = Color.DarkGray,
            //    Cursor = Cursors.Hand,
            //    OriginatingLineStyle = null,// new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow),
            //    Tag = null
            //};
            //emptyReference.MouseClick += new MouseEventHandler(emptyReference_MouseClick);
            //bottomLevelShapes.Add(emptyReference);

            //if (vertical)
            //  canvas1.DrawThreeLayerVertical(centreShape, topLevelShapes, bottomLevelShapes);
            //else
            shapeCanvas1.BackColor = this.BackColor;
            //shapeCanvas1.DrawThreeLayerHorizontal(centreShape, topLevelShapes, bottomLevelShapes, true);
            shapeCanvas1.Height = this.Height;
            //shapeCanvas1.DrawStar(centreShape, bottomLevelShapes);
            shapeCanvas1.KeepMainShapeFull = KeepMainShapeFull;
            shapeCanvas1.DrawThreeLayerVertical(centreShape, null, bottomLevelShapes);

            Cursor = Cursors.Default;
        }
        private void Populate()
        {
            if (TheEntity == null || BusyPopulating)
                return;

            BusyPopulating = true;

            try
            {
                Slyce.Common.Utility.SuspendPainting(this);
                HideAllFloatingToolstrips();
                Cursor = Cursors.WaitCursor;
                Font boldFont = new Font(Font, FontStyle.Bold);
                MainShape = new RawEntity(shapeCanvas1, TheEntity.Name, boldFont, TheEntity);
                MainShape.AutoSizeWidth = true;
                MainShape.Icon = EntityImage;
                MainShape.Enter += new EventHandler(centreShape_Enter);
                MainShape.Leave += new EventHandler(centreShape_Leave);
                MainShape.MouseClick += new MouseEventHandler(MainShape_MouseClick);
                //CategoryComponents.Parent = MainShape;
                //CategoryComponents.Properties.Clear();

                CategoryProperties.Parent = MainShape;
                CategoryProperties.IsExpanded = true;
                CategoryProperties.Properties.Clear();

                CategoryNavigation.Parent = MainShape;
                CategoryNavigation.Properties.Clear();

                MainShape.Categories.Add(CategoryProperties);

                if (TheEntity.InheritedProperties.Count() > 0)
                {
                    CategoryInheritedProperties.Parent = MainShape;
                    CategoryInheritedProperties.IsExpanded = true;
                    CategoryInheritedProperties.Properties.Clear();
                    MainShape.Categories.Add(CategoryInheritedProperties);
                }
                //MainShape.Categories.Add(CategoryComponents);
                MainShape.Categories.Add(CategoryNavigation);

                List<IColumn> columnsInComponents = new List<IColumn>();

                if (TheEntity.Components.Count > 0)
                {
                    foreach (var component in TheEntity.Components)
                    {
                        RawProperty cat = new RawProperty(component.Name, component);
                        cat.Enter += new EventHandler(Component_Enter);
                        cat.Leave += new EventHandler(Component_Leave);
                        cat.Click += new MouseEventHandler(Component_Click);
                        CategoryProperties.Properties.Add(cat);

                        foreach (var componentProperty in component.Properties)
                        {
                            columnsInComponents.Add(componentProperty.MappedColumn());

                            RawProperty prop = new RawProperty(componentProperty.PropertyName, componentProperty);
                            //prop.Click += new MouseEventHandler(Component_Click);
                            //prop.DoubleClick += new MouseEventHandler(Component_DoubleClick);
                            //prop.Enter += new EventHandler(Component_Enter);
                            //prop.Leave += new EventHandler(Component_Leave);
                            cat.SubProperties.Add(prop);
                        }
                    }
                }
                int propCount = TheEntity.Properties.Count();

                if (TheEntity.Properties.Count() > 0)
                {
                    //foreach (var property in TheEntity.Properties.Except(TheEntity.ForeignKeyPropertiesToExclude))
                    foreach (var property in TheEntity.Properties.Except(TheEntity.ForeignKeyPropertiesToExclude).Except(TheEntity.InheritedProperties))
                    {
                        if (!columnsInComponents.Contains(property.MappedColumn()))
                        {
                            RawProperty prop = new RawProperty(property.Name, property);
                            prop.BackColorFocused = Color.FromArgb(245, 208, 18);
                            prop.AllowSelection = true;
                            prop.Click += new MouseEventHandler(Property_Click);
                            CategoryProperties.Properties.Add(prop);
                        }
                    }
                    foreach (var property in TheEntity.InheritedProperties)
                    {
                        if (!columnsInComponents.Contains(property.MappedColumn()))
                        {
                            RawProperty prop = new RawProperty(property.Name, property);
                            prop.BackColorFocused = Color.FromArgb(245, 208, 18);
                            prop.AllowSelection = true;
                            prop.Click += new MouseEventHandler(Property_Click);
                            CategoryInheritedProperties.Properties.Add(prop);
                        }
                    }
                }
                Font boldUnderlineFont = new Font(Font, FontStyle.Bold | FontStyle.Underline);
                Font subTextFont = new Font(Font.FontFamily, Font.Size - 0.6F, FontStyle.Regular);
                List<RawShape> bottomLevelShapes = new List<RawShape>();
                List<RawShape> rightAlignedShapes = new List<RawShape>();

                #region MappedTables

                if (ShowMappedTables)
                {
                    bool hasMultiSchemas = false;

                    if (TheEntity.EntitySet.MappingSet.Database != null)
                        hasMultiSchemas = TheEntity.EntitySet.MappingSet.Database.GetSchemas().Count() > 1;

                    foreach (ITable table in MappedTables.OrderBy(t => t.Name))
                    {
                        RawTable tableShape = new RawTable(shapeCanvas1, table.Name, boldFont, table);

                        if (hasMultiSchemas)
                        {
                            tableShape.SubText = string.Format("  ({0}) ", table.Schema);
                            tableShape.FontSubText = subTextFont;
                        }
                        CustomLineCap startCap = LineCaps.None;
                        CustomLineCap endCap = LineCaps.None;
                        tableShape.OriginatingLineStyle.LineStyle = DashStyle.Dot;
                        //tableShape.OriginatingLineStyle.StartText = "";
                        //tableShape.OriginatingLineStyle.EndText = "";
                        tableShape.OriginatingLineStyle.MiddleText = GetDiscriminatorText(table);// "LastName = 'Anderson'";
                        tableShape.OriginatingLineStyle.StartCap = startCap;
                        tableShape.OriginatingLineStyle.EndCap = endCap;
                        //tableShape.BackColor1 = Color.Blue;
                        //tableShape.BackColor2 = Color.DarkBlue;
                        tableShape.RoundedCorners = false;
                        tableShape.Cursor = Cursors.Hand;
                        tableShape.Tag = table;
                        tableShape.Icon = table.IsView ? ViewImage : TableImage;

                        if (string.IsNullOrEmpty(tableShape.OriginatingLineStyle.MiddleText))
                            tableShape.OriginatingLineStyle.MiddleImage = FilterImageDisabled;
                        else
                            tableShape.OriginatingLineStyle.MiddleImage = FilterImage;

                        tableShape.OriginatingLineStyle.MiddleImageFocused = EditImage;
                        tableShape.OriginatingLineStyle.MouseClick += new MouseEventHandler(OriginatingLineStyle_MouseClick);
                        tableShape.MouseClick += new MouseEventHandler(tableShape_MouseClick);
                        tableShape.OriginatingLineStyle.MiddleImageClick += new MouseEventHandler(OriginatingLineStyle_MiddleImageClick);
                        tableShape.OriginatingLineStyle.DataObject = table;
                        tableShape.OriginatingLineStyle.ForeColor = Color.White;
                        tableShape.MouseDoubleClick += new MouseEventHandler(tableShape_MouseDoubleClick);

                        #region Setup discriminator
                        //throw new NotImplementedException("TODO: ddiscriminator stuff");
                        //if (TheEntity.Discriminator != null && TheEntity.Discriminator.RootGrouping != null)
                        //{
                        //    ArchAngel.Providers.EntityModel.Model.DatabaseLayer.Discrimination.Condition firstCondition = TheEntity.Discriminator.RootGrouping.Conditions.ElementAtOrDefault(0);

                        //    if (firstCondition != null)
                        //    {
                        //        IColumn column = firstCondition.Column;
                        //        ArchAngel.Providers.EntityModel.Model.DatabaseLayer.Discrimination.Operator op = firstCondition.Operator;
                        //        string exprText = firstCondition.ExpressionValue.Value;
                        //    }
                        //}
                        #endregion

                        //tableShape.OriginatingLineStyle.MiddleImageMouseOver += new EventHandler(OriginatingLineStyle_MiddleImageMouseOver);
                        rightAlignedShapes.Add(tableShape);
                    }
                    #region Add empty table
                    Slyce.Common.Controls.Diagramming.Shapes.RawShape emptyTable = new Slyce.Common.Controls.Diagramming.Shapes.RawShape(shapeCanvas1, "Add table...", boldUnderlineFont)
                    {
                        BackColor1 = Color.White,
                        BackColor2 = Color.White,
                        BorderColor = Color.Gray,
                        ForeColor = Color.Gray,
                        FocusForeColor = Color.Blue,
                        FocusBackColor1 = Color.WhiteSmoke,
                        FocusBackColor2 = Color.White,
                        FocusBorderColor = Color.DarkGray,
                        Cursor = Cursors.Hand,
                        Tag = null,
                        RoundedCorners = false,
                        OriginatingLineStyle = null //new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow)
                    };
                    emptyTable.MouseClick += new MouseEventHandler(emptyTable_MouseClick);
                    emptyTable.Cursor = Cursors.Hand;
                    //emptyTable.OriginatingLineStyle.MouseClick += new MouseEventHandler(OriginatingLineStyle_MouseClick);
                    rightAlignedShapes.Add(emptyTable);
                    #endregion
                }
                #endregion

                #region References
                if (ShowReferencedEntities)
                {
                    foreach (ReferenceImpl reference in TheEntity.References.OrderBy(r => r.Entity1 == TheEntity ? r.Entity2.Name : r.Entity1.Name))
                    {
                        RawEntity referencedEntityShape;
                        CustomLineCap startCap;
                        CustomLineCap endCap;

                        if (reference.Entity1 == TheEntity)
                        {
                            referencedEntityShape = new RawEntity(shapeCanvas1, reference.Entity2.Name, boldFont, reference.Entity2);
                            referencedEntityShape.OriginatingLineStyle.DataObject = reference;

                            if (ArchAngel.Interfaces.Cardinality.One.Equals(reference.Cardinality2))
                                startCap = LineCaps.One;
                            else if (ArchAngel.Interfaces.Cardinality.Many.Equals(reference.Cardinality2))
                                startCap = LineCaps.Many;
                            else if (ArchAngel.Interfaces.Cardinality.Zero.Equals(reference.Cardinality2))
                                startCap = LineCaps.Zero;
                            else
                                throw new NotImplementedException("Unexpected cardinality.");

                            if (ArchAngel.Interfaces.Cardinality.One.Equals(reference.Cardinality1))
                                endCap = LineCaps.One;
                            else if (ArchAngel.Interfaces.Cardinality.Many.Equals(reference.Cardinality1))
                                endCap = LineCaps.Many;
                            else if (ArchAngel.Interfaces.Cardinality.Zero.Equals(reference.Cardinality1))
                                endCap = LineCaps.Zero;
                            else
                                throw new NotImplementedException("Unexpected cardinality.");

                            referencedEntityShape.OriginatingLineStyle.StartTextDataMember = "End1Name";
                            referencedEntityShape.OriginatingLineStyle.EndTextDataMember = "End2Name";
                            referencedEntityShape.OriginatingLineStyle.StartImageClick += new LinkLine.MouseEndDelegate(OriginatingLineStyle_StartImageClick);
                            referencedEntityShape.OriginatingLineStyle.EndImageClick += new LinkLine.MouseEndDelegate(OriginatingLineStyle_EndImageClick);
                            referencedEntityShape.MouseClick += new MouseEventHandler(referencedEntityShape_MouseClick);
                            //referencedEntityShape.OriginatingLineStyle.MouseOverEnd1 += new LinkLine.MouseOverEndDelegate(OriginatingLineStyle_MouseOverEnd2);
                            //referencedEntityShape.OriginatingLineStyle.MouseOverEnd2 += new LinkLine.MouseOverEndDelegate(OriginatingLineStyle_MouseOverEnd1);
                            //reference..GetMappingSet().ReferenceMappings[0].

                            //RawCategory categoryNav = new RawCategory("Navigation", CategoryFont, referencedEntityShape);
                            //RawProperty navProp = new RawProperty(referencedEntityShape.OriginatingLineStyle.EndText, reference);
                            //navProp.Click += new MouseEventHandler(NavigationProperty_Click);
                            //categoryNav.Properties.Add(navProp);
                            //referencedEntityShape.Categories.Add(categoryNav);

                            RawProperty prop = new RawProperty(referencedEntityShape.OriginatingLineStyle.StartText, reference);
                            prop.DoubleClick += new MouseEventHandler(prop1_DoubleClick);
                            CategoryNavigation.Properties.Add(prop);
                        }
                        else if (reference.Entity2 == TheEntity)
                        {
                            referencedEntityShape = new RawEntity(shapeCanvas1, reference.Entity1.Name, boldFont, reference.Entity1);
                            referencedEntityShape.OriginatingLineStyle.DataObject = reference;

                            if (ArchAngel.Interfaces.Cardinality.One.Equals(reference.Cardinality1))
                                startCap = LineCaps.One;
                            else if (ArchAngel.Interfaces.Cardinality.Many.Equals(reference.Cardinality1))
                                startCap = LineCaps.Many;
                            else if (ArchAngel.Interfaces.Cardinality.Zero.Equals(reference.Cardinality1))
                                startCap = LineCaps.Zero;
                            else
                                throw new NotImplementedException("Unexpected cardinality.");

                            if (ArchAngel.Interfaces.Cardinality.One.Equals(reference.Cardinality2))
                                endCap = LineCaps.One;
                            else if (ArchAngel.Interfaces.Cardinality.Many.Equals(reference.Cardinality2))
                                endCap = LineCaps.Many;
                            else if (ArchAngel.Interfaces.Cardinality.Zero.Equals(reference.Cardinality2))
                                endCap = LineCaps.Zero;
                            else
                                throw new NotImplementedException("Unexpected cardinality.");

                            referencedEntityShape.OriginatingLineStyle.StartTextDataMember = "End2Name";
                            referencedEntityShape.OriginatingLineStyle.EndTextDataMember = "End1Name";
                            referencedEntityShape.OriginatingLineStyle.StartImageClick += new LinkLine.MouseEndDelegate(OriginatingLineStyle_EndImageClick);
                            referencedEntityShape.OriginatingLineStyle.EndImageClick += new LinkLine.MouseEndDelegate(OriginatingLineStyle_StartImageClick);
                            referencedEntityShape.MouseClick += new MouseEventHandler(referencedEntityShape_MouseClick);
                            //referencedEntityShape.OriginatingLineStyle.MouseOverEnd1 += new LinkLine.MouseOverEndDelegate(OriginatingLineStyle_MouseOverEnd1);
                            //referencedEntityShape.OriginatingLineStyle.MouseOverEnd2 += new LinkLine.MouseOverEndDelegate(OriginatingLineStyle_MouseOverEnd2);

                            //RawCategory categoryNav = new RawCategory("Navigation", CategoryFont, referencedEntityShape);
                            //RawProperty navProp = new RawProperty(referencedEntityShape.OriginatingLineStyle.EndText, reference);
                            //navProp.Click += new MouseEventHandler(NavigationProperty_Click);
                            //categoryNav.Properties.Add(navProp);
                            //referencedEntityShape.Categories.Add(categoryNav);

                            RawProperty prop = new RawProperty(referencedEntityShape.OriginatingLineStyle.StartText, reference);
                            prop.DoubleClick += new MouseEventHandler(prop2_DoubleClick);
                            CategoryNavigation.Properties.Add(prop);
                        }
                        else
                            throw new Exception("What the...??!");

                        referencedEntityShape.Cursor = Cursors.Hand;
                        referencedEntityShape.Icon = EntityImage;
                        referencedEntityShape.OriginatingLineStyle.LineStyle = DashStyle.Solid;
                        referencedEntityShape.OriginatingLineStyle.StartCap = startCap;
                        referencedEntityShape.OriginatingLineStyle.EndCap = endCap;

                        referencedEntityShape.OriginatingLineStyle.ForeColor = Color.White;
                        referencedEntityShape.OriginatingLineStyle.DefaultEndText = "not set";
                        referencedEntityShape.OriginatingLineStyle.StartImage = EditImage;
                        referencedEntityShape.OriginatingLineStyle.EndImage = EditImage;
                        referencedEntityShape.MouseDoubleClick += new MouseEventHandler(referencedEntityShape_MouseDoubleClick);
                        referencedEntityShape.Enter += new EventHandler(referencedEntityShape_Enter);
                        referencedEntityShape.Leave += new EventHandler(referencedEntityShape_Leave);
                        //referencedEntityShape.AutoSizeWidth = true;
                        bottomLevelShapes.Add(referencedEntityShape);
                    }
                    // Add empty reference
                    RawShape emptyReference = new RawShape(shapeCanvas1, "Add entity...", boldUnderlineFont)
                    {
                        BackColor1 = Color.White,
                        BackColor2 = Color.White,
                        BorderColor = Color.Gray,
                        ForeColor = Color.Gray,
                        FocusForeColor = Color.Blue,
                        FocusBackColor1 = Color.WhiteSmoke,
                        FocusBackColor2 = Color.White,
                        FocusBorderColor = Color.DarkGray,
                        Cursor = Cursors.Hand,
                        OriginatingLineStyle = null,// new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow),
                        Tag = null
                    };
                    //emptyReference.OriginatingLineStyle.ForeColor = Color.White;
                    emptyReference.MouseClick += new MouseEventHandler(emptyReference_MouseClick);
                    emptyReference.Cursor = Cursors.Hand;
                    bottomLevelShapes.Add(emptyReference);
                }
                #endregion

                //shapeCanvas1.BackColor = this.BackColor;
                ////shapeCanvas1.DrawStar(centreShape, outerShapes);
                ////shapeCanvas1.DrawThreeLayerHorizontal(centreShape, null, outerShapes, false);
                //shapeCanvas1.Height = this.Height;
                //shapeCanvas1.DrawThreeLayerVertical(centreShape, null, outerShapes);
                //Cursor = Cursors.Default;

                shapeCanvas1.SwimLane1 = new ShapeCanvas.SwimLaneStyle(Color.FromArgb(79, 124, 205), Color.Black, Color.White, 180F, "Referenced Entities", ShapeCanvas.SwimLaneStyle.Styles.Fill);
                shapeCanvas1.SwimLane3 = new ShapeCanvas.SwimLaneStyle(Color.FromArgb(79, 124, 205), Color.Black, Color.White, 0F, "Mapped Tables", ShapeCanvas.SwimLaneStyle.Styles.Fill);

                shapeCanvas1.BackColor = this.BackColor;
                //shapeCanvas1.DrawStar(centreShape, outerShapes);
                //shapeCanvas1.DrawThreeLayerHorizontal(centreShape, null, outerShapes, false);
                shapeCanvas1.Height = this.Height;
                //shapeCanvas1.DrawThreeLayerVertical(centreShape, null, bottomLevelShapes, rightAlignedShapes);
                shapeCanvas1.KeepMainShapeCentered = true;
                MainShape.RecalcSizeRequired = true;
                shapeCanvas1.DrawThreeLayerHorizontal(MainShape, bottomLevelShapes, rightAlignedShapes, KeepMainShapeFull);
                this.Focus();
                shapeCanvas1.Focus();
            }
            finally
            {
                this.AutoScrollPosition = ScrollPosition;
                Slyce.Common.Utility.ResumePainting(this);
                Cursor = Cursors.Default;
                BusyPopulating = false;
            }
        }
        private void Populate()
        {
            Font boldFont = new Font(Font, FontStyle.Bold);
            Font boldUnderlineFont = new Font(Font, FontStyle.Bold | FontStyle.Underline);
            RawShape centreShape = new RawEntity(shapeCanvas1, Entity.Name, boldFont, Entity);

            List<RawShape> level1Shapes = new List<RawShape>();
            List<RawShape> level3Shapes = new List<RawShape>();
            List<RawShape> rightAlignedShapes = new List<RawShape>();

            if (Entity.Parent == null)
            {
                RawShape emptyParent = new RawShape(shapeCanvas1, "Add parent...", boldUnderlineFont)
                {
                    BackColor1 = Color.White,
                    BackColor2 = Color.White,
                    BorderColor = Color.Gray,
                    ForeColor = Color.Gray,
                    FocusForeColor = Color.Blue,
                    FocusBackColor1 = Color.WhiteSmoke,
                    FocusBackColor2 = Color.White,
                    FocusBorderColor = Color.DarkGray,
                    Cursor = Cursors.Hand,
                    OriginatingLineStyle = null,//new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow),
                    Tag = null
                };
                emptyParent.MouseClick += new MouseEventHandler(emptyParent_MouseClick);
                level1Shapes.Add(emptyParent);
            }
            else
            {
                RawEntity parentShape = new RawEntity(shapeCanvas1, Entity.Parent.Name, boldFont, Entity.Parent);
                parentShape.OriginatingLineStyle = new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow);
                parentShape.MouseClick += new MouseEventHandler(parentShape_MouseClick);
                level1Shapes.Add(parentShape);

                foreach (var table in Entity.Parent.MappedTables())
                {
                    RawTable tableShape = new RawTable(shapeCanvas1, table.Name, boldFont, table);
                    tableShape.OriginatingLineStyle = new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow);
                    tableShape.MouseClick += new MouseEventHandler(tableShape_MouseClick);
                    rightAlignedShapes.Add(tableShape);
                }
            }
            foreach (var table in Entity.MappedTables())
            {
                RawTable tableShape = new RawTable(shapeCanvas1, table.Name, boldFont, table);
                tableShape.OriginatingLineStyle = new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow);
                tableShape.MouseClick += new MouseEventHandler(tableShape_MouseClick);
                rightAlignedShapes.Add(tableShape);
            }
            foreach (Entity child in Entity.Children)
            {
                RawEntity childShape = new RawEntity(shapeCanvas1, child.Name, boldFont, child);
                childShape.OriginatingLineStyle = new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow);
                childShape.MouseClick += new MouseEventHandler(childShape_MouseClick);
                level3Shapes.Add(childShape);

                foreach (var table in child.MappedTables())
                {
                    RawTable tableShape = new RawTable(shapeCanvas1, table.Name, boldFont, table);
                    tableShape.OriginatingLineStyle = new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow);
                    tableShape.MouseClick += new MouseEventHandler(tableShape_MouseClick);
                    rightAlignedShapes.Add(tableShape);
                }
            }
            #region Add empty child
            RawShape emptyChild = new RawShape(shapeCanvas1, "Add child...", boldUnderlineFont)
            {
                BackColor1 = Color.White,
                BackColor2 = Color.White,
                BorderColor = Color.Gray,
                ForeColor = Color.Gray,
                FocusForeColor = Color.Blue,
                FocusBackColor1 = Color.WhiteSmoke,
                FocusBackColor2 = Color.White,
                FocusBorderColor = Color.DarkGray,
                Cursor = Cursors.Hand,
                Tag = null,
                OriginatingLineStyle = null//new LinkLine(boldFont, DashStyle.Dot, "", "", "", LineCaps.None, LineCaps.SolidArrow)
            };
            emptyChild.MouseClick += new MouseEventHandler(emptyChild_MouseClick);
            //emptyChild.OriginatingLineStyle.MouseClick += new MouseEventHandler(OriginatingLineStyle_MouseClick);
            level3Shapes.Add(emptyChild);
            #endregion

            shapeCanvas1.BackColor = this.BackColor;
            shapeCanvas1.SwimLane1 = new ShapeCanvas.SwimLaneStyle(Color.FromArgb(100, 100, 100), Color.Black, Color.White, 90F, "Parent", ShapeCanvas.SwimLaneStyle.Styles.Line);
            shapeCanvas1.SwimLane2 = new ShapeCanvas.SwimLaneStyle(Color.FromArgb(100, 100, 100), Color.Black, Color.White, 90F, "Entity", ShapeCanvas.SwimLaneStyle.Styles.Line);
            shapeCanvas1.SwimLane3 = new ShapeCanvas.SwimLaneStyle(Color.FromArgb(100, 100, 100), Color.Black, Color.White, 90F, "Children", ShapeCanvas.SwimLaneStyle.Styles.Line);
            shapeCanvas1.SwimLane4 = new ShapeCanvas.SwimLaneStyle(Color.FromArgb(100, 100, 100), Color.Black, Color.White, 90F, "Mapped Tables", ShapeCanvas.SwimLaneStyle.Styles.Line);

            shapeCanvas1.BackColor = this.BackColor;
            shapeCanvas1.DrawVerticalSwimLanes(centreShape, level1Shapes, level3Shapes, rightAlignedShapes, "Parent", "Entity", "Children", "Mapped Tables");
        }