Click here to Skip to main content
15,885,877 members
Articles / Web Development / ASP.NET

Template-Based Code Generation with SmartCode

Rate me:
Please Sign up or sign in to vote.
4.82/5 (35 votes)
25 Dec 20067 min read 100.8K   3.5K   121  
SmartCode is a template based code generator.This tutorial describes the process of building a templates to SmartCode
/*
 * Copyright � 2005-2006 Danilo Mendez <danilo.mendez@kontac.net>
 * Adolfo Socorro <ajs@esolutionspr.com>
 * www.kontac.net 
 * All rights reserved.
 * Released under both BSD license and Lesser GPL library license.
 * Whenever there is any discrepancy between the two licenses,
 * the BSD license will take precedence.
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SmartCode.Model;
using SmartCode.Studio.Templates;

namespace SmartCode.Studio
{
    public partial class GenerationDlg : Form
    {
        public GenerationDlg()
        {
            InitializeComponent();
        }

        private void uiGenerate_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void GenerationDlg_Load(object sender, EventArgs e)
        {
            LoadTables();
            LoadViews();
            LoadAllTemplates();
        }


        private void LoadTables()
        {
            if (this.uiChkTables.Checked)
            {
                System.Collections.ArrayList allTables = SmartCode.Studio.SmartStudio.MainForm.GetAllTables();
                foreach (Table table in allTables)
                {
                    ListViewItem li = new ListViewItem(table.Name);
                    li.Tag = table;
                    li.ImageIndex = 3;
                    this.uiLVNamedObjects.Items.Add(li);
                }
            }
        }
        private void LoadViews()
        {
            if (this.uiChkViews.Checked)
            {
                System.Collections.ArrayList allTables = SmartCode.Studio.SmartStudio.MainForm.GetAllTables();
                foreach (Table table in allTables)
                {
                    ListViewItem li = new ListViewItem(table.Name);
                    li.Tag = table;
                    li.ImageIndex = 3;
                    this.uiLVNamedObjects.Items.Add(li);
                }
            }
        }


        private void LoadAllTemplates()
        {
            this.uiTVEntityTemplates.Nodes.Clear();


            foreach (KeyValuePair<string, LibraryInfo> pair in SmartCode.Studio.SmartStudio.MainForm.CurrentProject.Libraries)
            {
                TreeNode libraryNode = new TreeNode(pair.Value.AssemblyName, 0, 0);
                libraryNode.Tag = pair.Value;
                this.uiTVEntityTemplates.Nodes.Add(libraryNode);

                foreach (TemplateInfo template in pair.Value.Templates)
                {
                    if (!template.IsProjectTemplate)
                    {
                        TreeNode templateNode = new TreeNode(template.Name, 1, 1);
                        templateNode.Tag = template;
                        libraryNode.Nodes.Add(templateNode);

                        foreach (NamedObject namedObject in template.AssignedObjects)
                        {
                            TreeNode namedNode = new TreeNode(namedObject.Name, 2, 2);
                            namedNode.Tag = namedObject;
                            templateNode.Nodes.Add(namedNode);
                        }
                    }
                    else
                    {
                        ListViewItem li = new ListViewItem(template.Name);
                        li.Tag = template;
                        li.ImageIndex = 2;
                        li.Checked = template.Run;
                        this.uiLVProjectTemplates.Items.Add(li);
                    }
                }

                libraryNode.ExpandAll();

            }
        }

        private void uiBtnAdd_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem li in this.uiLVNamedObjects.SelectedItems)
            {
                NamedObject namedObject = li.Tag as NamedObject;
                if (namedObject != null)
                {
                    foreach (TreeNode templateNode in this.uiTVEntityTemplates.SelectedNodes)
                    {
                        TemplateInfo template = templateNode.Tag as TemplateInfo;
                        if (template != null)
                        {
                            if (!template.AssignedObjects.Contains(namedObject))
                            {
                                template.AssignedObjects.Add(namedObject);
                                TreeNode namedNode = new TreeNode(namedObject.Name, 2, 2);
                                namedNode.Tag = namedObject;
                                templateNode.Nodes.Add(namedNode);
                                templateNode.ExpandAll();
                            }
                        }

                    }
                }
            }
        }

        private void uiBtnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                System.Collections.ArrayList namedNodes = new System.Collections.ArrayList();
                foreach (TreeNode node in this.uiTVEntityTemplates.SelectedNodes)
                {
                    NamedObject obj = node.Tag as NamedObject;
                    if (obj != null)
                    {
                        namedNodes.Add(node);
                    }

                    TemplateInfo info = node.Tag as TemplateInfo;
                    if (info != null)
                    {
                        foreach (TreeNode namedNode in node.Nodes)
                        {
                            namedNodes.Add(namedNode);
                        }
                    }
                }
                RemoveNodes(namedNodes);
                
            }
            catch { }
        }

        private void RemoveNodes(System.Collections.ArrayList namedNodes)
        {

            foreach (TreeNode namedNode in namedNodes)
            {
                NamedObject obj = namedNode.Tag as NamedObject;
                if (obj != null)
                {
                    TemplateInfo parentTemplate = namedNode.Parent.Tag as TemplateInfo;
                    if (parentTemplate != null)
                    {
                        if (parentTemplate.AssignedObjects.Remove(obj))
                        {
                            this.uiTVEntityTemplates.SelectedNodes.Remove(namedNode);
                            namedNode.Remove();
                        }
                    }
                }
            }
        }

        private void uiLVNamedObjects_SelectedIndexChanged(object sender, EventArgs e)
        {
            EnableArrows();
        }

        private void uiTVEntityTemplates_AfterSelect(object sender, TreeViewEventArgs e)
        {
            EnableArrows();
        }

        private void uiTabTemplates_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiTabTemplates.SelectedIndex == 0)
            {
                EnableArrows();
                this.uiBtnAdd.Enabled = true;
                this.uiBtnRemove.Enabled = true;
            }
            else
            {
                this.uiBtnAdd.Enabled = false;
                this.uiBtnRemove.Enabled = false;
            }
        }

        /// <summary>
        /// TODO: Fix the bug
        /// </summary>
        private void EnableArrows()
        {

            //bool enableAdd = false;
            //bool enableRemove = false;

            //foreach (TreeNode node in this.uiTVEntityTemplates.SelectedNodes)
            //{
            //    if (node.Tag is TemplateInfo)
            //    {
            //        enableAdd = true;
            //    }
            //    if (node.Tag is NamedObject)
            //    {
            //        enableRemove = true;
            //    }
            //}

            //enableAdd &= uiTabTemplates.SelectedIndex == 0;
            //enableRemove &= uiTabTemplates.SelectedIndex == 0;

            //enableAdd &= uiLVNamedObjects.SelectedItems.Count > 0;

            //this.uiBtnAdd.Enabled = enableAdd;
            //this.uiBtnRemove.Enabled = enableRemove;
        }

        private void uiLVProjectTemplates_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            ListViewItem li = this.uiLVProjectTemplates.Items[e.Index];
            TemplateInfo template = li.Tag as TemplateInfo;
            if (template != null)
            {
                template.Run = e.CurrentValue == CheckState.Checked;
            }

        }

        private void uiBtnRemoveAll_Click(object sender, EventArgs e)
        {
            System.Collections.ArrayList namedNodes = new System.Collections.ArrayList();
            foreach (TreeNode LibraryNode in this.uiTVEntityTemplates.Nodes)
            {
                foreach (TreeNode TemplateNode in LibraryNode.Nodes)
                {
                    foreach (TreeNode NamedNode in TemplateNode.Nodes)
                    {
                        if (NamedNode.Tag is NamedObject)
                        {
                            namedNodes.Add(NamedNode);
                        }
                    }
                }
            }
            this.RemoveNodes(namedNodes);
        }

        private void Chk_CheckedChanged(object sender, EventArgs e)
        {
            this.uiLVNamedObjects.Items.Clear();
            LoadTables();
            LoadViews();
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Danilo is the creator of SmartRules, a Business Rules Engine. He is an industry consultant working primarily with companies interested in implementing dynamic rules programming concepts to add flexibility to their architectures on web, CE, and desktop platforms. He operates his own website, Kontac, where you will find more information.

To contact Danilo, email him at danilo.mendez@gmail.com.

Comments and Discussions