/****************************************************************************
 * @source      :
 * @description :
 ****************************************************************************
 * DATE         AUTHOR    DESCRIPTION
 * --------------------------------------------------------------------------
 * 2011/12/24   정승호    최초작성
 *
 ****************************************************************************/
//---------------------------------------------------------------------------

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "AppGlobalF.h"
#include "CDSCodeF.h"
#include "CDSFacilityF.h"
#include "CDSNodeF.h"
#include "CDSServiceLinkF.h"
#include "XiMapUtilF.h"
#pragma hdrstop

#include "DSRM040MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalendar"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxSplitter"
#pragma link "cxTextEdit"
#pragma link "cxTimeEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "FRAME_DsrcListF"
#pragma link "cxRadioGroup"
#pragma link "cxClasses"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxDBData"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridDBTableView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "FRAME_GisMapF"
#pragma link "cxCheckBox"
#pragma link "cxCalc"
#pragma resource "*.dfm"

TDSRM040M *DSRM040M = NULL;
//---------------------------------------------------------------------------
__fastcall TDSRM040M::TDSRM040M(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FUpdate= false;
    FTitle = "DSRC-RSE 정보제공구간 관리";
    TvList->OptionsView->NoDataToDisplayInfoText = "<DSRC-RSE 정보제공구간 정보가 없습니다>";
    TvLink->OptionsView->NoDataToDisplayInfoText = "<서비스링크 정보가 없습니다>";

    m_pGDC = TvList->DataController;

    FRAMEGisMap1->LbDef->Visible = false;
    FRAMEGisMap1->LbDef->Caption = "(선택메뉴를 클릭하여 위치를 선택합니다.)";
    FRAMEGisMap1->LbDef->Hint = FRAMEGisMap1->LbDef->Caption;

    FRAMEDsrcList1->PnlBottom->Visible = false;
    FRAMEDsrcList1->PnlTop->Visible = false;
    FRAMEDsrcList1->ColumnSel->Visible = false;
    //FRAMEDsrcList1->Column02->Visible = false;

    MyItsDsrcOfferSectManager = new TItsDsrcOfferSectManager();
    MyItsDsrcOfferSectManager->LoadFromDb();
}
//---------------------------------------------------------------------------

/*
 * 닫기버튼 이나 x버튼 클릭시 공통으로 처리하는 로직이들어간다..
 * Form과 DataModule class를 delete시킨다.
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TDSRM040M::CommClose()
{
    try
    {
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

/*
 * Form을 보여줄때 호출되는 event 메서드이다.
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TDSRM040M::FormShow(TObject *Sender)
{
    Refresh();

    FRAMEGisMap1->LinkLevel = 2;

    FormInit();
    ChangeEditMode(1);
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

/*
 * form 초기화
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TDSRM040M::FormInit()
{
    EdRseSectId->Text   = "";
    EdRseSectName->Text = "";
    SeCNGSSPD->Value    = 20;
    SeDELYSPD->Value    = 10;

    EdStartSpotId->Text   = "";
    EdStartSpotNm->Text   = "";
    EdEndSpotId->Text     = "";
    EdEndSpotNm->Text     = "";
    EdRseSectLength->Text = "";

    CMM_ClearGridTableView(TvLink);

    FRAMEGisMap1->xclntMap->ClearUserDrawPolyGon();
	FRAMEGisMap1->xclntMap->ClearPathInfo();
    FRAMEGisMap1->xclntMap->ReDraw();

    FNewDb = false;
}
//---------------------------------------------------------------------------

/*
 * Form이 Show되고 난 후 최초 1회 수행되는 타이머 이벤트
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TDSRM040M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    FRAMEGisMap1->LoadGisMap();
    //FRAMEGisMap1->ShowHideTrafficLayer(false);
    UpdateRseIcon();

    FRAMEDsrcList1->UpdateList();
    UpdateList();
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::UpdateList()
{
    CMM_ClearGridTableView(TvList);

    int nRow;
    try
    {
        TvList->BeginUpdate();

        FOR_STL(TItsDsrcOfferSect*, pObj, MyItsDsrcOfferSectManager->FLists)
        {
            if (pObj->DEL_YN == "Y") continue;

            nRow = m_pGDC->AppendRecord();
            m_pGDC->Values[nRow][Column01->Index] = pObj->OFFR_SECT_ID;
            m_pGDC->Values[nRow][Column02->Index] = pObj->OFFR_DRCT_NM;
            m_pGDC->Values[nRow][Column03->Index] = pObj->ID;
            m_pGDC->Values[nRow][Column04->Index] = pObj->PRE_ID;

            m_pGDC->Values[nRow][Column99->Index] = (int)pObj;
        }
    }
    __finally
    {
        TvList->EndUpdate();
        TvList->DataController->GotoFirst();
        TvList->DataController->FocusedRecordIndex = 0;
        //CxList->SetFocus();
    }

}
//---------------------------------------------------------------------------

/*
 * Close 버튼 클릭 이벤트 핸들러
 *  arguments
 *      Sender : event handler 객체
 *  return
 *      void
 */
