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

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

#include "DSRM030MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "FRAME_DsrcSectListF"
#pragma link "FRAME_FlashMapF"
#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 "cxEdit"
#pragma link "cxFilter"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxSplitter"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"

TDSRM030M *DSRM030M = NULL;
void TDSRM030M_FlashCall(TObject *ASender, const WideString request)
{
    try
    {
        if (!DSRM030M) return;
        DSRM030M->ShockwaveFlash1FlashCall1(ASender, request);
    }
    catch(Exception &exception)
    {
    }
}

//---------------------------------------------------------------------------
__fastcall TDSRM030M::TDSRM030M(TComponent* Owner)
    : TForm(Owner)
{
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    FUpdate= false;
    FTitle = "DSRC-RSE 수집구간 관리";
    MapStart = false;

    TvLink->OptionsView->NoDataToDisplayInfoText = FrmLang->lblNoInfo->Caption;//"<수집링크 정보가 없습니다>";

    FRAMEGisMap1->FlashCallbackFunc = TDSRM030M_FlashCall;
    FRAMEGisMap1->DrawMode = 1; //구간정보표출

    FRAMEGisMap1->ImgPan->Visible = true;
    FRAMEGisMap1->ImgZoomIn->Visible = true;
    FRAMEGisMap1->ImgZoomOut->Visible = true;
    FRAMEGisMap1->ImgSel->Visible = true;
    FRAMEGisMap1->ImgStnd->Visible = true;
    FRAMEGisMap1->ImgRefresh->Visible = true;

    FRAMEGisMap1->LbDef->Caption = "(선택버튼을 클릭하여 선택합니다.)";
    FRAMEGisMap1->LbDef->Hint = FRAMEGisMap1->LbDef->Caption;
    FRAMEGisMap1->LbDef->Visible = true;
    FRAMEGisMap1->ImgSel->Visible = false;

    FRAMEDsrcSectList1->PnlBottom->Visible = false;
    FRAMEDsrcSectList1->PnlTop->Visible = false;
    FRAMEDsrcSectList1->ColumnSel->Visible = false;
    FRAMEDsrcSectList1->Column02->Visible = false;


    m_pGDC = FRAMEDsrcSectList1->TvList->DataController;

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

    MyItsDsrcSectManager = new TItsDsrcSectManager();
    MyItsDsrcSectManager->LoadFromDb();
}
//---------------------------------------------------------------------------

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

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

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

/*
 * form 초기화
 *
 *  arguments
 *
 *  return
 *      void
 */
void __fastcall TDSRM030M::FormInit()
{
    EdRseSectId->Text   = "";
    EdRseSectName->Text = "";

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

    CMM_ClearGridTableView(TvLink);

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

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

    FRAMEGisMap1->LinkLevel = 1;
    FRAMEGisMap1->LoadGisMap();

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

void __fastcall TDSRM030M::UpdateList()
{
    FRAMEDsrcSectList1->LoadFromDb();
    FRAMEDsrcSectList1->UpdateList();
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TDSRM030M::DisplayInfo(TItsDsrcSect* AObj)
{
    FormInit();

    if (!AObj) return;

    EdRseSectId->Text   = AObj->RSE_SECT_ID;
    EdRseSectName->Text = AObj->RSE_SECT_NM;

    EdStartSpotId->Text   = AObj->STRT_ID;
    EdStartSpotNm->Text   = AObj->STRT_SPOT_NM;
    EdEndSpotId->Text     = AObj->END_ID;
    EdEndSpotNm->Text     = AObj->END_SPOT_NM;
    EdRseSectLength->Text = AObj->DSTC;

    ChangeEditMode(3);

    DisplaySectLink(AObj);
}
//---------------------------------------------------------------------------

void __fastcall TDSRM030M::UpdateRseIcon()
{
    if (!MapStart) return;

    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "RemoveFacility", FAC_RSE);

    try
    {
        //if (ChkViewIcon->Checked)
        {
            String sObjList = "";

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

                    String sLoc = TCommFlash::ReplaceName(pObj->ISTL_LCTN_NM);
                    sObjList += (pObj->ID + "," + "0," + sLoc + "," + pObj->LCTN_X + "," + pObj->LCTN_Y + "," + pObj->RSE_ID + "|");
                }
            }
            __finally
            {
                FRAMEDsrcList1->MyItsDsrcManager->FLists.UnLock();

                if (sObjList != "")
                {
                    sObjList = sObjList.SubString(1, sObjList.Length()-1);
                    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "AddFacility", FAC_RSE + "|" + sObjList);
                }
            }
        }
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

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

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

