Sunday, October 2, 2005

Oh, Glassfish

When peoples declared that mainstream Java application server is Tomcat, honestly I can say that until now, I never use this cat. App server installed in my PC is Resin from Caucho. Previously I also use Websphere from IBM, now uninstalled to free some space in my HD. Resin is my testing server (or learning server) I used when learning Java, Servlet and JSP during my university time (and now). But now, I am interested in Sun application server. Why? Because, it’s the first commercial Java EE application server that change its license from proprietary-commercial to an Open Source license.

Sun change the license of its application server and release it as GlassFish Project Long live Sun!

Now, excuse me, I need to download and play with this aquarium…


Edited: Add picture

Monday, June 27, 2005

A Poem of Purpose Driven Life

In a book titled "Purpose Driven Life", Rick Warren included a Russel Kelfer's poem like below:

You are who you are for a reason
You're part of an intricate plan
You're a precious and perfect unique design,
Called God's special woman or man

You look like you look for a reason
Our God made no mistake
He knit you together within the womb
You're just what he wanted to make

The parents you had were the ones he chose,
And no matter how you may feel,
They were custom-designed with God's plan in mind,
And they bear the Master's seal

No, that trauma you faced was not easy
And God wept that it hurt you so;
But it was allowed to shape your heart
So that into his likeness you'd grow

You are who you are for a reason,
You've been formed by the Master's rod
You are who you are, beloved,
Because there is a GOD!

Then, I'll translated it into Indonesian:
Kau ada sebagaimana kau ada karena suatu alasan
Kau adalah bagian dari suatu rencana yang rumit
Kau adalah suatu rancangan unik yang berharga dan sempurna
Disebut pria atau wanita khusus kepunyaan Allah

Kau terlihat seperti kau sekarang karena suatu alasan
Allah kita tidak pernah membuat kesalahan
Dia merajutmu menyatu dalam kandungan
Kau tepat seperti apa yang ingin Dia ciptakan

Orang tua yang kau miliki adalah pilihanNya,
Dan tidak peduli bagaimana perasaanmu,
Mereka secara khusus dibuat dengan pertimbangan rencana Allah
Dan mereka memikul meterai Tuhan

Tidak, trauma yang kau hadapi tidaklah mudah
Dan Allah menangis atas semua yang begitu menyakitimu
Tapi hal itu ditujukan untuk membentuk hatimu
Supaya kau bertumbuh menjadi serupa denganNya

Kau ada sebagaimana kau ada karena suatu alasan,
Kau telah dibentuk dengan tongkat Tuhan
Kau adalah kau sekarang, saudara kekasih,
Karena ada Allah!

Friday, June 3, 2005

Excel Report with Aspose.Excel

Another way to get reports done beside using Crystal Reports Disclaimer: I'm not putting all my codes here, such as querying database and return the result as DataSet

The engine: ExcelReport.cs
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Aspose.Excel;

namespace Dallanube.HumanCapital.Report
{
 /// 
 /// Summary description for ExcelReport.
 /// 
 public class ExcelReport
 {
  public Excel CreateExcel(String fileName)
  {
   String strDir=Directory.GetParent(Application.ExecutablePath).ToString();
   /// If you have purchased a License, set license like this: 
   String strLicense =  strDir + "\\Aspose.Excel.lic";    
   Excel.SetLicense(strLicense);

   Excel objExcel = new Excel();
   
   string strDesignerFile = strDir + "\\" + fileName + ".xls";
   objExcel.Open(strDesignerFile);
   
   SetStyles(objExcel);
   return objExcel;
  }

