Menu Array


My first move will be to replace the scalar NotepadMenuItem variables with a single array. I ll do that in small steps so as not to go wrong. First I ll put the menu items into an array and make the MenuForAccelerator() method use the array. Then I ll eliminate the scalars. Just for convenience, I think I ll start with an ArrayList, even though I m not sure if I can use it in the menu creation. The ArrayList will make for cleaner code ”at least I think it will:

 class XMLNotepad : Form { 
public TestableTextBox textbox;
private TextModel model;
private MenuItem insertPre;
private MenuItem insertSection;
private MenuItem insertUnorderedList;
private MenuItem insertOrderedList;
private ArrayList insertMenus;
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 == "^O") return openFile;
if (accelerator == "^S") return saveFile;
foreach( NotepadMenuItem m in insertMenus) { if (m.Text.IndexOf(accelerator) > = 0)
return m;
}
return null;
}
private void initialize(TextModel model) {
...
insertMenus = new ArrayList();
insertSection = new NotepadMenuItem (
"Insert &Section",
new EventHandler(MenuInsertTags),
TextModel.Tags.Section);
insertMenus.Add(insertSection);
insertPre = new NotepadMenuItem (
"Insert &Pre",
new EventHandler(MenuInsertTags),
TextModel.Tags.Pre );
insertMenus.Add(insertPre);
insertUnorderedList = new NotepadMenuItem (
"Insert &UL",
new EventHandler(MenuInsertTags),
TextModel.Tags.UnorderedList );
insertMenus.Add(insertUnorderedList);
insertOrderedList = new NotepadMenuItem (
"Insert &OL",
new EventHandler(MenuInsertTags),
TextModel.Tags.OrderedList );
insertMenus.Add(insertOrderedList);
this.Menu = new MainMenu(new MenuItem[]
{fileMenu, insertPre, insertSection, insertUnorderedList,
insertOrderedList} );

There s no complaining about this. The code is pretty clear, and it works the first time. Now let s use the ArrayList to define the main menu. That makes the above code look like this:

 insertMenus = new ArrayList(); 
insertMenus.Add (new NotepadMenuItem (
"Insert &Section",
new EventHandler(MenuInsertTags),
TextModel.Tags.Section));
insertMenus.Add (new NotepadMenuItem (
"Insert &Pre",
new EventHandler(MenuInsertTags),
TextModel.Tags.Pre ));
insertMenus.Add (new NotepadMenuItem (
"Insert &UL",
new EventHandler(MenuInsertTags),
TextModel.Tags.UnorderedList ));
insertMenus.Add (new NotepadMenuItem (
"Insert &OL",
new EventHandler(MenuInsertTags),
TextModel.Tags.OrderedList ));
this.Menu = new MainMenu(new MenuItem[] {fileMenu} );
this.Menu.MenuItems. AddRange((MenuItem[]) insertMenus.ToArray(typeof(NotepadMenuItem)));

I don t like that ugly typeof() usage, but it s a small price to pay for the use of the ArrayList elsewhere. We could build a custom class to avoid the typeof, but until Microsoft gives us generics, that seems like too much work for the benefit.

start sidebar
Lesson: Quick Reflection

The work I ve done so far in this chapter, including writing this much of the chapter, took me about thirty minutes. Already the code ”and the process ”is a bit better. Now when we add a new menu item, we don t have to add a private variable for each one or remember to add it to the MenuFor Accelerator() method ”a noticeable improvement. Thinking of the private menu items reminds me ”let s remove those to get this:

end sidebar
 
 class XMLNotepad : Form { 
public TestableTextBox textbox;
private TextModel model;
// private MenuItem insertPre;
// private MenuItem insertSection;
// private MenuItem insertUnorderedList;
// private MenuItem insertOrderedList;
private ArrayList insertMenus;
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;
...

I m showing them commented out for your convenience, but now I ll delete them for real. And the code compiles, and the tests run correctly.




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