void __fastcall TDSRM040M::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------

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

void __fastcall TDSRM040M::FormCloseQuery(TObject *Sender, bool &CanClose)
{
#if 0
    if (FUpdate)
    {
        POST_MSG(Application->MainForm->Handle, WM_DATABASE_REFRESH, WP_MSG_14, 0);
    }
#endif
    if (MyItsDsrcOfferSectManager)
    {
        delete MyItsDsrcOfferSectManager;
        MyItsDsrcOfferSectManager = NULL;
    }
    FRAMEDsrcList1->OnCloseQuery(CanClose);
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::DisplayInfo(TItsDsrcOfferSect* AObj)
{
    FormInit();

    if (!AObj) return;

    EdRseSectId->Text   = AObj->OFFR_SECT_ID;
    EdRseSectName->Text = AObj->OFFR_DRCT_NM;
    SeCNGSSPD->Value    = StrToFloatDef(AObj->CNGS_BASI_SPED, 0);
    SeDELYSPD->Value    = StrToFloatDef(AObj->DELY_BASI_SPED, 0);

    EdStartSpotId->Text   = AObj->ID;
    EdStartSpotNm->Text   = AObj->TARGETNM;
    EdEndSpotId->Text     = AObj->PRE_ID;
    EdEndSpotNm->Text     = AObj->PRENM;
    EdRseSectLength->Text = AObj->OBU_ENTR_DRCT_NMBR;

    DisplaySectLink(AObj);

    ChangeEditMode(3);
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::UpdateRseIcon()
{
	FRAMEGisMap1->xclntMap->ClearCCTVEvent(true);

    try
    {
        //if (ChkViewIcon->Checked)
        {
            try
            {
                FRAMEDsrcList1->MyItsDsrcManager->FLists.Lock();
                FOR_STL(TItsDsrc*, pObj, FRAMEDsrcList1->MyItsDsrcManager->FLists)
                {
                    if (pObj->DEL_YN == "Y") continue;

                    try {
                    String sTitle = "▶▶DSRC-RSE 정보◀◀|ID : " + pObj->ID + "|명칭 : " + pObj->ISTL_LCTN_NM;
                    FRAMEGisMap1->xclntMap->ADDCCTVEvent(DDPoint(StrToFloatDef(pObj->LCTN_X, 0), StrToFloatDef(pObj->LCTN_Y, 0)), pObj->ID, pObj->ISTL_LCTN_NM, sTitle, 6, 1, 7);
                    } catch(...) { }
                }
            }
            __finally
            {
                FRAMEDsrcList1->MyItsDsrcManager->FLists.UnLock();
            }
        }
    }
    catch(...)
    {
    }

	FRAMEGisMap1->xclntMap->ReDraw();
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::BtnEditClick(TObject *Sender)
{
    if (BtnEdit->Caption == "편집")
    {
        ChangeEditMode(0);
        FNewDb = false;
    }
    else
    {
        String sSectId = EdRseSectId->Text;
        ChangeEditMode(1);

        TItsDsrcOfferSect *pObj = MyItsDsrcOfferSectManager->FLists.Find(sSectId);
        if (pObj)
        {
            CMM_SetGridRow(CxList, TvList, sSectId, Column01->Index);
            DisplayInfo(pObj);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::ChangeEditMode(int AEdit)
{
    bool bEnabled = false;
    switch(AEdit)
    {
    case 0: //편집버튼클릭
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = "취소";
            BtnEdit->Hint    = "데이터 편집 취소";
        }
        break;
    case 1: //편집[취소]버튼클릭
        {
            FormInit(); //화면을 모두 지운다.
            BtnEdit->Enabled = false;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = false;

            BtnEdit->Caption = "편집";
            BtnEdit->Hint    = "데이터 편집";
            bEnabled = true;
        }
        break;
    case 2: //추가
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

            BtnEdit->Caption = "취소";
            BtnEdit->Hint    = "데이터 편집 취소";
        }
        break;
    case 3: //수정모드
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = true;
            BtnDelete->Enabled = true;
            BtnSave->Enabled = false;

            BtnEdit->Caption = "편집";
            BtnEdit->Hint    = "데이터 편집";
            bEnabled = true;
        }
        break;
    }

    if (AEdit == 0 || AEdit == 2)
    {
        CxList->Enabled = false;
    }
    else
    {
        CxList->Enabled = true;
    }

	EdRseSectName->Properties->ReadOnly = bEnabled;
	SeCNGSSPD->Properties->ReadOnly = bEnabled;
	SeDELYSPD->Properties->ReadOnly = bEnabled;

    cxCheckBox1->Enabled = !bEnabled;
    cxCheckBox2->Enabled = !bEnabled;
    BtnLinkAdd->Enabled = !bEnabled;
    BtnLinkDel->Enabled = !bEnabled;
}
//---------------------------------------------------------------------------


void __fastcall TDSRM040M::BtnInsertClick(TObject *Sender)
{
    FormInit();
    //신규아이디를 얻어와서 화면을 초기화한다.
    String sSectId;
    if (!MyItsDsrcOfferSectManager->GetNextOffrSectId(sSectId))
    {
        Application->MessageBox(L"새로운 제공구간 ID를 생성하지 못하였습니다.\r\n프로그램 종료후에 다시 시도해 보세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }

    EdRseSectId->Text = sSectId;
    ChangeEditMode(2);
    FNewDb = true;
}
//---------------------------------------------------------------------------

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

    String sSectId = EdRseSectId->Text;

    if (sSectId.IsEmpty())
    {
        Application->MessageBox(L"삭제할 DSRC-RSE 제공구간을 목록에서 선택하세요.\r\n목록에서 더블클릭하여 선택합니다.", FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }
    String sMsg;
    sMsg = "제공구간 ID: " + sSectId;
    sMsg+= "\r\n선택하신 DSRC-RSE 제공구간 정보를 삭제하시겠습니까?\r\n관련정보도 함께 삭제 됩니다.";

    if (Application->MessageBox(sMsg.c_str(), FTitle.c_str(), MB_YESNO|MB_ICONQUESTION|MB_APPLMODAL) != IDYES) return;

	TItsDsrcOfferSect *pObj = MyItsDsrcOfferSectManager->FLists.Find(sSectId);

	if (pObj)
	{
		if (MyItsDsrcOfferSectManager->DeleteOfferSect(pObj->OFFR_SECT_ID))
		{
            pObj->DEL_YN = "Y";
            UpdateList();
		}

        FUpdate = true;

        Application->MessageBox(L"DSRC-RSE 제공구간 정보삭제\r\nDSRC-RSE 제공구간 정보를 삭제 하였습니다.",
                                FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        FormInit();
        ChangeEditMode(1);
	}
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::BtnSaveClick(TObject *Sender)
{
    if (EdRseSectId->Text.IsEmpty()          ||
        EdRseSectName->Text.Trim().IsEmpty() ||
        EdStartSpotNm->Text.Trim().IsEmpty() ||
        EdEndSpotNm->Text.Trim().IsEmpty()   ||
        EdStartSpotId->Text.IsEmpty()        ||
        EdEndSpotId->Text.IsEmpty()           )
	{
        Application->MessageBox(L"★항목은 필수 입력 항목입니다.\r\n모두 입력후 진행해 주세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
	}

    String sSectId = EdRseSectId->Text;

    TItsDsrcOfferSect *pObj;
    if (FNewDb)
        pObj = new TItsDsrcOfferSect();
    else
        pObj = MyItsDsrcOfferSectManager->FLists.Find(sSectId);

    if (pObj)
    {
        pObj->OFFR_SECT_ID   = sSectId;
        pObj->OFFR_DRCT_NM   = EdRseSectName->Text.Trim();
        pObj->CNGS_BASI_SPED = VarToStr(SeCNGSSPD->Value);
        pObj->DELY_BASI_SPED = VarToStr(SeDELYSPD->Value);

        pObj->ID                  = EdStartSpotId->Text.Trim();
        pObj->OBU_ENTR_DRCT_NMBR  = EdRseSectLength->Text.Trim();
        pObj->PRE_ID              = EdEndSpotId->Text.Trim();
        pObj->IXR_DRCT_NUM        = pObj->OBU_ENTR_DRCT_NMBR;
        pObj->TARGETNM            = EdStartSpotNm->Text;
        pObj->PRENM               = EdEndSpotNm->Text;
        pObj->DEL_YN              = "N";

        pObj->Completed           = true;
        pObj->MEM_DEL             = "N";

        pObj->FSectLists.RemoveAll();

        try {
            TvLink->BeginUpdate();
            for (int ii = 0; ii < TvLink->DataController->RecordCount; ii++)
            {
                String sLinkId = TvLink->DataController->Values[ii][ColLink2->Index];

				TItsDsrcOfferSectInfr *pSectObj = new TItsDsrcOfferSectInfr();

				pSectObj->OFFR_SECT_ID = pObj->OFFR_SECT_ID;
				pSectObj->IFSC_ID      = sLinkId;
				pSectObj->ORD          = String(ii+1);
				pSectObj->DEL_YN       = "N";

				pObj->FSectLists.Push(pSectObj->ORD, pSectObj);
            }
        }
        __finally
        {
            TvLink->EndUpdate();
        }
        bool bResult;
        bResult = MyItsDsrcOfferSectManager->SaveOfferSect(pObj);

        if (bResult)
        {
            Application->MessageBox(L"DSRC-RSE 제공구간 정보저장\r\nDSRC-RSE 제공구간 정보를 저장 하였습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

            if (FNewDb)
            {
                MyItsDsrcOfferSectManager->FLists.Push(pObj->OFFR_SECT_ID, pObj);
            }

            FUpdate = true;

            UpdateList();
            CMM_SetGridRow(CxList, TvList, sSectId, Column01->Index);
#if 0
            DisplayInfo(pObj);
#else
            int nRow = TvList->DataController->FocusedRecordIndex;
            if( nRow <= -1 )
                return;

            int nMemPtr = TvList->DataController->Values[nRow][Column99->Index];
            TItsDsrcOfferSect* pObj = (TItsDsrcOfferSect*)nMemPtr;
            DisplayInfo(pObj);
#endif
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
	if (!ACellViewInfo) return;
	int nRow = TvList->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    int nMemPtr = TvList->DataController->Values[nRow][Column99->Index];
    TItsDsrcOfferSect* pObj = (TItsDsrcOfferSect*)nMemPtr;
    DisplayInfo(pObj);
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::FRAMEDsrcList1TvListCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
	if (!ACellViewInfo) return;
	int nRow = FRAMEDsrcList1->TvList->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    int nMemPtr = FRAMEDsrcList1->TvList->DataController->Values[nRow][FRAMEDsrcList1->Column99->Index];
    TItsDsrc* pObj = (TItsDsrc*)nMemPtr;
    FRAMEGisMap1->SelectObjectByPos(StrToFloatDef(pObj->LCTN_X, 0), StrToFloatDef(pObj->LCTN_Y, 0), true);
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::cxCheckBox1Click(TObject *Sender)
{
    if (cxCheckBox1->Checked)
    {
        if (cxCheckBox2->Checked) cxCheckBox2->Checked = false;
        FRAMEGisMap1->xclntMap->MapOperation = OnSelect;
    }
    else
    {
        FRAMEGisMap1->xclntMap->MapOperation = OnPanSt;
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::cxCheckBox2Click(TObject *Sender)
{
    if (cxCheckBox2->Checked)
    {
        if (cxCheckBox1->Checked) cxCheckBox1->Checked = false;
        FRAMEGisMap1->xclntMap->MapOperation = OnSelect;
    }
    else
    {
        FRAMEGisMap1->xclntMap->MapOperation = OnPanSt;
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::FRAMEGisMap1xclntMapCCTVClick(TMouseButton Button, TShiftState Shift, int CNT, TArr_CCTV_Rec INFO)
{
    if (FRAMEGisMap1->xclntMap->MapOperation != OnSelect) return;
    if (Button != mbLeft) return;

    if (CNT < 1) return;
    TCCTV_REC info = INFO[0];

    if (cxCheckBox1->Checked)
    {
        EdStartSpotId->Text = info.fID;
        EdStartSpotNm->Text = info.fAttr;
        int nAutoNum;
        if (MyItsDsrcOfferSectManager->GetAutoRseDrctNum(EdStartSpotId->Text, nAutoNum))
        {
            EdRseSectLength->Text = String(nAutoNum);
        }
    }
    else
    if (cxCheckBox2->Checked)
    {
        EdEndSpotId->Text = info.fID;
        EdEndSpotNm->Text = info.fAttr;
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::FRAMEGisMap1xclntMapMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    if (EdRseSectName->Properties->ReadOnly) return;

    FRAMEGisMap1->xclntMap->ClearUserDrawPolyGon();
    FRAMEGisMap1->xclntMap->ReDraw();

    if (FRAMEGisMap1->xclntMap->MapOperation != OnSelect) return;
    if (Button != mbLeft) return;

    String sLinkId;
    int nLayerIdx = FRAMEGisMap1->GetLayerIndex();
    if (nLayerIdx >= 0)
    {
        TXiMapUtil::SelectLinkByPos(FRAMEGisMap1->xsrvrMap, FRAMEGisMap1->xclntMap, nLayerIdx, X, Y, sLinkId);
        if (!sLinkId.IsEmpty())
        {
            EdLinkId->Text = sLinkId;
            if (BtnLinkAdd->Enabled)
            {
                if (Shift.Contains(ssCtrl))
                {
                    BtnLinkAddClick((TObject*)BtnLinkAdd);
                }
            }
        }
    }
return;

    if (FRAMEGisMap1->SelectLinkByPos(X, Y, sLinkId))
    {
        EdLinkId->Text = sLinkId;
        int nLayerIdx = FRAMEGisMap1->GetLayerIndex();
        if (nLayerIdx >= 0)
        {
            TXiMapUtil::SelectLinkByPos2(FRAMEGisMap1->xsrvrMap, FRAMEGisMap1->xclntMap, nLayerIdx, X, Y);
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::RefreshSectLink()
{
	FRAMEGisMap1->xclntMap->ClearPathInfo();
    FRAMEGisMap1->xclntMap->ReDraw();

    int nLayerIdx = FRAMEGisMap1->GetLayerIndex();
    if (nLayerIdx < 0) return;

    try
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();
        try
        {
            for (int ii = 0; ii < pGDC->RecordCount; ii++)
            {
                String sLinkId = pGDC->Values[ii][ColLink2->Index];
                TXiMapUtil::SelectLinkById(FRAMEGisMap1->xsrvrMap, FRAMEGisMap1->xclntMap, nLayerIdx, sLinkId);
            }
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }

    FRAMEGisMap1->xclntMap->ReDraw();
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::BtnLinkDelClick(TObject *Sender)
{
    int nSelRows = TvLink->Controller->SelectedRowCount;
    if (nSelRows <= 0)
    {
        Application->MessageBox(L"삭제를 원하는 서비스구간을 목록에서 선택하세요.",
                                FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return;
    }
    TvLink->Controller->DeleteSelection();
    RefreshSectLink();
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::BtnLinkAddClick(TObject *Sender)
{
    if (EdLinkId->Text.IsEmpty()) return;

    String sNewId = EdLinkId->Text;
    bool bFound = false;
    try
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();

        for (int ii = 0; ii < pGDC->RecordCount; ii++)
        {
            String sLinkId = pGDC->Values[ii][ColLink2->Index];
            if (sNewId == sLinkId)
            {
                bFound = true;
                break;
            }
        }
        if (bFound)
        {
            Application->MessageBox(L"이미 등록되어 있는 서비스구간 입니다.\r\n서비스 구간은 중복해서 등록 할 수 없습니다.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }

        TItsServiceLink *pLink = ItsServiceLinkManager->FLists.Find(sNewId);
        if (pLink)
        {
            nRow = pGDC->AppendRecord();
            pGDC->Values[nRow][ColLink2->Index] = sNewId;

            TItsNode *pNode;
            pGDC->Values[nRow][ColLink3->Index] = pLink->F_NODE_ID;
            pGDC->Values[nRow][ColLink5->Index] = pLink->T_NODE_ID;
            pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
            if (pNode)
            {
                pGDC->Values[nRow][ColLink4->Index] = pNode->NODE_NAME;
            }
            pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
            if (pNode)
            {
                pGDC->Values[nRow][ColLink6->Index] = pNode->NODE_NAME;
            }
        }
        else
        {
            Application->MessageBox(L"데이터베이스에 등록되어 있지 않는 서비스구간 입니다.\r\n데이터베이스의 정보를 확인하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }

    RefreshSectLink();
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::DisplaySectLink(TItsDsrcOfferSect* AObj)
{
    if (!AObj) return;

    CMM_ClearGridTableView(TvLink);

    try
    {
        int nRow;
        TcxDataController *pGDC = TvLink->DataController;
        TvLink->BeginUpdate();

        FOR_STL(TItsDsrcOfferSectInfr *, pObj, AObj->FSectLists)
        {
            String sLinkId = pObj->IFSC_ID;
            nRow = pGDC->AppendRecord();
            pGDC->Values[nRow][ColLink2->Index] = sLinkId;

            TItsServiceLink *pLink = ItsServiceLinkManager->FLists.Find(sLinkId);
            if (pLink)
            {
                TItsNode *pNode;
                pGDC->Values[nRow][ColLink3->Index] = pLink->F_NODE_ID;
                pGDC->Values[nRow][ColLink5->Index] = pLink->T_NODE_ID;
                pNode = ItsNodeManager->FLists.Find(pLink->F_NODE_ID);
                if (pNode)
                {
                    pGDC->Values[nRow][ColLink4->Index] = pNode->NODE_NAME;
                }
                pNode = ItsNodeManager->FLists.Find(pLink->T_NODE_ID);
                if (pNode)
                {
                    pGDC->Values[nRow][ColLink6->Index] = pNode->NODE_NAME;
                }
            }
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }

    RefreshSectLink();
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::TvLinkCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
	if (!ACellViewInfo) return;
	int nRow = TvLink->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    String sLinkId = TvLink->DataController->Values[nRow][ColLink2->Index];
    TXiMapUtil::MoveServiceLink(FRAMEGisMap1->xclntMap, sLinkId);
}
//---------------------------------------------------------------------------

void __fastcall TDSRM040M::ChkGisZoomClick(TObject *Sender)
{
    cxGroupBox1->Visible = !ChkGisZoom->Checked;
}
//---------------------------------------------------------------------------