  public void SaveExcel(Excel excel)
  {
   using (SaveFileDialog objFile = new SaveFileDialog())
   {
    objFile.Filter = "Excel files (*.xls)|*.xls|All files (*.*)|*.*";
    objFile.DefaultExt = "xls";

    if (objFile.ShowDialog() == DialogResult.OK)
    {
     try
     {
      excel.Save(objFile.FileName, FileFormatType.Default);
      ProcessStartInfo psi = new ProcessStartInfo(objFile.FileName); 
      Process.Start(psi); 
     }
     catch(Exception ex)
     {
      MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
    }
   }
  }

  private void SetStyles(Excel excel)
  {
   int intStyleIndex = excel.Styles.Add();
   Style objStyle = excel.Styles[intStyleIndex];
   objStyle.Font.IsBold = true;
   objStyle.Font.Size = 10;
   objStyle.HorizontalAlignment = TextAlignmentType.Left;
   objStyle.Name = "Bold";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Font.IsBold = true;
   objStyle.Font.Size = 10;
   objStyle.HorizontalAlignment = TextAlignmentType.Left;
   objStyle.ForegroundColor = Color.LightGray;
   objStyle.Name = "Header";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Font.IsBold = false;
   objStyle.Font.Size = 10;
   objStyle.HorizontalAlignment = TextAlignmentType.Right;
   objStyle.ForegroundColor = Color.LightGray;
   objStyle.Name = "Gray";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.TopBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.BottomBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;
   objStyle.Name = "Box";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.TopBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.BottomBorder].LineStyle=CellBorderType.Thin;
   objStyle.Name = "TopBottomLeftBorder";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.TopBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.BottomBorder].LineStyle=CellBorderType.Thin;
   objStyle.Name = "TopBottomRightBorder";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.TopBorder].LineStyle=CellBorderType.Thin;
   objStyle.HorizontalAlignment = TextAlignmentType.Center;
   objStyle.Name = "TopLeftBorder";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.BottomBorder].LineStyle=CellBorderType.Thin;
   objStyle.HorizontalAlignment = TextAlignmentType.Center;
   objStyle.Name = "BottomLeftBorder";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.TopBorder].LineStyle=CellBorderType.Thin;
   objStyle.Name = "TopRightBorder";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.BottomBorder].LineStyle=CellBorderType.Thin;
   objStyle.Name = "BottomRightBorder";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.TopBorder].LineStyle=CellBorderType.Thin;
   objStyle.Name = "TopBorder";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.TopBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.BottomBorder].LineStyle=CellBorderType.Thin;
   objStyle.Name = "TopBottomBorder";

   intStyleIndex = excel.Styles.Add();
   objStyle = excel.Styles[intStyleIndex];
   objStyle.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
   objStyle.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;
   objStyle.Name = "LeftRightBorder";
  }
 }
}