void __fastcall TDSRM030M::ChangeEditMode(int AEdit)
{
    bool bGisEdit = true;
    bool bEnabled = false;

    switch(AEdit)
    {
    case 0: //편집버튼클릭
        {
            BtnEdit->Enabled = true;
            BtnInsert->Enabled = false;
            BtnDelete->Enabled = false;
            BtnSave->Enabled = true;

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

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

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

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

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

	EdRseSectName->Properties->ReadOnly = bEnabled;

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

    FRAMEGisMap1->ImgSel->Visible = false;
    if (FRAMEGisMap1->MapStart)
    {
        //TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "ClearSelLink", "0");
        RefreshSectLink();
        FRAMEGisMap1->ImgSel->Visible = bGisEdit;
        if (bGisEdit == false)
        {
            if (MapStart)
            {
                TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "SelectMode", "0");   //PAN
            }
        }
    }
}
//---------------------------------------------------------------------------


void __fastcall TDSRM030M::BtnInsertClick(TObject *Sender)
{
    FormInit();
    //신규아이디를 얻어와서 화면을 초기화한다.
    String sSectId;
    if (!MyItsDsrcSectManager->GetNextSectId(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 TDSRM030M::BtnDeleteClick(TObject *Sender)
{
    m_pGDC = FRAMEDsrcSectList1->TvList->DataController;

    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;

	TItsDsrcSect *pObj = MyItsDsrcSectManager->FLists.Find(sSectId);

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

    String sSectId = EdRseSectId->Text;

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

    if (pObj)
    {
        pObj->RSE_SECT_ID   = sSectId;
        pObj->DSTC          = EdRseSectLength->Text.Trim();
        pObj->RSE_SECT_NM   = EdRseSectName->Text.Trim();

        pObj->STRT_ID       = EdStartSpotId->Text.Trim();
        pObj->STRT_SPOT_NM  = EdStartSpotNm->Text;
        pObj->END_ID        = EdEndSpotId->Text.Trim();
        pObj->END_SPOT_NM   = EdEndSpotNm->Text;

        pObj->DEL_YN              = "N";

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

        pObj->FLinkLists.RemoveAll();

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

				TItsDsrcSectLink *pLinkObj = new TItsDsrcSectLink();

				pLinkObj->RSE_SECT_ID = pObj->RSE_SECT_ID;
				pLinkObj->LINK_ID     = sLinkId;
				pLinkObj->LINK_ORD    = String(ii+1);
				pLinkObj->DEL_YN      = "N";

				pObj->FLinkLists.Push(pLinkObj->LINK_ORD, pLinkObj);
            }
        }
        __finally
        {
            TvLink->EndUpdate();
            pObj->DSTC = String(nLength);
        }
        bool bResult;
        bResult = MyItsDsrcSectManager->SaveSect(pObj);

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

            if (FNewDb)
            {
                MyItsDsrcSectManager->FLists.Push(pObj->RSE_SECT_ID, pObj);
            }

            FUpdate = true;

            UpdateList();

            CMM_SetGridRow(FRAMEDsrcSectList1->CxList, FRAMEDsrcSectList1->TvList, sSectId, FRAMEDsrcSectList1->Column01->Index);
            int nRow = FRAMEDsrcSectList1->TvList->DataController->FocusedRecordIndex;
            if( nRow <= -1 )
                return;

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

void __fastcall TDSRM030M::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 TDSRM030M::cxCheckBox1Click(TObject *Sender)
{
    if (cxCheckBox1->Checked)
    {
        if (cxCheckBox2->Checked) cxCheckBox2->Checked = false;
        FRAMEGisMap1->OnGisOperationClick(FRAMEGisMap1->ImgSel);
    }
    else
    {
        FRAMEGisMap1->OnGisOperationClick(FRAMEGisMap1->ImgPan);
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM030M::cxCheckBox2Click(TObject *Sender)
{
    if (cxCheckBox2->Checked)
    {
        if (cxCheckBox1->Checked) cxCheckBox1->Checked = false;
        FRAMEGisMap1->OnGisOperationClick(FRAMEGisMap1->ImgSel);
    }
    else
    {
        FRAMEGisMap1->OnGisOperationClick(FRAMEGisMap1->ImgPan);
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM030M::RefreshSectLink()
{
    if (!MapStart) return;

    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "ClearSelLink", "0");

    String sFirstLink = "";

    int nLength = 0;
    int nTmp;
    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];
                if (VarIsNull(pGDC->Values[ii][ColLink7->Index]))
                    nTmp = 0;
                else
                    nTmp = pGDC->Values[ii][ColLink7->Index];
                nLength += nTmp;

                if (sFirstLink == "") sFirstLink = sLinkId;
                TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "AddSelLink", sLinkId);
            }

            if (sFirstLink != "")
                TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "MoveLink", sFirstLink);
        }
        catch(...)
        {
        }
    }
    __finally
    {
        TvLink->EndUpdate();
        EdRseSectLength->Text = String(nLength);
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM030M::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 TDSRM030M::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;
        }

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

            TItsNode *pNode;
            pGDC->Values[nRow][ColLink3->Index] = pLink->FF_NODEID;
            pGDC->Values[nRow][ColLink5->Index] = pLink->FT_NODEID;
            pNode = ItsNodeManager->FLists.Find(pLink->FF_NODEID);
            if (pNode)
            {
                pGDC->Values[nRow][ColLink4->Index] = pNode->NODE_NAME;
            }
            pNode = ItsNodeManager->FLists.Find(pLink->FT_NODEID);
            if (pNode)
            {
                pGDC->Values[nRow][ColLink6->Index] = pNode->NODE_NAME;
            }

            pGDC->Values[nRow][ColLink7->Index] = StrToInt(pLink->FLINK_LENG);
        }
        else
        {
            Application->MessageBox(L"데이터베이스에 등록되어 있지 않는 링크구간 입니다.\r\n데이터베이스의 정보를 확인하세요.",
                                    FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
            return;
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }

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

void __fastcall TDSRM030M::DisplaySectLink(TItsDsrcSect* AObj)
{
    if (!AObj) return;

    CMM_ClearGridTableView(TvLink);

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

        FOR_STL(TItsDsrcSectLink *, pObj, AObj->FLinkLists)
        {
            if (pObj->DEL_YN == "Y" || pObj->MEM_DEL == "Y") continue;

            String sLinkId = pObj->LINK_ID;
            nRow = pGDC->AppendRecord();
            pGDC->Values[nRow][ColLink2->Index] = sLinkId;

            TItsLink *pLink = ItsLinkManager->FLists.Find(sLinkId);
            if (pLink)
            {
                TItsNode *pNode;
                pGDC->Values[nRow][ColLink3->Index] = pLink->FF_NODEID;
                pGDC->Values[nRow][ColLink5->Index] = pLink->FT_NODEID;
                pNode = ItsNodeManager->FLists.Find(pLink->FF_NODEID);
                if (pNode)
                {
                    pGDC->Values[nRow][ColLink4->Index] = pNode->NODE_NAME;
                }
                pNode = ItsNodeManager->FLists.Find(pLink->FT_NODEID);
                if (pNode)
                {
                    pGDC->Values[nRow][ColLink6->Index] = pNode->NODE_NAME;
                }

                pGDC->Values[nRow][ColLink7->Index] = StrToInt(pLink->FLINK_LENG);
            }
            else
            {
                pGDC->Values[nRow][ColLink7->Index] = 0;
            }
        }
    }
    __finally
    {
        TvLink->EndUpdate();
    }

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

void __fastcall TDSRM030M::TvLinkCellDblClick(TcxCustomGridTableView *Sender, TcxGridTableDataCellViewInfo *ACellViewInfo,
          TMouseButton AButton, TShiftState AShift, bool &AHandled)
{
    if (!MapStart) return;

	if (!ACellViewInfo) return;
	int nRow = TvLink->DataController->FocusedRecordIndex;
	if( nRow <= -1 )
		return;

    String sLinkId = TvLink->DataController->Values[nRow][ColLink2->Index];
    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "FindLink", sLinkId);
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TDSRM030M::ShockwaveFlash1FlashCall1(TObject *ASender, const WideString request)
{
    try
    {
        String sTemp;
        String sFunc;
        String sArgs;

        sTemp = request;

        int nFuncStrIdx = sTemp.Pos("\"");
        int nFuncEndIdx = sTemp.Pos("\" returntype");
        int nArgsStrIdx = sTemp.Pos("string>");
        int nArgsEndIdx = sTemp.Pos("</string");

        sFunc = sTemp.SubString(nFuncStrIdx + 1, nFuncEndIdx - (nFuncStrIdx + 1));
        sArgs = sTemp.SubString(nArgsStrIdx + 7, nArgsEndIdx - (nArgsStrIdx + 7));

        if (sFunc == "OnMapStart")
        {
            MapStart = true;

            String sShowIcon = ChkViewIcon->Checked ? "1" : "0";
            String sShowName = ChkViewName->Checked ? "1" : "0";
            TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "ShowFacility", "RSE|" + sShowIcon + "|" + sShowName);
            TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "MultiSelect",  "1");
        }
        else
        if (sFunc == "OnDrawTrafLink")
        {
            static bool bFirst = true;
            if (bFirst)
            {
                UpdateRseIcon();
                bFirst = true;
            }
        }
        else
        if (sFunc == "OnMousePos")
        {
            if (FRAMEGisMap1->ModeSelect)
            {
                TStringList *pStringList = new TStringList;
                pStringList->CommaText = sArgs;
                String sMapX = pStringList->Strings[0]; //lat
                String sMapY = pStringList->Strings[1]; //lng
                delete pStringList;
                try {
                    //SePosX->Value = sMapX.ToDouble();
                    //SePosY->Value = sMapY.ToDouble();
                } catch(...) {}
            }
        }
        else
        if (sFunc == "OnSelLink")
        {
            if (FRAMEGisMap1->ModeSelect && EdRseSectName->Properties->ReadOnly == false)
            {
                TStringList *pStringList = new TStringList;
                pStringList->CommaText = sArgs;

                String sLevl = pStringList->Strings[0]; //링크레벨
                String sId   = pStringList->Strings[1]; //링크아이디
                String sZoom = pStringList->Strings[2]; //줌레벨
                String sPosX = pStringList->Strings[3]; //x
                String sPosY = pStringList->Strings[4]; //y
                String sMapX = pStringList->Strings[5]; //lat
                String sMapY = pStringList->Strings[6]; //lng

                delete pStringList;

                EdLinkId->Text = sId;
#if 0
                if (BtnLinkAdd->Enabled)
                {
                    if (Shift.Contains(ssCtrl))
                    {
                        BtnLinkAddClick((TObject*)BtnLinkAdd);
                    }
                }
#endif
            }
        }
        else
        if (sFunc == "OnClickFacility")
        {
            if (FRAMEGisMap1->ModeSelect)
            {
                TStringList *pStringList = new TStringList;
                pStringList->CommaText = sArgs;

                String sType = pStringList->Strings[0]; //유형
                String sId   = pStringList->Strings[1]; //아이디
                String sName = pStringList->Strings[2]; //아이디

                delete pStringList;

                if (cxCheckBox1->Checked)
                {
                    EdStartSpotId->Text = sId;
                    EdStartSpotNm->Text = sName;
                }
                else
                if (cxCheckBox2->Checked)
                {
                    EdEndSpotId->Text = sId;
                    EdEndSpotNm->Text = sName;
                }
            }
        }
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TDSRM030M::ChkViewIconClick(TObject *Sender)
{
    if (!MapStart) return;

    String sShowIcon = ChkViewIcon->Checked ? "1" : "0";
    String sShowName = ChkViewName->Checked ? "1" : "0";
    TCommFlash::CallFunc(FRAMEGisMap1->ShockwaveFlash1, "ShowFacility", "RSE|" + sShowIcon + "|" + sShowName);
}
//---------------------------------------------------------------------------

