From b9324f86194b33b4419815da1a45beccf70c1cc4 Mon Sep 17 00:00:00 2001 From: Lukasz Baldyga Date: Fri, 23 Oct 2020 09:05:49 +0100 Subject: [PATCH] TextEditor now saves properly --- .../TextEditor.Designer.cs | 44 +++++- .../TextEditor.cs | 126 ++++++++++++++---- .../TextEditor.resx | 3 + 3 files changed, 148 insertions(+), 25 deletions(-) diff --git a/Advanced Software Engineering/Advanced Software Engineering/TextEditor.Designer.cs b/Advanced Software Engineering/Advanced Software Engineering/TextEditor.Designer.cs index 2441082..dbf1ad9 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/TextEditor.Designer.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/TextEditor.Designer.cs @@ -33,17 +33,22 @@ namespace Advanced_Software_Engineering /// private void InitializeComponent() { + this.components = new System.ComponentModel.Container(); this.textBox1 = new System.Windows.Forms.TextBox(); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.newFileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.saveToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.saveAsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.runToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.checkSyntaxToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.helpToolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem(); + this.label1 = new System.Windows.Forms.Label(); + this.timer1 = new System.Windows.Forms.Timer(this.components); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // @@ -58,15 +63,18 @@ namespace Advanced_Software_Engineering this.textBox1.Multiline = true; this.textBox1.Name = "textBox1"; this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both; - this.textBox1.Size = new System.Drawing.Size(602, 524); + this.textBox1.Size = new System.Drawing.Size(602, 509); this.textBox1.TabIndex = 0; this.textBox1.WordWrap = false; + this.textBox1.Click += new System.EventHandler(this.textBox1_Click); + this.textBox1.TextChanged += new System.EventHandler(this.handleKeypress); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem, this.runToolStripMenuItem, + this.checkSyntaxToolStripMenuItem, this.helpToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; @@ -80,6 +88,7 @@ namespace Advanced_Software_Engineering this.newFileToolStripMenuItem, this.openToolStripMenuItem, this.saveToolStripMenuItem, + this.saveAsToolStripMenuItem, this.exitToolStripMenuItem}); this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20); @@ -104,6 +113,14 @@ namespace Advanced_Software_Engineering this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; this.saveToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.saveToolStripMenuItem.Text = "Save"; + this.saveToolStripMenuItem.Click += new System.EventHandler(this.saveToolStripMenuItem_Click); + // + // saveAsToolStripMenuItem + // + this.saveAsToolStripMenuItem.Name = "saveAsToolStripMenuItem"; + this.saveAsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.saveAsToolStripMenuItem.Text = "Save As"; + this.saveAsToolStripMenuItem.Click += new System.EventHandler(this.saveAsToolStripMenuItem_Click); // // exitToolStripMenuItem // @@ -119,6 +136,12 @@ namespace Advanced_Software_Engineering this.runToolStripMenuItem.Text = "Run"; this.runToolStripMenuItem.Click += new System.EventHandler(this.runToolStripMenuItem_Click); // + // checkSyntaxToolStripMenuItem + // + this.checkSyntaxToolStripMenuItem.Name = "checkSyntaxToolStripMenuItem"; + this.checkSyntaxToolStripMenuItem.Size = new System.Drawing.Size(90, 20); + this.checkSyntaxToolStripMenuItem.Text = "Check Syntax"; + // // helpToolStripMenuItem // this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { @@ -141,16 +164,31 @@ namespace Advanced_Software_Engineering this.helpToolStripMenuItem1.Size = new System.Drawing.Size(107, 22); this.helpToolStripMenuItem1.Text = "Help"; // + // label1 + // + this.label1.AutoSize = true; + this.label1.Dock = System.Windows.Forms.DockStyle.Bottom; + this.label1.Location = new System.Drawing.Point(0, 535); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(0, 13); + this.label1.TabIndex = 2; + // + // timer1 + // + this.timer1.Interval = 1000; + // // Text_Editor // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(602, 548); + this.Controls.Add(this.label1); this.Controls.Add(this.textBox1); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "Text_Editor"; this.Text = "Advanced Software Engineering - Text Editor"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Text_Editor_FormClosing); this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Console_FormClosed); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); @@ -172,5 +210,9 @@ namespace Advanced_Software_Engineering private ToolStripMenuItem helpToolStripMenuItem; private ToolStripMenuItem aboutToolStripMenuItem; private ToolStripMenuItem helpToolStripMenuItem1; + private ToolStripMenuItem saveAsToolStripMenuItem; + private ToolStripMenuItem checkSyntaxToolStripMenuItem; + private Label label1; + private Timer timer1; } } \ No newline at end of file diff --git a/Advanced Software Engineering/Advanced Software Engineering/TextEditor.cs b/Advanced Software Engineering/Advanced Software Engineering/TextEditor.cs index 44969bc..0c0da47 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/TextEditor.cs +++ b/Advanced Software Engineering/Advanced Software Engineering/TextEditor.cs @@ -26,6 +26,7 @@ namespace Advanced_Software_Engineering { public Text_Editor() { SettingsAndHelperFunctions.NumberOfWindows++; InitializeComponent(); + UpdateTitle(); } public Text_Editor(OpenFileDialog openFile) { @@ -42,30 +43,106 @@ namespace Advanced_Software_Engineering { } void UpdateTitle() { - this.Text = DefaultTitleString + " - " + fileName; + if (fileName != "") this.Text = DefaultTitleString + " - " + fileName; + else this.Text = DefaultTitleString; if (unsavedChanges) this.Text = "* " + this.Text + " *"; } + private void saveFile() { + if (!okToOverwrite) saveFileAs(); + else { + StreamWriter fileStream = new StreamWriter(fileName); + try { + fileStream.Write(textBox1.Text); + unsavedChanges = false; + } catch (Exception e) { + new ErrorWindow("Write Failed!", "The program failed to write the file.", e.Message + "\n" + e.StackTrace, ErrorWindow.ERROR_MESSAGE).Show(); + } finally { + fileStream.Close(); + UpdateTitle(); + } + } + } + + private void saveFileAs() { + SaveFileDialog saveFileDialog = new SaveFileDialog(); + saveFileDialog.OverwritePrompt = true; + saveFileDialog.DefaultExt = ".txt"; + if (fileName != "") saveFileDialog.FileName = fileName; + saveFileDialog.ValidateNames = true; + + DialogResult dialogResult = saveFileDialog.ShowDialog(); + if (dialogResult == DialogResult.OK) { + StreamWriter fileStream = new StreamWriter(saveFileDialog.OpenFile()); + try { + fileStream.Write(textBox1.Text); + unsavedChanges = false; + okToOverwrite = true; + fileName = saveFileDialog.FileName; + } catch (Exception e) { + new ErrorWindow("Write Failed!", "The program failed to write the file.", e.Message + "\n" + e.StackTrace, ErrorWindow.ERROR_MESSAGE).Show(); + } finally { + fileStream.Close(); + UpdateTitle(); + } + } + } + + private void updateRowCol() { + int line = textBox1.GetLineFromCharIndex(textBox1.SelectionStart); + int column = textBox1.SelectionStart - textBox1.GetFirstCharIndexFromLine(line); + label1.Text = "row: " + line.ToString() + " col: " + column.ToString(); + } + + private void handleKeypress() { + updateSaveStatus(); + updateRowCol(); + } + + private void updateSaveStatus() { + if (!unsavedChanges) { + unsavedChanges = true; + UpdateTitle(); + } + } + + private void handleExit(FormClosingEventArgs e) { + if (unsavedChanges) { + DialogResult dialogResult = MessageBox.Show("You have unsaved work. Would you like to discard your work?", "Unsaved work!", MessageBoxButtons.YesNo, MessageBoxIcon.None); + if (e != null) e.Cancel = dialogResult != DialogResult.Yes; + else Close(); + } else Close(); + } + + private void openToolStripMenuItem_Click(object sender, EventArgs e) { + DialogResult result = DialogResult.Yes; + + if (unsavedChanges) { + result = MessageBox.Show("You have unsaved work. Would you like to discard your work and open a new file?", "Unsaved work!", MessageBoxButtons.YesNo, MessageBoxIcon.None); + } + + if (result == DialogResult.Yes) { + OpenFileDialog openFileDialog = new OpenFileDialog(); + + if (openFileDialog.ShowDialog() == DialogResult.OK) { + + if (newWindow) { + StreamReader fileStream = new StreamReader(openFileDialog.OpenFile()); + textBox1.Text = fileStream.ReadToEnd(); + UpdateTitle(openFileDialog.FileName); + } else new Text_Editor(openFileDialog).Show(); + } + } + } + private void Console_FormClosed(object sender, FormClosedEventArgs e) { if (DisplayForm != null && !DisplayForm.IsDisposed) DisplayForm.ReleaseCommandLock(); SettingsAndHelperFunctions.WindowClosed(); Dispose(); } - private void newFileToolStripMenuItem_Click(object sender, EventArgs e) { - new Text_Editor().Show(); - } - - private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { - new About_Window().Show(); - } - - private void exitToolStripMenuItem_Click(object sender, EventArgs e) { - Close(); - } - private void runToolStripMenuItem_Click(object sender, EventArgs e) { - if(DisplayForm == null || DisplayForm.IsDisposed) { + if (DisplayForm == null || DisplayForm.IsDisposed) { DisplayForm = new Draw_Preview(textBox1.Text); } else { Console.WriteLine("Removed all commands"); @@ -76,18 +153,19 @@ namespace Advanced_Software_Engineering { DisplayForm.Focus(); } - private void openToolStripMenuItem_Click(object sender, EventArgs e) { - OpenFileDialog openFileDialog = new OpenFileDialog(); + private void handleKeypress(object sender, EventArgs e) => handleKeypress(); - if (openFileDialog.ShowDialog() == DialogResult.OK) { + private void textBox1_Click(object sender, EventArgs e) => updateRowCol(); - if (newWindow) { - StreamReader fileStream = new StreamReader(openFileDialog.OpenFile()); - textBox1.Text = fileStream.ReadToEnd(); - UpdateTitle(openFileDialog.FileName); - } else new Text_Editor(openFileDialog).Show(); - } + private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) => saveFileAs(); + private void saveToolStripMenuItem_Click(object sender, EventArgs e) => saveFile(); + + private void Text_Editor_FormClosing(object sender, FormClosingEventArgs e) => handleExit(e); + + private void newFileToolStripMenuItem_Click(object sender, EventArgs e) => new Text_Editor().Show(); + private void aboutToolStripMenuItem_Click(object sender, EventArgs e) => new About_Window().Show(); + + private void exitToolStripMenuItem_Click(object sender, EventArgs e) => handleExit(null); - } } } diff --git a/Advanced Software Engineering/Advanced Software Engineering/TextEditor.resx b/Advanced Software Engineering/Advanced Software Engineering/TextEditor.resx index 3d0e18f..a18e0ed 100644 --- a/Advanced Software Engineering/Advanced Software Engineering/TextEditor.resx +++ b/Advanced Software Engineering/Advanced Software Engineering/TextEditor.resx @@ -120,6 +120,9 @@ 17, 17 + + 132, 17 + 59