Saturday, April 23, 2005

PropertyGenerator

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace PropertyGenerator
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class FrmMain : System.Windows.Forms.Form
 {
  private System.Windows.Forms.TextBox txtSource;
  private System.Windows.Forms.Button btnGenerate;
  private System.Windows.Forms.TextBox txtResult;
  private System.Windows.Forms.TextBox txtMember;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.ComboBox cboLanguageFrom;
  private System.Windows.Forms.TextBox txtStartingCharPos;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.ComboBox cboLanguageTo;
  private System.Windows.Forms.CheckBox cbxProperty;
  private System.Windows.Forms.CheckBox chkCreateRegion;
                private CheckBox chkEquals;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public FrmMain()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null) 
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.txtResult = new System.Windows.Forms.TextBox();
   this.txtSource = new System.Windows.Forms.TextBox();
   this.btnGenerate = new System.Windows.Forms.Button();
   this.txtMember = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.cboLanguageFrom = new System.Windows.Forms.ComboBox();
   this.label4 = new System.Windows.Forms.Label();
   this.txtStartingCharPos = new System.Windows.Forms.TextBox();
   this.label5 = new System.Windows.Forms.Label();
   this.cboLanguageTo = new System.Windows.Forms.ComboBox();
   this.cbxProperty = new System.Windows.Forms.CheckBox();
   this.chkCreateRegion = new System.Windows.Forms.CheckBox();
   this.chkEquals = new System.Windows.Forms.CheckBox();
   this.SuspendLayout();
   // 
   // txtResult
   // 
   this.txtResult.Location = new System.Drawing.Point(301, 0);
   this.txtResult.Multiline = true;
   this.txtResult.Name = "txtResult";
   this.txtResult.Size = new System.Drawing.Size(300, 300);
   this.txtResult.TabIndex = 1;
   // 
   // txtSource
   // 
   this.txtSource.Location = new System.Drawing.Point(-1, 0);
   this.txtSource.Multiline = true;
   this.txtSource.Name = "txtSource";
   this.txtSource.Size = new System.Drawing.Size(300, 300);
   this.txtSource.TabIndex = 0;
   // 
   // btnGenerate
   // 
   this.btnGenerate.Location = new System.Drawing.Point(510, 341);
   this.btnGenerate.Name = "btnGenerate";
   this.btnGenerate.Size = new System.Drawing.Size(75, 23);
   this.btnGenerate.TabIndex = 2;
   this.btnGenerate.Text = "Generate";
   this.btnGenerate.Click += new System.EventHandler(this.btnGenerate_Click);
   // 
   // txtMember
   // 
   this.txtMember.Location = new System.Drawing.Point(72, 344);
   this.txtMember.Name = "txtMember";
   this.txtMember.Size = new System.Drawing.Size(136, 20);
   this.txtMember.TabIndex = 3;
   // 
   // label1
   // 
   this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.label1.Location = new System.Drawing.Point(8, 347);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(64, 23);
   this.label1.TabIndex = 4;
   this.label1.Text = "Member";
   // 
   // label2
   // 
   this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.label2.Location = new System.Drawing.Point(216, 347);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(120, 23);
   this.label2.TabIndex = 5;
   this.label2.Text = "dont forget . (Dot)";
   // 
   // label3
   // 
   this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.label3.Location = new System.Drawing.Point(8, 318);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(64, 23);
   this.label3.TabIndex = 6;
   this.label3.Text = "Language";
   // 
   // cboLanguageFrom
   // 
   this.cboLanguageFrom.Items.AddRange(new object[] {
   "VB.NET",
   "C#",
   "Java"});
   this.cboLanguageFrom.Location = new System.Drawing.Point(72, 318);
   this.cboLanguageFrom.Name = "cboLanguageFrom";
   this.cboLanguageFrom.Size = new System.Drawing.Size(136, 21);
   this.cboLanguageFrom.TabIndex = 7;
   // 
   // label4
   // 
   this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.label4.Location = new System.Drawing.Point(347, 347);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(118, 23);
   this.label4.TabIndex = 8;
   this.label4.Text = "Starting char position";
   // 
   // txtStartingCharPos
   // 
   this.txtStartingCharPos.Location = new System.Drawing.Point(472, 344);
   this.txtStartingCharPos.Name = "txtStartingCharPos";
   this.txtStartingCharPos.Size = new System.Drawing.Size(32, 20);
   this.txtStartingCharPos.TabIndex = 9;
   this.txtStartingCharPos.Text = "0";
   // 
   // label5
   // 
   this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.label5.Location = new System.Drawing.Point(219, 318);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(32, 23);
   this.label5.TabIndex = 10;
   this.label5.Text = "To";
   // 
   // cboLanguageTo
   // 
   this.cboLanguageTo.Items.AddRange(new object[] {
   "VB.NET",
   "C#",
   "Java"});
   this.cboLanguageTo.Location = new System.Drawing.Point(251, 318);
   this.cboLanguageTo.Name = "cboLanguageTo";
   this.cboLanguageTo.Size = new System.Drawing.Size(136, 21);
   this.cboLanguageTo.TabIndex = 11;
   // 
   // cbxProperty
   // 
   this.cbxProperty.Checked = true;
   this.cbxProperty.CheckState = System.Windows.Forms.CheckState.Checked;
   this.cbxProperty.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.cbxProperty.Location = new System.Drawing.Point(408, 304);
   this.cbxProperty.Name = "cbxProperty";
   this.cbxProperty.Size = new System.Drawing.Size(104, 24);
   this.cbxProperty.TabIndex = 12;
   this.cbxProperty.Text = "Property";
   // 
   // chkCreateRegion
   // 
   this.chkCreateRegion.Checked = true;
   this.chkCreateRegion.CheckState = System.Windows.Forms.CheckState.Checked;
   this.chkCreateRegion.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.chkCreateRegion.Location = new System.Drawing.Point(408, 322);
   this.chkCreateRegion.Name = "chkCreateRegion";
   this.chkCreateRegion.Size = new System.Drawing.Size(104, 24);
   this.chkCreateRegion.TabIndex = 13;
   this.chkCreateRegion.Text = "Create Region";
   // 
   // chkEquals
   // 
   this.chkEquals.Checked = true;
   this.chkEquals.CheckState = System.Windows.Forms.CheckState.Checked;
   this.chkEquals.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
   this.chkEquals.Location = new System.Drawing.Point(510, 306);
   this.chkEquals.Name = "chkEquals";
   this.chkEquals.Size = new System.Drawing.Size(73, 24);
   this.chkEquals.TabIndex = 14;
   this.chkEquals.Text = "Equals";
   // 
   // FrmMain
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(592, 373);
   this.Controls.Add(this.chkEquals);
   this.Controls.Add(this.txtStartingCharPos);
   this.Controls.Add(this.chkCreateRegion);
   this.Controls.Add(this.cbxProperty);
   this.Controls.Add(this.cboLanguageTo);
   this.Controls.Add(this.label5);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.cboLanguageFrom);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.txtMember);
   this.Controls.Add(this.btnGenerate);
   this.Controls.Add(this.txtResult);
   this.Controls.Add(this.txtSource);
   this.Name = "FrmMain";
   this.Text = "Generator";
   this.Load += new System.EventHandler(this.FrmMain_Load);
   this.ResumeLayout(false);
   this.PerformLayout();

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main() 
  {
   Application.Run(new FrmMain());
  }

  private void btnGenerate_Click(object sender, System.EventArgs e)
  {
   int pos=Convert.ToInt32(txtStartingCharPos.Text.Trim());
   String[] sCodes;
   
   sCodes = txtSource.Text.Split('\n');
   String sResult="";
            String sEquals = "";
   foreach (String sCode in sCodes)
   {
    String[] sToken;
    sToken=sCode.Trim().Split(' ');
    switch (cboLanguageFrom.SelectedIndex)
    {
     case 0:
      if (cbxProperty.Checked)
      {
       switch (cboLanguageTo.SelectedIndex)
       {
        case 0: sResult+=
           "Public Property "+ sToken[1].Substring(pos) + "() As " + sToken[3] + "\n"+
           " Get\n"+
           "  Return Me."+ txtMember.Text + sToken[1] +"\n"+
           " End Get\n"+
           " Set(ByVal Value As "+ sToken[3] + ")\n"+
           "  Me."+ txtMember.Text + sToken[1] + " = Value\n"+
           " End Set\n"+
           "End Property\n\n";
         break;
        case 1: sResult+=
           "public "+sToken[3]+" "+sToken[1].Substring(pos)+"\n"+
           "{\n"+
           " get\n"+
           " {\n"+
           "  return "+ txtMember.Text + sToken[1] +";\n"+
           " }\n"+
           " set\n"+
           " {\n"+
           "  "+ txtMember.Text + sToken[1]+ " = value;\n"+
           " }\n"+
           "}\n";
         break;
        case 2: sResult+=
           "public "+ sToken[3] +" get"+ sToken[1].Substring(pos) +"() {\n"+
           " return "+ txtMember.Text + sToken[1] +";\n"+
           "}\n"+
           "public void set"+ sToken[1].Substring(pos) +"("+ sToken[3] +" "+ sToken[1].ToLower()[2] + sToken[1].Substring(3)+") {\n"+
           " "+ txtMember.Text + sToken[1] +" = "+ sToken[1].ToLower()[pos] + sToken[1].Substring(pos+1) +";\n"+
           "}\n\n";
         break;
       }
      }
      else
      {
       switch (cboLanguageTo.SelectedIndex)
       {
        case 0: sResult+=
           "Public Property "+ sToken[1].Substring(pos) + "() As " + sToken[3] + "\n"+
           " Get\n"+
           "  Return Me."+ txtMember.Text + sToken[1] +"\n"+
           " End Get\n"+
           " Set(ByVal Value As "+ sToken[3] + ")\n"+
           "  Me."+ txtMember.Text + sToken[1] + " = Value\n"+
           " End Set\n"+
           "End Property\n\n";
         break;
        case 1: 
        case 2: sResult+=
           "protected "+sToken[3]+" "+sToken[1]+";\n";
         break;
          
       }
      }
      break;
     case 1: 
      if (cbxProperty.Checked || chkEquals.Checked)
      {
       switch (cboLanguageTo.SelectedIndex)
       {
        case 0: sResult+=
           "Public Property "+ sToken[1].Substring(pos) + "() As " + sToken[3] + "\n"+
           " Get\n"+
           "  Return Me."+ txtMember.Text + sToken[1] +"\n"+
           " End Get\n"+
           " Set(ByVal Value As "+ sToken[3] + ")\n"+
           "  Me."+ txtMember.Text + sToken[1] + " = Value\n"+
           " End Set\n"+
           "End Property\n\n";
         break;
        case 1:
                                    string sTemp = sToken[2].Substring(pos, sToken[2].Length - (pos + 1));
                                    string s1stChar = sTemp[0].ToString().ToUpper();
                                    sTemp = s1stChar + sTemp.Substring(1);
                                    sResult+=
                                            "public "+sToken[1]+" "+ sTemp +"\n"+
           "{\n"+
           " get\n"+
           " {\n"+
           "  return "+ txtMember.Text + sToken[2] +"\n"+
           " }\n"+
           " set\n"+
           " {\n"+
           "  "+ txtMember.Text + sToken[2].Substring(0,sToken[2].Length-1)+ " = value;\n"+
           " }\n"+
           "}\n";

                                    string sCheckMember = sToken[2].Replace(";", "");
                                    sEquals +=
                                        "if (" + sCheckMember + " == null ? item." + sCheckMember + "  != null : !" + sCheckMember + ".Equals(item." + sCheckMember + " ))\n" +
                                        "    return false;\n";

         break;
        case 2: sResult+=
           "public "+ sToken[1] +" get"+ sToken[2].Substring(pos, sToken[2].Length-(pos+1)) +"() {\n"+
           " return "+ txtMember.Text + sToken[2] +"\n"+
           "}\n"+
           "public void set"+ sToken[2].Substring(pos, sToken[2].Length-(pos+1)) +"("+ sToken[1] +" "+ sToken[2].ToLower()[3] + sToken[2].Substring(4, sToken[2].Length-5)+") {\n"+
           " "+ txtMember.Text + sToken[2].Substring(0, sToken[2].Length-1) +" = "+ sToken[2].ToLower()[3] + sToken[2].Substring(4, sToken[2].Length-5) +";\n"+
           "}\n\n";
         break;
       }
      }
      else
      {
       switch (cboLanguageTo.SelectedIndex)
       {
        case 0: sResult+=
           "Public Property "+ sToken[1].Substring(pos) + "() As " + sToken[3] + "\n"+
           " Get\n"+
           "  Return Me."+ txtMember.Text + sToken[1] +"\n"+
           " End Get\n"+
           " Set(ByVal Value As "+ sToken[3] + ")\n"+
           "  Me."+ txtMember.Text + sToken[1] + " = Value\n"+
           " End Set\n"+
           "End Property\n\n";
         break;
        case 1: 
        case 2: sResult+=
           "protected "+sToken[3]+" "+sToken[1]+";\n";
         break;
          
       }
      }
      break;
    }

   }
            if (!cbxProperty.Checked)
            {
                sResult = "";
            }
   if (chkCreateRegion.Checked)
   {
    switch(cboLanguageTo.SelectedIndex)
    {
     case 0:
     case 1:
      sResult= "#region C#-Style\n\n" + sResult +
       "\n#endregion C#-Style";
      break;
     case 2:
      sResult= "#region Java-Style\n\n" + sResult +
       "\n#endregion Java-Style";
      break;
    }
   }
            if (chkEquals.Checked)
            {
                sEquals =
                     "public Boolean Equals(Object item)\n" +
                     "{\n" +
                     "  if (this == item)\n" +
                     "  return true;\n" + sEquals +
                     "  return true;\n" +
                     "}\n";

                switch (cboLanguageTo.SelectedIndex)
                {
                    case 0:
                    case 1:
                        sResult = sResult + sEquals;
                        break;
                    case 2:
                        break;
                }
            }
   txtResult.Text=sResult;
  }

  private void FrmMain_Load(object sender, System.EventArgs e)
  {
   cboLanguageFrom.SelectedIndex=1;
   cboLanguageTo.SelectedIndex=1;
  }
 }
}

PS: There are free refactoring tools comes with VS 2005+, but not for VS 2002/2003, check http://msdn.microsoft.com/en-us/library/ms379618(v=vs.80).aspx

No comments:

Post a Comment