DataManagerPanel.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using CommonAI.Zone;
  10. using CommonFroms.Utils;
  11. using CommonLang.Concurrent;
  12. using System.IO;
  13. using CommonLang.Property.Modeling;
  14. using CommonAI.Zone.ZoneEditor;
  15. using CommonLang;
  16. using CommonFroms.G2D;
  17. using CommonFroms.G2D.DataGrid;
  18. namespace CommonAIEditor
  19. {
  20. public interface IDataManager
  21. {
  22. object SelectedData { get; }
  23. TreeNode TreeRoot { get; }
  24. object GetNodeData(string id);
  25. object ShowSelectTemplateDialog(object obj);
  26. bool ShowSelectTemplateIDDialog(object obj, out int id);
  27. void SaveEditorStatus();
  28. }
  29. public partial class DataManagerPanel<T> : UserControl, IDataManager
  30. where T : class, ITemplateData, new()
  31. {
  32. private Random random = new Random();
  33. private string categoryText;
  34. private Type dataType;
  35. private G2DTreeNodeRoot<T> rootNode;
  36. public G2DTreeNodeRoot<T> TreeRoot { get { return rootNode; } }
  37. public string SaveDir { get { return rootNode.Dir; } }
  38. public DataManagerPanel(
  39. string category,
  40. string dir,
  41. string set_dir,
  42. ImageList imageList,
  43. string groupImageKey,
  44. string childImageKey)
  45. {
  46. InitializeComponent();
  47. if (!Directory.Exists(dir))
  48. {
  49. Directory.CreateDirectory(dir);
  50. }
  51. if (!Directory.Exists(set_dir))
  52. {
  53. Directory.CreateDirectory(set_dir);
  54. }
  55. this.dataType = typeof(T);
  56. this.categoryText = category;
  57. this.groupBtn_AddNode.Text = ("添加 " + category);
  58. this.rootNode = new G2DTreeNodeRoot<T>(category, dir, set_dir);
  59. this.rootNode.ImageKey = groupImageKey;
  60. this.rootNode.ChildsImageKey = childImageKey;
  61. this.rootNode.ContextMenuStrip = groupMenu;
  62. this.rootNode.ChildsContextMenuStrip = childMenu;
  63. this.treeView.SelectedImageKey = groupImageKey;
  64. this.treeView.ImageKey = groupImageKey;
  65. this.treeView.ImageList = imageList;
  66. this.treeView.TreeViewNodeSorter = new G2DTreeNodeComparer();
  67. this.treeView.Nodes.Add(rootNode);
  68. this.Dock = System.Windows.Forms.DockStyle.Fill;
  69. }
  70. public int GetTryLoadCount()
  71. {
  72. return rootNode.GetTryLoadCount();
  73. }
  74. public void LoadAll(AtomicInteger progress)
  75. {
  76. this.rootNode.LoadAll(Editor.Instance.MessageFactory, progress);
  77. this.rootNode.Invoke(() =>
  78. {
  79. this.rootNode.Expand();
  80. });
  81. }
  82. public void SetEnableDataGrid(bool e)
  83. {
  84. groupBtn_Balance.Visible = e;
  85. groupBtn_EditAll.Visible = e;
  86. groupBtn_Balance.Enabled = e;
  87. groupBtn_EditAll.Enabled = e;
  88. }
  89. public TreeView GetTreeView()
  90. {
  91. return treeView;
  92. }
  93. public void AddChildMenuItem(int index, ToolStripItem append)
  94. {
  95. childMenu.Items.Insert(index, append);
  96. }
  97. public void SaveEditorStatus()
  98. {
  99. rootNode.SaveState();
  100. }
  101. public T GetNodeData(string id)
  102. {
  103. G2DTreeNode<T> cn = rootNode.FindNode(id);
  104. if (cn != null)
  105. {
  106. return cn.Data;
  107. }
  108. return default(T);
  109. }
  110. public List<T> GetAllNodeData()
  111. {
  112. List<T> ret = new List<T>();
  113. foreach (G2DTreeNode<T> cn in rootNode.GetG2DList())
  114. {
  115. ret.Add(cn.Data);
  116. }
  117. return ret;
  118. }
  119. public List<G2DTreeNode<T>> GetAllNode()
  120. {
  121. List<G2DTreeNode<T>> ret = new List<G2DTreeNode<T>>();
  122. foreach (G2DTreeNode<T> cn in rootNode.GetG2DList())
  123. {
  124. ret.Add(cn);
  125. }
  126. return ret;
  127. }
  128. virtual public G2DTreeNode<T> CreateNodeData(string id)
  129. {
  130. G2DTreeNode<T> ret = new G2DTreeNode<T>(new T());
  131. ret.SetDataID(id);
  132. return ret;
  133. }
  134. public void SaveAll(AtomicInteger progress, bool check)
  135. {
  136. foreach (var node in GetAllNode())
  137. {
  138. node.Data.EditorPath = node.FullPath;
  139. }
  140. rootNode.SaveAll(Editor.Instance.MessageFactory, progress);
  141. if (check)
  142. {
  143. foreach (var data in GetAllNodeData())
  144. {
  145. string srcxml;
  146. string retxml;
  147. if (!EditorTemplates.ValidateBin<T>(data, Editor.Instance.MessageFactory, out srcxml, out retxml))
  148. {
  149. string sfile = SaveDir + "_conflict_" + data.TemplateID + ".src.txt";
  150. string dfile = SaveDir + "_conflict_" + data.TemplateID + ".bin.txt";
  151. File.WriteAllText(sfile, srcxml, CUtils.UTF8);
  152. File.WriteAllText(dfile, retxml, CUtils.UTF8);
  153. throw new Exception(SaveDir + "/" + data.TemplateID + ".xml" + " : Save Load 二进制序列化不匹配 !" + data.GetType() +
  154. "\n比较文件已存储到: " + dfile);
  155. }
  156. }
  157. }
  158. }
  159. public void SaveNode(G2DTreeNode<T> node)
  160. {
  161. var data = node.Data;
  162. data.EditorPath = node.FullPath;
  163. rootNode.SaveOne(data, Editor.Instance.MessageFactory);
  164. }
  165. public void RefreshData()
  166. {
  167. rootNode.Refresh();
  168. treeView.Refresh();
  169. }
  170. public List<FileInfo> GetSavedFiles()
  171. {
  172. return rootNode.ListSavedFiles();
  173. }
  174. public FileInfo GetListFile()
  175. {
  176. return rootNode.GetListFile();
  177. }
  178. public FileInfo GetMd5File()
  179. {
  180. return rootNode.GetMd5File();
  181. }
  182. public T GetSelectedData()
  183. {
  184. G2DTreeNode<T> cn = GetChildMenuNode();
  185. if (cn != null)
  186. {
  187. return cn.Data;
  188. }
  189. return default(T);
  190. }
  191. public G2DTreeNode<T> GetSelectedNode()
  192. {
  193. return GetChildMenuNode();
  194. }
  195. private G2DTreeNode<T> GetChildMenuNode()
  196. {
  197. TreeNode nd = treeView.SelectedNode;
  198. if (nd is G2DTreeNode<T>)
  199. {
  200. return nd as G2DTreeNode<T>;
  201. }
  202. return null;
  203. }
  204. private G2DTreeNodeGroup GetGroupMenuNode()
  205. {
  206. TreeNode nd = treeView.SelectedNode;
  207. if (nd is G2DTreeNodeGroup)
  208. {
  209. return nd as G2DTreeNodeGroup;
  210. }
  211. return null;
  212. }
  213. public T ShowSelectTemplateDialog(object obj)
  214. {
  215. G2DListSelectEditor<T> dialog = new G2DListSelectEditor<T>(
  216. this.TreeRoot, treeView.ImageList, obj);
  217. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  218. {
  219. return dialog.SelectedObject;
  220. }
  221. return null;
  222. }
  223. public bool ShowSelectTemplateIDDialog(object obj, out int id)
  224. {
  225. G2DListSelectEditor<T> dialog = new G2DListSelectEditor<T>(
  226. this.TreeRoot, treeView.ImageList, obj);
  227. if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  228. {
  229. if (dialog.SelectedObject != null)
  230. {
  231. id = dialog.SelectedObject.TemplateID;
  232. return true;
  233. }
  234. }
  235. id = 0;
  236. return false;
  237. }
  238. // ---------------------------------------------------------------------------------------
  239. #region EventHandlers
  240. private void groupBtn_AddNode_Click(object sender, EventArgs e)
  241. {
  242. G2DTreeNodeGroup parent = GetGroupMenuNode();
  243. if (parent != null)
  244. {
  245. string id = G2DTextDialog.Show(random.Next() + "", "添加" + categoryText);
  246. if (id != null)
  247. {
  248. try
  249. {
  250. G2DTreeNode<T> tn = CreateNodeData(id);
  251. if (rootNode.AddG2DNode(tn, parent))
  252. {
  253. parent.Expand();
  254. treeView.SelectedNode = tn;
  255. treeView.Refresh();
  256. }
  257. else
  258. {
  259. MessageBox.Show("无法添加");
  260. }
  261. }
  262. catch (Exception err)
  263. {
  264. MessageBox.Show(err.Message);
  265. }
  266. }
  267. }
  268. }
  269. private void groupBtn_AddGroup_Click(object sender, EventArgs e)
  270. {
  271. G2DTreeNodeGroup parent = GetGroupMenuNode();
  272. if (parent != null)
  273. {
  274. string gname = G2DTextDialog.Show("分组");
  275. if (gname != null)
  276. {
  277. G2DTreeNodeGroup group = parent.AddG2DGroup(gname);
  278. parent.Expand();
  279. treeView.SelectedNode = group;
  280. }
  281. }
  282. }
  283. private void groupBtn_Rename_Click(object sender, EventArgs e)
  284. {
  285. G2DTreeNodeGroup parent = GetGroupMenuNode();
  286. if (parent != null)
  287. {
  288. string gname = G2DTextDialog.Show(parent.Text, "重命名");
  289. if (gname != null)
  290. {
  291. parent.Text = gname;
  292. treeView.Refresh();
  293. }
  294. }
  295. }
  296. private void groupBtn_EditAll_Click(object sender, EventArgs e)
  297. {
  298. G2DTreeNodeGroup parent = GetGroupMenuNode();
  299. if (parent != null)
  300. {
  301. List<object> datas = new List<object>();
  302. foreach (TreeNode tn in parent.Nodes)
  303. {
  304. if (tn is G2DTreeNode<T>)
  305. {
  306. datas.Add((tn as G2DTreeNode<T>).Data);
  307. }
  308. }
  309. try
  310. {
  311. G2DDataGridEditor grid_editor = new G2DDataGridEditor(datas);
  312. grid_editor.ShowDialog();
  313. if (grid_editor.IsDataChanged)
  314. {
  315. RefreshData();
  316. }
  317. }
  318. catch (Exception err)
  319. {
  320. MessageBox.Show(err.Message);
  321. }
  322. }
  323. }
  324. private void groupBtn_Balance_Click(object sender, EventArgs e)
  325. {
  326. G2DTreeNodeGroup parent = GetGroupMenuNode();
  327. if (parent != null)
  328. {
  329. StringBuilder sb = new StringBuilder();
  330. List<object> datas = new List<object>();
  331. foreach (TreeNode tn in parent.Nodes)
  332. {
  333. if (tn is G2DTreeNode<T>)
  334. {
  335. T data = (tn as G2DTreeNode<T>).Data;
  336. datas.Add(data);
  337. sb.AppendLine("[" + data.ToString() + "]");
  338. }
  339. }
  340. if (MessageBox.Show("确定要配平数据?\n" + sb.ToString(), "集合内所有数据结构将会一致!") == DialogResult.OK)
  341. {
  342. sb.Clear();
  343. int count = 0;
  344. foreach (object src in datas)
  345. {
  346. foreach (object dst in datas)
  347. {
  348. try
  349. {
  350. if (UmlUtils.StructEquationBalancer(src, dst))
  351. {
  352. sb.AppendLine(src + " -> " + dst);
  353. count++;
  354. }
  355. }
  356. catch (Exception err)
  357. {
  358. MessageBox.Show(err.Message);
  359. }
  360. }
  361. }
  362. if (count > 0)
  363. {
  364. MessageBox.Show(string.Format("{0}个数据已配平!\n{1}", count, sb.ToString()));
  365. }
  366. }
  367. }
  368. }
  369. private void childBtn_SetID_Click(object sender, EventArgs e)
  370. {
  371. G2DTreeNode<T> node = GetChildMenuNode();
  372. if (node != null)
  373. {
  374. string id = G2DTextDialog.Show(node.DataID, node.Text);
  375. if (id != null)
  376. {
  377. try
  378. {
  379. if (rootNode.SetG2DNodeID(node, id))
  380. {
  381. //rootNode.Refresh();
  382. node.Refresh();
  383. }
  384. else
  385. {
  386. MessageBox.Show("无法重设ID,有冲突!");
  387. }
  388. }
  389. catch (Exception err)
  390. {
  391. MessageBox.Show(err.Message);
  392. }
  393. }
  394. }
  395. }
  396. private void childBtn_Duplicate_Click(object sender, EventArgs e)
  397. {
  398. G2DTreeNode<T> node = GetChildMenuNode();
  399. if (node != null)
  400. {
  401. string newname = random.Next() + "";
  402. while (newname != null)
  403. {
  404. newname = G2DTextDialog.Show(newname, "复制");
  405. if (newname != null)
  406. {
  407. if (rootNode.ContainsG2DNodeID(newname))
  408. {
  409. MessageBox.Show(newname + " 已存在!");
  410. }
  411. else
  412. {
  413. try
  414. {
  415. G2DTreeNode<T> newnode = node.Clone(newname);
  416. rootNode.AddG2DNode(newnode, node.Parent);
  417. return;
  418. }
  419. catch (Exception err)
  420. {
  421. MessageBox.Show(err.Message);
  422. }
  423. }
  424. }
  425. }
  426. }
  427. }
  428. private void childBtn_Delete_Click(object sender, EventArgs e)
  429. {
  430. G2DTreeNode<T> node = GetChildMenuNode();
  431. if (node != null)
  432. {
  433. DialogResult res = MessageBox.Show(
  434. "确认删除: " + node.Text,
  435. "确认",
  436. MessageBoxButtons.OKCancel);
  437. if (res == DialogResult.OK)
  438. {
  439. node.RemoveFromParent();
  440. }
  441. }
  442. }
  443. private void childBtn_EditSingle_Click(object sender, EventArgs e)
  444. {
  445. G2DTreeNode<T> node = GetChildMenuNode();
  446. if (node != null)
  447. {
  448. List<object> datas = new List<object>();
  449. datas.Add(node.Data);
  450. try
  451. {
  452. G2DDataGridEditor grid_editor = new G2DDataGridEditor(datas);
  453. grid_editor.ShowDialog();
  454. if (grid_editor.IsDataChanged)
  455. {
  456. RefreshData();
  457. }
  458. }
  459. catch (Exception err)
  460. {
  461. MessageBox.Show(err.Message);
  462. }
  463. }
  464. }
  465. private void treeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
  466. {
  467. treeView.SelectedNode = e.Node;
  468. if (e.Node is G2DTreeNode<T>)
  469. {
  470. G2DTreeNode<T> dnode = e.Node as G2DTreeNode<T>;
  471. propertyGrid.SelectedObject = new G2DPropertyDescriptor(dnode.Data);
  472. }
  473. else
  474. {
  475. propertyGrid.SelectedObject = null;
  476. }
  477. }
  478. private void treeView_ItemDrag(object sender, ItemDragEventArgs e)
  479. {
  480. treeView.DoDragDrop(e.Item, DragDropEffects.Move);
  481. }
  482. private void treeView_DragEnter(object sender, DragEventArgs e)
  483. {
  484. if (e.Data.GetDataPresent(typeof(G2DTreeNode<T>)))
  485. {
  486. e.Effect = DragDropEffects.Move;
  487. }
  488. else if (e.Data.GetDataPresent(typeof(G2DTreeNodeGroup)))
  489. {
  490. e.Effect = DragDropEffects.Move;
  491. }
  492. else
  493. {
  494. e.Effect = DragDropEffects.None;
  495. }
  496. }
  497. private void treeView_DragOver(object sender, DragEventArgs e)
  498. {
  499. Point pos = treeView.PointToClient(new Point(e.X, e.Y));
  500. TreeNode dropNode = this.treeView.GetNodeAt(pos);
  501. if (dropNode is G2DTreeNodeGroup)
  502. {
  503. e.Effect = DragDropEffects.Move;
  504. }
  505. else
  506. {
  507. e.Effect = DragDropEffects.None;
  508. }
  509. }
  510. private void treeView_DragDrop(object sender, DragEventArgs e)
  511. {
  512. Point pos = treeView.PointToClient(new Point(e.X, e.Y));
  513. TreeNode dropNode = this.treeView.GetNodeAt(pos);
  514. if (dropNode is G2DTreeNodeGroup)
  515. {
  516. G2DTreeNodeBase child_node = (G2DTreeNodeBase)e.Data.GetData(typeof(G2DTreeNode<T>));
  517. G2DTreeNodeBase group_node = (G2DTreeNodeBase)e.Data.GetData(typeof(G2DTreeNodeGroup));
  518. if (child_node == null && group_node == null)
  519. {
  520. MessageBox.Show("error");
  521. }
  522. else if (child_node != null)
  523. {
  524. child_node.RemoveFromParent();
  525. dropNode.Nodes.Add(child_node);
  526. treeView.SelectedNode = child_node;
  527. }
  528. else if (group_node != dropNode)
  529. {
  530. if (!G2DTreeNodeBase.ContainsChild(group_node, dropNode, true))
  531. {
  532. group_node.RemoveFromParent();
  533. dropNode.Nodes.Add(group_node);
  534. }
  535. }
  536. }
  537. }
  538. private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  539. {
  540. G2DTreeNode<T> tn = treeView.SelectedNode as G2DTreeNode<T>;
  541. if (tn != null)
  542. {
  543. tn.Refresh();
  544. }
  545. }
  546. #endregion
  547. object IDataManager.SelectedData
  548. {
  549. get { return this.GetSelectedData(); }
  550. }
  551. TreeNode IDataManager.TreeRoot
  552. {
  553. get { return this.TreeRoot; }
  554. }
  555. object IDataManager.GetNodeData(string id)
  556. {
  557. return this.GetNodeData(id);
  558. }
  559. object IDataManager.ShowSelectTemplateDialog(object obj)
  560. {
  561. return this.ShowSelectTemplateDialog(obj);
  562. }
  563. bool IDataManager.ShowSelectTemplateIDDialog(object obj, out int id)
  564. {
  565. return this.ShowSelectTemplateIDDialog(obj, out id);
  566. }
  567. }
  568. }