program/asp.net

한줄 달력

yoursyun 2008. 8. 21. 10:13
프로젝트 스케쥴러를 만들기위해 한줄짜리 달력을 class 로 만들어 보았다...

using System;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Class_OneLineCalendar의 요약 설명입니다.
/// </summary>
public class Class_OneLineCalendar
{
  public int Year = 0, Month = 0, _Today = 0;
  private string _PjStartDate = string.Empty, _PjEndDate = string.Empty, _PjFinishDate = string.Empty;

  public string PjStartDate
  {
    get { return _PjStartDate; }
    set { _PjStartDate = value; }
  }

  public string PjEndDate
  {
    get { return _PjEndDate; }
    set { _PjEndDate = value; }
  }

  public string PjFinishDate
  {
    get { return _PjFinishDate; }
    set { _PjFinishDate = value; }
  }

    public Class_OneLineCalendar()
    {
        //
        // TODO: 생성자 논리를 여기에 추가합니다.
        //
    Year = System.DateTime.Today.Year;
    Month = System.DateTime.Today.Month;
    _Today = System.DateTime.Today.Day;
    }

  ~Class_OneLineCalendar()
  {
    GC.WaitForPendingFinalizers();
    GC.Collect();
  }

  public Table Load()
  {
    Table tblDay = new Table();
    tblDay.Width = 600;   // % 로 적용하고 싶었는데... 방법을 못찾았다.
    tblDay.HorizontalAlign = HorizontalAlign.Center;
    tblDay.CellSpacing = 0;
    tblDay.BorderStyle = BorderStyle.Solid;
    tblDay.BorderWidth = 1;
    tblDay.BorderColor = System.Drawing.Color.Gray;

    int tCellWidth = 600 / Get_Day(Year, Month);
    string weekend = string.Empty;

    TableRow tRow = new TableRow();
    tblDay.Rows.Add(tRow);

    for (int i = 1; i <= Get_Day(Year, Month); i++)
    {
      TableCell tCell = new TableCell();
      tRow.Cells.Add(tCell);

      if (i < 10)
        tCell.Text = i.ToString() + "&nbsp;";
      else
        tCell.Text = i.ToString();

      tCell.BorderStyle = BorderStyle.Solid;
      tCell.BorderWidth = 1;

      weekend = Get_Week(Year, Month, i);

      // 설정된 기간이 있으면 해당 기간에 backgroud-color를 적용해 줬다.     
      if (isPjTerm(PjStartDate, PjEndDate, Year, Month, i))
        Set_Cell_BackgroundColor(tCell, 'p');

      if (isPjFinish(PjStartDate, PjFinishDate, Year, Month, i))
        Set_Cell_BackgroundColor(tCell, 'f');

      if (weekend.CompareTo("Sunday") == 0 || weekend.CompareTo("Saturday") == 0)
        Set_Cell_BackgroundColor(tCell, 'w');
    }

    return tblDay;
  }

  private string Get_Week(int year, int month, int day)
  {
    DateTime dt = new DateTime(year, month, day);

    return dt.DayOfWeek.ToString();
  }

  private bool isPjTerm(string pStart, string pEnd, int year, int month, int day)
  {
    bool rtnPjTerm = false;

    DateTime dt = new DateTime(year, month, day);

    if (dt.Date.CompareTo(ConvertDateTime(pStart)) >= 0
          && dt.Date.CompareTo(ConvertDateTime(pEnd)) <= 0)
      rtnPjTerm = true;

    return rtnPjTerm;
  }

  private bool isPjFinish(string pStart, string pEnd, int year, int month, int day)
  {
    bool rtnPjTerm = false;

    DateTime dt = new DateTime(year, month, day);
    if (!pEnd.Trim().Equals(""))
    {
      if (dt.Date.CompareTo(ConvertDateTime(pStart)) >= 0
            && dt.Date.CompareTo(ConvertDateTime(pEnd)) <= 0)
        rtnPjTerm = true;
    }

    return rtnPjTerm;
  }

  public DateTime ConvertDateTime(string pDate)
  {
    int year = Convert.ToInt16(pDate.Substring(0, 4));
    int month = Convert.ToInt16(pDate.Substring(4, 2));
    int day = Convert.ToInt16(pDate.Substring(6, 2));

    DateTime dt = new DateTime(year, month, day);

    return dt;
  }

  // BgColor code : w (주), p (기간 설정)
  private void Set_Cell_BackgroundColor(TableCell pCell, char BgColor)
  {
    switch (BgColor)
    {
      case 'w':
        pCell.BackColor = System.Drawing.Color.LightYellow;
        break;
      case 'p':
        pCell.BackColor = System.Drawing.Color.LightPink;       
        break;
      case 'f':
        pCell.BackColor = System.Drawing.Color.BlueViolet;
        break;
      default:
        break;
    }
  }

  private int Get_Day(int year, int month)
  {
    int[] days = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    if (month != 2)
      return days[month - 1];

    if (year % 4 == 0)  // 윤달
    {
      // 100으로 나누어 떨어지면 예외, 하지만 400으로 나누어 떨어지는 경우는 윤달이다.
      if (year % 100 == 0 && year % 400 != 0)
        return days[month - 1];
      else
        return days[month - 1] + 1;
    }

    return days[month - 1];
  }
}


작업한지 오래 된거라 기억은 잘 나지 않지만 ... Load() 메서드로 테이블 객체를 리턴할 수 있다.
리턴 받은 객체는 PlaceHolder 객체로 동적 생성을 하고, Literal 등의 컨트롤로 그릴 수 있을 것 이다.

PlaceHolder 컨트롤의 경우 약간 엽기 스러운 면이 있어서 해당 컨트롤로 작업하게 되면,
PostBack 발생시 PageLoad Method에서 다시 그려줘야 한다는 슬픔이... 역시 웹이구나라는 생각이 들게 해준다...
반응형