package caltool.view.edit; import caltool.model.edit.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; import mvp.*; /**** * * Class EditMenu is the pulldown menu view of the model class. The EditMenu widget is a Java JMenu. Anonymous instances * of JMenuItem are defined for each item in the menu. * */ public class EditMenu extends View { /** * Construct this with the given name as the pulldown label. The given * Edit is the companion model. */ public EditMenu(Screen screen, Edit edit, EditUI editUI) { super(screen, edit); } /** * Compose this by inserting each of its ten menu items into the pulldown * menu. The items are Undo, Redo, Cut, copy, Past, Delete, Select All, * and Find. JSeparators are placed after the 'Redo' and 'Select All' * items. */ public Component compose() { /* * Make the widget of this the JMenu. */ widget = new JMenu("Edit"); /* * Create a constant reference to the File model for use in the menu * item action listeners. This is necessary due to the subtle rules of * anonymous inner classes in Java. */ final Edit edit = (Edit) model; /* * Create the menu items using the following pattern: *
         *     JMenu.add(new JMenuItem("Item name").addActionListener(
         *         new ActionListener() {
         *             public void actionPerformed(ActionEvent e) {
         *                 Model.method()
         *                                                              
*/ /* * Add the 'Undo' menu item. */ ((JMenu) widget).add(new JMenuItem("Undo")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { edit.undo(); } } ); /* * Add the 'Redo' menu item. */ ((JMenu) widget).add(new JMenuItem("Redo")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { edit.redo(); } } ); /* * Add a separator. */ ((JMenu) widget).add(new JSeparator()); /* * Add the 'Cut' menu item. */ ((JMenu) widget).add(new JMenuItem("Cut")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { edit.cut(); } } ); /* * Add the 'Copy' menu item. */ ((JMenu) widget).add(new JMenuItem("Copy")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { edit.copy(); } } ); /* * Add the 'Paste' menu item. */ ((JMenu) widget).add(new JMenuItem("Paste")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { edit.paste(); } } ); /* * Add the 'Delete' menu item. */ ((JMenu) widget).add(new JMenuItem("Delete")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { edit.delete(); } } ); /* * Add the 'Select All' menu item. */ ((JMenu) widget).add(new JMenuItem("Select All")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { edit.selectAll(); } } ); /* * Add a separator. */ ((JMenu) widget).add(new JSeparator()); /* * Add the 'Find ...' menu item. */ ((JMenu) widget).add(new JMenuItem("Find ...")).addActionListener( new ActionListener() { public void actionPerformed(ActionEvent ev) { JOptionPane.showInputDialog("Search for: "); ((Edit) model).find(null); } } ); return widget; } }