One example: FrmRepPayPerMonth.cs (yeah, it's a windows form)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

using Aspose.Excel;
using Dallanube.HumanCapital.Impl;

namespace Dallanube.HumanCapital.Report
{
 public class FrmRepPayPerMonth : Dallanube.HumanCapital.Report.FrmBasePerMonth
 {
  private System.ComponentModel.IContainer components = null;

  public FrmRepPayPerMonth()
  {
   // This call is required by the Windows Form Designer.
   InitializeComponent();

   // TODO: Add any initialization after the InitializeComponent call
  }

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

  #region Designer generated code
  /// 
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// 
  private void InitializeComponent()
  {
   // 
   // FrmRepPayPerMonth
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(342, 143);
   this.Name = "FrmRepPayPerMonth";
   this.Load += new System.EventHandler(this.FrmRepPayPerMonth_Load);

  }
  #endregion

  private void FrmRepPayPerMonth_Load(object sender, System.EventArgs e)
  {
   if (!DesignMode)
   {
    this.Text   = GetString("rep.paypermonth");
   }
  }

  private void CreateReport(byte bytMonth, int intYear, ReportParameter parameter)
  {
   ushort intCurrentRow = parameter.FirstRow;
   byte intCurrentColumn = 0;
   
   String strName, strDept;
   DateTime dtmJoined;
   int intJobStatus;
   double dblSalary, dblAllowance, dblOtherAllowance;
   //double dblTotal;
   //double dblSumSalary=0;
   //double dblSumAllowance=0;
   //double dblSumOtherAllowance=0;
   //double dblSumTotal=0;

   String strCellSalaryStart="", strCellSalaryEnd="";
   String strCellOtherAllowanceStart="", strCellOtherAllowanceEnd="";
   String strCellAllowanceStart="", strCellAllowanceEnd="";
   String strCellTotalStart="", strCellTotalEnd="";

   Worksheet objSheet = parameter.ExcelFile.Worksheets["Sheet1"];
   objSheet.Name = parameter.ReportName;
   Cells objCells = objSheet.Cells;

   // set Month/Year
   DateTime dtmPeriod=new DateTime(intYear, bytMonth, 1);
   String strPeriod="PER: " + dtmPeriod.ToString("MMMM yyyy").ToUpper();

   Style objStyleBold = parameter.ExcelFile.Styles["Bold"];
   objCells[intCurrentRow-4, intCurrentColumn].Style=objStyleBold;
   objCells[intCurrentRow-4, intCurrentColumn].PutValue(strPeriod);
   //

   for(int i = 0; i < parameter.Table.Rows.Count; i ++)
   {
    strName    = parameter.Table.Rows[i]["realName"].ToString();
    strDept    = parameter.Table.Rows[i]["deptName"].ToString();
    dtmJoined   = Convert.ToDateTime(parameter.Table.Rows[i]["startDate"]);
    intJobStatus  = Convert.ToInt32(parameter.Table.Rows[i]["jobStatus"]);
    if (intJobStatus==1)
     dblSalary  = Convert.ToDouble(parameter.Table.Rows[i]["salary"]);
    else
     dblSalary  = Convert.ToDouble(parameter.Table.Rows[i]["dailyWages"]);
    dblOtherAllowance = Convert.ToDouble(parameter.Table.Rows[i]["otherAllowance"]);
    dblAllowance  = Convert.ToDouble(parameter.Table.Rows[i]["allowance"]);
    //dblTotal   = dblSalary + dblAllowance + dblOtherAllowance;
    
    //dblSumSalary+= dblSalary;
    //dblSumOtherAllowance+= dblOtherAllowance;
    //dblSumAllowance+= dblAllowance;
    //dblSumTotal += dblTotal;

    objCells[intCurrentRow, intCurrentColumn].PutValue(i+1);
    objCells[intCurrentRow, intCurrentColumn].Style.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
    objCells[intCurrentRow, intCurrentColumn].Style.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;

    objCells[intCurrentRow, (byte)(intCurrentColumn + 1)].PutValue(strName);
    objCells[intCurrentRow, (byte)(intCurrentColumn + 1)].Style.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
    objCells[intCurrentRow, (byte)(intCurrentColumn + 1)].Style.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;

    objCells[intCurrentRow, (byte)(intCurrentColumn + 2)].PutValue(strDept);
    objCells[intCurrentRow, (byte)(intCurrentColumn + 2)].Style.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
    objCells[intCurrentRow, (byte)(intCurrentColumn + 2)].Style.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;

    objCells[intCurrentRow, (byte)(intCurrentColumn + 3)].PutValue(dtmJoined);
    objCells[intCurrentRow, (byte)(intCurrentColumn + 3)].Style.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
    objCells[intCurrentRow, (byte)(intCurrentColumn + 3)].Style.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;

    if (i==0)
    {
     strCellSalaryStart=objCells[intCurrentRow, (byte)(intCurrentColumn + 4)].Name;
     strCellOtherAllowanceStart=objCells[intCurrentRow, (byte)(intCurrentColumn + 5)].Name;
     strCellAllowanceStart=objCells[intCurrentRow, (byte)(intCurrentColumn + 6)].Name;
    }
    if (i==(parameter.Table.Rows.Count-1))
    {
     strCellSalaryEnd=objCells[intCurrentRow, (byte)(intCurrentColumn + 4)].Name;
     strCellOtherAllowanceEnd=objCells[intCurrentRow, (byte)(intCurrentColumn + 5)].Name;
     strCellAllowanceEnd=objCells[intCurrentRow, (byte)(intCurrentColumn + 6)].Name;
    }

    objCells[intCurrentRow, (byte)(intCurrentColumn + 4)].PutValue(dblSalary);
    objCells[intCurrentRow, (byte)(intCurrentColumn + 4)].Style.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
    objCells[intCurrentRow, (byte)(intCurrentColumn + 4)].Style.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;
    strCellTotalStart = objCells[intCurrentRow, (byte)(intCurrentColumn + 4)].Name;
    
    if (dblOtherAllowance>0)
     objCells[intCurrentRow, (byte)(intCurrentColumn + 5)].PutValue(dblOtherAllowance);
    objCells[intCurrentRow, (byte)(intCurrentColumn + 5)].Style.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
    objCells[intCurrentRow, (byte)(intCurrentColumn + 5)].Style.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;

    objCells[intCurrentRow, (byte)(intCurrentColumn + 6)].PutValue(dblAllowance);
    objCells[intCurrentRow, (byte)(intCurrentColumn + 6)].Style.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
    objCells[intCurrentRow, (byte)(intCurrentColumn + 6)].Style.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;
    strCellTotalEnd = objCells[intCurrentRow, (byte)(intCurrentColumn + 6)].Name;

    //objCells[intCurrentRow, (byte)(intCurrentColumn + 7)].PutValue(dblTotal);
    objCells[intCurrentRow, (byte)(intCurrentColumn + 7)].Formula="=SUM("+ strCellTotalStart +":" + strCellTotalEnd +")";
    objCells[intCurrentRow, (byte)(intCurrentColumn + 7)].Style.Borders[BorderType.LeftBorder].LineStyle=CellBorderType.Thin;
    objCells[intCurrentRow, (byte)(intCurrentColumn + 7)].Style.Borders[BorderType.RightBorder].LineStyle=CellBorderType.Thin;

    intCurrentRow++;
   }

   Style objStyle = parameter.ExcelFile.Styles["TopBottomLeftBorder"];
   objCells[intCurrentRow, intCurrentColumn].Style = objStyle;
   
   objStyle = parameter.ExcelFile.Styles["TopBottomBorder"];
   Range objRange = objCells.CreateRange(intCurrentRow, 1, 1, 3);
   objRange.Style = objStyle;
   objCells[intCurrentRow, (byte)(intCurrentColumn+1)].PutValue("Total");
   
   objStyle = parameter.ExcelFile.Styles["Box"];
   objStyle.Custom="#,##0";
   //objCells[intCurrentRow, (byte)(intCurrentColumn+4)].PutValue(dblSumSalary);
   objCells[intCurrentRow, (byte)(intCurrentColumn+4)].Formula="=SUM("+ strCellSalaryStart +":" + strCellSalaryEnd +")";
   objCells[intCurrentRow, (byte)(intCurrentColumn+4)].Style = objStyle;
   strCellTotalStart = objCells[intCurrentRow, (byte)(intCurrentColumn+4)].Name;

   //objCells[intCurrentRow, (byte)(intCurrentColumn+5)].PutValue(dblSumOtherAllowance);
   objCells[intCurrentRow, (byte)(intCurrentColumn+5)].Formula="=SUM("+ strCellOtherAllowanceStart +":" + strCellOtherAllowanceEnd +")";
   objCells[intCurrentRow, (byte)(intCurrentColumn+5)].Style = objStyle;

   //objCells[intCurrentRow, (byte)(intCurrentColumn+6)].PutValue(dblSumAllowance);
   objCells[intCurrentRow, (byte)(intCurrentColumn+6)].Formula="=SUM("+ strCellAllowanceStart +":" + strCellAllowanceEnd +")";
   objCells[intCurrentRow, (byte)(intCurrentColumn+6)].Style = objStyle;
   strCellTotalEnd = objCells[intCurrentRow, (byte)(intCurrentColumn+6)].Name;

   //objCells[intCurrentRow, (byte)(intCurrentColumn+7)].PutValue(dblSumTotal);
   objCells[intCurrentRow, (byte)(intCurrentColumn+7)].Formula="=SUM("+ strCellTotalStart +":" + strCellTotalEnd +")";
   objCells[intCurrentRow, (byte)(intCurrentColumn+7)].Style = objStyle;
  }

  protected override void Print()
  {
   ExcelReport objReport = new ExcelReport();
   int intYear    = Convert.ToInt32(txtYear.Text);
   byte bytMonth   = Convert.ToByte(cboMonth.SelectedValue);
   DataSet ds    = Global.ReportProxy.CreateReportPayPerMonth(bytMonth, intYear);
    
   ReportParameter objParam= new ReportParameter();
   objParam.ExcelFile  = objReport.CreateExcel("RptDallanube01");
   objParam.Table   = ds.Tables[0];
   objParam.ReportName  = GetString("rep.paypermonth");
   objParam.FirstRow  = 7;

   CreateReport(bytMonth, intYear, objParam);
   for(int i = 0; i < objParam.ExcelFile.Worksheets.Count ; i ++)
   {
    Worksheet objSheet = objParam.ExcelFile.Worksheets[i];
    if(objSheet.Name != objParam.ReportName)
    {
     objParam.ExcelFile.Worksheets.RemoveAt(i);
     i --;
    }
   }

   objReport.SaveExcel(objParam.ExcelFile);
   objReport=null;
  }
 }
}

And ReportParameter.cs
using System;
using System.Data;
using Aspose.Excel;

namespace Dallanube.HumanCapital.Report
{
 /// 
 /// Summary description for ReportParameter.
 /// 
 public class ReportParameter
 {
  private Excel m_objExcelFile;
  private DataTable m_dtbTable;
  //private String m_strSourceSheet;
  private String m_strReportName;
  private ushort m_ushFirstRow;

  public Excel ExcelFile
  {
   get
   {
    return m_objExcelFile;
   }
   set
   {
    m_objExcelFile = value;
   }
  }
  public DataTable Table
  {
   get
   {
    return m_dtbTable;
   }
   set
   {
    m_dtbTable = value;
   }
  }
//  public String SourceSheet
//  {
//   get
//   {
//    return m_strSourceSheet;
//   }
//   set
//   {
//    m_strSourceSheet = value;
//   }
//  }
  public String ReportName
  {
   get
   {
    return m_strReportName;
   }
   set
   {
    m_strReportName = value;
   }
  }
  public ushort FirstRow
  {
   get
   {
    return m_ushFirstRow;
   }
   set
   {
    m_ushFirstRow = value;
   }
  }
 }
}
And for sure Aspose.Excel.dll is needed :D

Edited, reason: formatting codes

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

Monday, January 10, 2005