The XML Notepad Form


 using System; 
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using NUnit.Framework;
using System.Collections;
using System.Text.RegularExpressions;
namespace Notepad {
class XMLNotepad : Form {
public TestableTextBox textbox;
private TextModel model;
private MenuItem insertPre;
private MenuItem insertSection;
private MenuItem openFile;
private MenuItem saveFile;
private ModelAction enterAction;
private ModelAction shiftEnterAction;
private ModelAction insertSectionAction;
private ModelAction insertPreTagAction;
private ModelAction saveAction;
private ModelAction loadAction;
private String fileName;
private Boolean displayDialog = true;
public delegate void ModelAction();
public MenuItem MenuForAccelerator(string accelerator) {
if (accelerator == "&S") return insertSection;
if (accelerator == "&P") return insertPre;
if (accelerator == "^O") return openFile;
if (accelerator == "^S") return saveFile;
return null;
}
[STAThread]
static void Main(string[] args) {
Application.Run(new XMLNotepad());
}
public XMLNotepad() {
initialize(new TextModel());
}
public XMLNotepad(TextModel model) {
initialize(model);
}
private void initialize(TextModel model) {
InitializeDelegates(model);
this.model = model;
this.Text = "XML Notepad";
MenuItem fileMenu = new MenuItem("&File");
MenuItem newFile = new MenuItem("&New");
newFile.Click += new EventHandler(MenuFileNewOnClick);
newFile.Shortcut = Shortcut.CtrlN;
fileMenu.MenuItems.Add(newFile);
openFile = new MenuItem("&Open...");
openFile.Click += new EventHandler(MenuFileOpenOnClick);
openFile.Shortcut = Shortcut.CtrlO;
fileMenu.MenuItems.Add(openFile);
saveFile = new MenuItem("&Save");
saveFile.Click += new EventHandler(MenuFileSaveOnClick);
saveFile.Shortcut = Shortcut.CtrlS;
fileMenu.MenuItems.Add(saveFile); MenuItem saveAsFile = new MenuItem("Save &As...");
saveAsFile.Click += new EventHandler(MenuFileSaveAsOnClick);
fileMenu.MenuItems.Add(saveAsFile);
insertSection = new MenuItem (
"Insert &Section",
new EventHandler(MenuInsertSection));
insertPre = new MenuItem (
"Insert &Pre",
new EventHandler(MenuInsertPre));
this.Menu = new MainMenu(new MenuItem[] {fileMenu, insertPre,
insertSection} );
this.textbox = new TestableTextBox();
this.textbox.Parent = this;
this.textbox.Dock = DockStyle.Fill;
this.textbox.BorderStyle = BorderStyle.None;
this.textbox.Multiline = true;
this.textbox.ScrollBars = ScrollBars.Both;
this.textbox.AcceptsTab = true;
this.textbox.KeyDown += new KeyEventHandler(XMLKeyDownHandler);
this.textbox.KeyPress += new KeyPressEventHandler(XMLKeyPressHandler);
this.textbox.Visible = true;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textbox});
this.Name = "XMLNotepad";
}
private void InitializeDelegates(TextModel model) {
enterAction = new ModelAction(model.Enter);
shiftEnterAction = new ModelAction(model.InsertReturn);
insertSectionAction = new ModelAction(model.InsertSectionTags);
insertPreTagAction = new ModelAction(model.InsertPreTag);
saveAction = new ModelAction(this.SaveFile);
loadAction = new ModelAction(this.LoadFile);
}
void MenuInsertSection(object obj, EventArgs ea) { CallModel(insertSectionAction);
}

void MenuInsertPre(object obj, EventArgs ea) {
CallModel(insertPreTagAction);
}
void MenuFileNewOnClick(object obj, EventArgs ea) {
}
void MenuFileSaveOnClick(object obj, EventArgs ea) {
CallModel(saveAction);
}
void MenuFileOpenOnClick(object obj, EventArgs ea) {
FileOperation(new OpenFileDialog(), loadAction);
}
void MenuFileSaveAsOnClick(object obj, EventArgs ea) {
FileOperation(new SaveFileDialog(), saveAction);
}
private void FileOperation(FileDialog dialog, ModelAction action) {
if (displayDialog) {
DialogFileAction(dialog, action);
}
else {
CallModel(action);
}
displayDialog = true;
}
private void DialogFileAction(FileDialog dialog, ModelAction action) {
dialog.Filter = "xml files (*.xml)*.xmlAll files (*.*)*.*";
dialog.FilterIndex = 2 ;
dialog.RestoreDirectory = true ;
if(dialog.ShowDialog() == DialogResult.OK) {
fileName = dialog.FileName;
CallModel(action);
}
}
void SaveFile() {
using ( StreamWriter writer = File.CreateText(fileName) ) {
model.Save(writer);
}
}
void LoadFile() {
using ( StreamReader reader = File.OpenText(fileName) ) {
model.Load(reader);
}
}
public void XMLKeyPressHandler(object objSender, KeyPressEventArgs kea) {
if ((int) kea.KeyChar == (int) Keys.Enter) {
kea.Handled = true; // this code is here to avoid putting extra enters in the window.
// if removed, when you hit enter, the new <P> line breaks in two:
// <P>
// </P> like that.
}
}

private void CallModel(ModelAction modelAction) {
GetText();
modelAction();
PutText(textbox, model.LinesArray(), model.SelectionStart);
}
public void XMLKeyDownHandler(object objSender, KeyEventArgs kea) {
if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.None) {
CallModel(enterAction);
kea.Handled = true;
}
else if (kea.KeyCode == Keys.Enter && kea.Modifiers == Keys.Shift) {
CallModel(shiftEnterAction);
kea.Handled = true;
}
}
internal void PutText(ITestTextBox textbox, string[] lines,
int selectionStart) {
// this is Feature Envy big time.
textbox.Lines = lines;
textbox.SelectionStart = selectionStart;
textbox.ScrollToCaret();
}
private void GetText() {
model.SetLines(textbox.Lines);
model.SelectionStart = textbox.SelectionStart;
}
internal void CustomerTestPutText() {
PutText(textbox, model.LinesArray(), model.SelectionStart);
}
internal void SetFileName(String name) {
fileName = name;
}
internal void SetNoFileDialog() {
displayDialog = false;
}
}
}



Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net