//---------------------------------------------------------------------------

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "WindowMsgF.h"
#include "AppGlobalF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "IDB0100MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxSpinEdit"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"

TIDB0100M *IDB0100M = NULL;

//---------------------------------------------------------------------------
__fastcall TIDB0100M::TIDB0100M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FTitle = Caption;//"프로세스정보관리";
    FDbUpdate = false;

    MyItsProcessManager = new TItsProcessManager();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::CommClose()
{
    try
    {
        if (MyItsProcessManager)
        {
            delete MyItsProcessManager;
            MyItsProcessManager = NULL;
        }

        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::FormShow(TObject *Sender)
{
    Refresh();
    FormInit();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::FormInit()
{
    m_pGDC = TvList->DataController;

    TvList->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<현재 등록된 프로세스 정보가 없습니다>";

    //상태표출유무
	TcxComboBoxProperties *pcxCboProperties10 = (TcxComboBoxProperties*)Column10->Properties;
    pcxCboProperties10->Items->Clear();
	pcxCboProperties10->Items->Add("Y");
	pcxCboProperties10->Items->Add("N");
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    BtnSearchClick((TObject*)BtnSearch);   
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::BtnSearchClick(TObject *Sender)
{
    Application->ProcessMessages();
    TSqlCursor sqlCrs((TControl*)BtnSearch);

    RefreshData();
    CxList->SetFocus();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::RefreshData()
{
    MyItsProcessManager->LoadFromDb();
    SelListData();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::SelListData()
{
    FUpdate = false;
    CMM_ClearGridTableView(TvList);

    try
    {
        TvList->BeginUpdate();

        FOR_STL(TItsProcess*, pObj, MyItsProcessManager->FLists)
        {
            pObj->EditMode = DB_INIT;   // 미리초기화해준다.(삭제된것)
            pObj->Completed = true;
            if (pObj->DEL_YN == "Y" || pObj->MEM_DEL == "Y") continue;

            AddObjectItem(pObj, "-");
        }
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::BtnCloseClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::BtnExlSaveClick(TObject *Sender)
{
    TcxGrid          *pGrid = CxList;
    TcxGridTableView *pView = TvList;
    String            sTitle= Caption;//"프로세스정보";

    CMM_ExportToExcelFile(sTitle, pGrid, pView, this);
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::BtnInsertClick(TObject *Sender)
{
    int nMsgType = MB_OK|MB_ICONERROR|MB_APPLMODAL;

	String sId = InputBox(lblInput->Caption,//"프로세스 ID",
                          lblInQry->Caption,//"신규 프로세스 ID를 입력하세요.",
                          lblInput->Caption);//"최대30자리" );

    sId = sId.Trim();
	if (sId == lblInput->Caption //"프로세스 ID"
     || sId == "")
	{
		return;
	}
	if (AnsiString(sId).Length() > 30)
	{
        Application->MessageBox(lblDigitErr->Caption.c_str(),//L"프로세스 ID는  20Byte를 초과할 수 없습니다.",
                                FTitle.c_str(),
                                nMsgType);
		return;
	}

    TItsProcess *pDb = (TItsProcess *)MyItsProcessManager->FLists.Find(sId);
    if (pDb)
    {
        String sErr = FrmLang->lbIdErr->Caption + "[" + sId + "]";
        Application->MessageBox(sErr.c_str(),//("[" + sId + "]는 사용할 수 없는 프로세스 ID 입니다.").c_str(),
                                FTitle.c_str(), nMsgType);
        return;
    }

    try
    {
        TvList->BeginUpdate();

        TItsProcess *pObj = new TItsProcess();
        if (!pObj)
        {
            Application->MessageBox(FrmLang->lblMemErr->Caption.c_str(),//L"신규 프로세스 정보를 생성할 수 없습니다.[메모리오류]",
                                    FTitle.c_str(), nMsgType);
            return;
        }
        //pObj->Clear();

        pObj->SYST_ID    = sId;
        pObj->SYST_TYPE  = "";
        pObj->SYST_NMBR  = "0";
        pObj->SYST_NM    = "";
        pObj->SYST_IP_1  = "";
        pObj->SYST_IP_2  = "";
        pObj->PRGM_PORT  = "";
        pObj->HOST_NM    = "";
        pObj->OS_NM      = "";
        pObj->DEL_YN     = "N";
        pObj->STTS_DSPL_YN = "N";

        pObj->Completed    = false;
        pObj->EditMode     = DB_NEW;
        pObj->MEM_DEL      = "N";

        MyItsProcessManager->FLists.Push(pObj->SYST_ID, pObj);

        AddObjectItem(pObj, FrmLang->lblNew->Caption);//"신규");
    }
    __finally
    {
        LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::BtnDeleteClick(TObject *Sender)
{
    int nIdx = m_pGDC->FocusedRecordIndex;
    if (nIdx < 0) return;

    int nMemPtr = m_pGDC->Values[nIdx][Column99->Index];
    TItsProcess *pObj = (TItsProcess*)nMemPtr;
    pObj->EditMode = DB_DELETE;
    m_pGDC->Values[nIdx][Column00->Index] = FrmLang->lblDelete->Caption;//"삭제";
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    IDB0100M = NULL;
    Action = caFree;    
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::BtnEditClick(TObject *Sender)
{
    TListFocus tvFocus(TvList);

    if (BtnEdit->Caption == FrmLang->lblCancel->Caption)//"취소")
    {
        CxList->SetFocus();
        Application->ProcessMessages();
        SaveData();
    }

    FrmLang->ITSSkin_ButtonClick(BtnSearch, BtnEdit, BtnInsert, BtnDelete, BtnApply);
    ChangeEditMode(BtnEdit->Down);

    RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::ChangeEditMode(bool AEdit)
{
    bool bEditing = AEdit;

    try 
    {
        TvList->BeginUpdate();
        
        Column00->Visible = bEditing;
    
        Column02->Options->Editing = bEditing;
        Column03->Options->Editing = bEditing;
        Column04->Options->Editing = bEditing;
        Column05->Options->Editing = bEditing;
        Column06->Options->Editing = bEditing;
        Column07->Options->Editing = bEditing;
        Column08->Options->Editing = bEditing;
        Column09->Options->Editing = bEditing;
        Column10->Options->Editing = bEditing;
    }
    __finally 
    {
        TvList->EndUpdate();
        if (bEditing)
        {
            BtnEdit->Caption = FrmLang->lblCancel->Caption;//"취소";
            BtnEdit->Hint    = FrmLang->lblCancel->Hint;//데이터 편집 취소";
        }
        else
        {
            BtnEdit->Caption = FrmLang->lblEdit->Caption;//"편집";
            BtnEdit->Hint    = FrmLang->lblEdit->Hint;//데이터 편집";
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::TvListColumnPropertiesValidate(TObject *Sender, Variant &DisplayValue, TCaption &ErrorText, bool &Error)
{
    if (TvList->Controller->FocusedRow->IsFilterRow) return;

    int nRow = m_pGDC->FocusedRecordIndex;
    if (nRow < 0) return;

    int nMemPtr = TvList->DataController->Values[nRow][Column99->Index];
    TItsProcess *pObj = (TItsProcess*)nMemPtr;
    if (pObj->Completed == false)
    {
        //신규이거나 삭제인 경우 편집 내용에 따른 변화가 필요없다.
        return;
    }

    String SYST_TYPE = VarToStr(m_pGDC->Values[nRow][Column02->Index]);
    String SYST_NMBR = VarToStr(m_pGDC->Values[nRow][Column03->Index]);
    String SYST_NM   = VarToStr(m_pGDC->Values[nRow][Column04->Index]);
    String SYST_IP_1 = VarToStr(m_pGDC->Values[nRow][Column05->Index]);
    String SYST_IP_2 = VarToStr(m_pGDC->Values[nRow][Column06->Index]);
    String PRGM_PORT = VarToStr(m_pGDC->Values[nRow][Column07->Index]);
    String HOST_NM   = VarToStr(m_pGDC->Values[nRow][Column08->Index]);
    String OS_NM     = VarToStr(m_pGDC->Values[nRow][Column09->Index]);
    String STTS_DSPL_YN = VarToStr(m_pGDC->Values[nRow][Column10->Index]);


    TcxGridColumn *pColumn = TvList->Controller->FocusedColumn;
    if      (pColumn == Column02) SYST_TYPE = DisplayValue;
    else if (pColumn == Column03) SYST_NMBR = DisplayValue;
    else if (pColumn == Column04) SYST_NM   = DisplayValue;
    else if (pColumn == Column05) SYST_IP_1 = DisplayValue;
    else if (pColumn == Column06) SYST_IP_2 = DisplayValue;
    else if (pColumn == Column07) PRGM_PORT = DisplayValue;
    else if (pColumn == Column08) HOST_NM   = DisplayValue;
    else if (pColumn == Column09) OS_NM     = DisplayValue;
    else if (pColumn == Column10) STTS_DSPL_YN = DisplayValue;

    if (pObj->SYST_TYPE != SYST_TYPE ||
        pObj->SYST_NMBR != SYST_NMBR ||
        pObj->SYST_NM   != SYST_NM   ||
        pObj->SYST_IP_1 != SYST_IP_1 ||
        pObj->SYST_IP_2 != SYST_IP_2 ||
        pObj->PRGM_PORT != PRGM_PORT ||
        pObj->HOST_NM   != HOST_NM   ||
        pObj->STTS_DSPL_YN   != STTS_DSPL_YN   ||
        pObj->OS_NM     != OS_NM      )
    {
        m_pGDC->Values[nRow][Column00->Index] = FrmLang->lblModify->Caption;//"수정";
        pObj->EditMode = DB_UPDATE;
    }
    else
    {
        TvList->DataController->Values[nRow][Column00->Index] = "-";
        pObj->EditMode = DB_INIT;
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::BtnBestFitClick(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->ApplyBestFit(NULL, false, false);
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::ChkAutoWidthPropertiesChange(TObject *Sender)
{
    try
    {
        TvList->BeginUpdate();
		TvList->OptionsView->ColumnAutoWidth = ChkAutoWidth->Checked;
        BtnBestFit->Visible = !ChkAutoWidth->Checked;
	}
	__finally
	{
        TvList->EndUpdate();
	}
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::MnuDeleteCancelClick(TObject *Sender)
{
    //    
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::BtnApplyClick(TObject *Sender)
{
    FUpdate = true;
    BtnEdit->Down = false;
    BtnEdit->Click();
}
//---------------------------------------------------------------------------

bool __fastcall TIDB0100M::AddObjectItem(TItsProcess *pObj, String AMode)
{
    int nRow;
    try
    {
        nRow = m_pGDC->AppendRecord();
        pObj->GridIndex = nRow;

        m_pGDC->Values[nRow][Column00->Index] = AMode;

        m_pGDC->Values[nRow][Column01->Index] = pObj->SYST_ID;
        m_pGDC->Values[nRow][Column02->Index] = pObj->SYST_TYPE;
        m_pGDC->Values[nRow][Column03->Index] = pObj->SYST_NMBR;
        m_pGDC->Values[nRow][Column04->Index] = pObj->SYST_NM;
        m_pGDC->Values[nRow][Column05->Index] = pObj->SYST_IP_1;
        m_pGDC->Values[nRow][Column06->Index] = pObj->SYST_IP_2;
        m_pGDC->Values[nRow][Column07->Index] = pObj->PRGM_PORT;
        m_pGDC->Values[nRow][Column08->Index] = pObj->HOST_NM;
        m_pGDC->Values[nRow][Column09->Index] = pObj->OS_NM;

        if(pObj->STTS_DSPL_YN.IsEmpty())
        m_pGDC->Values[nRow][Column10->Index] = "N";
        else
        m_pGDC->Values[nRow][Column10->Index] = pObj->STTS_DSPL_YN;

        m_pGDC->Values[nRow][Column88->Index] = pObj->DEL_YN;

        m_pGDC->Values[nRow][Column99->Index] = (int)pObj;

        return true;
    }
    catch(...)
    {
    }
    return false;
}
//---------------------------------------------------------------------------

bool __fastcall TIDB0100M::IsUpdate()
{
    FOR_STL(TItsProcess*, pObj, MyItsProcessManager->FLists)
    {
        if (pObj->Completed && pObj->EditMode != DB_INIT)
            return true;

        if (!pObj->Completed && pObj->EditMode == DB_NEW)
            return true;
    }
    return false;
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::SaveData()
{
    if (!IsUpdate()) return;

    if (!FUpdate)
    {
        if (Application->MessageBox(FrmLang->lblEidtConfirm->Caption.c_str(),//L"변경된 정보가 있습니다.\r\n변경된 정보를 저장 하시겠습니까?",
                                    FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;
    }

    TADOQuery *pADOI = NULL;
    TADOQuery *pADOU = NULL;
    TADOQuery *pADOD = NULL;
	String sQryI;
	String sQryU;
	String sQryD;
    int    nEditMode;
    try
    {
        sQryI = "INSERT INTO TB_UNIT_SYST (                 \r\n"
                "            SYST_ID, SYST_TYPE, SYST_NMBR, \r\n"
                "            SYST_NM, SYST_IP_1, SYST_IP_2, \r\n"
                "            PRGM_PORT, HOST_NM, OS_NM,     \r\n"
                "            STTS_DSPL_YN, DEL_YN)          \r\n"
                "   VALUES (                                \r\n"
                "           :p01, :p02, :p03,               \r\n"
                "           :p04, :p05, :p06,               \r\n"
                "           :p07, :p08, :p09,               \r\n"
                "           :p10, 'N' )                     \r\n";

        sQryU = "UPDATE TB_UNIT_SYST         \r\n"
                "   SET SYST_TYPE = :p02,    \r\n"
                "       SYST_NMBR = :p03,    \r\n"
                "       SYST_NM   = :p04,    \r\n"
                "       SYST_IP_1 = :p05,    \r\n"
                "       SYST_IP_2 = :p06,    \r\n"
                "       PRGM_PORT = :p07,    \r\n"
                "       HOST_NM   = :p08,    \r\n"
                "       OS_NM     = :p09,    \r\n"
                "       STTS_DSPL_YN = :p10  \r\n"
//                "       DEL_YN    = :p10    \r\n"
                " WHERE SYST_ID      = :p01 \r\n";

        sQryD = "UPDATE TB_UNIT_SYST        \r\n"
                "   SET DEL_YN       = 'Y'  \r\n"
                " WHERE SYST_ID      = :p01 \r\n";

        int nRow;
        TvList->BeginUpdate();
        try
        {
            pADOI = new TADOQuery(NULL);
			pADOI->Close();
            pADOI->Connection = ITSDb_GetConnection();

            pADOU = new TADOQuery(NULL);
			pADOU->Close();
            pADOU->Connection = ITSDb_GetConnection();

            pADOD = new TADOQuery(NULL);
			pADOD->Close();
            pADOD->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADOI, sQryI);
            ITSDb_SQLText(pADOU, sQryU);
            ITSDb_SQLText(pADOD, sQryD);

            ITSDb_GetConnection()->BeginTrans();

            FOR_STL(TItsProcess*, pObj, MyItsProcessManager->FLists)
            {
                if (pObj->GridIndex < 0) continue;

                if (pObj->Completed && pObj->EditMode == DB_INIT)
                    continue;

                if (!pObj->Completed && pObj->EditMode != DB_NEW)
                    continue;

                nRow = pObj->GridIndex;

                String SYST_TYPE = VarToStr(m_pGDC->Values[nRow][Column02->Index]);
                String SYST_NMBR = VarToStr(m_pGDC->Values[nRow][Column03->Index]);
                String SYST_NM   = VarToStr(m_pGDC->Values[nRow][Column04->Index]);
                String SYST_IP_1 = VarToStr(m_pGDC->Values[nRow][Column05->Index]);
                String SYST_IP_2 = VarToStr(m_pGDC->Values[nRow][Column06->Index]);
                String PRGM_PORT = VarToStr(m_pGDC->Values[nRow][Column07->Index]);
                String HOST_NM   = VarToStr(m_pGDC->Values[nRow][Column08->Index]);
                String OS_NM     = VarToStr(m_pGDC->Values[nRow][Column09->Index]);
                String STTS_DSPL_YN = VarToStr(m_pGDC->Values[nRow][Column10->Index]);

                //pObj->SYST_ID   = pObj->SYST_ID;
                pObj->SYST_TYPE = SYST_TYPE;
                pObj->SYST_NMBR = SYST_NMBR;
                pObj->SYST_NM   = SYST_NM;
                pObj->SYST_IP_1 = SYST_IP_1;
                pObj->SYST_IP_2 = SYST_IP_2;
                pObj->PRGM_PORT = PRGM_PORT;
                pObj->HOST_NM   = HOST_NM;
                pObj->OS_NM     = OS_NM;
                pObj->STTS_DSPL_YN = STTS_DSPL_YN;
                pObj->DEL_YN    = pObj->DEL_YN;

                nEditMode = pObj->EditMode;
                if (pObj->Completed)
                {
                    if (pObj->EditMode == DB_DELETE)
                    {
                        //delete
                        ITSDb_SQLBind(pADOD, "p01", pObj->SYST_ID);
                        ITSDb_SQLExec(pADOD);
                        pObj->DEL_YN  = "Y";
                        pObj->MEM_DEL = "Y";
                    }
                    else
                    if (pObj->EditMode == DB_UPDATE)
                    {
                        //update
                        ITSDb_SQLBind(pADOU, "p01", pObj->SYST_ID);
                        ITSDb_SQLBind(pADOU, "p02", pObj->SYST_TYPE);
                        ITSDb_SQLBind(pADOU, "p03", pObj->SYST_NMBR);
                        ITSDb_SQLBind(pADOU, "p04", pObj->SYST_NM);
                        ITSDb_SQLBind(pADOU, "p05", pObj->SYST_IP_1);
                        ITSDb_SQLBind(pADOU, "p06", pObj->SYST_IP_2);
                        ITSDb_SQLBind(pADOU, "p07", pObj->PRGM_PORT);
                        ITSDb_SQLBind(pADOU, "p08", pObj->HOST_NM);
                        ITSDb_SQLBind(pADOU, "p09", pObj->OS_NM);
                        ITSDb_SQLBind(pADOU, "p10", pObj->STTS_DSPL_YN);
                        //ITSDb_SQLBind(pADOU, "p11", pObj->DEL_YN);
                        ITSDb_SQLExec(pADOU);
                        pObj->DEL_YN  = "N";
                        pObj->MEM_DEL = "N";
                    }
                }
                else
                {
                    if (pObj->EditMode == DB_NEW)
                    {
                        //insert
                        ITSDb_SQLBind(pADOI, "p01", pObj->SYST_ID);
                        ITSDb_SQLBind(pADOI, "p02", pObj->SYST_TYPE);
                        ITSDb_SQLBind(pADOI, "p03", pObj->SYST_NMBR);
                        ITSDb_SQLBind(pADOI, "p04", pObj->SYST_NM);
                        ITSDb_SQLBind(pADOI, "p05", pObj->SYST_IP_1);
                        ITSDb_SQLBind(pADOI, "p06", pObj->SYST_IP_2);
                        ITSDb_SQLBind(pADOI, "p07", pObj->PRGM_PORT);
                        ITSDb_SQLBind(pADOI, "p08", pObj->HOST_NM);
                        ITSDb_SQLBind(pADOI, "p09", pObj->OS_NM);
                        ITSDb_SQLBind(pADOI, "p10", pObj->STTS_DSPL_YN);
                        //ITSDb_SQLBind(pADOI, "p11", pObj->DEL_YN);
                        ITSDb_SQLExec(pADOI);
                        pObj->DEL_YN  = "N";
                        pObj->MEM_DEL = "N";
                    }
                }
                pObj->Completed = true;
            }
            ITSDb_GetConnection()->CommitTrans();
            Application->MessageBox(FrmLang->lblDbSave->Caption.c_str(),//L"프로세스정보를 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

            FDbUpdate = true;
        }
        catch(EDatabaseError &E)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            if (nEditMode == DB_DELETE)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQryD);
            else
            if (nEditMode == DB_UPDATE)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQryU);
            else
            if (nEditMode == DB_NEW)
                DBERRORMSG(Caption, String(E.ClassName()), E.Message, sQryI);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            ITSDb_GetConnection()->RollbackTrans();
            ::PostMessage(Application->MainForm->Handle, (UINT)(WM_USER+0xF4), (WPARAM)0xB2, (LPARAM)0xB2);
            if (nEditMode == DB_DELETE)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQryD);
            else
            if (nEditMode == DB_UPDATE)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQryU);
            else
            if (nEditMode == DB_NEW)
                DBERRORMSG(Caption, String(e.ClassName()), e.Message, sQryI);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADOI)
        {
            pADOI->Close();
            delete pADOI;
        }
        if (pADOU)
        {
            pADOU->Close();
            delete pADOU;
        }
        if (pADOD)
        {
            pADOD->Close();
            delete pADOD;
        }
        TvList->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
    SaveData();
    if (FDbUpdate)
    {
        POST_MSG(Application->MainForm->Handle, WM_DATABASE_REFRESH, WP_MSG_09, 0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::TvListKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if (Key == VK_DELETE)
	{
		if (BtnDelete->Enabled) BtnDeleteClick(Sender);
	}
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::TvListDataControllerFilterChanged(TObject *Sender)
{
    CMM_SetFilterLike(TvList);
    LblRecords->Caption = "("+FormatFloat("##,##0", m_pGDC->FilteredRecordCount) +"/"+FormatFloat("##,##0", m_pGDC->RecordCount) + ")";
}
//---------------------------------------------------------------------------

void __fastcall TIDB0100M::TvListCustomDrawCell(TcxCustomGridTableView *Sender, TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sType = AViewInfo->GridRecord->DisplayTexts[Column00->Index];
        if (sType == FrmLang->lblModify->Caption)//"수정")
        {
            ACanvas->Brush->Color = clOlive;
        }
        else if (sType == FrmLang->lblDelete->Caption)//"삭제")
        {
            ACanvas->Brush->Color = clRed;
        }
        else if (sType == FrmLang->lblNew->Caption)//"신규")
        {
            ACanvas->Brush->Color = clLime;
            if (AViewInfo->Focused) ACanvas->Font->Color = clBlack;
        }
    }
}
//---------------------------------------------------------------------------

