更改树控件时更新xml的类

 

 

// ReviseXml.h: interface for the CReviseXml class. // //////////////////////////////////////////////////////////////////////

#if !defined(AFX_REVISEXML_H__2EEDA24E_6753_446A_8921_2343B42D3835__INCLUDED_) #define AFX_REVISEXML_H__2EEDA24E_6753_446A_8921_2343B42D3835__INCLUDED_

#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000

class CReviseXml  { public:  CReviseXml();  virtual ~CReviseXml(); public:  BOOL ReviseSaveXml(BOOL bReviseID,string sBeginID,string sBeginText,string sEndText); private:  string GetID(string sEndText);  string GetReviseAttribute(string sEndText);  string GetEndValue(string sEndText);  void GetEndValueIPorPort(string sEndText); private:  //修改后的数据  string sEndServerPort;  string sEndMatrixIP;  string sEndMatrixPort;  string sEndMatrixType; };

#endif // !defined(AFX_REVISEXML_H__2EEDA24E_6753_446A_8921_2343B42D3835__INCLUDED_)

ReviseXml.cpp
// ReviseXml.cpp: implementation of the CReviseXml class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "PMAgentManage.h"
#include "PMAgentManageDlg.h"
#include "ReviseXml.h"
#include "Markup.h"
extern CTaskDeal gTaskDeal;

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
extern string g_sFilefullPath;

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CReviseXml::CReviseXml()
{
	
}

CReviseXml::~CReviseXml()
{
	
}

BOOL CReviseXml::ReviseSaveXml(BOOL bReviseID,string sBeginID,string sBeginText,string sEndText)
{
	CMarkup xml;
	xml.Load(g_sFilefullPath.c_str());
	xml.ResetMainPos();
	xml.FindElem();
	while (xml.FindChildElem("matrix"))
	{
		if (bReviseID == TRUE) //改id
		{
			if (sBeginID.c_str()==xml.GetChildAttrib("id"))
			{
				//提取出修改后的id
				string sEndID =GetID(sEndText);
				xml.SetChildAttrib("id",CString(sEndID.c_str()));
				xml.Save(g_sFilefullPath.c_str());	
				CPMAgentManageDlg* pCPMAgent = (CPMAgentManageDlg*)AfxGetApp()->m_pMainWnd;
				string sShowLog=pCPMAgent->GetCurrentTime();
				sShowLog = sShowLog + "修改"+"“" +sBeginText+"”"+"为"+"“"+sEndText+"”";
				pCPMAgent->WriteLog(sShowLog);
				return TRUE;
			}
		}
		if (bReviseID == FALSE)
		{
			if (sBeginID.c_str()==xml.GetChildAttrib("id"))
			{
				string sAttribute = GetReviseAttribute(sEndText);	
				
				if (sAttribute=="serv_port")
				{
					xml.SetChildAttrib(sAttribute.c_str(),CString(sEndServerPort.c_str()));
				}
				else if (sAttribute=="type")
				{
					xml.SetChildAttrib(sAttribute.c_str(),CString(sEndMatrixType.c_str()));
				}
				else if (sAttribute=="ip")
				{
					xml.SetChildAttrib(sAttribute.c_str(),CString(sEndMatrixIP.c_str()));
					xml.SetChildAttrib("port",CString(sEndMatrixPort.c_str()));
				}
				else if (sAttribute=="port")
				{
					xml.SetChildAttrib(sAttribute.c_str(),CString(sEndMatrixPort.c_str()));
				}
				else
				{
					return FALSE;
				}	
				xml.Save(g_sFilefullPath.c_str());
				CPMAgentManageDlg* pCPMAgent = (CPMAgentManageDlg*)AfxGetApp()->m_pMainWnd;
				string sShowLog=pCPMAgent->GetCurrentTime();
				sShowLog = sShowLog + "设置矩阵(ID."+sBeginID+"):"+"“"+sEndText+"”";
				pCPMAgent->WriteLog(sShowLog);
				return TRUE;
			}
		}
	}
	return FALSE;
}

string CReviseXml::GetID(string sEndText)
{
	//当获取的是id时
	char cTemp[100],cID[100];
	int i=0,j=0;
	strcpy(cTemp,sEndText.c_str());
	for (i=0;cTemp[i]!='\0';i++)
	{
		if (cTemp[i]>='0'&&cTemp[i]<='9')
		{
			cID[j]=cTemp[i];
			j++;
			cID[j] = '\0';
		}
	}
	string sID;
	sID=cID;//获取id为stirng sID
	return sID;
}

string CReviseXml::GetReviseAttribute(string sEndText)
{
	//获取出前4个汉字加一个冒号
	char cTem[100],cDest[100];
	strcpy(cTem,sEndText.c_str());
	char *sTem = cTem;
	memcpy(cDest,sTem,9);
	cDest[9]='\0';
	string sCompare=cDest;
	if (sCompare == "服务端口:")
	{
		//获取出改后的服务端口
		sEndServerPort=GetEndValue(sEndText);
		return "serv_port";
	}
	if (sCompare == "矩阵类型:")
	{
		//获取出改后的矩阵类型
		sEndMatrixType=GetEndValue(sEndText);
		if (sEndMatrixType=="XG-1200平台")
		{
			sEndMatrixType="0";
		}
		else if (sEndMatrixType=="中威平台")
		{
			sEndMatrixType="1";
		}
		else if (sEndMatrixType=="中创平台")
		{
			sEndMatrixType="2";
		}
		return "type";
	}
	if (sCompare == "矩阵地址:")
	{
		//获取改后的ip和端口
		GetEndValueIPorPort(sEndText);
		return "ip";
	}
	return "error";
}
//取出:后的值
string CReviseXml::GetEndValue(string sEndText)
{
	char array[100],cTem[100];
	int count=0;
	strcpy(cTem,sEndText.c_str());
	for(int i=9;i
 
 
//点击修改信息
void CPMAgentManageDlg::OnMenuitemRevise(NMHDR *pNMHDR,LRESULT *pResult) 
{
	// TODO: Add your command handler code here
	UpdateData(true);
	//CString str;
	g_sSelectStr =m_TreeCtrl.GetItemText(m_TreeCtrl.GetSelectedItem());
	//取出strName前4个汉字 CString strName;
	char cTem[100],cDest[100];
	strcpy(cTem,g_sSelectStr);
	char *sTem = cTem;
	memcpy(cDest,sTem,8);
	cDest[8]='\0';
	g_sCompare=cDest;
	if ( (g_sCompare == "矩阵状态") || (g_sCompare =="平台状态") || (g_sCompare =="创建时间") || (g_sCompare =="平台地址") )
	{
		MessageBox("此条信息禁止手动修改!");
		return;
	}
	else
	{
		m_TreeCtrl.ModifyStyle(NULL,TVS_EDITLABELS);      //设置编辑风格 TVS_EDITLABELS 表示可以控制鼠标单击修改树项的名称;
		HTREEITEM hItem=m_TreeCtrl.GetSelectedItem();
		m_TreeCtrl.EditLabel(hItem);	//TVN_BEGINLABELEDIT
	}
}

void CPMAgentManageDlg::OnBeginlabeleditTree(NMHDR* pNMHDR,LRESULT* pResult) 
{	
	/*	TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;*/
	// TODO: Add your control notification handler code here
	m_TreeCtrl.GetEditControl()->LimitText(1024);
	*pResult = 0;
}

void CPMAgentManageDlg::OnEndlabeleditTree(NMHDR* pNMHDR, LRESULT* pResult) 
{	
	/*	TV_DISPINFO* pTVDispInfo = (TV_DISPINFO*)pNMHDR;*/
	// TODO: Add your control notification handler code here
	HTREEITEM m_hInsertItem;
	CString strName; //修改后的数据
	m_TreeCtrl.GetEditControl()->GetWindowText(strName);
	if (strName.IsEmpty())
	{
		MessageBox("数据项名称不能为空,请重新输入!");
		CEdit *pEdit = m_TreeCtrl.EditLabel(m_hInsertItem);//begin------end
		return;
	}
	if (strName == g_sSelectStr)
	{
		return;
	}
	HTREEITEM hRoot = m_TreeCtrl.GetRootItem();
	HTREEITEM hFind = FindItem(hRoot,strName);          //判断数据是否相同
	string sBeginText = m_TreeCtrl.GetItemText(m_TreeCtrl.GetSelectedItem());//修改前的文本
	if (hFind == NULL)
	{
		char msg[64]={0};
		sprintf(msg,"修改信息为%s,确定吗?",strName);
		
		CString strText; //修改后的文本
		m_TreeCtrl.GetEditControl()->GetWindowText(strText.GetBuffer(200),200);
		//修改xml  
		string sEndText(strText.GetBuffer(100));
		strText.ReleaseBuffer();
		
		if (MessageBox(msg,"提示",MB_OKCANCEL) == IDOK)
		{
			BOOL nRet = ReviseXml(sBeginText,sEndText);
			if (nRet == TRUE)
			{
				//修改节点
				m_TreeCtrl.SetItemText(m_TreeCtrl.GetSelectedItem(),strText);//设置编辑后的文本为结点
			}	
			*pResult = TRUE;
		}
		else
		{
			m_TreeCtrl.DeleteItem(m_hInsertItem);
		}
	}
	else //找到相同数据
	{
		if ((g_sCompare == "北京一区") || (g_sCompare =="矩阵地址") || (g_sCompare =="服务端口") )
		{
			MessageBox("该数据已存在");
			CEdit *pEdit = m_TreeCtrl.EditLabel(m_hInsertItem);
		}
		else
		{
			char msg[64]={0};
			sprintf(msg,"修改信息为%s,确定吗?",strName);
			CString strText; //修改后的文本
			m_TreeCtrl.GetEditControl()->GetWindowText(strText.GetBuffer(200),200);
			//修改xml  
			string sEndText(strText.GetBuffer(100));
			strText.ReleaseBuffer();
			if (MessageBox(msg,"提示",MB_OKCANCEL) == IDOK)
			{
				BOOL nRet = ReviseXml(sBeginText,sEndText);
				if (nRet == TRUE)
				{
					//修改节点
					m_TreeCtrl.SetItemText(m_TreeCtrl.GetSelectedItem(),strText);//设置编辑后的文本为结点
				}
				*pResult = TRUE;
			}
			else
			{
				m_TreeCtrl.DeleteItem(m_hInsertItem);
			}
		}
	}
	*pResult = 0;
}

//防止修改的数据有相同 嵌套循环
HTREEITEM CPMAgentManageDlg::FindItem(HTREEITEM item,CString strText)
{
	HTREEITEM hFind;
	if (item==NULL)
	{
		return NULL;	
	}
	while(item!=NULL)
	{
		if (m_TreeCtrl.GetItemText(item)==strText)
		{
			return item;
		}
		if (m_TreeCtrl.ItemHasChildren(item))
		{
			item = m_TreeCtrl.GetChildItem(item);
			hFind = FindItem(item,strText);
			if (hFind)
			{
				return hFind;
			}
			else
			{
				item = m_TreeCtrl.GetNextSiblingItem(m_TreeCtrl.GetParentItem(item));
			}
		}
		else
		{
			item = m_TreeCtrl.GetNextSiblingItem(item);
			if (item == NULL)
			{
				return NULL;
			}
		}
	}
	return item;
} 

//判断编辑后  传按键进去  然后发送消息
BOOL CPMAgentManageDlg::IsTreeCtrlEditMessage(WPARAM KeyCode) 
{ 
	BOOL rvalue = FALSE;
	CWnd *pWnd = this;
	CTreeCtrl *treectrl = (CTreeCtrl *)pWnd->GetDlgItem(IDC_TREE); 
	if(!treectrl)
	{
		return rvalue; 
	}
	CWnd* focus = GetFocus();
	CEdit* edit = treectrl->GetEditControl(); 
	if((CEdit *)focus == edit) 
	{
		if (KeyCode == VK_ESCAPE)  
		{
			edit->SendMessage(WM_UNDO,KeyCode);  //ESC撤销
			edit->SendMessage(WM_KEYDOWN,KeyCode); //退出编辑
		}
		if (KeyCode == VK_RETURN)
		{
			edit->SendMessage(WM_KEYDOWN,KeyCode);   //回车就执行
		}
		rvalue = TRUE; 
	} 
	return rvalue;
} 
//重载回车键
void CPMAgentManageDlg::OnOK() 
{ 
	CWnd *pwnd=this->GetFocus(); 
	if((pwnd->GetDlgCtrlID()==IDC_LIST_AGENTINFO)||(pwnd->GetDlgCtrlID()==IDC_LISTBOX)||(pwnd->GetDlgCtrlID()==IDC_TREE))
	{
		return;
	}
	if(!IsTreeCtrlEditMessage(VK_RETURN)) 
	{
		EndDialog(IDOK); 
	}

} 
//重载退出键
void CPMAgentManageDlg::OnCancel() 
{ 
	if(!IsTreeCtrlEditMessage(VK_ESCAPE)) 
	{
		EndDialog(IDCANCEL); 
	}
}   

//修改xml文件 
BOOL CPMAgentManageDlg::ReviseXml(string sBeginText,string sEndText)
{
	HTREEITEM hParent = m_TreeCtrl.GetParentItem(m_TreeCtrl.GetSelectedItem());
	string sText = m_TreeCtrl.GetItemText(hParent);
	string sIDText = m_TreeCtrl.GetItemText(m_TreeCtrl.GetSelectedItem());
	if ( sText == "矩阵信息" )
	{
		//改id 先从string sBeginText中获取出原来的id
		string sBeginID = CollectID(sBeginText);
		BOOL nRet = gReviseXml.ReviseSaveXml(TRUE,sBeginID,sBeginText,sEndText);
		if (nRet ==FALSE)
		{
			MessageBox("输入格式有误!");
			return FALSE;
		}
		return TRUE;
	}
	else
	{
		//改其他信息,从string sText中获取出其对应的矩阵id
		string sBeginID = CollectID(sText);
		BOOL nRet = gReviseXml.ReviseSaveXml(FALSE,sBeginID,sBeginText,sEndText);
		if (nRet ==FALSE)
		{
			MessageBox("输入格式有误!");
			return FALSE;
		}
		return TRUE;
	}
}

//提文本中的id号
string CPMAgentManageDlg::CollectID(string sSelect)
{
	//当获取的是id时
	char cTemp[100],cID[100];
	int i=0,j=0;
	strcpy(cTemp,sSelect.c_str());
	for (i=0;cTemp[i]!='\0';i++)
	{
		if (cTemp[i]>='0'&&cTemp[i]<='9')
		{
			cID[j]=cTemp[i];
			j++;
			cID[j] = '\0';
		}
	}
	string sID;
	sID=cID;//获取id为stirng sID
	return sID;
}

 
  

你可能感兴趣的:(更改树控件时更新xml的类)