Chapter 26: Menu Arrays


As so often happens when we take a break, I got an idea about how to improve all that manual creation and searching of the insert menus . So I hurried home from lunch to share it with you.

The Pain

Even though we have improved the creation process for new menu items, it s still not perfect. Every time we create a new insert menu item, we have to deal with the code that defines the item, the code that adds the item to the menu, and the code that looks up the item. Check the highlighted items in the following code from the XMLNotepad class, for example:

 class XMLNotepad : Form { 
public TestableTextBox textbox;
private TextModel model;
private MenuItem insertPre;
private MenuItem insertSection;
private MenuItem insertUnorderedList;
private MenuItem insertOrderedList;
private MenuItem openFile;
private MenuItem saveFile;
private ModelAction enterAction;
private ModelAction shiftEnterAction;
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 == "&U") return insertUnorderedList;
if (accelerator == " & O") return insertOrderedList;
if (accelerator == "^O") return openFile;
if (accelerator == "^S") return saveFile;
return null;
}

private void initialize(TextModel model) {
...
insertSection = new NotepadMenuItem (
"Insert &Section",
new EventHandler(MenuInsertTags),
TextModel.Tags.Section);
insertPre = new NotepadMenuItem (
"Insert &Pre",
new EventHandler(MenuInsertTags),
TextModel.Tags.Pre );
insertUnorderedList = new NotepadMenuItem (
"Insert &UL",
new EventHandler(MenuInsertTags),
TextModel.Tags.UnorderedList );
insertOrderedList = new NotepadMenuItem (
"Insert & OL",
new EventHandler(MenuInsertTags),
TextModel.Tags.OrderedList );

this.Menu = new MainMenu(new MenuItem[]
{fileMenu, insertPre, insertSection, insertUnorderedList,
insertOrderedList } );
...

This is boring and repetitive. It includes duplicated code, and it causes duplication of effort. Plus, the TextModel needs some work as well:

 class TextModel { 
private ArrayList lines;
private int selectionStart;
private static string[] newParagraph = { "<P></P>" };
private static string[] paragraphSkip = { "<P>" };
private static string[] newListItem = { "<LI></LI>" };
private static string[] listItemSkip = { "<LI>" };
private static string[] emptyLine = { "" };
private static string[] emptyLineSkip = { "" };
private static string[] newSection = {"<sect1><title></title>","</sect1>" };
private static string[] sectionSkip = { "<sect1><title>" };
private static string[] newUnorderedList = {"<UL>","<LI></LI>","</UL>"};
private static string[] unorderedListSkip = { "<UL>", "<LI>" };
private static string[] newOrderedList = {" < OL > "," < LI >< /LI > "," < /OL > "}; private static string[] orderedListSkip = { " < OL > ", " < LI > " };
private static string[] newPre = { "<pre></pre>" };
private static string[] preSkip = { "<pre>" };
public enum Tags {
Pre = 1,
Section = 2,
UnorderedList = 3,
ListItem = 4,
Paragraph = 5,
OrderedList = 6
}
private static string[] InsertStrings(Tags tag) {
if (tag == Tags.Pre) return newPre;
else if (tag == Tags.Section) return newSection;
else if (tag == Tags.UnorderedList) return newUnorderedList;
else if (tag == Tags.OrderedList) return newOrderedList;
else if (tag == Tags.ListItem) return newListItem;
else return newParagraph;
}
private static string[] SkipStrings(Tags tag) {
if (tag == Tags.Pre) return preSkip;
else if (tag == Tags.Section) return sectionSkip;
else if (tag == Tags.UnorderedList) return unorderedListSkip;
else if (tag == Tags.OrderedList) return orderedListSkip;
else if (tag == Tags.ListItem) return listItemSkip;
else return paragraphSkip;
}

This, too, is definitely repetitive and somewhat troublesome . We need to do something about it.




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