|
| 1 | +using LeetcodeManager.Controller; |
| 2 | +using LeetcodeManager.Entity; |
| 3 | +using LeetcodeManager.Lib; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.ComponentModel; |
| 7 | +using System.Data; |
| 8 | +using System.Drawing; |
| 9 | +using System.Linq; |
| 10 | +using System.Text; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using System.Windows.Forms; |
| 13 | + |
| 14 | +namespace LeetcodeManager.View |
| 15 | +{ |
| 16 | + public partial class MasterForm : Form |
| 17 | + { |
| 18 | + private readonly ProblemInputController _problemController; |
| 19 | + private readonly TagInputController _tagController; |
| 20 | + public MasterForm() |
| 21 | + { |
| 22 | + InitializeComponent(); |
| 23 | + _problemController = new ProblemInputController(); |
| 24 | + _tagController = new TagInputController(); |
| 25 | + } |
| 26 | + |
| 27 | + protected override void OnLoad(EventArgs e) |
| 28 | + { |
| 29 | + base.OnLoad(e); |
| 30 | + //binding to tree |
| 31 | + var tags = _tagController.GetAllTags(); |
| 32 | + TreeNode root = treeViewTag.Nodes[0]; |
| 33 | + if (tags == null) return; |
| 34 | + foreach (var tag in tags) |
| 35 | + { |
| 36 | + TreeNode node = new TreeNode(tag.Name) { Tag = tag }; |
| 37 | + root.Nodes.Add(node); |
| 38 | + } |
| 39 | + treeViewTag.ExpandAll(); |
| 40 | + } |
| 41 | + |
| 42 | + protected override void OnClosing(CancelEventArgs e) |
| 43 | + { |
| 44 | + base.OnClosing(e); |
| 45 | + //MyDb: dispose |
| 46 | + } |
| 47 | + |
| 48 | + private void newTagToolStripMenuItem_Click(object sender, EventArgs e) |
| 49 | + { |
| 50 | + var input = new TagInputForm(); |
| 51 | + if (input.ShowDialog() != DialogResult.OK) |
| 52 | + return; |
| 53 | + var tag = new Tag() { Name = input.InputTag }; |
| 54 | + var tagdb = _tagController.GetATagByName(input.InputTag); |
| 55 | + if (!_tagController.IsNew(tagdb)) |
| 56 | + { |
| 57 | + SysHelper.ShowMessageWarning("Existed tag with this name, create failure!"); |
| 58 | + return; |
| 59 | + } |
| 60 | + tagdb =_tagController.SaveTagToDb(tag); |
| 61 | + treeViewTag.Nodes[0].Nodes.Add(new TreeNode(input.InputTag) { Tag = tagdb }); |
| 62 | + treeViewTag.ExpandAll(); |
| 63 | + } |
| 64 | + |
| 65 | + private void updateTagToolStripMenuItem_Click(object sender, EventArgs e) |
| 66 | + { |
| 67 | + if (treeViewTag.SelectedNode.Name == "rootNode") |
| 68 | + { |
| 69 | + MessageBox.Show("Root name cannot be revised!"); |
| 70 | + return; |
| 71 | + } |
| 72 | + var tagdel = treeViewTag.SelectedNode.Tag as Tag; |
| 73 | + if (tagdel == null) return; |
| 74 | + var rettag = _tagController.GetATagById(tagdel.TagId); |
| 75 | + if (rettag == null) |
| 76 | + { |
| 77 | + SysHelper.ShowMessageWarning("Unable to retrive from db!"); |
| 78 | + return; |
| 79 | + } |
| 80 | + var input = new TagInputForm(tagdel.Name); |
| 81 | + if (input.ShowDialog() != DialogResult.OK) |
| 82 | + return; |
| 83 | + rettag.Name = input.InputTag; |
| 84 | + _tagController.UpdateTagName(rettag); |
| 85 | + treeViewTag.SelectedNode.Text = input.InputTag; |
| 86 | + treeViewTag.ExpandAll(); |
| 87 | + } |
| 88 | + |
| 89 | + private void deleteTagToolStripMenuItem_Click(object sender, EventArgs e) |
| 90 | + { |
| 91 | + if (treeViewTag.SelectedNode.Name == "rootNode") |
| 92 | + { |
| 93 | + MessageBox.Show("Root of tree cannot be deleted!"); |
| 94 | + return; |
| 95 | + } |
| 96 | + try |
| 97 | + { |
| 98 | + var tagdel = treeViewTag.SelectedNode.Tag as Tag; |
| 99 | + if (tagdel == null) return; |
| 100 | + var rettag = _tagController.GetATagById(tagdel.TagId); |
| 101 | + if (rettag == null) |
| 102 | + { |
| 103 | + SysHelper.ShowMessageWarning("Unable to retrive from db!"); |
| 104 | + return; |
| 105 | + } |
| 106 | + _tagController.DeleteTag(rettag); |
| 107 | + treeViewTag.SelectedNode.Remove(); |
| 108 | + SysHelper.ShowMessageOK("deletion success"); |
| 109 | + } |
| 110 | + catch (Exception ex) |
| 111 | + { |
| 112 | + MessageBox.Show("deletion failure"); |
| 113 | + } |
| 114 | + treeViewTag.ExpandAll(); |
| 115 | + } |
| 116 | + |
| 117 | + private void treeViewTag_AfterSelect(object sender, TreeViewEventArgs e) |
| 118 | + { |
| 119 | + Tag tag = e.Node.Tag as Tag; |
| 120 | + if (tag == null) |
| 121 | + return; |
| 122 | + if (SysHelper.CollectionNullOrEmpty<Problem>(tag.Problems)) |
| 123 | + { |
| 124 | + problemBindingSource.DataSource = tag.Problems; |
| 125 | + return; |
| 126 | + } |
| 127 | + problemBindingSource.DataSource = tag.Problems; |
| 128 | + } |
| 129 | + |
| 130 | + //save question |
| 131 | + private void problemBindingNavigatorSaveItem_Click(object sender, EventArgs e) |
| 132 | + { |
| 133 | + this.Validate(); |
| 134 | + foreach (DataGridViewRow row in problemDataGridView.Rows) |
| 135 | + { |
| 136 | + Problem obj = row.DataBoundItem as Problem; |
| 137 | + if (obj == null) continue; |
| 138 | + _problemController.SaveProblem(obj); |
| 139 | + } |
| 140 | + _problemController.UpdateProblems(); //inlcude delete(this is the ability EF provides) |
| 141 | + problemDataGridView.Refresh(); |
| 142 | + } |
| 143 | + |
| 144 | + private void toolStripButtonDelAll_Click(object sender, EventArgs e) |
| 145 | + { |
| 146 | + this.Validate(); |
| 147 | + if (DialogResult.No == SysHelper.ShowMessageYesOrNo("Are you sure to delete all problems in this grid?")) |
| 148 | + return; |
| 149 | + problemBindingSource.DataSource = new List<Problem>(); |
| 150 | + } |
| 151 | + |
| 152 | + private void deleteALLLocalDataToolStripMenuItem_Click(object sender, EventArgs e) |
| 153 | + { |
| 154 | + if (DialogResult.No == MessageBox.Show("Are you sure to delete all data in database?", "LeetCodeManager", MessageBoxButtons.YesNo)) |
| 155 | + return; |
| 156 | + IList<Problem> deletes = new List<Problem>(); |
| 157 | + foreach(DataGridViewRow row in problemDataGridView.Rows) |
| 158 | + { |
| 159 | + var problem = row.DataBoundItem as Problem; |
| 160 | + if(problem!=null && !_problemController.IsNew(problem)) |
| 161 | + deletes.Add(problem); |
| 162 | + } |
| 163 | + _problemController.DeleteProblems(deletes); |
| 164 | + } |
| 165 | + |
| 166 | + private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) |
| 167 | + { |
| 168 | + ProblemInputForm problemInput = new ProblemInputForm(); |
| 169 | + if (problemInput.ShowDialog() == DialogResult.Cancel) |
| 170 | + return; |
| 171 | + Problem newproblem = problemInput.InputProblem; |
| 172 | + int cnt = problemDataGridView.Rows.Count; |
| 173 | + DataGridViewRow fillrow = problemDataGridView.Rows[cnt - 2]; |
| 174 | + fillrow.Cells[1].Value = newproblem.Title; |
| 175 | + fillrow.Cells[2].Value = newproblem.LtUrl; |
| 176 | + fillrow.Cells[3].Value = newproblem.CsdnAddress; |
| 177 | + fillrow.Cells[4].Value = newproblem.Content; |
| 178 | + fillrow.Cells[5].Value = newproblem.Tags; |
| 179 | + } |
| 180 | + |
| 181 | + private void toolStripButtonEdit_Click(object sender, EventArgs e) |
| 182 | + { |
| 183 | + var selectrows = problemDataGridView.SelectedRows; |
| 184 | + if(selectrows==null || selectrows.Count==0) |
| 185 | + { |
| 186 | + SysHelper.ShowMessageWarning("unselect any row, please select at least one row!"); |
| 187 | + return; |
| 188 | + } |
| 189 | + foreach(DataGridViewRow row in selectrows) |
| 190 | + { |
| 191 | + var frm = new ProblemInputForm(row.DataBoundItem as Problem); |
| 192 | + if (frm.ShowDialog() == DialogResult.Cancel) |
| 193 | + continue; |
| 194 | + Problem newproblem = frm.InputProblem; |
| 195 | + row.Cells[1].Value = newproblem.Title; |
| 196 | + row.Cells[2].Value = newproblem.LtUrl; |
| 197 | + row.Cells[3].Value = newproblem.CsdnAddress; |
| 198 | + row.Cells[4].Value = newproblem.Content; |
| 199 | + row.Cells[5].Value = newproblem.Tags; |
| 200 | + } |
| 201 | + _problemController.UpdateProblems(); |
| 202 | + } |
| 203 | + |
| 204 | + } |
| 205 | +} |
0 commit comments