SaveAsDialog.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. using PaintDotNet.Base.CommTool;
  12. using PaintDotNet.Base.SettingModel;
  13. namespace PaintDotNet.ImageCollect.ListOfLocation
  14. {
  15. public delegate void SaveHandler(string fileName);
  16. public class SaveAsDialog : Form
  17. {
  18. #region
  19. /// <summary>
  20. /// Required designer variable.
  21. /// </summary>
  22. private System.ComponentModel.IContainer components = null;
  23. /// <summary>
  24. /// Clean up any resources being used.
  25. /// </summary>
  26. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  27. protected override void Dispose(bool disposing)
  28. {
  29. if (disposing && (components != null))
  30. {
  31. components.Dispose();
  32. }
  33. base.Dispose(disposing);
  34. }
  35. #region Windows Form Designer generated code
  36. /// <summary>
  37. /// Required method for Designer support - do not modify
  38. /// the contents of this method with the code editor.
  39. /// </summary>
  40. private void InitializeComponent()
  41. {
  42. this.label1 = new System.Windows.Forms.Label();
  43. this.txtName = new System.Windows.Forms.TextBox();
  44. this.btnSave = new System.Windows.Forms.Button();
  45. this.SuspendLayout();
  46. //
  47. // label1
  48. //
  49. this.label1.AutoSize = true;
  50. this.label1.Location = new System.Drawing.Point(61, 82);
  51. this.label1.Name = "label1";
  52. this.label1.Size = new System.Drawing.Size(67, 15);
  53. this.label1.TabIndex = 0;
  54. //
  55. // txtName
  56. //
  57. this.txtName.Location = new System.Drawing.Point(143, 79);
  58. this.txtName.MaxLength = 20;
  59. this.txtName.Name = "txtName";
  60. this.txtName.Size = new System.Drawing.Size(259, 25);
  61. this.txtName.TabIndex = 1;
  62. //
  63. // btnSave
  64. //
  65. this.btnSave.Location = new System.Drawing.Point(195, 163);
  66. this.btnSave.Name = "btnSave";
  67. this.btnSave.Size = new System.Drawing.Size(88, 35);
  68. this.btnSave.TabIndex = 2;
  69. this.btnSave.UseVisualStyleBackColor = true;
  70. this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
  71. //
  72. // SaveAsDialog
  73. //
  74. this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
  75. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  76. this.ClientSize = new System.Drawing.Size(482, 269);
  77. this.Controls.Add(this.btnSave);
  78. this.Controls.Add(this.txtName);
  79. this.Controls.Add(this.label1);
  80. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
  81. this.MaximizeBox = false;
  82. this.MinimizeBox = false;
  83. this.Name = "SaveAsDialog";
  84. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  85. this.ResumeLayout(false);
  86. this.PerformLayout();
  87. }
  88. #endregion
  89. private System.Windows.Forms.Label label1;
  90. private System.Windows.Forms.TextBox txtName;
  91. private System.Windows.Forms.Button btnSave;
  92. #endregion
  93. public event SaveHandler Saved;
  94. private LocationModel m_locationModel;
  95. private List<LocationModel.Item> m_items = new List<LocationModel.Item>();
  96. public SaveAsDialog(List<LocationModel.Item> items)
  97. {
  98. m_locationModel = new LocationModel();
  99. m_items = items;
  100. InitializeComponent();
  101. this.label1.Text = PdnResources.GetString("Menu.Saveaas.Text") + ":";
  102. this.btnSave.Text = PdnResources.GetString("Menu.File.Save.Text");
  103. this.Text = PdnResources.GetString("Menu.File.Save.Text");
  104. }
  105. private void btnSave_Click(object sender, EventArgs e)
  106. {
  107. if(txtName.Text.Trim().Length == 0)
  108. {
  109. MessageBox.Show(PdnResources.GetString("Menu.Savenamecannobeempty.Text"));
  110. return;
  111. }
  112. string name = txtName.Text.Trim();
  113. string filePath = Application.StartupPath + "\\Config\\" + Startup.instance.SettingPrefix + "\\Location\\";
  114. if (!Directory.Exists(filePath))
  115. {
  116. Directory.CreateDirectory(filePath);
  117. }
  118. else
  119. {
  120. List<string> files = FileOperationHelper.GetFileList(filePath);
  121. foreach (string fileName in files)
  122. {
  123. if (fileName.Equals(name) || fileName.Equals(name + ".xml"))
  124. {
  125. MessageBox.Show(PdnResources.GetString("Menu.Namealreadyexists.text"));
  126. return;
  127. }
  128. }
  129. }
  130. m_locationModel.items = m_items;
  131. string stageModelXml = XmlSerializeHelper.XmlSerialize<LocationModel>(m_locationModel);
  132. filePath = filePath + name + ".xml";
  133. if (!FileOperationHelper.WriteStringToFile(stageModelXml, filePath, FileMode.Create))
  134. {
  135. MessageBox.Show(PdnResources.GetString("Menu.Savefailed.text"));
  136. }
  137. else
  138. {
  139. if(Saved != null)
  140. {
  141. Saved(name);
  142. }
  143. }
  144. this.Close();
  145. }
  146. }
  147. }